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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesAssemblyExtService;
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.IMesProductionRecordService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionPsInContext;
import cn.estsh.i3plus.mes.pcn.actor.shipping.dispatch.IFsmRouteDataService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.IStepService;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesCraftRouteDetail;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCenter;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StepResult;
import cn.estsh.i3plus.pojo.mes.util.MesExtEnumUtil;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Description : 前道工艺防错工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesProdCraftRouteCheckStepService")
public class MesProdCraftRouteCheckStepService extends BaseStepService {

    @Autowired
    public IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    public IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    public IMesProductionRecordService productionRecordService;

    @Autowired
    public IMesAssemblyExtService assemblyExtService;

    @Autowired
    private IFsmRouteDataService fsmRouteDataService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        //排序线 前道工艺防错工步
        if (MesExtEnumUtil.WORK_CENTER_TYPE.SORT.getValue() == productionProcessContextStepService.getCenterType(reqBean)) return ((IStepService) SpringContextsUtil.getBean("mesProdCraftRouteCheckSortStepService")).executeInState(reqBean);

        //非排序线 前道工艺防错工步
        return ((IStepService) SpringContextsUtil.getBean("mesProdCraftRouteCheckNosortStepService")).executeInState(reqBean);

    }

    //验证工位是否设置需要前道防错
    public Boolean isNeedCheckCraft(List<MesProductionPsInContext> productionPsInContextList) {
        Optional<MesProductionPsInContext> optional = productionPsInContextList.stream().filter(o -> (null != o &&
                o.getIsFinishCode().compareTo(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue()) == 0 && o.getIsCheckCraft().compareTo(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()) == 0)).findFirst();
        return (null == optional || !optional.isPresent()) ? false : true;
    }

    //查询工艺路线数据
    public void handleProdCraftData(StationRequestBean reqBean, MesWorkCenter workCenter, String partNo, Map<String, List<MesCraftRouteDetail>> craftRouteDataMap, Boolean isPart) {
        List<MesCraftRouteDetail> craftRouteDetailList = fsmRouteDataService.handleProdCraftData(reqBean, workCenter.getCenterType(), partNo, isPart);
        if (!CollectionUtils.isEmpty(craftRouteDetailList)) craftRouteDataMap.put(partNo, craftRouteDetailList);
    }

    //验证是否存在工艺强过码, 存在则保存 并返回强过的主条码
    public List<String> doHandleCraftJumpCode(StationRequestBean reqBean, List<MesProductionPsInContext> productionPsInContextList) {

        List<String> productSnList2Jump = null;

        for (MesProductionPsInContext productionPsInContext : productionPsInContextList) {

            if (null == productionPsInContext) continue;
            if (productionPsInContext.getCheckCraftResult().compareTo(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()) == 0) continue;
            if (StringUtils.isEmpty(productionPsInContext.getCraftJumpCode())) continue;

            if (CollectionUtils.isEmpty(productSnList2Jump)) productSnList2Jump = new ArrayList<>();

            productSnList2Jump.add(productionPsInContext.getProductSn());

            productionPsInContext.checkCraftResult(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());

        }

        //保存进料主条码数据
        if (!CollectionUtils.isEmpty(productSnList2Jump)) productionDispatchContextStepService.dispatchProductionPsInContext(reqBean, productionPsInContextList);

        return productSnList2Jump;

    }

}
