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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.yf.YfManyCellMonitorModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.actor.shipping.dispatch.IFsmCommonService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Slf4j
@Service("yfManyCellMonitorStepService")
public class YfManyCellMonitorStepService extends BaseStepService {

    @Autowired
    private IFsmCommonService fsmCommonService;

    @Autowired
    private YfManyCellMonitorModuleService manyCellMonitorModuleService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        log.info("工厂{}生产线{}工位{}: --- STEP EXECUTE --- YfManyCellMonitorStepService --- START --- ", reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode());

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

        fsmCommonService.checkWcpcMapForDoScan(reqBean);

        String plcCode = reqBean.getWcpcMap().get(MesPcnExtConstWords.PLC_CODE_UC);

        String plcValue = (String) manyCellMonitorModuleService.doHandlePlcData(reqBean, 1, plcCode, null);

        if (StringUtils.isEmpty(plcValue) || !plcValue.equals("true"))
            return execDynamicsCompleteAndSendMsgReturn(reqBean, resultBean, stepResult, false, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT, "正在监听PLC信号中...");

        manyCellMonitorModuleService.doHandlePlcData(reqBean, 2, plcCode, "false");

        log.info("工厂{}生产线{}工位{}: --- STEP EXECUTE --- YfManyCellMonitorStepService --- SUCCESS ---", reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode());

        this.sendMessage(reqBean, resultBean, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValueStr(), MesPcnEnumUtil.STATION_BUSI_TYPE.ADDITIONAL_TIPS, MesPcnEnumUtil.STATION_DATA_TYPE.CUSTOM);

        return execSuccessCompleteAndSendMsgReturn(reqBean, resultBean, stepResult, "已获取到PLC信号!");
    }

}
