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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IPartExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.IWorkCenterExtService;
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.bu.IOuterPackageSnService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu.bu3.IMesOuterPackageService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.InnerPackageRecordModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOuterPackageDetailRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOuterPackageRepository;
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.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesPackageDetail;
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.exception.ImppExceptionBuilder;
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工步业务----新工位返工流程扫描小包装条码
 *
 * @USER: steven.peng
 * @DATE: 2023-01-11 10:20
 */
@Slf4j
@Service("sxInnerPackageScanStepService")
public class SxInnerPackageScanStepService extends BaseStepService {
    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private IWorkCenterExtService workCenterExtService;

    @Autowired
    private IPackageExtService packageExtService;

    @Autowired
    private IOuterPackageSnService iOuterPackageSnService;

    @Autowired
    private IPartExtService iPartExtService;

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

    @Autowired
    private IMesOuterPackageService iMesOuterPackageService;

    @Autowired
    private MesOuterPackageRepository mesOuterPackageRepository;

    @Autowired
    MesOuterPackageDetailRepository mesOuterPackageDetailRepository;

    @Override
    public StepResult guide(StationRequestBean reqBean) {
        StationResultBean resultBean = new StationResultBean();
        String guide = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请扫描小包装条码");
        this.sendMessage(reqBean, resultBean, guide, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
        return super.guide(reqBean);
    }

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

        String err;
        StationResultBean resultBean = new StationResultBean();

        // 获取当前工单，并判断当前工单类型
        MesWorkOrderExt workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo());
        if (null == workOrderExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]无效,不能操作"), reqBean.getWorkOrderNo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        if (MathOperation.compareTo(workOrderExt.getCompleteQty(), workOrderExt.getQty()) >= 0) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]数量已完成,不能操作"), reqBean.getWorkOrderNo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        if (MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue() != workOrderExt.getWorkOrderStatus()) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]状态为[%s],不能操作"),
                    reqBean.getWorkOrderNo(), MesPcnExtEnumUtil.WORK_ORDER_STATUS.valueOfDescription(workOrderExt.getWorkOrderStatus()));
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        // 获取上一工步扫描的大包装条码
        String outerPackageNo = (String) reqBean.getDataMap().get(MesPcnExtConstWords.OUTER_PACKAGE_NO);
        // 获取上一工步扫描的大包装条码的类型
        String outerPackageNoType = (String) reqBean.getDataMap().get(MesPcnExtConstWords.OUTER_PACKAGE_NO_TYPE);

        Integer snType = null;
        if(outerPackageNoType.equals(String.valueOf(MesPcnExtEnumUtil.ENCODE_CODE_TYPE.BIG_PACK_SN.getValue()))){
            snType = MesPcnExtEnumUtil.PRODUCE_SN_TYPE.STANDARD_SN.getValue();
        }else if(outerPackageNoType.equals(String.valueOf(MesPcnExtEnumUtil.ENCODE_CODE_TYPE.BIG_PACK_ATTEMPT_SN.getValue()))){
            snType = MesPcnExtEnumUtil.PRODUCE_SN_TYPE.ATTEMPT_SN.getValue();
        }else if(outerPackageNoType.equals(String.valueOf(MesPcnExtEnumUtil.ENCODE_CODE_TYPE.BIG_PACK_REWORK_SN.getValue()))){
            snType = MesPcnExtEnumUtil.PRODUCE_SN_TYPE.REMAKE_SN.getValue();
        }

        // 通过扫描的小包装条码获取包装明细
        MesPackageDetail packageDetail = packageExtService.getPackageDetailByPackNo(reqBean.getOrganizeCode(), reqBean.getScanInfo());
        if(null == packageDetail){
            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(), packageDetail.getSerialNumber());
        if (produceSnExt == null) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "小包装条码对应的本体条码[%s]无效"), packageDetail.getSerialNumber());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonCompleteStop(err);
        }
        if(produceSnExt.getSnType() != snType){
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "条码[%s]无效"), packageDetail.getSerialNumber());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonCompleteStop(err);
        }

        // 从redis里面取到小包装条码的记录
        List<InnerPackageRecordModel> outerPackageRecordModelList = getLastRecord(reqBean);
        if (!CollectionUtils.isEmpty(outerPackageRecordModelList)) {
            List<String> list =  outerPackageRecordModelList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getPackageNo()))).map(InnerPackageRecordModel::getPackageNo).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(list)) {
                Integer index = list.indexOf(reqBean.getScanInfo());
                if (index.compareTo(-1) != 0) {
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前小包装条码[%s]已扫描，不可重复扫描"), reqBean.getScanInfo());
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                    return StepResult.getNonComplete(err);
                }
            }

            List<Integer> snTypeList =  outerPackageRecordModelList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getSnType()))).map(InnerPackageRecordModel::getSnType).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(snTypeList)) {
                Integer i =  snTypeList.get(0);
                if (i != produceSnExt.getSnType()) {
                    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "当前小包装条码[%s]和之前扫描的小包装条码类型不符，请检查"), reqBean.getScanInfo());
                    this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                    return StepResult.getNonComplete(err);
                }
            }
        }

        //校验大包装条码表里面不能存在此小包装条码
        List<MesOuterPackageDetail> mesOuterPackageDetailList = iOuterPackageSnService.getOuterPackageDetailListByPackageNo(reqBean.getOrganizeCode(), reqBean.getScanInfo());
        if (!CollectionUtils.isEmpty(mesOuterPackageDetailList)) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "此小包装条码[%s]已经绑定大包装条码"), reqBean.getScanInfo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        // 校验小包装条码是否归属于此工单，校验小包装条码对应的本体条码是否归于此工单，校验小包装条码对应本体条码状态是不是正常下线的
        // 通过组织机构代码和扫描的小包装条码查询出工单信息
        MesPackageExt pack = packageExtService.getPackageByPackNo(reqBean.getOrganizeCode(), reqBean.getScanInfo());
        if (null == pack) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "内包装条码[%s]对应的工单不存在"), reqBean.getScanInfo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        // 若当前工单号和扫描的小包装条码工单号一致
        // 校验小包装条码对应的本体条码是否归于此工单
        MesOuterPackageDetail mesOuterPackageDetail = packageExtService.getBigPackageDetailByPackNoAndWorkOrderNo(reqBean.getOrganizeCode(), reqBean.getScanInfo(),reqBean.getWorkOrderNo());
        if (null == mesOuterPackageDetail) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "小包装条码[%s]不存在当前工单[%s]"), reqBean.getScanInfo(),reqBean.getWorkOrderNo());
            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(), packageDetail.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);
