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.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
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.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description : 扫描可回用包装条码工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate 2024/9/11 13:53
 * @Modify:
 **/
@Slf4j
@Service("mesRecyclablePackageScanStepService")
public class MesRecyclablePackageScanStepService extends BaseStepService {

    @Autowired
    private IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private IMesProductionCustomContextStepService productionCustomContextStepService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

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

        //当前工序已存在读取待验证的可回用包装条码信息
        if (productionDispatchContextStepService.checkScanRecyclablePackageIsExistContext(reqBean)) return stepResult;

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

        if (StringUtils.isEmpty(reqBean.getScanInfo())) stepSendGuideAndThrowEx(reqBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()), "请扫描可回用包装条码!");

        doHandleScanRecyclablePackageContext(reqBean, stepResult, reqBean.getScanInfo());

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

    //封装可回用包装条码信息 (扫描)
    private List<MesEquipVariableCollectContext> doHandleScanRecyclablePackageContext(StationRequestBean reqBean, StepResult stepResult, String scanInfo) {

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

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

        //保存设备当前一轮工序的待验证的可回用包装条码信息
        productionDispatchContextStepService.dispatchScanRecyclablePackageContext(reqBean, equipVariableCollectContextList);

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

        stepResult.msg(String.format("当前扫描信息可回用包装条码[%s]!", scanInfo));

        return equipVariableCollectContextList;

    }

}
