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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProductEncodeCfgExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxNumberRuleCheckAttributeService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesNumberRuleExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPartExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesProduceSnExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpSnBindOrderWorkOrderModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesNumberRuleExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesPartExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProduceSnExtRepository;
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.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
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.impp.framework.boot.util.ImppRedis;
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.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:通用工步一:EP条码关联工单-绑定条码校验工步
 * @USER: xinwang.yi
 * @DATE: 2021-12-27 16:36
 */
@Slf4j
@Service("epSnBindOrderSnBindCheckStepService")
public class EpSnBindOrderSnBindCheckStepService extends BaseStepService {

    @Resource(name = MesPcnExtConstWords.REDIS_RES_PCN)
    private ImppRedis redisMesPcn;

    @Autowired
    private MesPartExtRepository partExtRepository;

    @Autowired
    private IProductEncodeCfgExtService productEncodeCfgExtService;

    @Autowired
    private MesNumberRuleExtRepository NumberRuleExtRepository;

    @Autowired
    private ISxNumberRuleCheckAttributeService iSxNumberRuleCheckAttributeService;

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private MesProduceSnExtRepository produceSnExtRepository;

    @Override
    public StepResult guide(StationRequestBean reqBean) {
        StationResultBean resultBean = new StationResultBean();

        String curProdType = (String) doHandleCurProdType(reqBean, 1, null);
        try {
            Integer curProdTypeIntValue = Integer.valueOf(curProdType);
            if(curProdTypeIntValue == MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.SINGLE_BIND.getValue()){
                String guide = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请扫描需要绑定的产品条码");
                this.sendMessage(reqBean, resultBean, guide, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }else if(curProdTypeIntValue == MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.BATCH_BIND.getValue()){
                List<EpProduceSnExtModel> produceSnExtModelList = (List<EpProduceSnExtModel>) redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.SN_MODEL_LIST);
                if(CollectionUtils.isEmpty(produceSnExtModelList)){
                    String guide = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请扫描需要绑定的区间[首]产品条码");
                    this.sendMessage(reqBean, resultBean, guide, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                }else{
                    String guide = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请扫描需要绑定的区间[尾]产品条码");
                    this.sendMessage(reqBean, resultBean, guide, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                }
            }
        } catch (NumberFormatException e) {
            String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前类型[%s]不是数字"), curProdType);
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            foundExThrowNoShowMsg();
        }
        return super.guide(reqBean);
    }

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        String scanInfo = reqBean.getScanInfo();
        if(StringUtils.isEmpty(scanInfo)){
            doHandleCurWorkOrder(reqBean, 3, null);
            String 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);
        }

        //当前工单
        EpSnBindOrderWorkOrderModel curWorkOrderNo = (EpSnBindOrderWorkOrderModel) doHandleCurWorkOrder(reqBean, 1, null);

        if(curWorkOrderNo == null){
            String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前执行工单[%s]缓存数据有误,请重新选择工单"), curWorkOrderNo.getWorkOrderNo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        if(curWorkOrderNo.getQty() <= curWorkOrderNo.getBindQty()){
            String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前执行工单[%s]已达到计划数量,请选择其它工单"), curWorkOrderNo.getWorkOrderNo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        DdlPackBean packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(curWorkOrderNo.getPartNo(), MesPcnExtConstWords.PART_NO, packBean);
        MesPartExt partDb = partExtRepository.getByProperty(packBean);

        if(partDb == null){
            doHandleProduceSnList(reqBean, 3, null);
                String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前执行工单[%s]关联的物料号[%s]不存在"), curWorkOrderNo.getWorkOrderNo(),curWorkOrderNo.getPartNo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        Integer codeType;
        if(curWorkOrderNo.getWorkOrderType() == MesPcnExtEnumUtil.WORK_ORDER_TYPE.STANDARD_ORDER.getValue()){
            codeType = MesPcnExtEnumUtil.ENCODE_CODE_TYPE.STANDARD_SERIAL_SN.getValue();
        }else{
            codeType = MesPcnExtEnumUtil.ENCODE_CODE_TYPE.ATTEMPT_SERIAL_SN.getValue();
        }

        String ruleCode;
        try {
            ruleCode = productEncodeCfgExtService.getRuleCodeByMatchType(reqBean.getOrganizeCode(), codeType, partDb, reqBean.getWorkCenterCode());
        } catch (Exception e) {
            doHandleProduceSnList(reqBean, 3, null);
            String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前执行工单号[%s]关联的物料[%s]:[%s]"),curWorkOrderNo.getWorkOrderNo(), curWorkOrderNo.getPartNo(), e.getMessage());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(ruleCode, MesPcnExtConstWords.RULE_CODE, packBean);
        MesNumberRuleExt numberRuleDb = NumberRuleExtRepository.getByProperty(packBean);

        if(numberRuleDb == null || StringUtils.isEmpty(numberRuleDb.getRuleLengthSpilt()) || StringUtils.isEmpty(numberRuleDb.getMethodName())){
            doHandleCurWorkOrder(reqBean, 3, null);
            String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前执行工单号[%s]关联的物料[%s]配置的编码规则代码[%s]无效"), curWorkOrderNo.getWorkOrderNo(), curWorkOrderNo.getPartNo(), ruleCode);
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(scanInfo,MesPcnExtConstWords.SERIAL_NUMBER,packBean);
        MesProduceSnExt mesProduceSnExt = produceSnExtRepository.getByProperty(packBean);
        if(mesProduceSnExt != null){
            String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前条码[%s]已被绑定"),scanInfo);
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        Map<String,Object> map = new LinkedHashMap<>();

        map.put(MesPcnExtConstWords.SERIAL_NUMBER, scanInfo);
        map.put(MesPcnExtConstWords.NUMBER_RULE_EXT, numberRuleDb);
        map.put(MesPcnExtConstWords.PART_EXT, partDb);

        Map<String,Object> resultMap = iSxNumberRuleCheckAttributeService.doCheckNumberRule(map);
        if((Boolean)resultMap.get(MesPcnExtConstWords.RESULT) == false){
            doHandleProduceSnList(reqBean, 3, null);
            String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前执行工单号[%s]关联的物料[%s]配置的编码规则代码[%s]无效"),curWorkOrderNo.getWorkOrderNo(), curWorkOrderNo.getPartNo(), ruleCode);
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
        DdlPreparedPack.getNumEqualPack(2, MesPcnExtConstWords.IS_VALID, packBean);
        DdlPreparedPack.getStringEqualPack(scanInfo,MesPcnExtConstWords.SERIAL_NUMBER,packBean);
        MesProduceSnExt mesProduceSnExtDb = produceSnExtRepository.getByProperty(packBean);

        EpProduceSnExtModel  produceSnExtModel = new EpProduceSnExtModel();
        produceSnExtModel.setSerialNumber(scanInfo);
        produceSnExtModel.setProductSn(scanInfo);
        produceSnExtModel.setWorkOrderNo(curWorkOrderNo.getWorkOrderNo());
        produceSnExtModel.setPartNo(curWorkOrderNo.getPartNo());
        produceSnExtModel.setPartNameRdd(curWorkOrderNo.getPartNameRdd());
        produceSnExtModel.setPrintStatus(MesPcnExtEnumUtil.PRODUCE_SN_PRINT_STATUS.PRINTED.getValue());
        produceSnExtModel.setPrintCount(1);
        produceSnExtModel.setSnStatus(MesPcnExtEnumUtil.PRODUCE_SN_STATUS.CREATE.getValue());
        produceSnExtModel.setQcStatus(MesPcnExtEnumUtil.PRODUCE_SN_QC_STATUS.QUALIFIED.getValue());
        produceSnExtModel.setSnType(curWorkOrderNo.getWorkOrderType());
        produceSnExtModel.setQty(new Double(1));
        produceSnExtModel.setLastWorkOrderNo(curWorkOrderNo.getWorkOrderNo());
        produceSnExtModel.setOrganizeCode(reqBean.getOrganizeCode());
        produceSnExtModel.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        produceSnExtModel.setSystemSyncStatusExt(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        produceSnExtModel.setPartLotNo(String.valueOf(resultMap.get(MesPcnExtConstWords.L)));
        produceSnExtModel.setSeqNo(String.valueOf(resultMap.get(MesPcnExtConstWords.SEQNO)));
        if(resultMap.containsKey(MesPcnExtConstWords.SEQNO2)){
            produceSnExtModel.setSeqNo2(String.valueOf(resultMap.get(MesPcnExtConstWords.SEQNO2)));
        }
        produceSnExtModel.setSnWithoutSeqNo(String.valueOf(resultMap.get(MesPcnExtConstWords.SN_WITHOUT_SEQNO)));

        if(mesProduceSnExtDb != null){
            produceSnExtModel.setId(mesProduceSnExtDb.getId());
        }

        List<EpProduceSnExtModel> produceSnExtModelList = (List<EpProduceSnExtModel>)doHandleProduceSnModelList(reqBean, 1, null);

        String curProdType = (String) doHandleCurProdType(reqBean, 1, null);
        if(CollectionUtils.isEmpty(produceSnExtModelList)){
            produceSnExtModelList = new ArrayList<>();
            produceSnExtModelList.add(produceSnExtModel);

            if(Integer.valueOf(curProdType) == MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.SINGLE_BIND.getValue()){
                MesProduceSnExt produceSnExt = new MesProduceSnExt();
                BeanUtils.copyProperties(produceSnExtModelList.get(0), produceSnExt);
                ConvertBean.serviceModelInitialize(produceSnExt, reqBean.getUserInfo());
                produceSnExtService.packSnExtProcess(produceSnExt, reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode());
                produceSnExt.setModifyDatetimeExt(produceSnExt.getModifyDatetime());
                List<MesProduceSnExt> produceSnExtList = new ArrayList<>();
                produceSnExtList.add(produceSnExt);
                doHandleProduceSnList(reqBean, 2, produceSnExtList);
                String message = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]绑定校验成功"),reqBean.getScanInfo());
                this.sendMessage(reqBean, resultBean, message, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

                return StepResult.getSuccessComplete(message);

            }else if(Integer.valueOf(curProdType) == MesPcnExtEnumUtil.EP_SN_BIND_ORDER_PROD_TYPE.BATCH_BIND.getValue()){
                doHandleProduceSnModelList(reqBean, 2, produceSnExtModelList);
                String message = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "区间[首]产品条码[%s]绑定校验成功"),reqBean.getScanInfo());
                this.sendMessage(reqBean, resultBean, message, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                return StepResult.getNonComplete(message);
            }
        }else {
            if(produceSnExtModelList.get(0).getSerialNumber().equals(produceSnExtModel.getSerialNumber())){
                doHandleProduceSnList(reqBean, 3, null);
                String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前扫描区间[首]产品条码[%s]与区间[尾]产品条码[%s]一致,请重新扫描区间[首]产品条码"), produceSnExtModelList.get(0).getSerialNumber(), reqBean.getScanInfo());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }

            String snWithoutSeqNo= produceSnExtModelList.get(0).getSnWithoutSeqNo();
            log.info("首条码匹配部分：{}",snWithoutSeqNo);
            log.info("尾条码匹配部分：{}",produceSnExtModel.getSnWithoutSeqNo());
            if(!snWithoutSeqNo.equals(produceSnExtModel.getSnWithoutSeqNo())){
                doHandleProduceSnList(reqBean, 3, null);
                String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前扫描区间[首]产品条码[%s]与区间[尾]产品条码[%s]除流水号以外部分不一致,请重新扫描区间[首]产品条码"), produceSnExtModelList.get(0).getSerialNumber(), reqBean.getScanInfo());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }

            String seqNoF = produceSnExtModelList.get(0).getSeqNo();
            String seqNoF2 = produceSnExtModelList.get(0).getSeqNo2();
            String seqNoS = produceSnExtModel.getSeqNo();
            String seqNoS2 = produceSnExtModel.getSeqNo2();
            if(Integer.valueOf(seqNoF).compareTo(Integer.valueOf(seqNoS)) >= 0 ){
                doHandleProduceSnList(reqBean, 3, null);
                String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前扫描区间[首]产品条码[%s]的流水号[%s]不小于区间[尾]产品条码[%s]的流水号[%s],请重新扫描区间[首]产品条码"),produceSnExtModelList.get(0).getSerialNumber(), produceSnExtModelList.get(0).getSeqNo(), reqBean.getScanInfo(), produceSnExtModel.getSeqNo());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }

            if(!StringUtils.isEmpty(seqNoS2) && Integer.valueOf(seqNoF2 ).compareTo(Integer.valueOf(seqNoS2)) >= 0 ){
                doHandleProduceSnList(reqBean, 3, null);
                String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前扫描区间[首]产品条码[%s]的二段流水号[%s]不小于区间[尾]产品条码[%s]的二段流水号[%s],请重新扫描区间[首]产品条码"),produceSnExtModelList.get(0).getSerialNumber(), seqNoF2, reqBean.getScanInfo(), seqNoS2);
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }

            BigInteger curSubQty = BigInteger.valueOf(Long.parseLong(produceSnExtModel.getSeqNo())).subtract(BigInteger.valueOf(Integer.valueOf(produceSnExtModelList.get(0).getSeqNo()))).add(BigInteger.valueOf(1));
            BigInteger unBindQty = BigInteger.valueOf(curWorkOrderNo.getQty().intValue()).subtract(BigInteger.valueOf(curWorkOrderNo.getBindQty().intValue()));

            if(curSubQty.compareTo(unBindQty) > 0){
                doHandleProduceSnList(reqBean, 3, null);
                String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "前扫描区间[首-尾]产品条码的流水号[%s-%s]的区间数量[%s]已经超过剩余未绑定数量[%s],请重新扫描区间[首]产品条码"),produceSnExtModelList.get(0).getSeqNo(), produceSnExtModel.getSeqNo(), curSubQty, unBindQty);
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }

            if(!StringUtils.isEmpty(seqNoF2)){
                if(Integer.valueOf(seqNoS).compareTo(Integer.valueOf(seqNoF)) != Integer.valueOf(seqNoS2).compareTo(Integer.valueOf(seqNoF2))){
                    doHandleProduceSnList(reqBean, 3, null);
                    String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "前扫描区间[首-尾]产品条码的2段流水号[%s-%s]的区间数量和1段流水号区间数量不一致,请将产品条码调整好再进行绑定"),produceSnExtModelList.get(0).getSeqNo(), produceSnExtModel.getSeqNo());
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                    return StepResult.getNonComplete(err);
                }
            }

            List<EpProduceSnExtModel> addProduceSnExtModelList = new ArrayList<>();
            List<String> seqNoList = new ArrayList<>();
            List<String> seqNo2List = null;

            seqNoListCollect(seqNoList, seqNoF, seqNoS);
            if(!StringUtils.isEmpty(seqNoF2)){
                seqNo2List = new ArrayList<>();
                seqNoListCollect(seqNo2List, seqNoF2, seqNoS2);
            }
            Iterator<String> seqNoListIterator = seqNoList.stream().iterator();

            while (seqNoListIterator.hasNext()){
                String seqNo = seqNoListIterator.next();
                EpProduceSnExtModel epProduceSnExtModel = new EpProduceSnExtModel();
                epProduceSnExtModel.setSeqNo(seqNo);
                epProduceSnExtModel.setSerialNumber(snWithoutSeqNo.replaceFirst(MesPcnExtConstWords.SEQNO, seqNo));
                epProduceSnExtModel.setProductSn(snWithoutSeqNo.replaceFirst(MesPcnExtConstWords.SEQNO, seqNo));
                BeanUtils.copyProperties(produceSnExtModel, epProduceSnExtModel, MesPcnExtConstWords.PRODUCT_SN, MesPcnExtConstWords.SEQ_NO, MesPcnExtConstWords.SERIAL_NUMBER);
                addProduceSnExtModelList.add(epProduceSnExtModel);
            }

            if(!CollectionUtils.isEmpty(seqNo2List)){
                for (int seqIndex = 0; seqIndex < seqNo2List.size();seqIndex++) {
                    addProduceSnExtModelList.get(seqIndex).setSeqNo2(seqNo2List.get(seqIndex));
                    String serialNumber = addProduceSnExtModelList.get(seqIndex).getSerialNumber().replaceFirst(MesPcnExtConstWords.SEQNO2, seqNo2List.get(seqIndex));
                    addProduceSnExtModelList.get(seqIndex).setSerialNumber(serialNumber);
                    addProduceSnExtModelList.get(seqIndex).setProductSn(serialNumber);
                }
            }

            String leftSn = snWithoutSeqNo.substring(0, snWithoutSeqNo.indexOf(MesPcnExtConstWords.SEQNO));

            packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
            DdlPreparedPack.getStringRightLikerPack(leftSn, MesPcnExtConstWords.SERIAL_NUMBER, packBean);
            List<MesProduceSnExt> produceSnExtDbList = produceSnExtRepository.findByHqlWhere(packBean);
            if(!CollectionUtils.isEmpty(produceSnExtDbList)){
                List<String> serialNumberDbList = produceSnExtDbList.stream().map(MesProduceSnExt::getSerialNumber).collect(Collectors.toList());
                List<EpProduceSnExtModel> addProduceSnExtModelIsExistDbList = addProduceSnExtModelList.stream().filter(epProduceSnExtModel -> serialNumberDbList.contains(epProduceSnExtModel.getSerialNumber())).collect(Collectors.toList());
                if(!CollectionUtils.isEmpty(addProduceSnExtModelIsExistDbList)){
                    String err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前扫描区间[首]产品条码[%s]与区间[尾]产品条码[%s]中有[%s]个产品条码绑定生产工单,请重新扫描区间[首]产品条码"), produceSnExtModelList.get(0).getSerialNumber(), scanInfo, addProduceSnExtModelIsExistDbList.size());
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                    doHandleProduceSnList(reqBean, 3 ,null);
                    return StepResult.getNonComplete(err);
                }
            }

            List<String> collect = addProduceSnExtModelList.stream().map(EpProduceSnExtModel::getSerialNumber).collect(Collectors.toList());
            packBean = DdlPackBean.getDdlPackBean(reqBean.getOrganizeCode());
            DdlPreparedPack.getNumEqualPack(2, MesPcnExtConstWords.IS_VALID, packBean);
            DdlPreparedPack.getInPack(collect, MesPcnExtConstWords.SERIAL_NUMBER, packBean);
            List<MesProduceSnExt> produceSnExtListMeta = produceSnExtRepository.findByHqlWhere(packBean);

            Iterator<MesProduceSnExt> mesProduceSnExtIterator = produceSnExtListMeta.stream().iterator();

            while (mesProduceSnExtIterator.hasNext()){
                MesProduceSnExt produceSnExt = mesProduceSnExtIterator.next();
                Iterator<EpProduceSnExtModel> produceSnExtModelIteratorTemp = addProduceSnExtModelList.stream().iterator();
                while (produceSnExtModelIteratorTemp.hasNext()){
                    EpProduceSnExtModel snExtModel = produceSnExtModelIteratorTemp.next();
                    if(snExtModel.getSerialNumber().equals(produceSnExt.getSerialNumber())){
                        snExtModel.setId(produceSnExt.getId());
                    }
                }
            }

            MesProduceSnExt  produceSnExt = new MesProduceSnExt();
            BeanUtils.copyProperties(produceSnExtModelList.get(0), produceSnExt);
            ConvertBean.serviceModelInitialize(produceSnExt, reqBean.getUserInfo());
            //参数多了产线代码
            produceSnExtService.packSnExtProcess(produceSnExt, reqBean.getOrganizeCode(),reqBean.getWorkCenterCode(), reqBean.getWorkCellCode());
            produceSnExt.setModifyDatetimeExt(produceSnExt.getModifyDatetime());
            if(StringUtils.isEmpty(produceSnExtModelList.get(0).getSeqNo2())){
                produceSnExt.setSeqNo(produceSnExt.getSeqNo());
            }else{
                produceSnExt.setSeqNo(new StringJoiner(MesPcnExtConstWords.COMMA).add(produceSnExtModelList.get(0).getSeqNo()).add(produceSnExtModelList.get(0).getSeqNo2()).toString());
            }
            List<MesProduceSnExt> produceSnExtList = new ArrayList<>();
            produceSnExtList.add(produceSnExt);

            Iterator<EpProduceSnExtModel> epProduceSnExtModelIterator = addProduceSnExtModelList.stream().iterator();
            while (epProduceSnExtModelIterator.hasNext()){
                MesProduceSnExt produceSn = new MesProduceSnExt();
                EpProduceSnExtModel snExtModel = epProduceSnExtModelIterator.next();
                BeanUtils.copyProperties(snExtModel, produceSn);
                produceSn.setCreateDatetime(produceSnExt.getCreateDatetime());
                produceSn.setCreateUser(produceSnExt.getCreateUser());
                produceSn.setModifyDatetime(produceSnExt.getModifyDatetime());
                produceSn.setModifyUser(produceSnExt.getModifyUser());
                produceSn.setProcessCode(produceSnExt.getProcessCode());
                produceSn.setNextProcessCode(produceSnExt.getNextProcessCode());
                produceSn.setWorkCenterCode(produceSnExt.getWorkCenterCode());
                produceSn.setWorkCellCode(produceSnExt.getWorkCellCode());
                produceSn.setModifyDatetimeExt(produceSnExt.getModifyDatetime());
                if(StringUtils.isEmpty(snExtModel.getSeqNo2())){
                    produceSn.setSeqNo(produceSn.getSeqNo());
                }else{
                    produceSn.setSeqNo(new StringJoiner(MesPcnExtConstWords.COMMA).add(snExtModel.getSeqNo()).add(snExtModel.getSeqNo2()).toString());
                }
                produceSnExtService.packSnExtProcess(produceSn, reqBean.getOrganizeCode(),reqBean.getWorkCenterCode(), reqBean.getWorkCellCode());
                produceSnExtList.add(produceSn);
            }
            doHandleProduceSnList(reqBean, 2, produceSnExtList);
            String message = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "区间[尾]产品条码[%s]绑定校验成功"), scanInfo);
            this.sendMessage(reqBean, resultBean, message, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
        }
        return StepResult.getSuccessComplete();
    }

    public synchronized Object doHandleCurProdType(StationRequestBean reqBean, Integer flag, String curProdType) {
        switch (flag) {
            case 1:
                return redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_PROD_TYPE);
            case 2:
                return redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_PROD_TYPE, curProdType, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            case 3:
                redisMesPcn.deleteHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_PROD_TYPE);
                return true;
            default:
                return null;
        }
    }

    public synchronized Object doHandleProduceSnModelList(StationRequestBean reqBean, Integer flag, List<EpProduceSnExtModel> produceSnExtModelList) {
        switch (flag) {
            case 1:
                return redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.SN_MODEL_LIST);
            case 2:
                return redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.SN_MODEL_LIST, produceSnExtModelList, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            case 3:
                redisMesPcn.deleteHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.SN_MODEL_LIST);
                return true;
            default:
                return null;
        }
    }

