//package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.base.bu3;
//
//import cn.estsh.i3plus.ext.mes.pcn.api.base.bu3.IBuQManageService;
//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.mes.pcn.util.DateUtil;
//import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
//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.enumutil.MesEnumUtil;
//import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
//import cn.estsh.i3plus.pojo.mes.model.GenSerialNoModel;
//import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//import org.springframework.util.CollectionUtils;
//
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//import java.util.function.Function;
//import java.util.stream.Collectors;
//
///**
// * @PROJECT_NAME: i3plus-mes-panasonic-bu
// * @DESCRIPTION:
// * @USER: xinwang.yi
// * @DATE: 2023-04-17 9:23
// */
//@Service
//public class BuQManageService implements IBuQManageService {
//
//    @Autowired
//    private MesOrderJobRepository orderJobRepository;
//
//    @Autowired
//    private MesQManageTaskDetailRepository qManageTaskDetailRepository;
//
//    @Autowired
//    private ISyncFuncService syncFuncService;
//
//    @Autowired
//    private MesWorkCenterExtRepository workCenterExtRepository;
//
//    @Autowired
//    private MesProcessQManageRepository processQManageRepository;
//
//    @Autowired
//    private MesProcessCellRepository processCellRepository;
//
//    //@Override
//    //public void generateWeekCheck(String organizeCode, String workCenterCode) {
//    //    generateOrderJob(organizeCode, workCenterCode, MesPcnExtEnumUtil.MES_Q_MANAGER_CHECK_TYPE.WEEK_CHECK.getValue());
//    //}
//
//    @Override
//    public void generateMonthCheck(String organizeCode, String workCenterCode) {
//        generateOrderJob(organizeCode, workCenterCode, MesPcnExtEnumUtil.MES_Q_MANAGER_CHECK_TYPE.MONTH_CHECK.getValue());
//    }
//
//    private void generateOrderJob(String organizeCode, String workCenterCode, String checkType) {
//        List<MesProcessCell> processCellList = processCellRepository.findByProperty(
//                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.WORK_CENTER_CODE},
//                new Object[]{organizeCode, CommonEnumUtil.IS_VAILD.VAILD.getValue(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), workCenterCode});
//        Map<String, MesProcessCell> processCellMap = processCellList.stream().collect(Collectors.toMap(o -> o.getProcessCode(), Function.identity(), (x, y) -> y));
//        List<MesProcessQManage> qManageList = findQManageByCenterCode(organizeCode, processCellList, checkType);
//        if (!CollectionUtils.isEmpty(qManageList)) {
//            MesWorkCenterExt workCenter = getWorkCenter(workCenterCode, organizeCode);
//            MesOrderJob newOrderJob = new MesOrderJob();
//            newOrderJob.setTaskType(MesPcnExtEnumUtil.MES_OR_TASK_TYPE.PREPARE_TASK.getValue());
//            newOrderJob.setOrderType(MesPcnExtEnumUtil.MES_OR_ORDER_TYPE.Q_MANAGE.getValue());
//            newOrderJob.setOrderNo(getOrderNo(organizeCode));
//            newOrderJob.setWorkCenterCode(workCenterCode);
//            newOrderJob.setOrderStatus(MesPcnExtEnumUtil.MES_ORJ_ORDER_STATUS.CREATE.getValue());
//            newOrderJob.setOrderDate(DateUtil.formatDate("yyyy-MM-dd", new Date()));
//            newOrderJob.setWorkTime(DateUtil.formatDate("yyyy-MM-dd", new Date()));
//            newOrderJob.setGenerateTime(DateUtil.formatDate("yyyy-MM-dd", new Date()));
//            newOrderJob.setOrganizeCode(organizeCode);
//            newOrderJob.setIsValid(MesPcnExtEnumUtil.IS_VAILD.VAILD.getValue());
//            newOrderJob.setIsDeleted(MesEnumUtil.TRUE_OR_FALSE.FALSE.getValue());
//            newOrderJob.setCreateDatetime(DateUtil.formatDate());
//            newOrderJob.setModifyDatetime(DateUtil.formatDate());
//            newOrderJob.setCheckType(checkType);
//            newOrderJob.setHandlerMethod(MesPcnExtEnumUtil.HANDLER_METHOD.MANUALLY_CREATE.getValue());
//            newOrderJob.setShiftCode(workCenter.getShiftCode());
//            MesOrderJob job = orderJobRepository.save(newOrderJob);
//
//            for (MesProcessQManage processQManage : qManageList) {
//                MesQManageTaskDetail qManageTaskDetail = new MesQManageTaskDetail();
//                qManageTaskDetail.setOrderNo(job.getOrderNo());
//                BeanUtils.copyProperties(processQManage, qManageTaskDetail, "id");
//                qManageTaskDetail.setCheckType(checkType);
//                qManageTaskDetail.setShiftCode(workCenter.getShiftCode());
//                qManageTaskDetail.setWorkCenterCode(workCenterCode);
//                MesProcessCell mesProcessCell = processCellMap.get(processQManage.getProcessCode());
//                if(mesProcessCell != null){
//                    qManageTaskDetail.setWorkCellCode(mesProcessCell.getWorkCellCode());
//                    qManageTaskDetail.setWorkCellName(mesProcessCell.getWorkCellName());
//                    qManageTaskDetail.setProcessName(mesProcessCell.getProcessName());
//                }
//
//                qManageTaskDetailRepository.save(qManageTaskDetail);
//            }
//        }else{
//            throw ImppExceptionBuilder.newInstance()
//                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
//                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
//                    .setErrorDetail("产线【%s】未维护月点检项目，请确认",workCenterCode)
//                    .build();
//        }
//    }
//
//    private MesWorkCenterExt getWorkCenter(String workCenterCode, String organizeCode) {
//        return workCenterExtRepository.getByProperty(
//                new String[]{"workCenterCode", "organizeCode", "isValid", "isDeleted"},
//                new Object[]{workCenterCode, organizeCode,
//                        CommonEnumUtil.IS_VAILD.VAILD.getValue(),
//                        CommonEnumUtil.IS_DEAL.NO.getValue()});
//    }
//
//    public List<MesProcessQManage> findQManageByCenterCode(String organizeCode, List<MesProcessCell> processCellList, String checkType) {
//        if(CollectionUtils.isEmpty(processCellList)) return null;
//        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
//        DdlPreparedPack.getStringEqualPack(checkType, MesPcnExtConstWords.CHECK_TYPE, ddlPackBean);
//        DdlPreparedPack.getInPackList(processCellList.stream().map(MesProcessCell::getProcessCode).distinct().collect(Collectors.toList()), MesPcnExtConstWords.PROCESS_CODE, ddlPackBean);
//
//        return processQManageRepository.findByHqlWhere(ddlPackBean);
//    }
//
//    private String getOrderNo(String organizeCode) {
//        String orderNo = null;
//        try {
//            BaseResultBean oneCodeResult = syncFuncService.syncSerialNo(new GenSerialNoModel(MesPcnExtConstWords.ORDER_NO_RULE), null, organizeCode, 1);
//            if (oneCodeResult != null && oneCodeResult.isSuccess()) {
//                orderNo = oneCodeResult.getResultList().get(0).toString();
//            }
//        } catch (Exception e) {
//        }
//        return orderNo;
//    }
//}
