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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IShiftExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.IShiftProdCenterRecordService;
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.IProduceSnRepairService;
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.api.busi.bu3.ISxEsopCenterRemainCfgService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.dao.IProduceSnRepairDao;
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.InnerOuterPackageModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesInnerPackageTravelRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProduceSnRepairRepository;
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.BaseModuleService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
import cn.estsh.i3plus.mes.pcn.util.PojoAttrUtil;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesShift;
import cn.estsh.i3plus.pojo.mes.bean.MesStateMachineStatus;
import cn.estsh.i3plus.pojo.mes.model.AttrBean;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
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.util.*;
import java.util.stream.Collectors;

/**
 * @Description : 展示组件：新工位大包装条码打印页面
 * @Reference :
 * @Author :steven.peng
 * @CreateDate : 2023-01-11 19:18
 * @Modify:
 **/
@Slf4j
@Service("sxOuterPackageModuleService")
public class SxOuterPackageModuleService extends BaseModuleService {

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private IWorkCenterExtService workCenterExtService;

    @Autowired
    private IShiftExtService shiftExtService;

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private IProduceSnRepairDao produceSnRepairDao;

    @Autowired
    private IShiftProdCenterRecordService shiftProdCenterRecordService;

    @Autowired
    private IPackageExtService iPackageExtService;

    @Autowired
    private IMesOuterPackageService iMesOuterPackageService;

    @Autowired
    IOuterPackageSnService iOuterPackageSnService;

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

    @Autowired
    private MesInnerPackageTravelRepository mesInnerPackageTravelRepository;

    @Autowired
    private IProduceSnRepairService produceSnRepairService;

    @Autowired
    private ISxEsopCenterRemainCfgService esopCenterRemainCfgService;

    private static final String LAST_SELECT_ORDER = "LAST_SELECT_ORDER";

    @Override
    public void init(StationRequestBean reqBean) {
        StationResultBean resultBean = new StationResultBean();
        String err;

        resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.MODULE_CONTENT.getValue());
        resultBean.setDataType(MesPcnEnumUtil.STATION_DATA_TYPE.CUSTOM.getValue());
        resultBean.setCustomPageName(MesPcnExtConstWords.NOUMENON_PACKAGE_MODULE);
       // resultBean.setDataAttrList(packDataAttrList(reqBean));
        resultBean.setSpecialDisplayData(getStepColIndent(reqBean));

        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();
        }
        MesShift shiftDb = shiftExtService.getShiftByShiftCodeAndCenterCode(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), workCenterExt.getShiftCode());
        if (null == shiftDb) {
            this.sendMessage(reqBean, resultBean);
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产线[%s]未关联班次代码[%s]"), reqBean.getWorkCenterCode(), workCenterExt.getShiftCode());
            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.BUSINESS_EXCEPTION.getCode())
                    .setErrorDetail("生产线[%s]未关联班次代码[%s]", reqBean.getWorkCenterCode(), workCenterExt.getShiftCode())
                    .build();
        }
        String workTime = shiftProdCenterRecordService.getWorkTime(shiftDb.getStartTime(), shiftDb.getEndTime());
        if (StringUtils.isEmpty(workTime) || !workTime.equals(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.BUSINESS_EXCEPTION.getCode())
                    .setErrorDetail("生产线[%s]需要重新启动班次", reqBean.getWorkCenterCode())
                    .build();
        }

        Boolean result = doPackLastSelectOrder(reqBean);
        if (StringUtils.isEmpty(reqBean.getWorkOrderNo())) {
            this.sendMessage(reqBean, resultBean);
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请选择工单");
            this.sendMessage(reqBean,  resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.BUSINESS_EXCEPTION.getCode())
                    .setErrorDetail("请选择工单")
                    .build();
        }
        MesWorkOrderExt workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo());
        if (null == workOrderExt || MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue() != workOrderExt.getWorkOrderStatus()) {
            if (result) {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put(MesPcnExtConstWords.WORK_ORDER_NO, reqBean.getWorkOrderNo());
                resultBean.setResultMap(resultMap);
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]不能在此工位操作"), reqBean.getWorkOrderNo());
                this.sendMessage(reqBean,  resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            }
            this.sendMessage(reqBean, resultBean);
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.BUSINESS_EXCEPTION.getCode())
                    .setErrorDetail("工单[%s]不能在此工位操作", reqBean.getWorkOrderNo())
                    .build();
        }
        //MesInnerPackageTravel mesInnerPackageTravel = new MesInnerPackageTravel();
        List<MesInnerPackageTravel> mesInnerPackageTravelList = null;

