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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.jx.IJxProdBusiStrategyService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.jx.JxSnLineOnModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.jx.JxProdBusiStrategyParamsBuilder;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.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.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.StringUtils;

/**
 * @Description : 嘉兴条码上线完成工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("jxSnLineOnCompleteStepService")
public class JxSnLineOnCompleteStepService extends BaseStepService {

    @Autowired
    private JxSnLineOnModuleService snLineOnModuleService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {

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

        StationResultBean resultBean = new StationResultBean();

        if (StringUtils.isEmpty(reqBean.getTray()) && StringUtils.isEmpty(reqBean.getProductSn())) return execNonCompleteAndSendMsgReturn(reqBean, resultBean, "入参缺少管理编码或产品条码!");

        String curExecWorkOrderNo = getCurExecWorkOrderNo(reqBean, resultBean);

        StepResult stepResult = execLineOnComplete(reqBean, resultBean);

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

        return execDynamicsCompleteAndSendMsgReturn(reqBean, resultBean, stepResult, true, MesPcnEnumUtil.STATION_BUSI_TYPE.RUNNING_INFO, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT,
                String.format("管理编码[%s]过程条码[%s]产品条码[%s]关联生产工单[%s]上线完成!", reqBean.getTray(), reqBean.getSerialNumber(), reqBean.getProductSn(), curExecWorkOrderNo));
    }

    private String getCurExecWorkOrderNo(StationRequestBean reqBean, StationResultBean resultBean) {
        String curExecWorkOrderNo = (String) snLineOnModuleService.doHandleCurExecWorkOrder(reqBean, 1, null);
        if (StringUtils.isEmpty(curExecWorkOrderNo)) execExpSendMsgAndThrowEx(reqBean, resultBean, "此前操作的工单号已丢失,请选择启动状态的生产工单!");
        return curExecWorkOrderNo;
    }

    private StepResult execLineOnComplete(StationRequestBean reqBean, StationResultBean resultBean) {
        StepResult stepResult = ((IJxProdBusiStrategyService) SpringContextsUtil.getBean(MesPcnExtEnumUtil.JX_PROD_BUSI_STRATEGY.COMMON.getClazz()))
                .execLineOnComplete(new JxProdBusiStrategyParamsBuilder.builder()
                        .organizeCode(reqBean.getOrganizeCode())
                        .userInfo(reqBean.getUserInfo())
                        .location(reqBean.getWorkCenterCode(), reqBean.getWorkCellCode())
                        .manageCode(reqBean.getTray())
                        .serialNumber(reqBean.getSerialNumber())
                        .productSn(reqBean.getProductSn())
                        .obj(reqBean)
                        .build());
        if (!stepResult.isCompleted()) execExpSendMsgAndThrowEx(reqBean, resultBean, stepResult.getMsg());
        return stepResult;
    }

}