    public synchronized Object doHandleProduceSnList(StationRequestBean reqBean, Integer flag, List<MesProduceSnExt> produceSnExtList) {
        switch (flag) {
            case 1:
                return redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.SN_MODEL_LIST);
            case 2:
                return redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.SN_MODEL_LIST, produceSnExtList, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            case 3:
                redisMesPcn.deleteHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.SN_MODEL_LIST);
                return true;
            default:
                return null;
        }
    }

    private synchronized Object doHandleCurWorkOrder(StationRequestBean reqBean, Integer flag, EpSnBindOrderWorkOrderModel curWorkOrderNo) {
        switch (flag) {
            case 1:
                return redisMesPcn.getHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_WORK_ORDER);
            case 2:
                return redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_WORK_ORDER, curWorkOrderNo, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            case 3:
                redisMesPcn.deleteHash(getCacheKey(reqBean, MesPcnExtConstWords.PROD_DATA), MesPcnExtConstWords.CUR_WORK_ORDER);
                return true;
            default:
                return null;
        }
    }

    private void seqNoListCollect(List<String> seqNoList,String seqNoF, String seqNoS){
        for (int seq = Integer.valueOf(seqNoF)+1; seq <= Integer.valueOf(seqNoS); seq++){
            StringBuffer seqStrbuf = new StringBuffer();
            String seqStr = String.valueOf(seq);
            if(seqStr.length() < seqNoF.length()){
                while (seqStrbuf.length() < seqNoF.length()-seqStr.length()){
                    seqStrbuf.append("0");
                }
            }
            seqStrbuf.append(seqStr);
            seqNoList.add(seqStrbuf.toString());
        }
    }


}