//        }
        // 当本体条码为正常下线的状态
        if (produceSnExt.getSnStatus().equals(MesPcnExtEnumUtil.PRODUCE_SN_STATUS.OFFLINE.getValue())) {
            return dealScan(reqBean, outerPackageNo,packageDetail);
        } else {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "扫描的小包装条码[%s]对应的本体条码[%s]的状态不为正常下线"), reqBean.getScanInfo(), packageDetail.getSerialNumber());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonCompleteStop(err);
        }

    }

    public StepResult dealScan(StationRequestBean reqBean, String outerPackageNo,MesPackageDetail packageDetail) {
        //查询物料表，获取物料信息里面配置的包装数量
        String err;
        StationResultBean resultBean = new StationResultBean();

        double size;
        try{
            size =  Double.valueOf(getLastOuterPackageSize(reqBean));
        }catch (Exception e){
            size = 0;
        }
        if(size == 0) {
            //查询物料表，获取物料信息里面配置的包装数量
            MesPartExt mesPartExt = iPartExtService.getPartExt(reqBean.getOrganizeCode(), reqBean.getPartNo());
            if (null != mesPartExt && !StringUtils.isEmpty(mesPartExt.getQty2())) {
                size = mesPartExt.getQty2();
                // 放入redis缓存 封包数量
                putOuterPackageSize(reqBean,size);
            } else {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "物料[%s]未配置包装数量"), reqBean.getPartNo());
                sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                return StepResult.getNonComplete(err);
            }
        }

        // 从redis里面取到小包装条码的记录
        List<InnerPackageRecordModel> innerPackageRecordModelList1 = new ArrayList<>();
        List<InnerPackageRecordModel> innerPackageRecordModelList = getLastRecord(reqBean);
        if (!CollectionUtils.isEmpty(innerPackageRecordModelList)) {
            innerPackageRecordModelList1.addAll(innerPackageRecordModelList);
        }
        //    reqBean.getDataMap().put(MesPcnExtConstWords.INNER_PACKAGE_NO
        MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), packageDetail.getSerialNumber());
        if (null == produceSnExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]无效"), packageDetail.getSerialNumber());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        ProduceSnExtModel snModel = new ProduceSnExtModel();
        BeanUtils.copyProperties(produceSnExt, snModel, MesPcnExtConstWords.ID);
        snModel.setWorkCenterCode(reqBean.getWorkCenterCode());
        snModel.setWorkCellCode(reqBean.getWorkCellCode());
        reqBean.getDataMap().put(MesPcnExtConstWords.PRODUCE_SN, snModel);

        InnerPackageRecordModel om = new InnerPackageRecordModel();
        om.setPackageNo(reqBean.getScanInfo());
        om.setOrganizeCode(reqBean.getOrganizeCode());
        om.setSerialNumber(packageDetail.getSerialNumber());
        om.setWorkOrderNo(reqBean.getWorkOrderNo());
        om.setSnType(produceSnExt.getSnType());
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss ");
        om.setCreateTime(sdf.format(new Date()));
        innerPackageRecordModelList1.add(om);
        // 校验扫描的小包装条码是否达到封箱数量
        // 表示此时需要进行封箱
        if (innerPackageRecordModelList1.size() == size) {
            List<String> list = innerPackageRecordModelList1.stream().map(InnerPackageRecordModel::getPackageNo).collect(Collectors.toList());

            MesWorkCenterExt workCenterExt = workCenterExtService.getWorkCenterExtByWorkCenterCode(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode());
            if (null == workCenterExt || (StringUtils.isEmpty(workCenterExt.getShiftCode()) || StringUtils.isEmpty(workCenterExt.getWorkTime()))) {
                this.sendMessage(reqBean, resultBean);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产线[%s]未启动班次"), reqBean.getWorkCenterCode());
                this.sendMessage(reqBean,  resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("生产线[%s]未启动班次", reqBean.getWorkCenterCode())
                        .build();
            }
            // 更新大包装条码主表数据
            MesOuterPackage mesOuterPackage = iMesOuterPackageService.getOutPackageBySn(reqBean.getOrganizeCode(), outerPackageNo);
            //mesOuterPackage.setPackageLabelTemplate(mesProdPackExt.getBigPackageLabelTemplate());
            mesOuterPackage.setWorkOrderNo(reqBean.getWorkOrderNo());
            mesOuterPackage.setShiftCode(workCenterExt.getShiftCode());
            mesOuterPackage.setWorkTime(workCenterExt.getWorkTime());
            mesOuterPackage.setWorkCenterCode(reqBean.getWorkCenterCode());
            mesOuterPackage.setWorkCellCode(reqBean.getWorkCellCode());
            mesOuterPackage.setLotNo(workCenterExt.getWorkTime());
            mesOuterPackage.setQty(new Double(list.size()));
            // 改为封箱
            mesOuterPackage.setIsSealed(MesPcnExtEnumUtil.PACKAGE_IS_SEALED.SEALED.getValue());
            //mesOuterPackageRepository.update(mesOuterPackage);
            mesOuterPackageRepository.save(mesOuterPackage);
            list.forEach(k -> {
                MesOuterPackageDetail md = iMesOuterPackageService.getOutPackageDetailByInnerSn(reqBean.getOrganizeCode(),k);

                md.setBindStatus(MesPcnExtEnumUtil.PACKAGE_BIND_STATUS.BINGDING.getValue());
                md.setOuterPackageNo(outerPackageNo);
                md.setInnerPackageNo(k);
                md.setWorkCenterCode(reqBean.getWorkCenterCode());
                md.setWorkCellCode(reqBean.getWorkCellCode());
                md.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
                md.setWorkOrderNo(reqBean.getWorkOrderNo());
                mesOuterPackageDetailRepository.save(md);
            });

            reqBean.getDataMap().put("outerPackageNo", outerPackageNo);
            reqBean.getDataMap().put("packageNoList", innerPackageRecordModelList1);
            deleteRecord(reqBean);
            // 删除redis中缓存的封包数量
            deleteOuterPackageSize(reqBean);

            return StepResult.getSuccessComplete();
        } else if (innerPackageRecordModelList1.size() < size) {
            // 若list为空 表示之前redis里面没值，直接放入即可
            if (null == innerPackageRecordModelList || innerPackageRecordModelList.size() == 0) {
                putLastRecord(reqBean, innerPackageRecordModelList1);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "小包装条码[%s]扫描成功！当前已扫描[%s]个小包装条码，距离打印大包装条码还需扫描[%s]个小包装条码"), reqBean.getScanInfo(), innerPackageRecordModelList1.size(), (size - innerPackageRecordModelList1.size()));
                sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                return StepResult.getNonComplete();
            }
            List<Integer> snTypeList = innerPackageRecordModelList1.stream().map(InnerPackageRecordModel::getSnType).collect(Collectors.toList());
            // 校验 正常大包装条码只可绑定正常小包装条码；再生改造大包装条码只可绑定再生改造小包装条码
            if (!(snTypeList.contains(produceSnExt.getSnType()))) {
                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.getNonComplete(err);
            }
            putLastRecord(reqBean, innerPackageRecordModelList1);
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "小包装条码[%s]扫描成功！当前已扫描[%s]个小包装条码，距离打印大包装条码还需扫描[%s]个小包装条码"), reqBean.getScanInfo(), innerPackageRecordModelList1.size(), (size - innerPackageRecordModelList1.size()));
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return StepResult.getNonComplete();
        } else {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "缓存的小包装条码数量大于封箱数量"), reqBean.getPartNo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

    }
    private String getLastSelectOrderKey(StationRequestBean reqBean) {
        return getCacheKey(reqBean, MesPcnExtConstWords.INNER_PACKAGE_REWORK);
    }

    private List<InnerPackageRecordModel> getLastRecord(StationRequestBean reqBean) {
        List<InnerPackageRecordModel> outerPackageRecordModelList = (List<InnerPackageRecordModel>) redisMesPcn.getObject(getLastSelectOrderKey(reqBean));
        if (null == outerPackageRecordModelList || outerPackageRecordModelList.size() == 0) {
            return outerPackageRecordModelList;
        } else {
            List<InnerPackageRecordModel> newList = outerPackageRecordModelList.stream().filter(o -> o.getWorkOrderNo().equals(reqBean.getWorkOrderNo()) && o.getOrganizeCode().equals(reqBean.getOrganizeCode())).collect(Collectors.toList());
            return newList;
        }
    }

    private List<InnerPackageRecordModel> getAllLastRecord(StationRequestBean reqBean) {
        List<InnerPackageRecordModel> outerPackageRecordModelList = (List<InnerPackageRecordModel>) redisMesPcn.getObject(getLastSelectOrderKey(reqBean));
        return outerPackageRecordModelList;
    }
    private void deleteRecord(StationRequestBean reqBean) {

        List<InnerPackageRecordModel> outerPackageRecordModelList = getAllLastRecord(reqBean);
        if(!CollectionUtils.isEmpty(outerPackageRecordModelList)){
            List<InnerPackageRecordModel> newList =  outerPackageRecordModelList.stream().filter(o -> !(o.getWorkOrderNo().equals(reqBean.getWorkOrderNo()) && o.getOrganizeCode().equals(reqBean.getOrganizeCode()))).collect(Collectors.toList());
            redisMesPcn.putObject(getLastSelectOrderKey(reqBean),newList,MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
        }
    }
    public void putLastRecord(StationRequestBean reqBean, List<InnerPackageRecordModel> outerPackageRecordModels) {

        List<InnerPackageRecordModel> outerPackageRecordModelList = getAllLastRecord(reqBean);
        if(CollectionUtils.isEmpty(outerPackageRecordModelList)){
            redisMesPcn.putObject(getLastSelectOrderKey(reqBean),outerPackageRecordModels,MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
        }else {
            List<InnerPackageRecordModel> newList =  outerPackageRecordModelList.stream().filter(o -> !(o.getWorkOrderNo().equals(reqBean.getWorkOrderNo()) && o.getOrganizeCode().equals(reqBean.getOrganizeCode()))).collect(Collectors.toList());

            if(CollectionUtils.isEmpty(newList)){
                newList = new ArrayList<>();
            }
            newList.addAll(outerPackageRecordModels);
            redisMesPcn.putObject(getLastSelectOrderKey(reqBean),newList,MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
        }
    }


    private String getLastSizeKey(StationRequestBean reqBean) {
        return getCacheKey(reqBean, MesPcnExtConstWords.OUTER_PACKAGE_SIZE)  + ":" + reqBean.getWorkOrderNo();
    }

    private Double getLastOuterPackageSize(StationRequestBean reqBean) {
        Double outerPackageSize = (Double) redisMesPcn.getObject(getLastSizeKey(reqBean));
        if(null == outerPackageSize){
            return 0.0;
        }
        return outerPackageSize;

    }

    private void deleteOuterPackageSize(StationRequestBean reqBean) {
        String cacheKey = getLastSizeKey(reqBean);
        if(StringUtils.isEmpty(cacheKey)){

        }else {
            redisMesPcn.deleteKey(cacheKey);
        }
    }

    public void putOuterPackageSize(StationRequestBean reqBean, double outerPackageSize) {
        redisMesPcn.putObject(getLastSizeKey(reqBean),outerPackageSize, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
    }

}
