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

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.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.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Description : 多工位场景触发定位分屏工步
 * @Author : wangjie
 **/
@Slf4j
@Service("mesManyCellTriggerTabSwitchStepService")
public class MesManyCellTriggerTabSwitchStepService extends BaseStepService {

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

        //从工位对象的DataMap中取出固定传参[CLIENT_INFO], 值固定格式:工厂@生产线@工位   可以调用IShippingDispatchService.getActorClientInfo
        String clientInfo = (null != reqBean.getDataMap() && reqBean.getDataMap().containsKey(MesPcnExtConstWords.CLIENT_INFO)) ? (String) reqBean.getDataMap().get(MesPcnExtConstWords.CLIENT_INFO) : reqBean.getClientInfo();

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

        if (null != reqBean.getDataMap() && reqBean.getDataMap().containsKey(MesPcnExtConstWords.CLIENT_INFO)) reqBean.getDataMap().remove(MesPcnExtConstWords.CLIENT_INFO);

        StationRequestBean stationRequestBean = new StationRequestBean();
        BeanUtils.copyProperties(reqBean, stationRequestBean);
        stationRequestBean.setClientInfo(clientInfo);

        return clientInfo.equals(reqBean.getClientInfo()) ?
                stepDynamicsCompleteAndSendMsgReturn(stationRequestBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()),
                        stepResult, true, MesPcnEnumUtil.STATION_BUSI_TYPE.TAB_SWITCH, MesPcnEnumUtil.STATION_DATA_TYPE.CUSTOM, String.format("客户端[%s]监听自动切换页面成功!", reqBean.getClientInfo())) :
                stepDynamicsCompleteAndSendMsgReturn(stationRequestBean, resultBean.writeDbLog(MesPcnEnumUtil.WORK_CELL_SCAN_MONITOR_LOG_TYPE.PROCESS.getValue()),
                        stepResult, true, MesPcnEnumUtil.STATION_BUSI_TYPE.TAB_SWITCH, MesPcnEnumUtil.STATION_DATA_TYPE.CUSTOM, String.format("多工位页面切换,从客户端[%s]切换到客户端[%s]!", reqBean.getClientInfo(), clientInfo));

    }

}
