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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesProdOrgExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.BaseStepService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.IShippingDispatchService;
import cn.estsh.i3plus.mes.pcn.serviceimpl.fsm.IStepService;
import cn.estsh.i3plus.mes.pcn.websocket.StationWebSocket;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkCellExtendCfg;
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.impp.framework.boot.util.SpringContextsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Description : 验证通过后切换分屏扫描框工步 （多工位场景当前工位验证通过后切换至下个虚拟子工位）
 * @Author : wangjie
 **/
@Slf4j
@Service("mesManyCellIsVerificationStepService")
public class MesManyCellIsVerificationStepService extends BaseStepService {

    @Autowired
    private IMesProdOrgExtService prodOrgExtService;

    @Autowired
    private IShippingDispatchService shippingDispatchService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

        //根据生产线代码,工位代码查询主子工位实虚关系信息[大于等于Bak工位的seq的数据排在前面]
        List<MesWorkCellExtendCfg> workCellExtendCfgList = prodOrgExtService.getWorkCellExtendCfgRvListSortByBak(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode());

        if (CollectionUtils.isEmpty(workCellExtendCfgList)) return stepSuccessCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult, String.format("生产线[%s]工位[%s]未配置实虚工位对应关系", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode()));

        for (MesWorkCellExtendCfg workCellExtendCfg : workCellExtendCfgList) {

            if (null == workCellExtendCfg) continue;

            //排除当前工位
            if (workCellExtendCfg.getWorkCenterCodeBak().equals(reqBean.getWorkCenterCode()) && workCellExtendCfg.getWorkCellCodeBak().equals(reqBean.getWorkCellCode())) continue;

            //验证下个工位是否在线
            String clientInfo = shippingDispatchService.getActorClientInfo(reqBean.getOrganizeCode(), workCellExtendCfg.getWorkCenterCodeBak(), workCellExtendCfg.getWorkCellCodeBak());
            if (CollectionUtils.isEmpty(StationWebSocket.getStationWebSocketList(clientInfo))) continue;

            //调用多工位场景触发定位分屏工步, CLIENT_INFO是必要传参
            reqBean.getDataMap().put(MesPcnExtConstWords.CLIENT_INFO, clientInfo);
            return ((IStepService) SpringContextsUtil.getBean("mesManyCellTriggerTabSwitchStepService")).executeInState(reqBean);

        }

        return stepSuccessCompleteAndSendMsgReturn(reqBean, resultBean.writeDbLog(), stepResult,
                String.format("生产线[%s]工位[%s]切换分屏失败!原因:实体生产线[%s]工位[%s]%s!", reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(),
                        workCellExtendCfgList.get(0).getWorkCenterCode(), workCellExtendCfgList.get(0).getWorkCellCode(), workCellExtendCfgList.size() == 1 ? "仅关联了当前操作的虚拟工位" : "关联的其他虚拟工位当前均不在线"));

    }

}
