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

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.mes.pcn.serviceimpl.fsm.process.BaseProcessMonitorService;
import cn.estsh.i3plus.pojo.mes.model.StationRequestBean;
import cn.estsh.i3plus.pojo.mes.model.StationResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

@Service
@Primary
public class MesProductionProcessMonitorService extends BaseProcessMonitorService {

    @Autowired
    private IMesProductionProcessContextStepService productionProcessContextStepService;

    @Autowired
    private IMesProductionDispatchContextStepService productionDispatchContextStepService;

    @Autowired
    private IMesProductionCustomContextStepService productionCustomContextStepService;

    //工序开始
    @Override
    public Boolean doProcessStart(StationRequestBean requestBean) {
        //保存上下文工位扫描监控信息
        productionProcessContextStepService.dispatchScanMonitorContext(requestBean);
        //父类清除工步客制化区域数据【装配件清单】
        return super.doProcessStart(requestBean);
    }

    //工序结束
    @Override
    public Boolean doProcessComplete(StationRequestBean requestBean) {
        //补DB日志 [获取开模ID赋值scanInfo] [获取上下文产出条码数据信息集合赋值resultList]
        writeDbLogService.doRestoreDbLog(requestBean, new StationResultBean()
                .restoreDbLog().scanInfo(productionProcessContextStepService.getScanMonitorContextMouldRecordId(requestBean)).resultList(productionDispatchContextStepService.getProductionPsOutContext(requestBean)));
        //清除上下文中的所有业务数据
        productionDispatchContextStepService.flushProductionDispatchContext(requestBean);
        //父类发送工序完成音
        return super.doProcessComplete(requestBean);
    }

    //重置工序
    @Override
    public Boolean doJumpProcess(StationRequestBean requestBean) {
        //清除上下文中的所有业务数据
        productionDispatchContextStepService.flushProductionDispatchContext(requestBean);
        //清除可复用条码数据
        productionCustomContextStepService.removeRepeatAssemblySn(requestBean);
        return super.doJumpProcess(requestBean);
    }

    //客户端上线
    @Override
    public Boolean doOnLine(StationRequestBean requestBean) {
        //父类删除当前工步集
        super.doOnLine(requestBean);
        //清除上下文中的所有业务数据
        productionDispatchContextStepService.flushProductionDispatchContext(requestBean);
        //清除上下文中的所有基础数据
        productionProcessContextStepService.flushProductionProcessContext(requestBean);
        //清除可复用条码数据
        productionCustomContextStepService.removeRepeatAssemblySn(requestBean);
        return true;
    }

    //客户端下线
    @Override
    public Boolean doOffLine(StationRequestBean requestBean) { return super.doOffLine(requestBean); }

    //工位流程重置 【父类调度方法已经调用: 删除当前工步集; 清除工位工序流程; TASK_COMPLETE】
    @Override
    public Boolean fsmReset(StationRequestBean requestBean) {
        //清除上下文中的所有业务数据
        productionDispatchContextStepService.flushProductionDispatchContext(requestBean);
        //清除上下文中的所有基础数据
        productionProcessContextStepService.flushProductionProcessContext(requestBean);
        return super.fsmReset(requestBean);
    }

}
