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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.context.MesProductionPartContext;
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.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.Optional;

/**
 * @Description : 生产工单顺序防错工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesProductSeqCheckStepService")
public class MesProductSeqCheckStepService extends BaseStepService {

    @Autowired
    public IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    public IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    public IMesWorkOrderExtService workOrderExtService;

    @Autowired
    public IMesProduceSnExtService produceSnExtService;

    @Autowired
    public IMesAssemblyExtService assemblyExtService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        //排序线 生产工单顺序防错工步
        if (MesExtEnumUtil.WORK_CENTER_TYPE.SORT.getValue() == productionProcessContextStepService.getCenterType(reqBean)) return ((IStepService) SpringContextsUtil.getBean("mesProductSeqCheckSortStepService")).executeInState(reqBean);

        //非排序线 生产工单顺序防错工步
        return ((IStepService) SpringContextsUtil.getBean("mesProductSeqCheckNosortStepService")).executeInState(reqBean);

    }

    //验证是否存在加工单
    public Boolean checkIsExistWorkOrder(List<MesProductionPartContext> productionPartContextList) {

        if (CollectionUtils.isEmpty(productionPartContextList)) return false;

        Optional<MesProductionPartContext> optional = productionPartContextList.stream().filter(o -> (null != o && !StringUtils.isEmpty(o.getWorkOrderNo()))).findFirst();

        return (null == optional || !optional.isPresent()) ? false : true;

    }

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

    //验证是否已经全部通过
    public Boolean isExistNeedCheckProductSeq(List<MesProductionPartContext> productionPartContextList) {
        Optional<MesProductionPartContext> optional = productionPartContextList.stream().filter(o -> (null != o &&
                o.getIsFinishCode().compareTo(CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue()) == 0 && o.getCheckSeqResult().compareTo(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()) != 0)).findFirst();
        return (null == optional || !optional.isPresent()) ? false : true;
    }

    //验证是否存在工艺强过码, 存在则保存 并返回强过的加工单
    public List<String> doHandleCraftJumpCode(StationRequestBean reqBean, List<MesProductionPartContext> productionPartContextList) {

        List<String> workOrderNoList2Jump = null;

        for (MesProductionPartContext productionPartContext : productionPartContextList) {

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

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

            workOrderNoList2Jump.add(productionPartContext.getWorkOrderNo());

            productionPartContext.checkSeqResult(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());

        }

        //保存上下文产出零件信息
        if (!CollectionUtils.isEmpty(workOrderNoList2Jump)) productionDispatchContextStepService.dispatchProductionPartContext(reqBean, productionPartContextList);

        return workOrderNoList2Jump;

    }

}
