package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.step.bu;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IPackageExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesInnerPackageTravel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPackageExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesInnerPackageTravelRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.ConvertBeanExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Description : 工步业务-写入小包装条码履历表
 * @Reference :
 * @Author : steven.peng
 * @CreateDate : 2023-01-15
 * @Modify:
 **/
@Slf4j
@Service("sxCreateInnerPackageSnTravelStepService")
public class SxCreateInnerPackageSnTravelStepService extends BaseStepService {

    @Autowired
    private MesInnerPackageTravelRepository mesInnerPackageTravelRepository;

    @Autowired
    private IPackageExtService packageExtService;

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("SxCreateSnTravelStepService[execute] 参数：{}", reqBean);

        StationResultBean resultBean = new StationResultBean();
        String err;

        if (StringUtils.isEmpty(reqBean.getOrganizeCode())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "写入小包装履历表工步需要传入工厂");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        if (StringUtils.isEmpty(reqBean.getSerialNumber())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "写入小包装履历表工步需要传入产品条码");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        MesPackageExt mesPackageExt = (MesPackageExt) reqBean.getDataMap().get(MesPcnExtConstWords.MES_PACKAGE_EXT);
        if (null == mesPackageExt || StringUtils.isEmpty(mesPackageExt.getPackageNo())) {
            mesPackageExt = getMesPackageExtModel(reqBean);
        }
        if (null == mesPackageExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "小包装条码[%s]无效"), reqBean.getScanInfo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonCompleteStop(err);
        }
        //  // 校验小包装条码对应的本体条码 查询出本体条码的信息
        MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), reqBean.getSerialNumber());
        if (produceSnExt == null) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "条码[%s]无效"), reqBean.getSerialNumber());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonCompleteStop(err);
        }

        MesInnerPackageTravel mesInnerPackageTravel = new MesInnerPackageTravel();
        BeanUtils.copyProperties(mesPackageExt, mesInnerPackageTravel);
        ConvertBean.serviceModelInitialize(mesInnerPackageTravel, mesPackageExt.getModifyUser());
        mesInnerPackageTravel.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        new ConvertBeanExt(mesInnerPackageTravel).convertBean(mesInnerPackageTravel);
       // produceSnTravelExtService.insertProduceSnTravelExt(snModel);
        mesInnerPackageTravel.setWorkCellCode(reqBean.getWorkCellCode());
        mesInnerPackageTravel.setSnType(produceSnExt.getSnType());
        mesInnerPackageTravel.setWorkTime(""); // 需要从mes_work_center获取
        mesInnerPackageTravelRepository.insert(mesInnerPackageTravel);
        return StepResult.getSuccessComplete();
    }

    private MesPackageExt getMesPackageExtModel(StationRequestBean reqBean) {
        MesPackageExt pack = packageExtService.getPackageByPackNo(reqBean.getOrganizeCode(), reqBean.getScanInfo());
        return pack;
    }
}
