package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.step;

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.MesProductionProcessContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionPsInContext;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.mes.bean.MesProdRouteOptParam;
import cn.estsh.i3plus.pojo.mes.bean.MesProdRuleIgnoreCfg;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCell;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Description : 显示前道所有工序装配件工步【排序】
 * @Author : wangjie
 **/
@Slf4j
@Service("mesAssemblyShowSortPreCraftStepService")
public class MesAssemblyShowSortPreCraftStepService extends MesAssemblyShowSortStepService {

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        return super.execute(reqBean);
    }

    public MesProdRuleContext getProdRuleSortContext(StationRequestBean reqBean, MesProductionProcessContext productionProcessContext, MesCellEquipContext cellEquipContext, MesProductionPsInContext productionPsInContext,
                                                     Map<String, List<MesProdRuleIgnoreCfg>> prodRuleIgnoreCfgMap, Optional<Map<String, MesProdRouteOptParam>> stepParamMap) {
        Map<String, MesWorkCell> workCellMap = productionProcessContextStepService.dispatchWorkCellMap(reqBean);
        MesProdRuleContext prodRuleContext = new MesProdRuleContext(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), reqBean.getProcessCode(), productionProcessContext.getCraftCode())
                .equipmentCode(cellEquipContext.getEquipmentCode()).workOrderNo(productionPsInContext.getWorkOrderNo()).productSn(productionPsInContext.getProductSn()).foreignKey(productionPsInContext.getForeignKey());

        //是否剔除平行工位[工步参数]
        String isFilterParallel = (null != stepParamMap && stepParamMap.isPresent() && stepParamMap.get().containsKey(MesPcnExtConstWords.IS_FILTER_PARALLEL)) ? stepParamMap.get().get(MesPcnExtConstWords.IS_FILTER_PARALLEL).getParamValue() : null;
        //是否根据工序顺序号过滤后道[工步参数]
        String isFilterAfterCraft = (null != stepParamMap && stepParamMap.isPresent() && stepParamMap.get().containsKey(MesPcnExtConstWords.IS_FILTER_AFTER_CRAFT)) ? stepParamMap.get().get(MesPcnExtConstWords.IS_FILTER_AFTER_CRAFT).getParamValue() : null;

        //【排序线】获取产品加工规则 [前道所有装配件] -同时- 关联不可用规则; 不剔除平行工位 ; 不过滤后道
        return prodRuleCfgExtService.getProdRuleSortPreCraftContext(prodRuleContext, workCellMap, prodRuleIgnoreCfgMap, isFilterCfg(isFilterParallel), isFilterCfg(isFilterAfterCraft));
    }

    public Boolean showProductionAssembly(StationRequestBean reqBean, StationResultBean resultBean, MesWorkCenter workCenter, List<MesProdRuleContext> prodRuleContextList) {
        return showProductionAssembly(reqBean, resultBean, workCenter, prodRuleContextList, true, false);
    }

}
