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.pojo.context.MesEquipVariableCollectContext;
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.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.IStepService;
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.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.stream.Collectors;

/**
 * @Description : 根据读取条码生成零件条码工步 【读取的主条码无须进行验证,直接通过此工步生成产品条码信息】
 * @Author : wangjie
 **/
@Slf4j
@Service("mesProductSnGenerateByReadStepService")
public class MesProductSnGenerateByReadStepService extends BaseStepService {

    @Autowired
    private IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

        //获取上下文信息
        MesProductionProcessContext productionProcessContext = productionProcessContextStepService.dispatchCurCellEquipment(reqBean);

        //配置错误 抛出异常
        if (!productionProcessContext.getSuccess()) stepExpSendMsgAndThrowEx(reqBean, resultBean.writeDbLog(), productionProcessContext.getMessage());

        //存储生产过程上下文对象
        productionProcessContextStepService.dispatchProductionProcessContext(reqBean, productionProcessContext);

        //获取上下文产品加工规则数据信息集合
        List<MesProdRuleContext> prodRuleContextList = productionDispatchContextStepService.getProdRuleDataContext(reqBean);
        if (CollectionUtils.isEmpty(prodRuleContextList)) stepExpSendMsgAndThrowEx(reqBean, resultBean.writeDbLog(), "当前不存在非排序加工规则数据,请重置工序!");

        //获取上下文扫/读信息:主条码
        List<MesEquipVariableCollectContext> equipVariableCollectContextList = productionDispatchContextStepService.getScanProductSnContext(reqBean);

        //没有待验证的主条码
        if (CollectionUtils.isEmpty(equipVariableCollectContextList)) return stepNonCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult, "当前没有需要验证的主条码!");

        //清除本次已获取得到的主条码信息
        productionDispatchContextStepService.removeScanProductSnContext(reqBean);

        //搜集主条码值,去除空腔,去重
        List<String> productSnList = (equipVariableCollectContextList.stream()
                .filter(o -> (null != o && !StringUtils.isEmpty(o.getEquipVariableValue()) && !o.getEquipVariableValue().equals(productionProcessContext.getFinishCode())))
                .map(MesEquipVariableCollectContext::getEquipVariableValue).collect(Collectors.toList())
        ).stream().filter(o -> !StringUtils.isEmpty(o)).distinct().collect(Collectors.toList());

        //没有待验证的主条码
        if (CollectionUtils.isEmpty(productSnList)) return stepNonCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult, "当前未获取到有效的主条码!");

        if (productSnList.size() < prodRuleContextList.size()) {
            return stepNonCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult,
                    String.format("当前读取到主条码%s个数[%s]不满足当前实际产品腔数[%s]", productSnList.toString(), productSnList.size(), prodRuleContextList.size()));
        }

        //封装上下文进料零件条码信息集合
        List<MesProductionPsInContext> productionPsInContextList = new ArrayList<>();
        Integer index = 0;
        for (MesProdRuleContext prodRuleContext : prodRuleContextList) {
            if (null == prodRuleContext || StringUtils.isEmpty(prodRuleContext.getForeignKey())) continue;
            productionPsInContextList.add(new MesProductionPsInContext(reqBean.getOrganizeCode(), productSnList.get(index)).foreignKey(prodRuleContext.getForeignKey()));
            index ++;
        }

        //保存进料主条码数据
        productionDispatchContextStepService.dispatchProductionPsInContext(reqBean, productionPsInContextList);

        //调用生成零件条码工步
        return ((IStepService) SpringContextsUtil.getBean("mesProductSnGenerateStepService")).executeInState(reqBean);

    }

}
