//package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.step;
//
//import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionDispatchContextStepService;
//import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionProcessContextStepService;
//import cn.estsh.i3plus.ext.mes.pcn.api.busi.IWriteVariableService;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesCellEquipContext;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProdRuleContext;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionAssemblyContext;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionProcessContext;
//import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
//import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
//import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
//import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
//import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
//import cn.estsh.i3plus.pojo.mes.bean.MesEquipmentProdParamCfg;
//import cn.estsh.i3plus.pojo.mes.bean.MesEquipmentVariable;
//import cn.estsh.i3plus.pojo.mes.bean.MesProdRouteOptParam;
//import cn.estsh.i3plus.pojo.mes.bean.MesWorkCenter;
//import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
//import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
//import cn.estsh.i3plus.pojo.mes.model.StepResult;
//import cn.estsh.i3plus.pojo.mes.repository.MesEquipmentProdParamCfgRepository;
//import cn.estsh.i3plus.pojo.mes.repository.MesEquipmentVariableRepository;
//import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
//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.stream.Collectors;
//
///**
// * @Description : 发送客户端主条码
// * @Author : zxw
// **/
//@Slf4j
//@Service("mesWriteMainBarcodeCmdStepService")
//public class MesWriteMainBarcodeCmdStepService extends BaseStepService {
//
//    @Autowired
//    private IMesProductionProcessContextStepService productionProcessContextStepService;
//
//    @Autowired
//    private IMesProductionDispatchContextStepService productionDispatchContextStepService;
//
//    @Autowired
//    private IWriteVariableService writeVariableService;
//
//    @Autowired
//    private MesEquipmentVariableRepository mesEquipmentVariableRepository;
//
//    @Autowired
//    private MesEquipmentProdParamCfgRepository mesEquipmentProdParamCfgRepository;
//
//
//    @Override
//    public StepResult execute(StationRequestBean reqBean) {
//
//        StationResultBean resultBean = new StationResultBean();
//
//        StepResult stepResult = StepResult.getSuccessComplete();
//
//        //获取工步参数
//        Optional<Map<String, MesProdRouteOptParam>> stepParamMap = getStepParams(reqBean);
//
//        //获取上下文信息
//        MesProductionProcessContext productionProcessContext = productionProcessContextStepService.dispatchCurCellEquipment(reqBean, stepParamMap);
//
//        //当前工位使用的设备
//        MesCellEquipContext cellEquipContext = productionProcessContext.getCurCellEquip();
//
//        //获取上下文加工规则数据信息集合
//        List<MesProdRuleContext> prodRuleContextList = productionDispatchContextStepService.getProdRuleDataContext(reqBean);
//
//        if (CollectionUtils.isEmpty(prodRuleContextList)) {
//            return stepSuccessCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult, "未找到加工规则，无需发送加工参数!");
//        }
//        List<MesEquipmentVariable> mesEquipmentVariableList = new ArrayList<>();
//        List<MesEquipmentProdParamCfg> mesEquipmentProdParamCfgList = new ArrayList<>();
//        Map<Integer, List<MesEquipmentProdParamCfg>> mesEquipmentVariableMap =  getMesEquipmentProdParamCfgs(reqBean, prodRuleContextList, mesEquipmentProdParamCfgList);
//        // 过滤掉非条码的
//        mesEquipmentVariableMap =  filterMsg(mesEquipmentVariableMap);
//
//        mesEquipmentVariableMap.forEach((k, v) -> {
//           v.stream().forEach(mesEquipmentProdParamCfg -> {
//               DdlPackBean packBean = DdlPackBean.getDdlPackBean();
//               DdlPreparedPack.getNumEqualPack(mesEquipmentProdParamCfg.getEquipVariableId(), "id", packBean);
//               MesEquipmentVariable mesEquipmentVariable = mesEquipmentVariableRepository.getByProperty(packBean);
//               if (mesEquipmentVariable == null) {
//                   return;
//               }
//               mesEquipmentVariableList.add(mesEquipmentVariable);
//               mesEquipmentProdParamCfgList.add(mesEquipmentProdParamCfg);
//               execSendEquipParamsCmd(reqBean, mesEquipmentProdParamCfg.getMatchRule(), mesEquipmentVariable, cellEquipContext.getKepwareFlag(mesEquipmentVariable.getChannel()), k, stepParamMap);
//           });
//        });
//
//
//        return stepSuccessCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), stepResult, "发送加工参数成功!");
//    }
//
//    /**
//     * 根据排序和非排序 获取对应的加工参数
//     * @param reqBean
//     * @param prodRuleContextList
//     * @param mesEquipmentProdParamCfgList
//     * @return
//     */
//    private Map<Integer, List<MesEquipmentProdParamCfg>> getMesEquipmentProdParamCfgs(StationRequestBean reqBean, List<MesProdRuleContext> prodRuleContextList, List<MesEquipmentProdParamCfg> mesEquipmentProdParamCfgList) {
//
//        MesProductionProcessContext productionProcessContext = productionProcessContextStepService.dispatchCurCellEquipment(reqBean);
//
//        Map<Integer, List<MesEquipmentProdParamCfg>> map = new HashMap<>();
//        for (MesProdRuleContext mesProdRuleContext : prodRuleContextList) {
//            List<MesEquipmentProdParamCfg> mesEquipmentProdParamCfgs= new ArrayList<>();
//            //从上下文中取出生产线对象
//            MesWorkCenter workCenter = productionProcessContext.getWorkCenter();
//            // 获取加工参数
//            if (MesExtEnumUtil.WORK_CENTER_TYPE.NOSORT.getValue() == workCenter.getCenterType()){
//
//                DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean();
//                DdlPreparedPack.getNumEqualPack(mesProdRuleContext.getPid(), "sourceId", ddlPackBean);
//                mesEquipmentProdParamCfgs = mesEquipmentProdParamCfgRepository.findByHqlWhere(ddlPackBean);
//            } else {
//                MesWorkCenter mesWorkCenter = new MesWorkCenter();
//                mesWorkCenter.setWorkCenterCode(reqBean.getWorkCenterCode());
//                List<MesProductionAssemblyContext> assemblyDataContext = mesProdRuleContext.getAssemblyDataContext(mesWorkCenter);
//                List<Long> sourceIds = assemblyDataContext.stream().map(MesProductionAssemblyContext::getSourceId).collect(Collectors.toList());
//
//                DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean();
//                DdlPreparedPack.getInPackList(sourceIds, "sourceId", ddlPackBean);
//                mesEquipmentProdParamCfgs = mesEquipmentProdParamCfgRepository.findByHqlWhere(ddlPackBean);
//            }
//            if (!CollectionUtils.isEmpty(mesEquipmentProdParamCfgs)) {
//                mesEquipmentProdParamCfgs = mesEquipmentProdParamCfgs.stream().distinct().collect(Collectors.toList());
//            }
//            map.put(mesProdRuleContext.getForeignKey(), mesEquipmentProdParamCfgs);
//        }
//        return map;
//    }
//
//    private void execSendEquipParamsCmd(StationRequestBean reqBean,
//                                              String value, MesEquipmentVariable equipmentVariable, String kepwareFlag, Integer foreignKey, Optional<Map<String, MesProdRouteOptParam>> stepParamMap) {
//        //最大重试次数[工步参数]
//        Integer maxRetryTimes = getMaxRetryTimes(stepParamMap);
//
//        writeVariableService.writeVariable(reqBean, value, equipmentVariable, kepwareFlag, foreignKey, null, maxRetryTimes);
//    }
//
//    //最大重试次数[工步参数]
//    private Integer getMaxRetryTimes(Optional<Map<String, MesProdRouteOptParam>> stepParamMap) {
//        Integer maxRetryTimes = null;
//        try {
//            maxRetryTimes = (null != stepParamMap && stepParamMap.isPresent() && stepParamMap.get().containsKey(MesPcnExtConstWords.MAX_RETRY_TIMES)) ? Integer.valueOf(stepParamMap.get().get(MesPcnExtConstWords.MAX_RETRY_TIMES).getParamValue()) : null;
//        } catch (NumberFormatException e) {
//        }
//        return !StringUtils.isEmpty(maxRetryTimes) ? maxRetryTimes : 3;
//    }
//
//    private  Map<Integer, List<MesEquipmentProdParamCfg>>  filterMsg(Map<Integer, List<MesEquipmentProdParamCfg>> map){
//
//        if (CollectionUtils.isEmpty(map)) {
//            return  null;
//        }
//        map.forEach((k,v) -> {
//            v = v.stream().filter(cfg -> StringUtils.isEmpty(cfg.getMatchRule()) && cfg.getMatchRule()
//                    .toUpperCase().startsWith("BARCODE")).collect(Collectors.toList());        });
//        return map;
//    }
//}
