//package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.bu3;
//
//import cn.estsh.i3plus.ext.mes.pcn.api.base.ICheckErrorLogService;
//import cn.estsh.i3plus.ext.mes.pcn.api.base.IWorkOrderStartService;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.*;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.*;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
//import cn.estsh.i3plus.mes.pcn.api.iservice.busi.ISyncFuncService;
//import cn.estsh.i3plus.platform.common.convert.ConvertBean;
//import cn.estsh.i3plus.platform.common.tool.TimeTool;
//import cn.estsh.i3plus.pojo.base.bean.BaseResultBean;
//import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
//import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
//import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
//import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel;
//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 java.util.*;
//import java.util.stream.Collectors;
//
///**
// * @author wangjie
// * @version 1.0
// * @date 2021/1/13 9:28
// **/
//@Slf4j
//@Service("buQManageOrderService")
//public class BuQManageOrderService implements IWorkOrderStartService {
//
//    @Autowired
//    private MesWorkCenterExtRepository workCenterExtRepository;
//
//    @Autowired
//    private MesOrderRuleRepository orderRuleRepository;
//
//    @Autowired
//    private MesProcessCellRepository processCellRepository;
//
//    @Autowired
//    private MesProcessQManageRepository processQManageRepository;
//
//    @Autowired
//    private MesOrderJobRepository orderJobRepository;
//
//    @Autowired
//    private MesQManageTaskDetailRepository qManageTaskDetailRepository;
//
//    @Autowired
//    private ISyncFuncService syncFuncService;
//
//    @Autowired
//    private ICheckErrorLogService checkErrorLogService;
//
//    @Override
//    public void doGenCheckOrder(MesWorkOrderExt workOrderExt, String userInfo) {
//        MesCheckErrorLog checkErrorLog = new MesCheckErrorLog();
//        checkErrorLog.setGenerationMethod(MesPcnExtEnumUtil.GENERATION_METHOD.FROM_WORK_ORDER_START.getValue());
//        checkErrorLog.setGeneratingModule(MesPcnExtEnumUtil.MES_OR_ORDER_TYPE.Q_MANAGE.getDescription());
//
//        String msg;
//        try {
//            MesWorkCenterExt workCenterExt = workCenterExtRepository.getByProperty(
//                    new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.WORK_CENTER_CODE},
//                    new Object[]{workOrderExt.getOrganizeCode(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), workOrderExt.getWorkCenterCode()});
//            if (null == workCenterExt) {
//                msg = String.format("Q管理生成单据:工单[%s]生产线[%s]无效", workOrderExt.getOrderNo(), workOrderExt.getWorkCenterCode());
//                log.info(msg);
//                checkErrorLogService.insertCheckErrorLog(checkErrorLog, msg, userInfo);
//                return;
//            }
//            if (StringUtils.isEmpty(workCenterExt.getShiftCode()) || StringUtils.isEmpty(workCenterExt.getWorkTime())) {
//                msg = String.format("Q管理生成单据:工单[%s]生产线[%s]未启动班次", workOrderExt.getOrderNo(), workOrderExt.getWorkCenterCode());
//                log.info(msg);
//                checkErrorLogService.insertCheckErrorLog(checkErrorLog, msg, userInfo);
//                return;
//            }
//            List<MesOrderRule> orderRuleList = orderRuleRepository.findByProperty(
//                    new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.IS_DELETED,
//                            MesPcnExtConstWords.ORDER_TYPE, MesPcnExtConstWords.CREATE_RULE, MesPcnExtConstWords.WORK_CENTER_CODE},
//                    new Object[]{workOrderExt.getOrganizeCode(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(),
//                            MesPcnExtEnumUtil.MES_OR_ORDER_TYPE.Q_MANAGE.getValue(), MesPcnExtEnumUtil.MES_OR_CREATE_RULE.WORK_ORDER_CREATE.getValue(), workOrderExt.getWorkCenterCode()});
//            Optional<MesOrderRule> orderRuleOp = CollectionUtils.isEmpty(orderRuleList) ? null :
//                    orderRuleList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getShiftCode()) && o.getShiftCode().equals(workCenterExt.getShiftCode()))).findFirst();
//            if (null == orderRuleOp || !orderRuleOp.isPresent()) {
//                orderRuleOp = CollectionUtils.isEmpty(orderRuleList) ? null : orderRuleList.stream().filter(o -> (null != o && StringUtils.isEmpty(o.getShiftCode()))).findFirst();
//            }
//            if (null == orderRuleOp || !orderRuleOp.isPresent()) {
//                msg = String.format("Q管理生成单据:工单[%s]未配置生产线代码[%s][工单创建][Q管理]班次为[%s]或班次为空的单据规则信息,请检查", workOrderExt.getOrderNo(), workOrderExt.getWorkCenterCode(), workCenterExt.getShiftCode());
//                log.info(msg);
//                checkErrorLogService.insertCheckErrorLog(checkErrorLog, msg, userInfo);
//                return;
//            }
//
//            List<MesProcessCell> processCellList = processCellRepository.findByProperty(
//                    new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.WORK_CENTER_CODE},
//                    new Object[]{workOrderExt.getOrganizeCode(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), workOrderExt.getWorkCenterCode()});
//            Map<String, List<MesProcessCell>> processCodeMap = CollectionUtils.isEmpty(processCellList) ? null :
//                    processCellList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getProcessCode()))).collect(Collectors.groupingBy(MesProcessCell::getProcessCode));
//            if (CollectionUtils.isEmpty(processCodeMap)) {
//                msg = String.format("Q管理生成单据:工单[%s]生产线代码[%s]未维护工序工位信息", workOrderExt.getOrderNo(), workOrderExt.getWorkCenterCode());
//                log.info(msg);
//                checkErrorLogService.insertCheckErrorLog(checkErrorLog, msg, userInfo);
//                return;
//            }
//            List<String> processCodeList = new ArrayList<>(processCodeMap.keySet());
//
//            DdlPackBean packBean = DdlPackBean.getDdlPackBean(workOrderExt.getOrganizeCode());
//            DdlPreparedPack.getStringEqualPack(workOrderExt.getPartNo(), MesPcnExtConstWords.PART_NO, packBean);
//            DdlPreparedPack.getStringEqualPack(MesPcnExtEnumUtil.MES_Q_MANAGER_CHECK_TYPE.ORDER_CHECK.getValue(), MesPcnExtConstWords.CHECK_TYPE, packBean);
//            DdlPreparedPack.getInPackList(processCodeList, MesPcnExtConstWords.PROCESS_CODE, packBean);
//
//            List<MesProcessQManage> processQManageList = processQManageRepository.findByHqlWhere(packBean);
//            if (CollectionUtils.isEmpty(processQManageList)) {
//                msg = String.format("Q管理生成单据:工单[%s]物料编码[%s]与工序代码%s未配置Q管理项目信息,请检查", workOrderExt.getOrderNo(), workOrderExt.getPartNo(), processCodeList.toString());
//                log.info(msg);
//                checkErrorLogService.insertCheckErrorLog(checkErrorLog, msg, userInfo);
//                return;
//            }
//
//            //根据单据类型【Q管理】、产线代码和产线信息中的班次代码、生产日期获取Q管理单信息
//            packBean = DdlPackBean.getDdlPackBean(workOrderExt.getOrganizeCode());
//            DdlPreparedPack.getNumEqualPack(MesPcnExtEnumUtil.MES_OR_ORDER_TYPE.Q_MANAGE.getValue(), MesPcnExtConstWords.ORDER_TYPE, packBean);
//            DdlPreparedPack.getStringEqualPack(workOrderExt.getWorkCenterCode(), MesPcnExtConstWords.WORK_CENTER_CODE, packBean);
//            DdlPreparedPack.getStringEqualPack(workCenterExt.getShiftCode(), MesPcnExtConstWords.SHIFT_CODE, packBean);
//            DdlPreparedPack.getStringEqualPack(workCenterExt.getWorkTime(), MesPcnExtConstWords.WORK_TIME, packBean);
//            DdlPreparedPack.getStringEqualPack(workOrderExt.getOrderNo(), MesPcnExtConstWords.WORK_ORDER_NO, packBean);
//            List<MesOrderJob> orderJobList = orderJobRepository.findByHqlWhere(packBean);
//            if (CollectionUtils.isEmpty(orderJobList)) {
//                String orderNo = insertMesOrderJob(workOrderExt, workCenterExt, orderRuleOp.get().getTaskType(), userInfo, checkErrorLog);
//                if (StringUtils.isEmpty(orderNo)) {
//                    return;
//                }
//                insertMesQManageTaskDetail(orderNo, processQManageList, processCodeMap, userInfo);
//            } else {
//                //根据作业单号查询明细数据
//                List<MesQManageTaskDetail> qManageTaskDetailList = new ArrayList<>();
//                List<String> orderNoList = orderJobList.stream().filter(o -> null != o).map(MesOrderJob::getOrderNo).collect(Collectors.toList());
//                for (String orderNo : orderNoList) {
//                    if (StringUtils.isEmpty(orderNo)) {
//                        continue;
//                    }
//                    packBean = DdlPackBean.getDdlPackBean(workOrderExt.getOrganizeCode());
//                    DdlPreparedPack.getStringEqualPack(orderNo, MesPcnExtConstWords.ORDER_NO, packBean);
//                    qManageTaskDetailList.addAll(qManageTaskDetailRepository.findByHqlWhere(packBean));
//                }
//
//                Map<String, List<MesProcessQManage>> processQManageDbMap = CollectionUtils.isEmpty(processQManageList) ? null :
//                        processQManageList.stream().filter(o -> null != o).collect(Collectors.groupingBy(o -> new StringJoiner(MesPcnExtConstWords.AND)
//                                .add(o.getProcessCode()).add(o.getPartNo()).add(o.getManageProjects()).add(o.getScrewType()).add(o.getTighteningTorque()).toString()));
//
//                Map<String, List<MesQManageTaskDetail>> qManageTaskDetailDbMap = CollectionUtils.isEmpty(qManageTaskDetailList) ? null :
//                        qManageTaskDetailList.stream().filter(o -> null != o).collect(Collectors.groupingBy(o -> new StringJoiner(MesPcnExtConstWords.AND)
//                                .add(o.getProcessCode()).add(o.getPartNo()).add(o.getManageProjects()).add(o.getScrewType()).add(o.getTighteningTorque()).toString()));
//
//                List<MesProcessQManage> newProcessQManageList = null;
//                //获取两个存在差异的数据
//                for (String key : processQManageDbMap.keySet()) {
//                    if (CollectionUtils.isEmpty(qManageTaskDetailDbMap) || !qManageTaskDetailDbMap.containsKey(key)) {
//                        if (CollectionUtils.isEmpty(newProcessQManageList)) {
//                            newProcessQManageList = new ArrayList<>();
//                        }
//                        newProcessQManageList.addAll(processQManageDbMap.get(key));
//                    }
//                }
//
//                if (!CollectionUtils.isEmpty(newProcessQManageList)) {
//                    String orderNo = insertMesOrderJob(workOrderExt, workCenterExt, orderRuleOp.get().getTaskType(), userInfo, checkErrorLog);
//                    if (StringUtils.isEmpty(orderNo)) {
//                        return;
//                    }
//                    insertMesQManageTaskDetail(orderNo, newProcessQManageList, processCodeMap, userInfo);
//                }
//            }
//        } catch (Exception e) {
//            checkErrorLogService.insertCheckErrorLog(checkErrorLog, e.getMessage(), userInfo);
//            log.info(e.getMessage());
//        }
//
//    }
//
//
//    private String insertMesOrderJob(MesWorkOrderExt workOrderExt, MesWorkCenterExt workCenterExt, Integer taskType, String userInfo, MesCheckErrorLog checkErrorLog){
//        List<String> orderNoList = getOrderNoByRuleCode(1,
//                MesPcnExtConstWords.ORDER_NO_RULE, new GenSerialNoModel(), userInfo, workOrderExt.getOrganizeCode());
//        if (CollectionUtils.isEmpty(orderNoList)) {
//            String msg = String.format("Q管理生成单据:工单[%s]单据编号未生成成功", workOrderExt.getOrderNo());
//            log.info(msg);
//            checkErrorLogService.insertCheckErrorLog(checkErrorLog, msg, userInfo);
//            return null;
//        }
//        MesOrderJob orderJob = new MesOrderJob();
//        orderJob.setOrderType(MesPcnExtEnumUtil.MES_OR_ORDER_TYPE.Q_MANAGE.getValue());
//        orderJob.setTaskType(taskType);
//        orderJob.setOrderNo(orderNoList.get(0));
//        orderJob.setWorkCenterCode(workOrderExt.getWorkCenterCode());
//        orderJob.setShiftCode(workCenterExt.getShiftCode());
//        orderJob.setWorkOrderNo(workOrderExt.getOrderNo());
//        orderJob.setOrderStatus(MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CREATE.getValue());
//        orderJob.setWorkTime(workCenterExt.getWorkTime());
//        orderJob.setOrderDate(TimeTool.getToday());
//        orderJob.setGenerateTime(TimeTool.getNowTime(true));
//        orderJob.setOrganizeCode(workOrderExt.getOrganizeCode());
//        ConvertBean.serviceModelInitialize(orderJob, userInfo);
//        orderJobRepository.insert(orderJob);
//        return orderJob.getOrderNo();
//    }
//
//    private void insertMesQManageTaskDetail(String orderNo, List<MesProcessQManage> processQManageList, Map<String, List<MesProcessCell>> processCodeMap, String userInfo){
//        for(MesProcessQManage processQManage : processQManageList){
//            if (null == processQManage) {
//                continue;
//            }
//            MesQManageTaskDetail qManageTaskDetail = new MesQManageTaskDetail();
//            BeanUtils.copyProperties(processQManage, qManageTaskDetail, MesPcnExtConstWords.ID);
//            qManageTaskDetail.setOrderNo(orderNo);
//
//            List<MesProcessCell> processCellList = processCodeMap.get(processQManage.getProcessCode());
//            if (!CollectionUtils.isEmpty(processCellList)) {
//                qManageTaskDetail.setWorkCellCode(processCellList.get(0).getWorkCellCode());
//                qManageTaskDetail.setWorkCellName(processCellList.get(0).getWorkCellName());
//                qManageTaskDetail.setProcessName(processCellList.get(0).getProcessName());
//            }
//            qManageTaskDetail.setIsTeamConfirm(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
//            qManageTaskDetail.setIsQualityConfirm(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
//            ConvertBean.serviceModelInitialize(qManageTaskDetail, userInfo);
//            qManageTaskDetailRepository.insert(qManageTaskDetail);
//        }
//    }
//
//    public List<String> getOrderNoByRuleCode(int number, String ruleCode, GenSerialNoModel genSerialNoModel, String userName, String org) {
//        genSerialNoModel.setRuleCode(ruleCode);
//        BaseResultBean<String> serialNumberBean;
//        try {
//            serialNumberBean = syncFuncService.syncSerialNo(genSerialNoModel, userName, org, Integer.valueOf(number));
//        } catch (Exception e) {
//            return null;
//        }
//        if (serialNumberBean != null && serialNumberBean.isSuccess()) {
//            return serialNumberBean.getResultList();
//        } else {
//            return null;
//        }
//    }
//
//}
