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

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.api.busi.IProduceSnRepairService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.WorkCenterContainerLocationService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.ep.EpPutPalletModuleService;
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.bean.MesProduceSnRepair;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxPutPalletModuleModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpPutPalletPlcDataModel;
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.api.iservice.busi.IStepCommonMethodService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.*;
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 cn.estsh.i3plus.pojo.mes.repository.MesPlcRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description : 工步业务-EP装托PLC模式验证产品信息工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("epPutPalletPlcWorkCheckProdStepService")
public class EpPutPalletPlcWorkCheckProdStepService extends BaseStepService {

    @Autowired
    private EpPutPalletModuleService putPalletModuleService;

    @Autowired
    private IStepCommonMethodService stepCommonMethodService;

    @Autowired
    private MesPlcRepository plcRepository;

    @Autowired
    private WorkCenterContainerLocationService workCenterContainerLocationService;

    @Autowired
    private IPackageExtService packageExtService;

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private IProduceSnRepairService produceSnRepairService;

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

        StationResultBean resultBean = new StationResultBean();
        String err = "";

        Map<String, MesProdRouteOptParam> stepParamMap = stepCommonMethodService.getStepCfgInfo(reqBean);
        if (CollectionUtils.isEmpty(stepParamMap)) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[装托PLC模式验证产品信息工步]未配置必要工步参数[%s][%s][%s][%s]"),
                    MesPcnExtConstWords.EP_PCN_PUT_PALLET_PROD_HAND_SHAKE_SIGNAL, MesPcnExtConstWords.EP_PCN_PUT_PALLET_PROD_DATA,
                    MesPcnExtConstWords.EP_PCN_PUT_PALLET_COMPLETE_HAND_SHAKE_SIGNAL, MesPcnExtConstWords.EP_PCN_PUT_PALLET_COMPLETE_DATA);
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        EpPutPalletPlcDataModel plcDateModel = (EpPutPalletPlcDataModel) putPalletModuleService.doHandleCurPlcData(reqBean, 1, null);
        if (null == plcDateModel) {
            putPalletModuleService.doHandleCurExecPlcTask(reqBean, 3, null);
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "缓存中缺失[CUR_PLC_DATA]信息,已删除缓存中的[CUR_EXEC_PLC_TASK]信息,将重新获取数据");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return StepResult.getSuccessComplete(err);
        }
        if (plcDateModel.getIsCheck()) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "缓存中[CUR_PLC_DATA]信息已被验证,将重新获取数据");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return StepResult.getSuccessComplete(err);
        }

        EpPutPalletPlcDataModel curCtNoModel = (EpPutPalletPlcDataModel) putPalletModuleService.doHandleCurCtNo(reqBean, 1, null);
        Boolean boo1, boo2;
        String[] valueArr;
        if (StringUtils.isEmpty(plcDateModel.getCtNo()) && StringUtils.isEmpty(plcDateModel.getPackageNo())) {
            //完托组数据校验
            String pcnCompleteHandShakeSignal = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.EP_PCN_PUT_PALLET_COMPLETE_HAND_SHAKE_SIGNAL, reqBean, resultBean);
            String pcnCompleteData = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.EP_PCN_PUT_PALLET_COMPLETE_DATA, reqBean, resultBean);

            MesPlc pcnCompleteHandShakeSignalPlc = getPlc(pcnCompleteHandShakeSignal, MesPcnExtConstWords.EP_PCN_PUT_PALLET_COMPLETE_HAND_SHAKE_SIGNAL, reqBean, resultBean);
            MesPlc pcnCompleteDataPlc = getPlc(pcnCompleteData, MesPcnExtConstWords.EP_PCN_PUT_PALLET_COMPLETE_DATA, reqBean, resultBean);

            valueArr = getCompleteValue(reqBean, curCtNoModel);

            try {
                boo1 = putPalletModuleService.doWriteOpcParamValue(pcnCompleteHandShakeSignalPlc, "1");
                boo2 = putPalletModuleService.doWriteOpcParamValue(pcnCompleteDataPlc, valueArr[0]);
                if (boo1 && boo2) {
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "%sPCN写入完托组信号位[%s][1]数据位[%s]成功"),
                            valueArr[1], pcnCompleteHandShakeSignalPlc.getTagAddress(), pcnCompleteDataPlc.getTagAddress(), valueArr[0]);
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

                    if (valueArr[0].equals("0")) {
                        plcDateModel.setBoo(true);
                    }
                    plcDateModel.setIsCheck(true);
                    putPalletModuleService.doHandleCurPlcData(reqBean, 2, plcDateModel);

                } else {
//                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "%sPCN写入完托组信号位[1]数据位[%s]失败"), valueArr[1], valueArr[0]);
//                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "%sPCN写入完托组地址位[%s][%s]信号位[1]数据位[%s]失败"),
                            valueArr[1], pcnCompleteHandShakeSignalPlc.getTagAddress(), pcnCompleteDataPlc.getTagAddress(), valueArr[0]);
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                }
            } catch (Exception e) {
//                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "%sPCN写入完托组信号位[1]数据位[%s]时发生异常:[%s]"), valueArr[1], valueArr[0], e.getMessage());
//                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "%sPCN写入完托组地址位[%s][%s]信号位[1]数据位[%s]时发生异常:[%s]"),
                        valueArr[1], pcnCompleteHandShakeSignalPlc.getTagAddress(), pcnCompleteDataPlc.getTagAddress(), valueArr[0], e.getMessage());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            }

        } else {
            //产品组数据校验
            String pcnProdHandShakeSignal = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.EP_PCN_PUT_PALLET_PROD_HAND_SHAKE_SIGNAL, reqBean, resultBean);
            String pcnProdData = getValueByStepParamMap(stepParamMap, MesPcnExtConstWords.EP_PCN_PUT_PALLET_PROD_DATA, reqBean, resultBean);

            MesPlc pcnProdHandShakeSignalPlc = getPlc(pcnProdHandShakeSignal, MesPcnExtConstWords.EP_PCN_PUT_PALLET_PROD_HAND_SHAKE_SIGNAL, reqBean, resultBean);
            MesPlc pcnProdDataPlc = getPlc(pcnProdData, MesPcnExtConstWords.EP_PCN_PUT_PALLET_PROD_DATA, reqBean, resultBean);

            List<SxPutPalletModuleModel> putPalletModuleModelList = (null == curCtNoModel || !curCtNoModel.getBoo()) ? null :
                    (List<SxPutPalletModuleModel>) putPalletModuleService.doHandlePackageDataByCtNo(reqBean, curCtNoModel.getCtNo(), 1, null);

            String[] valueArr1 = getCtNoValue(reqBean, putPalletModuleModelList, curCtNoModel, plcDateModel.getCtNo());
            String[] valueArr2 = getPackageNoValue(reqBean, putPalletModuleModelList, plcDateModel, valueArr1[0]);
            valueArr = getProdValue(reqBean, valueArr1, valueArr2, plcDateModel);

            try {
                boo1 = putPalletModuleService.doWriteOpcParamValue(pcnProdHandShakeSignalPlc, "1");
                boo2 = putPalletModuleService.doWriteOpcParamValue(pcnProdDataPlc, valueArr[0]);
                if (boo1 && boo2) {
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "%sPCN写入产品组信号位[%s][1]数据位[%s][%s]成功"),
                            valueArr[1], pcnProdHandShakeSignalPlc.getTagAddress(), pcnProdDataPlc.getTagAddress(), valueArr[0]);
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

                    if (valueArr[0].equals("0")) {
                        plcDateModel.setBoo(true);
                    }
                    plcDateModel.setIsCheck(true);
                    putPalletModuleService.doHandleCurPlcData(reqBean, 2, plcDateModel);

                } else {
//                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "%sPCN写入产品组信号位[1]数据位[%s]失败"), valueArr[1], valueArr[0]);
//                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "%sPCN写入产品组地址位[%s][%s]信号位[1]数据位[%s]失败"),
                            valueArr[1], pcnProdHandShakeSignalPlc.getTagAddress(), pcnProdDataPlc.getTagAddress(), valueArr[0]);
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                }
            } catch (Exception e) {
//                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "%sPCN写入产品组信号位[1]数据位[%s]时发生异常:[%s]"), valueArr[1], valueArr[0], e.getMessage());
//                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "%sPCN写入产品组地址位[%s][%s]信号位[1]数据位[%s]时发生异常:[%s]"),
                        valueArr[1], pcnProdHandShakeSignalPlc.getTagAddress(), pcnProdDataPlc.getTagAddress(), valueArr[0], e.getMessage());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            }
        }
        return StepResult.getSuccessComplete();
    }

    private String[] getProdValue(StationRequestBean reqBean, String[] valueArr1, String[] valueArr2, EpPutPalletPlcDataModel plcDateModel) {
        String[] valueArr = new String[2];
        if (valueArr1[0].equals("0") && valueArr2[0].equals("0")) {
            valueArr[0] = "0";
            valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组指令中的容器代码[%s]与包装条码[%s]验证有效!"),
                    plcDateModel.getCtNo(), plcDateModel.getPackageNo());
        } else {
            if (valueArr1[0].equals("0")) {
                valueArr[0] = valueArr2[0];
            } else if (valueArr2[0].equals("0")) {
                valueArr[0] = valueArr1[0];
            } else {
                valueArr[0] = "3";
            }
            valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "%s%s"), valueArr1[1], valueArr2[1]);
        }
        return valueArr;
    }

    private String[] getCompleteValue(StationRequestBean reqBean, EpPutPalletPlcDataModel curCtNoModel) {
        String[] valueArr = new String[2];
        valueArr[0] = "1";
        if (null == curCtNoModel || !curCtNoModel.getBoo()) {
            valueArr[1] = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前未固定容器代码,不能执行接收到的完托指令!");
        } else {
            List<SxPutPalletModuleModel> putPalletModuleModelList = (List<SxPutPalletModuleModel>) putPalletModuleService.doHandlePackageDataByCtNo(reqBean, curCtNoModel.getCtNo(), 1, null);
            if (CollectionUtils.isEmpty(putPalletModuleModelList)) {
                valueArr[0] = "2";
                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前容器[%s]未绑定产品条码,空托状态,不能执行接收到的完托指令!"), curCtNoModel.getCtNo());
            } else {
                MesContainer container = workCenterContainerLocationService.getContainerByCtNo(reqBean.getOrganizeCode(), curCtNoModel.getCtNo());
                if (null == container) {
                    valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前容器[%s]无效,不能执行接收到的完托指令,请删除已装托的产品并更换容器!"), curCtNoModel.getCtNo());
                } else {
                    if (String.valueOf(MesPcnEnumUtil.MES_CONTAINER_STATUS.OCCUPY.getValue()).equals(container.getUseStatus())) {
                        valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前容器[%s]的当前状态[占用],不能执行接收到的完托指令,请删除已装托的产品并更换容器!"), container.getCtNo());
                    } else {
                        valueArr[0] = "0";
                        valueArr[1] = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "完托指令接收成功!");
                    }
                }
            }
        }
        return valueArr;
    }

    private String[] getCtNoValue(StationRequestBean reqBean, List<SxPutPalletModuleModel> putPalletModuleModelList, EpPutPalletPlcDataModel curCtNoModel, String ctNo) {
        String[] valueArr = new String[2];
        valueArr[0] = "1";
        if (StringUtils.isEmpty(ctNo)) {
            valueArr[1] = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组未写入有效的容器到PLC地址位中!");
        } else {
            if (!CollectionUtils.isEmpty(putPalletModuleModelList)) {
                if (curCtNoModel.getCtNo().equals(ctNo)) {
                    valueArr[0] = "0";
                    valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组指令中的容器代码[%s]有效!"), ctNo);
                } else {
                    valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入到PLC地址位中的容器代码[%s]与当前正在装托中的容器代码[%s]不一致!"), ctNo, curCtNoModel.getCtNo());
                }
            } else {
                MesContainer container = workCenterContainerLocationService.getContainerByCtNo(reqBean.getOrganizeCode(), ctNo);
                if (null == container) {
                    valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组指令中的容器代码[%s]无效!"), ctNo);
                } else {
                    if (String.valueOf(MesPcnEnumUtil.MES_CONTAINER_STATUS.OCCUPY.getValue()).equals(container.getUseStatus())) {
                        valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组指令中的容器代码[%s]的当前状态[占用]!"), ctNo);
                    } else {
                        String curPartNo = (String) putPalletModuleService.doHandleCurPartNo(reqBean, 1, null);
                        MesPartContainerCapacity partContainerCapacity = workCenterContainerLocationService.getPartContainerCapacity(reqBean.getOrganizeCode(), container.getCtCode(), curPartNo);
                        if (null == partContainerCapacity) {
                            valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组指令中的容器代码[%s]对应的容器类型[%s]与物料编码[%s]未配置产品容器容量信!"), ctNo, container.getCtCode(), curPartNo);
                        } else {
                            if (StringUtils.isEmpty(partContainerCapacity.getCtQty()) || partContainerCapacity.getCtQty().compareTo(0) == 0) {
                                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组指令中的容器代码[%s]对应的容器类型[%s]与物料编码[%s]在产品容器容量配置信息中未配置容量!"), ctNo, container.getCtCode(), curPartNo);
                            } else {
                                if (null == curCtNoModel || !curCtNoModel.getCtNo().equals(ctNo)) {
                                    curCtNoModel = new EpPutPalletPlcDataModel();
                                    curCtNoModel.setCtNo(ctNo);
                                    curCtNoModel.setBoo(false);
                                    curCtNoModel.setReadDatetime(TimeTool.getNowTime(true));
                                    putPalletModuleService.doHandleCurCtNo(reqBean, 2, curCtNoModel);
                                }
                                putPalletModuleService.doHandleCurCtQty(reqBean, 2, partContainerCapacity.getCtQty());
                                valueArr[0] = "0";
                                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组指令中的容器代码[%s]有效!"), ctNo);
                            }
                        }
                    }
                }
            }
        }
        return valueArr;
    }

    private String[] getPackageNoValue(StationRequestBean reqBean, List<SxPutPalletModuleModel> putPalletModuleModelList, EpPutPalletPlcDataModel plcDateModel, String ctNoValue) {
        String[] valueArr = new String[2];
        valueArr[0] = "2";
        if (StringUtils.isEmpty(plcDateModel.getPackageNo())) {
            valueArr[1] = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组未写入有效的包装条码到PLC地址位中!");
            return valueArr;
        }
        MesPackageExt pack = packageExtService.getPackageByPackNo(reqBean.getOrganizeCode(), plcDateModel.getPackageNo());
        if (null == pack) {
            valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]不存在!"), plcDateModel.getPackageNo());
            return valueArr;
        }
        if (StringUtils.isEmpty(pack.getLotNo())) {
            valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]没有批次号!"), pack.getPackageNo());
            return valueArr;
        }
        if (!CollectionUtils.isEmpty(putPalletModuleModelList) && !putPalletModuleModelList.get(0).getPartNo().equals(pack.getPartNo())) {
            valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]关联的物料编码[%s]与当前已装托的物料编码[%s]不一致!"),
                    pack.getPackageNo(), pack.getPartNo(), putPalletModuleModelList.get(0).getPartNo());
            return valueArr;
        }

        String curPartNo = (String) putPalletModuleService.doHandleCurPartNo(reqBean, 1, null);
        String curWorkOrderNo = (String) putPalletModuleService.doHandleCurWorkOrderNo(reqBean, 1, null);
        Integer curWorkOrderType = (Integer) putPalletModuleService.doHandleCurWorkOrderType(reqBean, 1, null);
        if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() != curWorkOrderType) {
            if (!pack.getWorkOrderNo().equals(curWorkOrderNo)) {
                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]不属于当前选择的工单[%s]!"), pack.getPackageNo(), curWorkOrderNo);
                return valueArr;
            }
        }

        if (!pack.getPartNo().equals(curPartNo)) {
            valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]关联的物料号[%s]与当前选择的工单[%s]关联的物料号[%s]不一致!"),
                    pack.getPackageNo(), pack.getPartNo(), curWorkOrderNo, curPartNo);
            return valueArr;
        }

        MesPackageDetail packageDetail = packageExtService.getPackageDetailByPackNo(reqBean.getOrganizeCode(), pack.getPackageNo());
        if (null == packageDetail) {
            valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]对应的明细信息不存在!"), pack.getPackageNo());
            return valueArr;
        }

        MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), packageDetail.getSerialNumber());
        if (null == produceSnExt) {
            valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]关联的产品条码[%s]对应的产品条码信息不存在!"),
                    pack.getPackageNo(), packageDetail.getSerialNumber());
            return valueArr;
        }

        if (MesPcnExtEnumUtil.PRODUCE_SN_QC_STATUS.QUALIFIED.getValue() != produceSnExt.getQcStatus()) {
            valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]关联的产品条码[%s]质量状态为[%s]!"),
                    pack.getPackageNo(), produceSnExt.getSerialNumber(), MesPcnExtEnumUtil.PRODUCE_SN_QC_STATUS.valueOfDescription(produceSnExt.getQcStatus()));
            return valueArr;
        }

        if (MesPcnExtEnumUtil.PRODUCE_SN_STATUS.OFFLINE.getValue() != produceSnExt.getSnStatus()) {
            valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]关联的产品条码[%s]状态为[%s]!"),
                    pack.getPackageNo(), produceSnExt.getSerialNumber(), MesPcnExtEnumUtil.PRODUCE_SN_STATUS.valueOfDescription(produceSnExt.getSnStatus()));
            return valueArr;
        }

        String idStr = pack.getId().toString();
        String lotNo = pack.getLotNo();
        if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() == curWorkOrderType) {
            MesProduceSnRepair produceSnRepair = produceSnRepairService.getProduceSnRepairBySn(reqBean.getOrganizeCode(), packageDetail.getSerialNumber());
            if (null == produceSnRepair) {
                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]对应的产品条码[%s]未关联返工工单[%s]!"),
                        pack.getPackageNo(), packageDetail.getSerialNumber(), curWorkOrderNo);
                return valueArr;
            }
            if (!produceSnRepair.getWorkOrderNo().equals(curWorkOrderNo)) {
                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]对应的产品条码关联的最新返工工单为[%s],不属于当前选择的返工工单[%s]!"),
                        pack.getPackageNo(), produceSnRepair.getWorkOrderNo(), curWorkOrderNo);
                return valueArr;
            }
            if (!reqBean.getWorkCenterCode().equals(produceSnRepair.getWorkCenterCode())) {
                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]关联的返工工单类型的产品条码[%s]关联的生产线[%s]与当前生产线[%s]不一致!"),
                        pack.getPackageNo(), produceSnRepair.getSerialNumber(), produceSnRepair.getWorkCenterCode(), reqBean.getWorkCenterCode());
                return valueArr;
            }
            if (StringUtils.isEmpty(produceSnRepair.getLotNo())) {
                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]关联的返工工单类型的产品条码[%s]未下线!"),
                        pack.getPackageNo(), produceSnRepair.getSerialNumber());
                return valueArr;
            }
            if (!StringUtils.isEmpty(produceSnRepair.getCtNo())) {
                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]已经被装托在容器编码[%s]中!"),
                        pack.getPackageNo(), produceSnRepair.getCtNo());
                return valueArr;
            }
            idStr = produceSnRepair.getId().toString();
            lotNo = produceSnRepair.getLotNo();
        } else {
            if (!reqBean.getWorkCenterCode().equals(pack.getWorkCenterCode())) {
                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]关联的生产线[%s]与当前生产线[%s]不一致!"),
                        pack.getPackageNo(), pack.getWorkCenterCode(), reqBean.getWorkCenterCode());
                return valueArr;
            }
            if (!StringUtils.isEmpty(pack.getCtNo())) {
                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]已经被装托在容器编码[%s]中!"),
                        pack.getPackageNo(), pack.getCtNo());
                return valueArr;
            }
        }

        if (!CollectionUtils.isEmpty(putPalletModuleModelList)) {
            putPalletModuleModelList = putPalletModuleModelList.stream().sorted(Comparator.comparing(SxPutPalletModuleModel::getLotNo).reversed()).collect(Collectors.toList());
            if (TimeTool.stringParseToDate(lotNo, MesPcnExtConstWords.DATE_FORMAT).compareTo(TimeTool.stringParseToDate(putPalletModuleModelList.get(0).getLotNo(), MesPcnExtConstWords.DATE_FORMAT)) < 0) {
                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]的批次号[%s]早于上一个已装托包装条码的批次号[%s]!"),
                        pack.getPackageNo(), lotNo, putPalletModuleModelList.get(0).getLotNo());
                return valueArr;
            }
        }

        if (ctNoValue.equals("0")) {
            SxPutPalletModuleModel model = new SxPutPalletModuleModel();
            model.setIdStr(idStr);
            model.setWorkOrderType(curWorkOrderType);
            model.setWorkOrderNo(curWorkOrderNo);
            model.setPartNo(pack.getPartNo());
            model.setLotNo(lotNo);
            model.setPackageNo(pack.getPackageNo());
            model.setCtNo(plcDateModel.getCtNo());
            model.setScanDatetime(TimeTool.getNowTime(true));
            if (!(Boolean) putPalletModuleService.doHandlePackageDataByCtNo(reqBean, plcDateModel.getCtNo(), 0, model)) {
                valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]已装托在容器代码[%s]中!"), pack.getPackageNo(), plcDateModel.getCtNo());
                return valueArr;
            }

            EpPutPalletPlcDataModel packageNoModel = new EpPutPalletPlcDataModel();
            packageNoModel.setPackageNo(pack.getPackageNo());
            packageNoModel.setBoo(false);
            packageNoModel.setReadDatetime(TimeTool.getNowTime(true));
            packageNoModel.setPutPalletModuleModel(model);
            putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 2, packageNoModel);

        } else {
            if (!CollectionUtils.isEmpty(putPalletModuleModelList)) {
                SxPutPalletModuleModel model = new SxPutPalletModuleModel();
                model.setIdStr(idStr);
                model.setWorkOrderType(curWorkOrderType);
                model.setWorkOrderNo(curWorkOrderNo);
                model.setPartNo(pack.getPartNo());
                model.setLotNo(lotNo);
                model.setPackageNo(pack.getPackageNo());
                model.setCtNo(putPalletModuleModelList.get(0).getCtNo());
                model.setScanDatetime(TimeTool.getNowTime(true));
                if (!(Boolean) putPalletModuleService.doHandlePackageDataByCtNo(reqBean, putPalletModuleModelList.get(0).getCtNo(), 0, model)) {
                    valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组写入的包装条码[%s]已装托在容器代码[%s]中!"), pack.getPackageNo(), putPalletModuleModelList.get(0).getCtNo());
                    return valueArr;
                }
            }
        }

        valueArr[0] = "0";
        valueArr[1] = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品组指令中的包装条码[%s]有效!"), pack.getPackageNo());
        return valueArr;
    }


    private String getValueByStepParamMap(Map<String, MesProdRouteOptParam> stepParamMap, String code, StationRequestBean reqBean, StationResultBean resultBean) {
        String value = null;
        if (!CollectionUtils.isEmpty(stepParamMap) && stepParamMap.containsKey(code)
                && !StringUtils.isEmpty(stepParamMap.get(code).getParamValue())) {
            value = stepParamMap.get(code).getParamValue();
        }
        if (StringUtils.isEmpty(value)) {
            String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "[装托PLC模式读取产品信息工步]未配置必要工步参数[%s]"), code);
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }
        return value;
    }

    public MesPlc getPlc(String paramValue, String code, StationRequestBean reqBean, StationResultBean resultBean) {
        MesPlc plc = (MesPlc) putPalletModuleService.doHandleCachePlc(reqBean, 1, code, null);
        if (null == plc) {
            plc = plcRepository.getByProperty(
                    new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.PLC_CODE},
                    new Object[]{reqBean.getOrganizeCode(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), paramValue});
            if (null != plc) {
                putPalletModuleService.doHandleCachePlc(reqBean, 2, code, plc);
            } else {
                String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "PLC[%s]在数据库不存在"), paramValue);
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                foundExThrowNoShowMsg();
            }
        }
        return plc;
    }

}
