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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IWorkCenterExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IPackageExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkCenterExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkCenterExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.BuErrorMessageModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.bean.MesPackageDetail;
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.ImppRedis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

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

    @Resource(name = MesPcnExtConstWords.REDIS_RES_PCN)
    private ImppRedis redisMesPcn;

    @Autowired
    private IPackageExtService packageExtService;

    @Autowired
    private IWorkCenterExtService workCenterExtService;

    @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 execute(StationRequestBean reqBean) {
        log.info("sxBuNoumenonPackageScanPackNoStepService[execute] 参数：{}", reqBean);

        StationResultBean resultBean = new StationResultBean();
        String err;

        BuErrorMessageModel errorMessageModel = new BuErrorMessageModel();
        errorMessageModel.setScanInfo(reqBean.getScanInfo());
        errorMessageModel.setErrorStepCode("sxBuNoumenonPackageScanPackNoStepService");
        errorMessageModel.setErrorStepName("本体包装扫描包装条码工步");

        if (StringUtils.isEmpty(reqBean.getOrganizeCode())) {
            errorMessageModel.setErrorMessage("入参缺少工厂代码");
            redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.ERROR_MESSAGE), MesPcnExtConstWords.ERROR_MESSAGE, errorMessageModel, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            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())) {
            errorMessageModel.setErrorMessage("入参缺少包装条码");
            redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.ERROR_MESSAGE), MesPcnExtConstWords.ERROR_MESSAGE, errorMessageModel, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            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.getSerialNumber())) {
            errorMessageModel.setErrorMessage("入参缺少产品条码");
            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);
        }

        MesPackageDetail packageDetail = packageExtService.getPackageDetailBySn(reqBean.getOrganizeCode(), reqBean.getSerialNumber());
        if (null == packageDetail) {
            errorMessageModel.setErrorMessage("产品条码[%s]未打包");
            redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.ERROR_MESSAGE), MesPcnExtConstWords.ERROR_MESSAGE, errorMessageModel, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]未打包"), reqBean.getSerialNumber());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        if (!packageDetail.getPackageNo().equals(reqBean.getScanInfo())) {
            errorMessageModel.setErrorMessage("产品条码[%s]关联的包装条码为[%s],操作失败");
            redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.ERROR_MESSAGE), MesPcnExtConstWords.ERROR_MESSAGE, errorMessageModel, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]关联的包装条码为[%s],操作失败"),
                    reqBean.getSerialNumber(), packageDetail.getPackageNo());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        MesWorkCenterExt workCenterExt = workCenterExtService.getWorkCenterExtByWorkCenterCode(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode());
        if (null == workCenterExt) {
            errorMessageModel.setErrorMessage("生产线代码[%s]无效");
            redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.ERROR_MESSAGE), MesPcnExtConstWords.ERROR_MESSAGE, errorMessageModel, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产线代码[%s]无效"), reqBean.getWorkCenterCode());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        if (StringUtils.isEmpty(workCenterExt.getShiftCode()) || StringUtils.isEmpty(workCenterExt.getWorkTime())) {
            errorMessageModel.setErrorMessage("生产线代码[%s]未启动班次");
            redisMesPcn.putHash(getCacheKey(reqBean, MesPcnExtConstWords.ERROR_MESSAGE), MesPcnExtConstWords.ERROR_MESSAGE, errorMessageModel, MesPcnEnumUtil.EXPIRE_TIME.NEVER.getValue());
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产线代码[%s]未启动班次"), reqBean.getWorkCenterCode());
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }
        WorkCenterExtModel centerModel = new WorkCenterExtModel();
        BeanUtils.copyProperties(workCenterExt, centerModel, MesPcnExtConstWords.ID);
        reqBean.getDataMap().put(MesPcnExtConstWords.WORK_CENTER, centerModel);
        reqBean.setPackageNo(reqBean.getScanInfo());

        return StepResult.getSuccessComplete();
    }

}
