package com.glxp.mipsdl.service.thrsys;

import cn.hutool.core.util.ReflectUtil;
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.basic.BasicUdirelDao;
import com.glxp.mipsdl.dao.thrsys.ThrProductsDao;
import com.glxp.mipsdl.entity.basic.BasicUdirelEntity;
import com.glxp.mipsdl.entity.thrsys.ThrProductsEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 第三方系统产品Service
 */
@Slf4j
@Service
public class ThrProductService {

    @Resource
    private ThirdSysConfig thirdSysConfig;
    @Resource
    private BasicUdirelDao basicUdirelDao;
    @Resource
    private ThrProductsDao thrProductsDao;

    /**
     * 查询第三方系统对应的产品信息
     *
     * @param relId
     * @return
     */
    private ThrProductsEntity getThrProduct(String relId) {
        if (StrUtil.isNotBlank(relId)) {
            BasicUdirelEntity basicUdirelEntity = basicUdirelDao.selectOne(new LambdaQueryWrapper<BasicUdirelEntity>().eq(BasicUdirelEntity::getId, relId));
            if (null != basicUdirelEntity) {
                try {
                    String code = (String) ReflectUtil.getFieldValue(basicUdirelEntity, basicUdirelEntity.getClass().getField(thirdSysConfig.getThirdId()));
                    if (StrUtil.isBlank(code)) {
                        log.error("第三方产品信息未关联，查询失败，产品编码：{}", relId);
                    } else {
                        ThrProductsEntity thrProductsEntity = thrProductsDao.selectOne(new LambdaQueryWrapper<ThrProductsEntity>().eq(ThrProductsEntity::getCode, code));
                        log.info("查询第三方系统产品信息成功，产品编码：{}，产品名称：{}，UDI管理系统产品编码：{}", thrProductsEntity.getCode(), thrProductsEntity.getName(), relId);
                        return thrProductsEntity;
                    }
                } catch (Exception e) {
                    log.error("未查询到产品编码对应的产品信息，产品编码：{}", relId);
                }
            }
            log.info("未查询到对应的第三方系统产品信息，产品编码：{}", relId);
        }
        log.info("查询第三方产品信息失败，产品编码参数为空");
        return null;
    }


}
