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

import cn.estsh.i3plus.ext.mes.api.busi.ISxReportFormService;
import cn.estsh.i3plus.ext.mes.api.busi.bu.IBuSxReportFormService;
import cn.estsh.i3plus.ext.mes.apiservice.dao.IOutPutStatisticsDao;
import cn.estsh.i3plus.ext.mes.apiservice.dao.IProjectContainersComputeChartDao;
import cn.estsh.i3plus.ext.mes.apiservice.dao.IUnWorkTimeStatisticsDao;
import cn.estsh.i3plus.ext.mes.apiservice.dao.IWorkOrderExtDao;
import cn.estsh.i3plus.ext.mes.apiservice.dao.bu.IMesDataTravelDao;
import cn.estsh.i3plus.ext.mes.apiservice.dao.bu.IMesOuterPackageRetrospectDao;
import cn.estsh.i3plus.ext.mes.apiservice.util.EnumExtUtil;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesBusiErrorMessage;
import cn.estsh.i3plus.ext.mes.pojo.bean.SxMesDataTravel;
import cn.estsh.i3plus.ext.mes.pojo.model.CompleteRateChartModel;
import cn.estsh.i3plus.ext.mes.pojo.model.ProjectContainersComputeChartParamModel;
import cn.estsh.i3plus.ext.mes.pojo.model.ProjectContainersComputeChartResultModel;
import cn.estsh.i3plus.ext.mes.pojo.model.UnWorkTimeStatisticsModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.BuBusiErrorMessageModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.MesOuterPackageRetrospectModel;
import cn.estsh.i3plus.ext.mes.pojo.model.bu.SxMesDataTravelExtModel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesBusiErrorMessageRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.SxMesDataTravelRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesPartCategory;
import cn.estsh.i3plus.pojo.mes.repository.MesPartCategoryRepository;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
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.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class BuSxReportFormService implements IBuSxReportFormService {

    @Autowired
    private IOutPutStatisticsDao outPutStatisticsDao;

    @Autowired
    private MesPartCategoryRepository partCategoryRepository;

    @Autowired
    private IUnWorkTimeStatisticsDao unWorkTimeStatisticsDao;

    @Autowired
    private ISxReportFormService sxReportFormService;

    @Autowired
    private IProjectContainersComputeChartDao projectContainersComputeChartDao;

    @Autowired
    private MesBusiErrorMessageRepository busiErrorMessageRepository;

    @Autowired
    private SxMesDataTravelRepository dataTravelRepository;

    @Autowired
    private IMesDataTravelDao dataTravelDao;

    @Autowired
    private IMesOuterPackageRetrospectDao outerPackageRetrospectDao;

    @Autowired
    private IWorkOrderExtDao workOrderExtDao;

    private Double getDuration(String startTime, String endTime) {
        try {
            return new Double(TimeTool.getSecoundsBetweenTime(1, startTime, endTime) / 60);
        } catch (Exception e) {
            return new Double(0);
        }
    }

    private Double getTotalTimeAmount(List<UnWorkTimeStatisticsModel> modelList) {
        return modelList.stream().filter(o -> (null != o &&
                        !StringUtils.isEmpty(o.getStartTime()) && !StringUtils.isEmpty(o.getEndTime()) && !StringUtils.isEmpty(o.getWorkerQty()))).
                mapToDouble(o -> MathOperation.mul(getDuration(o.getStartTime(), o.getEndTime()), o.getWorkerQty())).sum();
    }

    @Override
    public String getRateDecimalTwo(Double d1, Double d2) {
        if (StringUtils.isEmpty(d2) || MathOperation.compareTo(d2, new Double(0)) == 0) {
            return null;
        }
        return MathOperation.div(MathOperation.mul(d1, 100), d2, 2) + MesExtConstWords.PERCENT;
    }

    @Override
    public ListPager<ProjectContainersComputeChartResultModel> queryProjectBadRate(ProjectContainersComputeChartParamModel paramModel, Pager pager) {
        pager = PagerHelper.getPager(pager, projectContainersComputeChartDao.queryProjectBadRateCount(paramModel));
        List<ProjectContainersComputeChartResultModel> resultModelListByDefectCode = projectContainersComputeChartDao.queryProjectBadRatePager(paramModel, pager);
        return new ListPager(resultModelListByDefectCode, pager);
    }

    @Override
    public ListPager<ProjectContainersComputeChartResultModel> queryProjectContainersComputeChart(ProjectContainersComputeChartParamModel paramModel, Pager pager) {
        switch (MesExtEnumUtil.PCC_DATA_DIMENSION.codeOf(paramModel.getDataDimension())) {
            case DEFECT_CODE:
                pager = PagerHelper.getPager(pager, projectContainersComputeChartDao.queryPartCheckDetailByDefectCodeCount(paramModel));
                List<ProjectContainersComputeChartResultModel> resultModelListByDefectCode= projectContainersComputeChartDao.queryPartCheckDetailByDefectCodePager(paramModel,pager);
                return new ListPager(resultModelListByDefectCode, pager);
            case BAD_PART_NO:
                pager = PagerHelper.getPager(pager, projectContainersComputeChartDao.queryPartCheckDetailByBadPartNoCount(paramModel));
                List<ProjectContainersComputeChartResultModel> resultModelListByBadPartNo= projectContainersComputeChartDao.queryPartCheckDetailByBadPartNoPager(paramModel,pager);
                return new ListPager(resultModelListByBadPartNo, pager);
            case BAD_PART_VENDER:
                pager = PagerHelper.getPager(pager, projectContainersComputeChartDao.queryPartCheckDetailByBadPartVenderCount(paramModel));
                List<ProjectContainersComputeChartResultModel> resultModelListByBadPartVender= projectContainersComputeChartDao.queryPartCheckDetailByBadPartVenderPager(paramModel,pager);
                return new ListPager(resultModelListByBadPartVender, pager);
            case REPAIR_JUDGE:
                pager = PagerHelper.getPager(pager, projectContainersComputeChartDao.queryPartCheckDetailByBadRepairJudgeCount(paramModel));
                List<ProjectContainersComputeChartResultModel> resultModelListByBadRepairJudge= projectContainersComputeChartDao.queryPartCheckDetailByRepairJudgePager(paramModel,pager);
                return new ListPager(resultModelListByBadRepairJudge, pager);
            default:
                return new ListPager(null, pager);
        }
    }

    /**
     * 根据不良项目维度查询工程集计
     *
     * @param paramModel
     * @return
     */
    public ListPager<ProjectContainersComputeChartResultModel> queryProjectContainersComputeChartByDefectCode(ProjectContainersComputeChartParamModel paramModel, Pager pager) {
        String organizeCode = paramModel.getOrganizeCode();
        //查询生产台数
        List<ProjectContainersComputeChartResultModel> produceQtyList= projectContainersComputeChartDao.queryProduceQty(paramModel);
        //
        for (ProjectContainersComputeChartResultModel model : produceQtyList) {
            projectContainersComputeChartDao.queryBadQty(paramModel);
        }
        return null;
    }


    @Override
    public List<CompleteRateChartModel> queryAttainmentRateChart(CompleteRateChartModel model) {
        String organizeCode = model.getOrganizeCode();

        //查询删除状态的工单
        //DdlPackBean packBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
        //DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.WORK_ORDER_STATUS.REMOVE.getValue(), MesExtConstWords.WORK_ORDER_STATUS, packBean);
        //List<MesWorkOrderExt> orderRemove = orderExtRepository.findByHqlWhere(packBean);
        //工单数据
        List<CompleteRateChartModel> workOrderModelList = workOrderExtDao.queryCompleteRateChart(model);
        //Map<String, CompleteRateChartModel> orderRemoveMap = workOrderModelList.stream().filter(completeRateChartModel -> completeRateChartModel.getWorkOrderStatus() == MesExtEnumUtil.WORK_ORDER_STATUS.REMOVE.getValue()).collect(Collectors.toMap(CompleteRateChartModel::getWorkOrderNo, supplier -> supplier));
        workOrderModelList = workOrderModelList.stream().filter(completeRateChartModel ->
                        completeRateChartModel.getWorkOrderStatus() != MesExtEnumUtil.WORK_ORDER_STATUS.REMOVE.getValue() &&
                        completeRateChartModel.getWorkOrderStatus() != MesExtEnumUtil.WORK_ORDER_STATUS.CREATE.getValue()
                ).collect(Collectors.toList());
        log.info("达成率一览表成功获取到工单计划数量数据,数量量[{}]",(workOrderModelList==null?0:workOrderModelList.size()));

        List<CompleteRateChartModel> resultList = null;
        List<CompleteRateChartModel> modelList = outPutStatisticsDao.queryCompleteRateChart(model);
        //modelList = modelList.stream().filter(m -> !orderRemoveMap.containsKey(m.getWorkOrderNo())).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(modelList) && CollectionUtils.isEmpty(workOrderModelList)) {
            log.info("达成率一览表未查询到数据,维度[{}][{}]",
                    MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.valueOfDescription(model.getDimension1()));
            return resultList;
        }
        log.info("达成率一览表已查询到数据,维度[{}][{}]",
                MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.valueOfDescription(model.getDimension1()));

        //通过工单和工作时间过滤掉与产量统计表中重复的数据
        Map<String, CompleteRateChartModel> workOrderMapByNoAndTime = workOrderModelList.stream().collect(Collectors.toMap(workOrderModel -> new StringJoiner(MesExtConstWords.AND).add(workOrderModel.getWorkOrderNo()).add(workOrderModel.getWorkTime().substring(0,10)).toString(), Function.identity() ));
        List<String> workOrderNoAndTimeList = modelList.stream().map(outPutStatisticsModel -> new StringJoiner(MesExtConstWords.AND).add(outPutStatisticsModel.getWorkOrderNo()).add(outPutStatisticsModel.getWorkTime()).toString()).collect(Collectors.toList());
        for (String wo : workOrderNoAndTimeList) {
            if(workOrderMapByNoAndTime.containsKey(wo)){
                workOrderMapByNoAndTime.remove(wo);
            }
        }
        //过滤数据后重新转换成List
        if(!CollectionUtils.isEmpty(workOrderMapByNoAndTime)){
            workOrderMapByNoAndTime.values().forEach(completeRateChartModel -> {
                completeRateChartModel.setWorkTime(completeRateChartModel.getWorkTime().substring(0, 10));
            });
            modelList.addAll(workOrderMapByNoAndTime.values());
        }

        //EP获取复用工单的计划数量数据
        List<CompleteRateChartModel> workOrderModelReuseList;
        if (organizeCode != null && organizeCode.contains(MesExtConstWords.CJ)){
            workOrderModelReuseList = workOrderExtDao.queryCompleteRateChartReuse(model);
            if(!CollectionUtils.isEmpty(workOrderModelReuseList)){
                workOrderModelReuseList = workOrderModelReuseList.stream().filter(completeRateChartModel ->
                        completeRateChartModel.getWorkOrderStatus() != MesExtEnumUtil.WORK_ORDER_STATUS.REMOVE.getValue() &&
                                completeRateChartModel.getWorkOrderStatus() != MesExtEnumUtil.WORK_ORDER_STATUS.CREATE.getValue()
                ).collect(Collectors.toList());
                workOrderModelReuseList.forEach(completeRateChartModel -> {
                    completeRateChartModel.setWorkTime(completeRateChartModel.getWorkTime().substring(0, 10));
                });
                modelList.addAll(workOrderModelReuseList);
            }
        }

        //根据时间维修修改日期字段为区间范围值
        packAttainmentRateChartWorkTime(modelList, model);
        //数据根据日期和物料分组转换Map
        Map<String, List<CompleteRateChartModel>> modelMap = packAttainmentRateChartMap(modelList, model.getDimension1());
        if (CollectionUtils.isEmpty(modelMap)) {
            return resultList;
        }

        log.info("达成率一览表根据维度封装数据成功,维度[{}][{}]",
                MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.valueOfDescription(model.getDimension1()));
        Map<String, List<MesPartCategory>> categoryCodeMap = getCategoryCode3List(organizeCode);

        for (String key : modelMap.keySet()) {
            if (StringUtils.isEmpty(key)) {
                continue;
            }
            List<CompleteRateChartModel> itemList = modelMap.get(key);
            if (CollectionUtils.isEmpty(itemList)) {
                continue;
            }
            if (!StringUtils.isEmpty(model.getPartNo()) && !model.getPartNo().equals(itemList.get(0).getPartNo())) {
                continue;
            }

            String[] keyArr = key.split(MesExtConstWords.AND);
            CompleteRateChartModel result = new CompleteRateChartModel();
            packAttainmentRateChartResult(result, keyArr, model.getDimension1(), itemList.get(0), categoryCodeMap);
            result.setPartNo(itemList.get(0).getPartNo());
            result.setPartNameRdd(itemList.get(0).getPartNameRdd());
            result.setPlanQty(getAttainmentRateChartPlanQty(itemList, keyArr[0], model.getDimension()));
            result.setQty(getAttainmentRateChartQty(itemList));
            result.setAttainmentRate(getRateDecimalTwo(result.getQty(), result.getPlanQty()));

            if (CollectionUtils.isEmpty(resultList)) {
                resultList = new ArrayList<>();
            }
            resultList.add(result);
        }

        log.info("达成率一览表数据开始重新排序,维度[{}][{}]",
                MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.valueOfDescription(model.getDimension1()));
        if (!CollectionUtils.isEmpty(resultList)) {
            resultList = sortAttainmentRateChartResult(resultList, model.getDimension1());
            log.info("达成率一览表数据重新排序成功,维度[{}][{}]",
                    MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.valueOfDescription(model.getDimension1()));
        }
        return resultList;
    }

    private Map<String, List<MesPartCategory>> getCategoryCode3List(String organizeCode) {
        Map<String, List<MesPartCategory>> codeMap = null;
        List<MesPartCategory> partCategoryList = partCategoryRepository.findByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID, MesExtConstWords.CATEGORY_TYPE},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), MesEnumUtil.PART_CATEGORY_TYPE.CATEGORY_THREE.getValue()});
        if (!CollectionUtils.isEmpty(partCategoryList)) {
            codeMap = partCategoryList.stream().filter(o -> null != o).collect(Collectors.groupingBy(MesPartCategory::getCategoryCode));
        }
        return codeMap;
    }

    private void packAttainmentRateChartWorkTime(List<CompleteRateChartModel> modelList, CompleteRateChartModel model) {
        if (MesExtEnumUtil.REPORT_DIMENSION_DATE.ONE.getValue() == model.getDimension()) {
            List<CompleteRateChartModel> modelListOrder = modelList.stream()
                    .filter(o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()))).sorted(Comparator.comparing(CompleteRateChartModel::getWorkTime)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(modelListOrder)) {
                String workTime = new StringJoiner(MesExtConstWords.TO)
                        .add(!StringUtils.isEmpty(model.getWorkTimeStart()) ? model.getWorkTimeStart() : modelListOrder.get(0).getWorkTime())
                        .add(!StringUtils.isEmpty(model.getWorkTimeEnd()) ? model.getWorkTimeEnd() : modelListOrder.get(modelListOrder.size() - 1).getWorkTime()).toString();
                modelList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()))).forEach(o -> {
                    o.setWorkTime(workTime);
                });
            }
        } else if (MesExtEnumUtil.REPORT_DIMENSION_DATE.YEAR.getValue() == model.getDimension()) {
            modelList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()))).forEach(o -> {
                o.setWorkTime(o.getWorkTime().substring(0, 4));
            });
        } else if (MesExtEnumUtil.REPORT_DIMENSION_DATE.MONTH.getValue() == model.getDimension()) {
            modelList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()))).forEach(o -> {
                o.setWorkTime(o.getWorkTime().substring(0, 7));
            });
        }
    }

    private Map<String, List<CompleteRateChartModel>> packAttainmentRateChartMap(List<CompleteRateChartModel> modelList, Integer dimension) {
        Map<String, List<CompleteRateChartModel>> modelMap;
        if (CollectionUtils.isEmpty(modelList)) {
            return null;
        }
        switch (MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.getByValue(dimension)) {
            case DATE:
                modelMap = modelList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()) && !StringUtils.isEmpty(o.getPartNo())))
                        .collect(Collectors.groupingBy(o -> (new StringJoiner(MesExtConstWords.AND)
                                .add(o.getWorkTime()).add(o.getPartNo()).toString())));
                break;
            case IS_DONT:
                modelMap = modelList.stream().filter(
                                o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()) && !StringUtils.isEmpty(o.getIsDont())))
                        .collect(Collectors.groupingBy(o -> (new StringJoiner(MesExtConstWords.AND)
                                .add(o.getWorkTime()).add(o.getIsDont()).add(o.getPartNo()).toString())));
                break;
            case DONT_CLASS:
                modelMap = modelList.stream().filter(
                                o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()) && !StringUtils.isEmpty(o.getIsDont())
                                        && !StringUtils.isEmpty(o.getDontClass())))
                        .collect(Collectors.groupingBy(o -> (new StringJoiner(MesExtConstWords.AND)
                                .add(o.getWorkTime()).add(o.getIsDont()).add(o.getDontClass()).add(o.getPartNo()).toString())));
                break;
            case CENTER:
            default:
                modelMap = modelList.stream().filter(
                                o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()) && !StringUtils.isEmpty(o.getIsDont())
                                        && !StringUtils.isEmpty(o.getDontClass()) && !StringUtils.isEmpty(o.getWorkCenterCode())))
                        .collect(Collectors.groupingBy(o -> (new StringJoiner(MesExtConstWords.AND)
                                .add(o.getWorkTime()).add(o.getIsDont()).add(o.getDontClass()).add(o.getWorkCenterCode()).add(o.getPartNo()).toString())));
                break;
        }
        return modelMap;
    }

    private void packAttainmentRateChartResult(CompleteRateChartModel result, String[] keyArr, Integer dimension,
                                               CompleteRateChartModel model, Map<String, List<MesPartCategory>> categoryCodeMap) {
        result.setWorkTime(keyArr[0]);
        if (!StringUtils.isEmpty(model.getCategoryCode3())) {
            result.setCategoryCode3(model.getCategoryCode3());
            result.setCategoryCode3Name((!CollectionUtils.isEmpty(categoryCodeMap) && categoryCodeMap.containsKey(model.getCategoryCode3())) ? categoryCodeMap.get(model.getCategoryCode3()).get(0).getCategoryName() : "");
        } else {
            result.setCategoryCode3("");
            result.setCategoryCode3Name("");
        }
        switch (MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.getByValue(dimension)) {
            case DATE:
                break;
            case IS_DONT:
                result.setIsDont(keyArr[1]);
                break;
            case DONT_CLASS:
                result.setIsDont(keyArr[1]);
                result.setDontClass(keyArr[2]);
                break;
            case CENTER:
            default:
                result.setIsDont(keyArr[1]);
                result.setDontClass(keyArr[2]);
                result.setWorkCenterCode(keyArr[3]);
                result.setWorkCenterName(model.getWorkCenterName());
                break;
        }
    }

    private Double getAttainmentRateChartPlanQty(List<CompleteRateChartModel> modelList, String workTime, Integer dimension) {
        if (CollectionUtils.isEmpty(modelList)){
            return new Double(0);
        }
        List<CompleteRateChartModel> resultList;
        if (MesExtEnumUtil.REPORT_DIMENSION_DATE.ONE.getValue() == dimension) {
            resultList = modelList.stream().filter(o -> (null != o &&
                    !StringUtils.isEmpty(o.getOrderStartTime()) && !StringUtils.isEmpty(o.getPlanQty()) &&
                    TimeTool.stringParseToDate(o.getOrderStartTime().substring(0, 10), MesExtConstWords.DATE_FORMAT).compareTo(TimeTool.stringParseToDate(modelList.get(0).getWorkTime().split(MesExtConstWords.TO)[0], MesExtConstWords.DATE_FORMAT)) >= 0 &&
                    TimeTool.stringParseToDate(o.getOrderStartTime().substring(0, 10), MesExtConstWords.DATE_FORMAT).compareTo(TimeTool.stringParseToDate(modelList.get(0).getWorkTime().split(MesExtConstWords.TO)[1], MesExtConstWords.DATE_FORMAT)) <= 0)).collect(Collectors.toList());
        } else {
            resultList = modelList.stream().filter(o -> (null != o &&
                    !StringUtils.isEmpty(o.getOrderStartTime()) && o.getOrderStartTime().contains(workTime) && !StringUtils.isEmpty(o.getPlanQty()))).collect(Collectors.toList());
        }
        resultList = CollectionUtils.isEmpty(resultList) ? null :
                resultList.stream().filter(o -> null != o).distinct()
                        .collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(CompleteRateChartModel::getWorkOrderNo))), ArrayList::new)
                        );
        return CollectionUtils.isEmpty(resultList) ? new Double(0) :
                resultList.stream().filter(o -> null != o).mapToDouble(CompleteRateChartModel::getPlanQty).sum();
    }

    private Double getAttainmentRateChartQty(List<CompleteRateChartModel> modelList) {
        return CollectionUtils.isEmpty(modelList) ? new Double(0) :
                modelList.stream().filter(o -> null != o).mapToDouble(CompleteRateChartModel::getQty).sum();
    }

    private List<CompleteRateChartModel> sortAttainmentRateChartResult(List<CompleteRateChartModel> resultList, Integer dimension) {
        switch (MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.getByValue(dimension)) {
            case DATE:
                resultList = resultList.stream().sorted(Comparator.comparing(CompleteRateChartModel::getWorkTime)
                        .thenComparing(CompleteRateChartModel::getCategoryCode3)
                        .thenComparing(CompleteRateChartModel::getPartNo)).collect(Collectors.toList());
                break;
            case IS_DONT:
                resultList = resultList.stream().sorted(Comparator.comparing(CompleteRateChartModel::getWorkTime)
                        .thenComparing(CompleteRateChartModel::getIsDont)
                        .thenComparing(CompleteRateChartModel::getCategoryCode3)
                        .thenComparing(CompleteRateChartModel::getPartNo)).collect(Collectors.toList());
                break;
            case DONT_CLASS:
                resultList = resultList.stream().sorted(Comparator.comparing(CompleteRateChartModel::getWorkTime)
                        .thenComparing(CompleteRateChartModel::getIsDont)
                        .thenComparing(CompleteRateChartModel::getDontClass)
                        .thenComparing(CompleteRateChartModel::getCategoryCode3)
                        .thenComparing(CompleteRateChartModel::getPartNo)).collect(Collectors.toList());
                break;
            case CENTER:
            default:
                resultList = resultList.stream().sorted(Comparator.comparing(CompleteRateChartModel::getWorkTime)
                        .thenComparing(CompleteRateChartModel::getIsDont)
                        .thenComparing(CompleteRateChartModel::getDontClass)
                        .thenComparing(CompleteRateChartModel::getWorkCenterCode)
                        .thenComparing(CompleteRateChartModel::getCategoryCode3)
                        .thenComparing(CompleteRateChartModel::getPartNo)).collect(Collectors.toList());
                break;
        }
        return resultList;
    }

    private List<CompleteRateChartModel> sortCompleteRateChartResult(List<CompleteRateChartModel> resultList, Integer dimension) {
        switch (MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.getByValue(dimension)) {
            case DATE:
                resultList = resultList.stream().sorted(Comparator.comparing(CompleteRateChartModel::getWorkTime)
                        .thenComparing(CompleteRateChartModel::getCategoryCode3)).collect(Collectors.toList());
                break;
            case IS_DONT:
                resultList = resultList.stream().sorted(Comparator.comparing(CompleteRateChartModel::getWorkTime)
                        .thenComparing(CompleteRateChartModel::getIsDont)
                        .thenComparing(CompleteRateChartModel::getCategoryCode3)).collect(Collectors.toList());
                break;
            case DONT_CLASS:
                resultList = resultList.stream().sorted(Comparator.comparing(CompleteRateChartModel::getWorkTime)
                        .thenComparing(CompleteRateChartModel::getIsDont)
                        .thenComparing(CompleteRateChartModel::getDontClass)
                        .thenComparing(CompleteRateChartModel::getCategoryCode3)).collect(Collectors.toList());
                break;
            case CENTER:
            default:
                resultList = resultList.stream().sorted(Comparator.comparing(CompleteRateChartModel::getWorkTime)
                        .thenComparing(CompleteRateChartModel::getIsDont)
                        .thenComparing(CompleteRateChartModel::getDontClass)
                        .thenComparing(CompleteRateChartModel::getWorkCenterCode)
                        .thenComparing(CompleteRateChartModel::getCategoryCode3)).collect(Collectors.toList());
                break;
        }
        return resultList;
    }

    @Override
    public List<CompleteRateChartModel> queryCompleteRateChart(CompleteRateChartModel model) {

        List<CompleteRateChartModel> workOrderModelList = workOrderExtDao.queryCompleteRateChart(model);

        //查询删除状态的工单
        //DdlPackBean packBean = DdlPackBean.getDdlPackBean(model.getOrganizeCode());
        //DdlPreparedPack.getNumEqualPack(MesExtEnumUtil.WORK_ORDER_STATUS.REMOVE.getValue(), MesExtConstWords.WORK_ORDER_STATUS, packBean);
        //List<MesWorkOrderExt> orderRemove = orderExtRepository.findByHqlWhere(packBean);
        //Map<String, MesWorkOrderExt> orderRemoveMap = orderRemove.stream().collect(Collectors.toMap(MesWorkOrderExt::getOrderNo, supplier -> supplier));

        workOrderModelList = workOrderModelList.stream().filter(completeRateChartModel ->
                completeRateChartModel.getWorkOrderStatus() != MesExtEnumUtil.WORK_ORDER_STATUS.REMOVE.getValue() &&
                        completeRateChartModel.getWorkOrderStatus() != MesExtEnumUtil.WORK_ORDER_STATUS.CREATE.getValue()
        ).collect(Collectors.toList());

        List<CompleteRateChartModel> resultList = null;
        List<CompleteRateChartModel> modelList = outPutStatisticsDao.queryCompleteRateChart(model);

        //modelList = modelList.stream().filter(m -> !orderRemoveMap.containsKey(m.getWorkOrderNo())).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(modelList) && CollectionUtils.isEmpty(workOrderModelList)) {
            log.info("完结率一览表未查询到数据,维度[{}][{}]",
                    MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.valueOfDescription(model.getDimension1()));
            return resultList;
        }

        log.info("完结率一览表已查询到数据,维度[{}][{}]",
                MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.valueOfDescription(model.getDimension1()));

        Map<String, CompleteRateChartModel> workOrderMapByNoAndTime = workOrderModelList.stream().collect(Collectors.toMap(workOrderModel -> new StringJoiner(MesExtConstWords.AND).add(workOrderModel.getWorkOrderNo()).add(workOrderModel.getWorkTime().substring(0,10)).toString(), Function.identity() ));
        List<String> workOrderNoAndTimeList = modelList.stream().map(outPutStatisticsModel -> new StringJoiner(MesExtConstWords.AND).add(outPutStatisticsModel.getWorkOrderNo()).add(outPutStatisticsModel.getWorkTime()).toString()).collect(Collectors.toList());

        for (String wo : workOrderNoAndTimeList) {
            if(workOrderMapByNoAndTime.containsKey(wo)){
                workOrderMapByNoAndTime.remove(wo);
            }
        }
        if(!CollectionUtils.isEmpty(workOrderMapByNoAndTime)){
            workOrderMapByNoAndTime.values().forEach(completeRateChartModel -> {
                completeRateChartModel.setWorkTime(completeRateChartModel.getWorkTime().substring(0, 10));
            });
            modelList.addAll(workOrderMapByNoAndTime.values());
        }

        packAttainmentRateChartWorkTime(modelList, model);

        Map<String, List<CompleteRateChartModel>> modelMap = packCompleteRateChartMap(modelList, model.getDimension1());
        if (CollectionUtils.isEmpty(modelMap)) {
            return resultList;
        }

        log.info("完结率一览表根据维度封装数据成功,维度[{}][{}]",
                MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.valueOfDescription(model.getDimension1()));

        Map<String, List<MesPartCategory>> categoryCodeMap = getCategoryCode3List(model.getOrganizeCode());

        for (String key : modelMap.keySet()) {
            if (StringUtils.isEmpty(key)) {
                continue;
            }
            List<CompleteRateChartModel> itemList = modelMap.get(key);
            if (CollectionUtils.isEmpty(itemList)) {
                continue;
            }

            String[] keyArr = key.split(MesExtConstWords.AND);
            CompleteRateChartModel result = new CompleteRateChartModel();
            packAttainmentRateChartResult(result, keyArr, model.getDimension1(), itemList.get(0), categoryCodeMap);

            result.setPlanQty(getAttainmentRateChartPlanQty(itemList, keyArr[0], model.getDimension()));
            result.setQty(getAttainmentRateChartQty(itemList));
            result.setAttainmentRate(getRateDecimalTwo(result.getQty(), result.getPlanQty()));
            result.setCompleteRate(getChartCompleteRate(itemList, keyArr[0], model.getDimension()));

            if (CollectionUtils.isEmpty(resultList)) {
                resultList = new ArrayList<>();
            }
            resultList.add(result);
        }

        log.info("完结率一览表数据开始重新排序,维度[{}][{}]",
                MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.valueOfDescription(model.getDimension1()));
        if (!CollectionUtils.isEmpty(resultList)) {
            resultList = sortCompleteRateChartResult(resultList, model.getDimension1());
            log.info("完结率一览表数据重新排序成功,维度[{}][{}]",
                    MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.valueOfDescription(model.getDimension1()));
        }
        return resultList;
    }


    private Map<String, List<CompleteRateChartModel>> packCompleteRateChartMap(List<CompleteRateChartModel> modelList, Integer dimension) {
        Map<String, List<CompleteRateChartModel>> modelMap;
        if (CollectionUtils.isEmpty(modelList)) {
            return null;
        }
        switch (MesExtEnumUtil.REPORT_DIMENSION_ATTAINMENT_RATE_CHART.getByValue(dimension)) {
            case DATE:
                modelMap = modelList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getWorkTime())))
                        .collect(Collectors.groupingBy(o -> (new StringJoiner(MesExtConstWords.AND)
                                .add(o.getWorkTime()).add(o.getCategoryCode3()).toString())));
                break;
            case IS_DONT:
                modelMap = modelList.stream().filter(
                                o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()) && !StringUtils.isEmpty(o.getIsDont())))
                        .collect(Collectors.groupingBy(o -> (new StringJoiner(MesExtConstWords.AND)
                                .add(o.getWorkTime()).add(o.getIsDont()).add(o.getCategoryCode3()).toString())));
                break;
            case DONT_CLASS:
                modelMap = modelList.stream().filter(
                                o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()) && !StringUtils.isEmpty(o.getIsDont())
                                        && !StringUtils.isEmpty(o.getDontClass())))
                        .collect(Collectors.groupingBy(o -> (new StringJoiner(MesExtConstWords.AND)
                                .add(o.getWorkTime()).add(o.getIsDont()).add(o.getDontClass()).add(o.getCategoryCode3()).toString())));
                break;
            case CENTER:
            default:
                modelMap = modelList.stream().filter(
                                o -> (null != o && !StringUtils.isEmpty(o.getWorkTime()) && !StringUtils.isEmpty(o.getIsDont())
                                        && !StringUtils.isEmpty(o.getDontClass()) && !StringUtils.isEmpty(o.getWorkCenterCode())))
                        .collect(Collectors.groupingBy(o -> (new StringJoiner(MesExtConstWords.AND)
                                .add(o.getWorkTime()).add(o.getIsDont()).add(o.getDontClass()).add(o.getWorkCenterCode()).add(o.getCategoryCode3()).toString())));
                break;
        }
        return modelMap;
    }

    private String getChartCompleteRate(List<CompleteRateChartModel> modelList, String workTime, Integer dimension) {
        String completeRate = null;
        Double total = new Double(0);
        Double complete = new Double(0);
        Map<String, List<CompleteRateChartModel>> partNoMap = modelList.stream().filter(o -> null != o).collect(Collectors.groupingBy(CompleteRateChartModel::getPartNo));
        for (String partNo : partNoMap.keySet()) {
            if (StringUtils.isEmpty(partNo)) {
                continue;
            }
            List<CompleteRateChartModel> itemList = partNoMap.get(partNo);
            Double planQty = getAttainmentRateChartPlanQty(itemList, workTime, dimension);
            Double qty = getAttainmentRateChartQty(itemList);
            String attainmentRate = getRateDecimalTwo(qty, planQty);
            if (StringUtils.isEmpty(attainmentRate)) {
                continue;
            }
            try {
                Double attainmentRateD = Double.valueOf(attainmentRate.substring(0, attainmentRate.length() - 1));
                if (MathOperation.compareTo(attainmentRateD, 95) >= 0 && MathOperation.compareTo(attainmentRateD, 100) <= 0) {
                    complete = MathOperation.add(complete, new Double(1));
                }
                total = MathOperation.add(total, new Double(1));
            } catch (NumberFormatException e) {
                continue;
            }
        }
        if (MathOperation.compareTo(total, new Double(0)) != 0) {
            completeRate = getRateDecimalTwo(complete, total);
        }
        return completeRate;
    }

    @Override
    public List<UnWorkTimeStatisticsModel> queryWorkTimeMonthlyPerformance(UnWorkTimeStatisticsModel model) {
        List<UnWorkTimeStatisticsModel> resultList = null;
        List<UnWorkTimeStatisticsModel> modelList = unWorkTimeStatisticsDao.queryUnWorkTimeStatistics(model);
        if (CollectionUtils.isEmpty(modelList)) {
            log.info("BU作业时间月绩未查询到数据,维度[{}][{}]",
                    MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_UN_WORK_TIME_STATISTICS.valueOfDescription(model.getDimension1()));
            return resultList;
        }
        log.info("BU作业时间月绩已查询到数据,维度[{}][{}]",
                MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_UN_WORK_TIME_STATISTICS.valueOfDescription(model.getDimension1()));
        sxReportFormService.packUnWorkTimeStatisticsWorkTime(modelList, model);

        Map<String, List<UnWorkTimeStatisticsModel>> modelMap= sxReportFormService.packUnWorkTimeStatisticsMap(modelList, model.getDimension1());
        if (CollectionUtils.isEmpty(modelMap)) {
            return resultList;
        }

        log.info("BU作业时间月绩根据维度封装数据成功,维度[{}][{}]",
                MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_UN_WORK_TIME_STATISTICS.valueOfDescription(model.getDimension1()));

        model.setWorkOrderType(MesExtEnumUtil.WORK_ORDER_TYPE.STANDARD_ORDER.getValue());
        List<UnWorkTimeStatisticsModel> outPutStatisticsList = outPutStatisticsDao.queryOutPutStatisticsForReportForm(model);
        if (!CollectionUtils.isEmpty(outPutStatisticsList)) {

            sxReportFormService.packUnWorkTimeStatisticsWorkTime(outPutStatisticsList, model);
        } else {
            log.info("BU作业时间月绩未查询到产量数据,维度[{}][{}]",
                    MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_UN_WORK_TIME_STATISTICS.valueOfDescription(model.getDimension1()));
        }

        log.info("BU作业时间月绩已查询到产量数据,维度[{}][{}]",
                MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_UN_WORK_TIME_STATISTICS.valueOfDescription(model.getDimension1()));

        Map<String, List<UnWorkTimeStatisticsModel>> outPutStatisticsMap = sxReportFormService.packUnWorkTimeStatisticsMap(outPutStatisticsList, model.getDimension1());

        log.info("BU作业时间月绩根据维度封装产量数据成功,维度[{}][{}]",
                MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_UN_WORK_TIME_STATISTICS.valueOfDescription(model.getDimension1()));

        for (String key : modelMap.keySet()) {
            if (StringUtils.isEmpty(key)) {
                continue;
            }
            List<UnWorkTimeStatisticsModel> itemList = modelMap.get(key);
            if (CollectionUtils.isEmpty(itemList)) {
                continue;
            }

            String[] keyArr = key.split(MesExtConstWords.AND);
            UnWorkTimeStatisticsModel result = new UnWorkTimeStatisticsModel();
            sxReportFormService.packWorkTimeStatisticsResult(result, keyArr, model.getDimension1(), itemList.get(0));

            //总勤务时间=班次时长(常规生产类型)*人数汇总+加班时长(非常规生产类型)*人数汇总
            result.setTotalServiceTime(sxReportFormService.getTotalServiceTime(itemList));
            //非作业时间=停机时长（异常结束时间-开始时间）*人数然后汇总
            result.setTotalUnWorkTime(sxReportFormService.getTotalUnWorkTime(itemList, null));
            //非作业率=非作业时间/总勤务时间*100%,保留2位小数
            result.setUnWorkTimeRate(getRateDecimalTwo(result.getTotalUnWorkTime(), result.getTotalServiceTime()));
            //总作业时间=勤务时间-非作业时间
            result.setTotalWorkTime(MathOperation.sub(result.getTotalServiceTime(), result.getTotalUnWorkTime()));
            //总标准作业时间=各产品实绩数量*产品标准工时然后进行汇总
            result.setTotalStandardWorkTime(sxReportFormService.getTotalStandardWorkTime(outPutStatisticsMap, key));
            //St能率=标准作业时间/作业时间,以百分比显示,取2位小数
            result.setStRate(getRateDecimalTwo(result.getTotalStandardWorkTime(), result.getTotalWorkTime()));
            //综合能率=标准作业时间/勤务时间,以百分比显示,取2位小数
            result.setSynthesizeRate(getRateDecimalTwo(result.getTotalStandardWorkTime(), result.getTotalServiceTime()));
            //能率损失=作业时间-标准作业时间,如果是负数则显示0
            result.setLoseWorkTime(sxReportFormService.getLoseWorkTime(result.getTotalWorkTime(), result.getTotalStandardWorkTime()));
            //稼动率=作业时间/勤务时间,以百分比显示,取2位小数
            result.setUtilizationRate(getRateDecimalTwo(result.getTotalWorkTime(), result.getTotalServiceTime()));

            if (CollectionUtils.isEmpty(resultList)) {
                resultList = new ArrayList<>();
            }
            resultList.add(result);
        }

        log.info("BU作业时间月绩数据开始重新排序,维度[{}][{}]",
                MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_UN_WORK_TIME_STATISTICS.valueOfDescription(model.getDimension1()));
        if (!CollectionUtils.isEmpty(resultList)) {
            resultList = sxReportFormService.sortWorkTimeStatisticsResult(resultList, model.getDimension1());
            log.info("BU作业时间月绩数据重新排序成功,维度[{}][{}]",
                    MesExtEnumUtil.REPORT_DIMENSION_DATE.valueOfDescription(model.getDimension()), MesExtEnumUtil.REPORT_DIMENSION_UN_WORK_TIME_STATISTICS.valueOfDescription(model.getDimension1()));
        }
        return resultList;
    }

    @Override
    public ListPager<MesBusiErrorMessage> queryBusiErrorMassageByListPager(BuBusiErrorMessageModel model, Pager pager) {
        DdlPackBean packBean =  MesExtHqlPack.getBusiErrorMessage(model);
        packBean.setOrderByStr(" order by createDatetime desc ");
        pager = PagerHelper.getPager(pager,busiErrorMessageRepository.findByHqlWhereCount(packBean));
        List<MesBusiErrorMessage> busiErrorMessageList = busiErrorMessageRepository.findByHqlWherePage(packBean, pager);
        return new ListPager(busiErrorMessageList, pager);
    }

    /*************************** 数据履历报表 START ****************************/
    @Override
    public ListPager<SxMesDataTravelExtModel> queryMesDataTravelByListPager(SxMesDataTravel model, Pager pager) {
        pager = PagerHelper.getPager(pager, dataTravelDao.querySxMesDataTravelExtCount(model));
        List<SxMesDataTravelExtModel> dataTravelList = dataTravelDao.querySxMesDataTravelExtByPager(model , pager);
        //DdlPackBean packBean =  MesExtHqlPack.getMesDataTravel(model);
        //packBean.setOrderByStr(" order by createDatetime desc ");
        //pager = PagerHelper.getPager(pager, dataTravelRepository.findByHqlWhereCount(packBean));
        //List<SxMesDataTravel> dataTravelList = dataTravelRepository.findByHqlWherePage(packBean, pager);
        //注释 增加字段名称的代码
//        Map<String, MesTravelMenuFieldConfig> travelMenuFieldConfigMap = travelMenuFieldConfigService.getTravelMenuFieldConfig(model.getOrganizeCode());
//        if(!CollectionUtils.isEmpty(dataTravelList)){
//            for (SxMesDataTravelExtModel dataTravel : dataTravelList) {
//                String key =  new StringJoiner(MesExtConstWords.AND).add(dataTravel.getMenuCode().toString()).add(dataTravel.getFieldCode()).toString();
//                if(travelMenuFieldConfigMap.containsKey(key)){
//                    MesTravelMenuFieldConfig mesTravelMenuFieldConfig = travelMenuFieldConfigMap.get(key);
//                    dataTravel.setFieldName(mesTravelMenuFieldConfig.getFieldName());
//                }
//            }
//        }
        return new ListPager(dataTravelList, pager);
    }

    @Override
    public void updateDataTravel(SxMesDataTravel model) {
        SxMesDataTravel existed = dataTravelRepository.getById(model.getId());
        if (existed == null) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("数据履历[%s]记录不存在", model.getFieldCode())
                    .build();
        }
        existed.setVoucher(model.getVoucher());
        existed.setInitialWorkpieceNo(model.getInitialWorkpieceNo());
        ConvertBean.serviceModelUpdate(existed, model.getModifyUser());
        dataTravelRepository.save(model);
    }

    @Override
    public void dataTravelResultConfirm(Long[] ids, Integer confirmType, String userName) {
        for (Long id : ids) {
            SxMesDataTravel dataTravelById = dataTravelRepository.getById(id);
            if(dataTravelById == null){
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                        .setErrorDetail("id【%s】记录不存在",id)
                        .build();
            }
            if(confirmType == MesExtEnumUtil.DATA_TRAVEL_CONFIRM_TYPE.SKILL_CONFIRM.getValue()){
                dataTravelById.setSkillIsConfirm(MesExtEnumUtil.CONFIRM_TYPE.YES.getValue());
                dataTravelById.setSkillIsConfirmUser(userName);
                dataTravelById.setSkillIsConfirmTime(TimeTool.getNowTime(MesExtConstWords.DATETIME_FORMAT));
            }else{
                dataTravelById.setManufacturingIsConfirm(MesExtEnumUtil.CONFIRM_TYPE.YES.getValue());
                dataTravelById.setManufacturingIsConfirmUser(userName);
                dataTravelById.setManufacturingIsConfirmTime(TimeTool.getNowTime(MesExtConstWords.DATETIME_FORMAT));
            }
            dataTravelRepository.save(dataTravelById);
        }
    }

    private void getContentDesc(SxMesDataTravel dataTravel, String enumClassName){
        try {
            Map<String, String> enumValueAndDescByName = EnumExtUtil.getEnumValueAndDescByName(MesExtEnumUtil.class, enumClassName);
            dataTravel.setBeforeChange(enumValueAndDescByName.get(dataTravel.getBeforeChange()));
            dataTravel.setAfterChange(enumValueAndDescByName.get(dataTravel.getAfterChange()));
        } catch (Exception e) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("菜单【%s】-字段【%s】-转换类【%s】转换异常", MesExtEnumUtil.FIELD_MENU_TYPE.valueOfDescription(dataTravel.getMenuCode()), dataTravel.getFieldName(), enumClassName)
                    .build();
        }

    }


    /*************************** 数据履历报表 END ****************************/

    @Override
    public ListPager queryMesOuterPackageRetrospectPager(MesOuterPackageRetrospectModel model, Pager pager) {
        pager = PagerHelper.getPager(pager, outerPackageRetrospectDao.queryMesOuterPackageRetrospectCount(model));
        List<MesOuterPackageRetrospectModel> outerPackageRetrospectModels = outerPackageRetrospectDao.queryMesOuterPackageRetrospectByPager(model , pager);
        for (MesOuterPackageRetrospectModel outerPackageRetrospectModel : outerPackageRetrospectModels) {
            outerPackageRetrospectModel.setIsSealedName(MesEnumUtil.PACKAGE_IS_SEALED.valueOfDescription(outerPackageRetrospectModel.getIsSealed()));
            outerPackageRetrospectModel.setWorkOrderTypeName(outerPackageRetrospectModel.getWorkOrderType() != null ? MesExtEnumUtil.WORK_ORDER_TYPE.valueOfDescription(outerPackageRetrospectModel.getWorkOrderType()) : "");
        }
        return new ListPager(outerPackageRetrospectModels, pager);
    }
}
