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

import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.SxPutPalletModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxPutPalletModuleModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxPutPalletPlcDataModel;
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.platform.common.tool.TimeTool;
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.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description : 工步业务-本体装托人工模式扫描包装条码工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("sxPutPalletHandWorkScanPackageNoStepService")
public class SxPutPalletHandWorkScanPackageNoStepService extends BaseStepService {

    @Autowired
    private SxPutPalletModuleService putPalletModuleService;

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

        try {
            StationResultBean resultBean = new StationResultBean();
            String err;

            //从PLC读取到包装条码并校验成功,通知机械臂搬运失败后切换人工模式时，这里直接跳转满托校验
            SxPutPalletPlcDataModel curExecPackageNoModel = (SxPutPalletPlcDataModel) putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 1, null);
            if (null != curExecPackageNoModel && curExecPackageNoModel.getBoo()) {
                putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 3, null);
                StepResult stepResult = StepResult.getNonComplete();
                stepResult.setNextTriggerEvent(MesPcnExtEnumUtil.PUT_PALLET_PROD_TYPE.PLC_WORK.name());
                return stepResult;
            }

            if (StringUtils.isEmpty(reqBean.getScanInfo())) {
                SxPutPalletPlcDataModel curCtNoModel = (SxPutPalletPlcDataModel) putPalletModuleService.doHandleCurCtNo(reqBean, 1, null);
                if (null != curCtNoModel && curCtNoModel.getBoo()) {
                    if (CollectionUtils.isEmpty((List<SxPutPalletModuleModel>) putPalletModuleService.doHandlePackageDataByCtNo(reqBean, curCtNoModel.getCtNo(), 1, null))) {
                        if (null == putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 1, null)) {
                            putPalletModuleService.doHandleDisUnlockLocation(reqBean, 3, null);
                        }
                    }
                }

                err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请扫描包装条码");
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
                foundExThrowNoShowMsg();
            }

            SxPutPalletPlcDataModel packageNoModel = new SxPutPalletPlcDataModel();
            packageNoModel.setPackageNo(reqBean.getScanInfo());
            packageNoModel.setBoo(false);
            packageNoModel.setReadDatetime(TimeTool.getNowTime(true));
            putPalletModuleService.doHandleCacheCurExecPackageNo(reqBean, 2, packageNoModel);
            putPalletModuleService.doHandleDisUnlockLocation(reqBean, 2, true);
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "包装条码[%s]扫描成功"), reqBean.getScanInfo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

            return StepResult.getSuccessComplete();
        } finally {
            reqBean.setScanInfo(null);
        }
    }

}
