package com.glxp.mipsdl.service.thrsys;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.glxp.mipsdl.config.ThirdSysConfig;
import com.glxp.mipsdl.dao.thrsys.ThrSystemBusApiDao;
import com.glxp.mipsdl.entity.thrsys.ThrSystemBusApiEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class ThrSysBusTypeService {

    @Resource
    private ThrSystemBusApiDao thrSystemBusApiDao;
    @Resource
    private ThirdSysConfig thirdSysConfig;

    /**
     * 查询对应的第三方单据类型的编码
     *
     * @param busType
     * @return
     */
    public String getThrBusTypeCode(String busType) {
        if (StrUtil.isNotBlank(busType)) {
            LambdaQueryWrapper<ThrSystemBusApiEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.select(ThrSystemBusApiEntity::getThirdBuyCode)
                    .eq(ThrSystemBusApiEntity::getThirdSys, thirdSysConfig.getThirdSysConfig().getThirdId())
                    .eq(ThrSystemBusApiEntity::getCode, busType);
            ThrSystemBusApiEntity thrSystemBusApiEntity = thrSystemBusApiDao.selectOne(wrapper);
            if (null != thrSystemBusApiEntity) {
                log.info("单据类型转换成功，原类型：{}，第三方单据类型：{}", busType, thrSystemBusApiEntity.getThirdBuyCode());
                return thrSystemBusApiEntity.getThirdBuyCode();
            }
            log.info("查询第三方单据类型为空，原类型：{}", busType);
            return null;
        }
        log.info("查询第三方单据类型编码失败，单据类型编码参数为空");
        return null;
    }

    /**
     * 查询对应的第三方单据类型编码
     *
     * @param busType
     * @return
     */
    public ThrSystemBusApiEntity getThrBusType(String busType) {
        if (StrUtil.isNotBlank(busType)) {
            LambdaQueryWrapper<ThrSystemBusApiEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ThrSystemBusApiEntity::getThirdSys, thirdSysConfig.getThirdSysConfig().getThirdId())
                    .eq(ThrSystemBusApiEntity::getCode, busType);
            ThrSystemBusApiEntity thrSystemBusApiEntity = thrSystemBusApiDao.selectOne(wrapper);
            if (null != thrSystemBusApiEntity) {
                log.info("单据类型转换成功，原类型：{}，第三方单据类型：{}", busType, thrSystemBusApiEntity.getThirdBuyCode());
                return thrSystemBusApiEntity;
            }
            log.info("查询第三方单据类型为空，原类型：{}", busType);
            return null;
        }
        log.info("查询第三方单据类型编码失败，单据类型编码参数为空");
        return null;
    }


}
