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

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.jx.IMesManageCodeService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnRepairService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnTravelExtService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.jx.SxNoumenonSnPopUpOnlineModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtEnumUtil;
import cn.estsh.i3plus.mes.pcn.serviceimpl.base.BaseStepService;
import cn.estsh.i3plus.mes.pcn.util.LocaleUtil;
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;

/**
 * @Description : 工步业务-本体包装校验条码有效性工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("sxNoumenonSnOnlineCheckSnStepService")
public class SxNoumenonSnOnlineCheckSnStepService extends BaseStepService {

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private IProduceSnRepairService produceSnRepairService;

    @Autowired
    private IMesManageCodeService manageCodeService;

    @Autowired
    private IProduceSnTravelExtService produceSnTravelExtService;

    @Autowired
    private SxNoumenonSnPopUpOnlineModuleService popUpOnlineModuleService;

    @Override
    public StepResult guide(StationRequestBean reqBean) {
        StationResultBean resultBean = new StationResultBean();
        String guide = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请扫描管理码/产品条码");
        this.sendMessage(reqBean, resultBean, guide, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
        return super.guide(reqBean);
    }

    @Override
    public StepResult init(StationRequestBean reqBean) {
        StationResultBean resultBean = new StationResultBean();
        resultBean.setBusiType(MesPcnEnumUtil.STATION_BUSI_TYPE.STEP_CONTENT.getValue());
        this.sendMessage(reqBean, resultBean);
        return super.init(reqBean);
    }

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("SxNoumenonSnOnlineCheckSnStepService[execute] 参数：{}", reqBean);

        StationResultBean resultBean = new StationResultBean();

        StepResult stepResult = StepResult.getSuccessComplete();

        String err;

        if (StringUtils.isEmpty(reqBean.getOrganizeCode())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少工厂代码");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        if (StringUtils.isEmpty(reqBean.getWorkOrderNo())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少工单号");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        if (StringUtils.isEmpty(reqBean.getScanInfo())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少管理码/产品条码");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        MesWorkOrderExt workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo());

        if (null == workOrderExt) return execNonCompleteAndSendMsgReturn(reqBean, resultBean, stepResult, String.format("工单[%s]无效,不能操作", reqBean.getWorkOrderNo()));

        if (MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue() != workOrderExt.getWorkOrderStatus()) return execNonCompleteAndSendMsgReturn(reqBean, resultBean, stepResult,
                String.format("工单[%s]状态为[%s],不能操作", reqBean.getWorkOrderNo(), MesPcnExtEnumUtil.WORK_ORDER_STATUS.valueOfDescription(workOrderExt.getWorkOrderStatus())));

        // 扫描内容 为 管理码
        String scanInfo = reqBean.getScanInfo();

        boolean isNeedCreateSn = false;

        //MesProduceSnExt produceSnExtDb = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), scanInfo);

        //if (produceSnExtDb != null) return execNonCompleteAndSendMsgReturn(reqBean, resultBean, stepResult,
        //        String.format("管理码已被工单[%s]占用,操作失败", produceSnExtDb.getWorkOrderNo()));

        if (workOrderExt.getWorkOrderType() == MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue()) {
            MesProduceSnRepair produceSnRepairBySn = produceSnRepairService.getProduceSnRepairBySn(reqBean.getOrganizeCode(), scanInfo);

            if (null == produceSnRepairBySn) {

                return execNonCompleteAndSendMsgReturn(reqBean, resultBean, stepResult,
                        String.format("返工条码[%s]不存在,操作失败", reqBean.getScanInfo(), produceSnRepairBySn.getWorkOrderNo(), reqBean.getWorkOrderNo()));

            } else if(produceSnRepairBySn.getWorkOrderNo().equals(reqBean.getWorkOrderNo())) {
                return execNonCompleteAndSendMsgReturn(reqBean, resultBean, stepResult,
                        String.format("返工条码[%s]已绑定工单[%s]与当前工单[%s]不一致,操作失败", reqBean.getScanInfo(), produceSnRepairBySn.getWorkOrderNo(), reqBean.getWorkOrderNo()));
            }
        } else {
            MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), scanInfo);
            if (produceSnExt == null) {

                isNeedCreateSn = true;

                MesManageCode manageCode = manageCodeService.getManageCode(reqBean.getOrganizeCode(), scanInfo);

                if (manageCode == null) return execNonCompleteAndSendMsgReturn(reqBean, resultBean, stepResult,
                            String.format("管理码[%s]无效,操作失败", reqBean.getScanInfo()));


                if (!manageCode.getWorkCenterCode().equals(reqBean.getWorkCenterCode())) return execNonCompleteAndSendMsgReturn(reqBean, resultBean, stepResult,
                            String.format("当前管理码[%s]所属产线[%s]与当前产线[%s]不一致,操作失败", reqBean.getScanInfo(), manageCode.getWorkCenterCode(), reqBean.getWorkCenterCode()));

            } else if (!produceSnExt.getLastWorkOrderNo().equals(reqBean.getWorkOrderNo())) return execNonCompleteAndSendMsgReturn(reqBean, resultBean, stepResult,
                        String.format("条码[%s]已绑定工单[%s],与当前工单[%s]不一致,操作失败", reqBean.getScanInfo(), produceSnExt.getWorkOrderNo(), reqBean.getWorkOrderNo()));
        }

        MesProduceSnTravelExt produceSnTravelExtDb = produceSnTravelExtService.getProduceSnTravelExtByOrderNoAndWoAndSn(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode(), reqBean.getWorkCellCode(), reqBean.getWorkOrderNo(), reqBean.getScanInfo());

        if (produceSnTravelExtDb != null) return execNonCompleteAndSendMsgReturn(reqBean, resultBean, stepResult,
                    String.format("管理码[%s]已经过此工位,请更改管理码再进行操作", reqBean.getScanInfo()));

        reqBean.getDataMap().put(MesPcnExtConstWords.WORK_ORDER_NO, reqBean.getWorkOrderNo());
        reqBean.getDataMap().put(MesPcnExtConstWords.MANAGE_CODE, reqBean.getScanInfo());

        reqBean.setSerialNumber(reqBean.getScanInfo());
        reqBean.setPartNo(workOrderExt.getPartNo());
        reqBean.setPartName(workOrderExt.getPartNameRdd());

        if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() == workOrderExt.getWorkOrderType()) {
            stepResult.setNextTriggerEvent(MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.name());
        } else if (isNeedCreateSn){
            stepResult.setNextTriggerEvent("CREATE_SN");
        } else {
            stepResult.setNextTriggerEvent(MesPcnExtEnumUtil.WORK_ORDER_TYPE.STANDARD_ORDER.name());
        }

        popUpOnlineModuleService.doHandleManageCode(reqBean, 2, reqBean.getScanInfo());

        return stepResult;
    }





}
