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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesProductionCustomContextStepService;
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.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.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCenter;
import cn.estsh.i3plus.pojo.mes.model.StationKvBean;
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.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.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description : 扫描加工单工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesWorkOrderScanStepService")
public class MesWorkOrderScanStepService extends BaseStepService {

    @Autowired
    private IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private IMesProductionCustomContextStepService productionCustomContextStepService;

    @Override
    public StepResult guide(StationRequestBean reqBean) {

        productionCustomContextStepService.sendStepContextMessage(reqBean);

        return stepSuccessCompleteAndSendGuideReturn(reqBean, new StationResultBean().writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), "请扫描加工单!");

    }

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

        //扫描信息置空
        String scanInfo = reqBean.resetScanInfo(reqBean.getScanInfo());

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

        //获取生产过程上下文对象有异常信息 抛出异常
        if (!productionProcessContext.getSuccess()) stepExpSendMsgAndThrowEx(reqBean, resultBean.writeDbLog(), productionProcessContext.getMessage());

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

        //从上下文中取出生产线对象
        MesWorkCenter workCenter = productionProcessContext.getWorkCenter();

        //如果没有扫描信息, 取手选工单信息, 封装扫/读信息:加工单信息
        List<MesEquipVariableCollectContext> equipVariableCollectContextList;
        //优先验证扫描加工单
        if (!StringUtils.isEmpty(scanInfo)) equipVariableCollectContextList = doHandleScanWorkOrderNoContext(reqBean, stepResult, scanInfo);
            //选择加工单
        else equipVariableCollectContextList = doHandleScanWorkOrderNoContext(reqBean, stepResult, workCenter);

        //扫描信息为空
        if (CollectionUtils.isEmpty(equipVariableCollectContextList)) stepSendGuideAndThrowEx(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), "请扫描加工单!");

        //保存上下文扫/读信息:加工单
        productionDispatchContextStepService.dispatchScanWorkOrderNoContext(reqBean, equipVariableCollectContextList);

        if (StringUtils.isEmpty(stepResult.getMsg())) return stepResult;

        return stepSuccessCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.SCAN.getValue()).scanInfo(scanInfo), stepResult, stepResult.getMsg());

    }

    //封装扫/读信息:加工单信息 (扫描)
    private List<MesEquipVariableCollectContext> doHandleScanWorkOrderNoContext(StationRequestBean reqBean, StepResult stepResult, String scanInfo) {

        List<MesEquipVariableCollectContext> equipVariableCollectContextList = new ArrayList<>();

        equipVariableCollectContextList.add(new MesEquipVariableCollectContext(reqBean.getOrganizeCode(), scanInfo, MesExtEnumUtil.CELL_MESSAGE_SOURCE.SCAN.getValue()));

        //发送工步内容
        productionCustomContextStepService.sendStepContextMessage(reqBean, scanInfo, MesExtEnumUtil.CELL_MESSAGE_SOURCE.SCAN);

        stepResult.msg(String.format("当前扫描信息加工单[%s]!", scanInfo));

        return equipVariableCollectContextList;

    }

    //封装扫/读信息:加工单信息  (选择)
    private List<MesEquipVariableCollectContext> doHandleScanWorkOrderNoContext(StationRequestBean reqBean, StepResult stepResult, MesWorkCenter workCenter) {

        //排序线不支持选择加工单
        if (workCenter.getCenterType().compareTo(MesExtEnumUtil.WORK_CENTER_TYPE.NOSORT.getValue()) != 0) return null;

        //获取手动选择的腔数及工单信息上下文, 扫描的加工单也会存入其中
        List<StationKvBean> chooseCavityOrder = productionProcessContextStepService.getFunctionChooseCavityOrderContext(reqBean);

        if (CollectionUtils.isEmpty(chooseCavityOrder)) return null;

        String cellMessageSorce = chooseCavityOrder.stream().filter(o -> (null != o && o.getKey().equals(MesPcnExtConstWords.CELL_MESSAGE_SOURCE))).findFirst().get().getValue();
        String isConsume = chooseCavityOrder.stream().filter(o -> (null != o && o.getKey().equals(MesPcnExtConstWords.CRAFT_JUMP_CODE))).findFirst().get().getValue();
        List<String> chooseList = new ArrayList<>(Arrays.asList(chooseCavityOrder.stream().filter(o -> (null != o && o.getKey().equals(MesPcnExtConstWords.WORK_ORDER_NO))).findFirst().get().getValue().split(MesPcnExtConstWords.SEMICOLON)));

        List<MesEquipVariableCollectContext> equipVariableCollectContextList = new ArrayList<>();
        chooseList.stream().filter(o -> !StringUtils.isEmpty(o)).forEach(o ->
                equipVariableCollectContextList.add(new MesEquipVariableCollectContext(reqBean.getOrganizeCode(), o, Integer.valueOf(cellMessageSorce)).isConsume(Integer.valueOf(isConsume))));

        //发送工步内容
        String scanInfo = chooseCavityOrder.stream().filter(o -> (null != o && !o.getKey().equals(MesPcnExtConstWords.CELL_MESSAGE_SOURCE))).map(o -> o.getName() + MesPcnExtConstWords.SQUARE_BRACKETS_L + o.getValue() + MesPcnExtConstWords.SQUARE_BRACKETS_R).collect(Collectors.joining(MesPcnExtConstWords.SEMICOLON));
        productionCustomContextStepService.sendStepContextMessage(reqBean, scanInfo, MesExtEnumUtil.CELL_MESSAGE_SOURCE.getByValue(Integer.valueOf(cellMessageSorce)));

        return equipVariableCollectContextList;

    }

}