//      workOrderExt = new MesWorkOrderExt();
//      BeanUtils.copyProperties(workOrderExtList.get(0), workOrderExt);
        if (workOrderExt.getWorkOrderType() == MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue()) {
            mesInnerPackageTravelList = mesInnerPackageTravelRepository.findByProperty(
                    new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                            MesPcnExtConstWords.WORK_CENTER_CODE, MesPcnExtConstWords.WORK_CELL_CODE, MesPcnExtConstWords.LAST_WORK_ORDER_NO},
                    new Object[]{reqBean.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                            reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), reqBean.getWorkOrderNo()});
            reqBean.setWorkOrderNo(workOrderExt.getOrderNo());
            resultBean.setResultMap(packResultMap(workOrderExt));
            resultBean.setDataAttrList(packDataAttrListRework(reqBean));
            resultBean.setResultList(packResultListReWork(reqBean, workOrderExt,mesInnerPackageTravelList));
            this.sendMessage(reqBean, resultBean);

//                    produceSnTravelExtList = produceSnTravelExtService.getProduceSnTravelExtListByLastOrderNo(
//                            reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), reqBean.getWorkOrderNo());
        } else {
//                    produceSnTravelExtList = produceSnTravelExtService.getProduceSnTravelExtListByOrderNo(
//                            reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), reqBean.getWorkOrderNo());
            mesInnerPackageTravelList = mesInnerPackageTravelRepository.findByProperty(
                    new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                            MesPcnExtConstWords.WORK_CENTER_CODE, MesPcnExtConstWords.WORK_CELL_CODE, MesPcnExtConstWords.LAST_WORK_ORDER_NO},
                    new Object[]{reqBean.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                            reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), reqBean.getWorkOrderNo()});
            reqBean.setWorkOrderNo(workOrderExt.getOrderNo());
            resultBean.setResultMap(packResultMap(workOrderExt));
            resultBean.setDataAttrList(packDataAttrListRework(reqBean));
            resultBean.setResultList(packResultListNormal(reqBean, workOrderExt,mesInnerPackageTravelList));
            this.sendMessage(reqBean, resultBean);
        }

        esopCenterRemainCfgService.doCachedOrderByEsopCenterRemainCfg(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), workOrderExt);

    }
    private List<InnerOuterPackageModel> packResultListNormal(StationRequestBean reqBean, MesWorkOrderExt workOrderExt,List<MesInnerPackageTravel> mesInnerPackageTravelList) {
        List<InnerOuterPackageModel> modelList = null;
        // 通过工单号查询得到小包装条码表集合
        List<MesPackageExt> mesPackageExtList = iPackageExtService.getPackageListByWorkOrderNo(reqBean.getOrganizeCode(),reqBean.getWorkOrderNo());
        List<MesOuterPackageDetail>  outerPackageDetailList = iMesOuterPackageService.queryOuterPackageDetailByWorkOrderNo(reqBean.getOrganizeCode(),reqBean.getWorkOrderNo());
        Map<String, List<MesInnerPackageTravel>> snTravelExtMap = CollectionUtils.isEmpty(mesInnerPackageTravelList) ? null :
                mesInnerPackageTravelList.stream().filter(o -> null != o).collect(Collectors.groupingBy(MesInnerPackageTravel::getPackageNo));
       // List<MesProduceSnExt> produceSnExtList = produceSnExtService.getProduceSnExtListByOrderNo(reqBean.getOrganizeCode(), workOrderExt.getOrderNo());
        if (!CollectionUtils.isEmpty(mesPackageExtList)) {
            for (MesPackageExt mesPackageExt : mesPackageExtList) {
                if (null == mesPackageExt) {
                    continue;
                }
                if (CollectionUtils.isEmpty(modelList)) {
                    modelList = new ArrayList<>();
                }
                InnerOuterPackageModel model = new InnerOuterPackageModel();
                MesOuterPackageDetail mesOuterPackageDetail = iMesOuterPackageService.getBindingOutPackageDetailByInnerSn(reqBean.getOrganizeCode(),mesPackageExt.getPackageNo());
                if(!(null == mesOuterPackageDetail) && !StringUtils.isEmpty(mesOuterPackageDetail.getOuterPackageNo())){
                    model.setOuterPackageNo(mesOuterPackageDetail.getOuterPackageNo());
                }
                BeanUtils.copyProperties(mesPackageExt, model, MesPcnExtConstWords.ID);
                model.setInnerPackageNo(mesPackageExt.getPackageNo());
                model.setSnStatusName((!CollectionUtils.isEmpty(snTravelExtMap) && snTravelExtMap.containsKey(mesPackageExt.getPackageNo()))
                        ? LocaleUtil.transferLanguage(reqBean.getUserInfo(), "已扫描") : null);
                model.setCreateDatetime((!CollectionUtils.isEmpty(snTravelExtMap) && snTravelExtMap.containsKey(mesPackageExt.getPackageNo()))
                        ? snTravelExtMap.get(mesPackageExt.getPackageNo()).get(0).getCreateDatetime() : null);
                modelList.add(model);

                //model.setSnStatusName(MesPcnExtEnumUtil.PRODUCE_SN_STATUS.valueOfDescription(mesPackageExt.getPrintStatus()))
            }
        }
        List<InnerOuterPackageModel> resultList = null;
        if(!CollectionUtils.isEmpty(modelList)) {
            List<InnerOuterPackageModel> resultList1 = modelList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getCreateDatetime()))).collect(Collectors.toList());
            List<InnerOuterPackageModel> resultList2 = modelList.stream().filter(o -> (null != o && StringUtils.isEmpty(o.getCreateDatetime()))).collect(Collectors.toList());
            resultList1 = CollectionUtils.isEmpty(resultList1) ? null :
                    resultList1.stream().sorted(Comparator.comparing(InnerOuterPackageModel::getCreateDatetime).reversed()).collect(Collectors.toList());
            resultList2 = CollectionUtils.isEmpty(resultList2) ? null :
                    resultList2.stream().sorted(Comparator.comparing(InnerOuterPackageModel::getInnerPackageNo)).collect(Collectors.toList());

            if (!CollectionUtils.isEmpty(resultList1) || !CollectionUtils.isEmpty(resultList2)) {
                resultList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(resultList1)) {
                    resultList.addAll(resultList1);
                }
                if (!CollectionUtils.isEmpty(resultList2)) {
                    resultList.addAll(resultList2);
                }
            }
        }

        return resultList;
    }
    // 返工流程 去查大包装明细表获取数据
    private List<InnerOuterPackageModel> packResultListReWork(StationRequestBean reqBean, MesWorkOrderExt workOrderExt,List<MesInnerPackageTravel> mesInnerPackageTravelList) {
        List<InnerOuterPackageModel> modelList = null;
        // 通过工单号得到包装详细信息和条码信息
        List<MesOuterPackageDetail> outerPackageDetailList = iOuterPackageSnService.getOuterPackageDetailListByWorkOrderNo(reqBean.getOrganizeCode(),reqBean.getWorkOrderNo());
        if (!CollectionUtils.isEmpty(outerPackageDetailList)) {
            for (MesOuterPackageDetail mesOuterPackageDetail : outerPackageDetailList) {
                if (null == mesOuterPackageDetail) {
                    continue;
                }
                MesProduceSnRepair mesProduceSnRepair = produceSnRepairService.getProduceSnRepairBySnAndOrderNo(reqBean.getOrganizeCode(),mesOuterPackageDetail.getSerialNumber(),reqBean.getWorkOrderNo());

                if (CollectionUtils.isEmpty(modelList)) {
                    modelList = new ArrayList<>();
                }
                InnerOuterPackageModel model = new InnerOuterPackageModel();
                if(!(null == mesOuterPackageDetail) && !StringUtils.isEmpty(mesOuterPackageDetail.getOuterPackageNo())){
                    model.setOuterPackageNo(mesOuterPackageDetail.getOuterPackageNo());
                }
                BeanUtils.copyProperties(mesOuterPackageDetail, model, MesPcnExtConstWords.ID);
                model.setInnerPackageNo(mesOuterPackageDetail.getInnerPackageNo());
                model.setSnStatusName("");
                model.setCreateDatetime("");
                if (null != mesProduceSnRepair && !StringUtils.isEmpty(mesProduceSnRepair.getOutWorkCenterTime())){
                    model.setSnStatusName("已扫描");
                    model.setCreateDatetime(mesProduceSnRepair.getOutWorkCenterTime());
                }
                //  model.setSnStatusName(MesPcnExtEnumUtil.PRODUCE_SN_STATUS.valueOfDescription(mesPackageExt.getPrintStatus()));
               /* model.setSnStatusName((!CollectionUtils.isEmpty(snTravelExtMap) && snTravelExtMap.containsKey(mesPackageExt.getPackageNo()))
                        ? LocaleUtil.transferLanguage(reqBean.getUserInfo(), "已扫描") : null);
                model.setCreateDatetime((!CollectionUtils.isEmpty(snTravelExtMap) && snTravelExtMap.containsKey(mesPackageExt.getPackageNo()))
                        ? snTravelExtMap.get(mesPackageExt.getPackageNo()).get(0).getCreateDatetime() : null);*/
                modelList.add(model);
            }
        }
        List<InnerOuterPackageModel> resultList = null;
        if (!CollectionUtils.isEmpty(modelList)) {
            List<InnerOuterPackageModel> resultList1 = modelList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getOutWorkCenterTime()))).collect(Collectors.toList());
            List<InnerOuterPackageModel> resultList2 = modelList.stream().filter(o -> (null != o && StringUtils.isEmpty(o.getOutWorkCenterTime()))).collect(Collectors.toList());
            resultList1 = CollectionUtils.isEmpty(resultList1) ? null :
                    resultList1.stream().sorted(Comparator.comparing(InnerOuterPackageModel::getOutWorkCenterTime).reversed()).collect(Collectors.toList());
            resultList2 = CollectionUtils.isEmpty(resultList2) ? null :
                    resultList2.stream().sorted(Comparator.comparing(InnerOuterPackageModel::getInnerPackageNo)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(resultList1) || !CollectionUtils.isEmpty(resultList2)) {
                resultList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(resultList1)) {
                    resultList.addAll(resultList1);
                }
                if (!CollectionUtils.isEmpty(resultList2)) {
                    resultList.addAll(resultList2);
                }
            }
        }
        return resultList;
    }

    private List<ProduceSnExtModel> packResultList(StationRequestBean reqBean, MesWorkOrderExt workOrderExt) {
        List<ProduceSnExtModel> modelList = null;
        List<MesProduceSnExt> produceSnExtList = produceSnExtService.getProduceSnExtListByOrderNo(reqBean.getOrganizeCode(), workOrderExt.getOrderNo());
        if (!CollectionUtils.isEmpty(produceSnExtList)) {
            for (MesProduceSnExt produceSnExt : produceSnExtList) {
                if (null == produceSnExt) {
                    continue;
                }
                if (CollectionUtils.isEmpty(modelList)) {
                    modelList = new ArrayList<>();
                }
                ProduceSnExtModel model = new ProduceSnExtModel();
                BeanUtils.copyProperties(produceSnExt, model, MesPcnExtConstWords.ID);
                model.setSnStatusName(MesPcnExtEnumUtil.PRODUCE_SN_STATUS.valueOfDescription(produceSnExt.getSnStatus()));
                modelList.add(model);
            }
        } else {
            if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() == workOrderExt.getWorkOrderType()) {
                ProduceSnExtModel param = new ProduceSnExtModel(reqBean.getOrganizeCode(), workOrderExt.getOrderNo());
                modelList = produceSnRepairDao.queryProduceSnRepairList(param);
                if (!CollectionUtils.isEmpty(modelList)) {
                    for (ProduceSnExtModel model : modelList) {
                        if (null == model) {
                            continue;
                        }
                        model.setSnStatusName(MesPcnExtEnumUtil.PRODUCE_SN_STATUS.valueOfDescription(model.getSnStatus()));
                    }
                }
            }
        }

        List<ProduceSnExtModel> resultList = null;
        if (!CollectionUtils.isEmpty(modelList)) {
            List<ProduceSnExtModel> resultList1 = modelList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getOutWorkCenterTime()))).collect(Collectors.toList());
            List<ProduceSnExtModel> resultList2 = modelList.stream().filter(o -> (null != o && StringUtils.isEmpty(o.getOutWorkCenterTime()))).collect(Collectors.toList());
            resultList1 = CollectionUtils.isEmpty(resultList1) ? null :
                    resultList1.stream().sorted(Comparator.comparing(ProduceSnExtModel::getOutWorkCenterTime).reversed()).collect(Collectors.toList());
            resultList2 = CollectionUtils.isEmpty(resultList2) ? null :
                    resultList2.stream().sorted(Comparator.comparing(ProduceSnExtModel::getSerialNumber)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(resultList1) || !CollectionUtils.isEmpty(resultList2)) {
                resultList = new ArrayList<>();
                if (!CollectionUtils.isEmpty(resultList1)) {
                    resultList.addAll(resultList1);
                }
                if (!CollectionUtils.isEmpty(resultList2)) {
                    resultList.addAll(resultList2);
                }
            }
        }
        return resultList;
    }

    private Map<String, Object> packResultMap(MesWorkOrderExt workOrderExt) {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put(MesPcnExtConstWords.WORK_ORDER_NO, workOrderExt.getOrderNo());
        resultMap.put(MesPcnExtConstWords.WORK_ORDER_TYPE_NAME, MesPcnExtEnumUtil.WORK_ORDER_TYPE.valueOfDescription(workOrderExt.getWorkOrderType()));
        resultMap.put(MesPcnExtConstWords.PART_NO, workOrderExt.getPartNo());
        resultMap.put(MesPcnExtConstWords.PART_NAME, workOrderExt.getPartNameRdd());
        resultMap.put(MesPcnExtConstWords.QTY, workOrderExt.getQty());
        resultMap.put(MesPcnExtConstWords.COMPLETE_QTY, workOrderExt.getCompleteQty());
        resultMap.put(MesPcnExtConstWords.MISSING_QTY, workOrderExt.getMissingQty());
        return resultMap;
    }

    private List<AttrBean> packDataAttrList(StationRequestBean reqBean) {
        List<AttrBean> attrBeanList = new ArrayList<>();
       // PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.SERIAL_NUMBER, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.INNER_PACKAGE_NO, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "小包装条码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.OUTER_PACKAGE_NO, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "大包装条码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList,"snStatusName", LocaleUtil.transferLanguage(reqBean.getUserInfo(), "状态"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, "createDatetime", LocaleUtil.transferLanguage(reqBean.getUserInfo(), "扫描时间"));
        return attrBeanList;
    }
    private List<AttrBean> packDataAttrListRework(StationRequestBean reqBean) {
        List<AttrBean> attrBeanList = new ArrayList<>();
        // PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.SERIAL_NUMBER, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.INNER_PACKAGE_NO, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "小包装条码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, MesPcnExtConstWords.OUTER_PACKAGE_NO, LocaleUtil.transferLanguage(reqBean.getUserInfo(), "大包装条码"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList,"snStatusName", LocaleUtil.transferLanguage(reqBean.getUserInfo(), "状态"));
        PojoAttrUtil.loadPojoAttrs(attrBeanList, "createDatetime", LocaleUtil.transferLanguage(reqBean.getUserInfo(), "扫描时间"));
        return attrBeanList;
    }

    private Boolean doPackLastSelectOrder(StationRequestBean reqBean) {
        if (StringUtils.isEmpty(reqBean.getWorkOrderNo())) {
            String workOrderNo = getLastSelectOrder(reqBean);
            if (!StringUtils.isEmpty(workOrderNo)) {
                reqBean.setWorkOrderNo(workOrderNo);
                deleteLastSelectOrder(reqBean);
                return true;
            }
        }
        return false;
    }

    private String getLastSelectOrderKey(StationRequestBean reqBean) {
        return getCacheKey(reqBean, LAST_SELECT_ORDER);
    }

    private String getLastSelectOrder(StationRequestBean reqBean) {
        return (String) redisMesPcn.getObject(getLastSelectOrderKey(reqBean));
    }

    public void putLastSelectOrder(StationRequestBean reqBean) {
        redisMesPcn.putObject(getLastSelectOrderKey(reqBean), reqBean.getWorkOrderNo(), MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
    }

    private void deleteLastSelectOrder(StationRequestBean reqBean) {
        redisMesPcn.deleteKey(getLastSelectOrderKey(reqBean));
    }

    @Override
    public boolean execute(StationRequestBean reqBean) {
        return true;
    }

    @Override
    public boolean execStateModule(StationRequestBean reqBean, List<MesStateMachineStatus> states, Map<String, String> wcpcMap) {
        init(reqBean);
        return true;
    }

}
