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

import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.busi.station.ep.EpSnLineOffModuleService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkOrderExtModel;
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.platform.common.tool.MathOperation;
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.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 : 工步业务-EP打包下线初始化工单工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("epSnLineOffInitOrderStepService")
public class EpSnLineOffInitOrderStepService extends BaseStepService {

    @Autowired
    private EpSnLineOffModuleService snLineOffModuleService;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

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

    @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("EpSnLineOffInitOrderStepService[execute] 参数：{}", reqBean);

        StationResultBean resultBean = new StationResultBean();
        String err;

        String curWorkOrderNo = (String) snLineOffModuleService.doHandleCurWorkOrderNo(reqBean, 1, null);
        String selectWorkOrderNo = (String) snLineOffModuleService.doHandleSelectWorkOrderNo(reqBean, 1, null);
        if (StringUtils.isEmpty(curWorkOrderNo) && StringUtils.isEmpty(selectWorkOrderNo)) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "请选择工单");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.GUIDE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            foundExThrowNoShowMsg();
        }

        MesWorkOrderExt workOrderExt = null;
        if (!StringUtils.isEmpty(selectWorkOrderNo)) {
            snLineOffModuleService.doHandleSelectWorkOrderNo(reqBean, 3, null);
            workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), selectWorkOrderNo);
            if (null == workOrderExt) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]不存在,选择工单失败"), selectWorkOrderNo);
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                if (StringUtils.isEmpty(curWorkOrderNo)) {
                    foundExThrowNoShowMsg();
                } else {
                    return StepResult.getNonComplete(err);
                }
            }

            if (!workOrderExt.getWorkCenterCode().equals(reqBean.getWorkCenterCode())) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]关联的生产线[%s]与当前生产线[%s]不一致,选择工单失败"),
                        selectWorkOrderNo, workOrderExt.getWorkCenterCode(), reqBean.getWorkCenterCode());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                if (StringUtils.isEmpty(curWorkOrderNo)) {
                    foundExThrowNoShowMsg();
                } else {
                    return StepResult.getNonComplete(err);
                }
            }

            if (MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue() != workOrderExt.getWorkOrderStatus()) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]状态为[%s],选择工单失败"),
                        selectWorkOrderNo, MesPcnExtEnumUtil.WORK_ORDER_STATUS.valueOfDescription(workOrderExt.getWorkOrderStatus()));
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                if (StringUtils.isEmpty(curWorkOrderNo)) {
                    foundExThrowNoShowMsg();
                } else {
                    return StepResult.getNonComplete(err);
                }
            }

            snLineOffModuleService.doHandleCurWorkOrderNo(reqBean, 2, selectWorkOrderNo);
        }

        if (null == workOrderExt) {
            workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), curWorkOrderNo);
            if (null == workOrderExt) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]无效"), curWorkOrderNo);
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                foundExThrowNoShowMsg();
            }
            if (MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue() != workOrderExt.getWorkOrderStatus()) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]状态为[%s],选择工单失败"),
                        selectWorkOrderNo, MesPcnExtEnumUtil.WORK_ORDER_STATUS.valueOfDescription(workOrderExt.getWorkOrderStatus()));
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                foundExThrowNoShowMsg();
            }
        }

        if (MathOperation.compareTo(workOrderExt.getQty(), workOrderExt.getCompleteQty()) == 0) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]完成数量已达标,请重新选择工单"),
                    workOrderExt.getOrderNo(), MesPcnExtEnumUtil.WORK_ORDER_STATUS.valueOfDescription(workOrderExt.getWorkOrderStatus()));
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            foundExThrowNoShowMsg();
        }

        // 如果进行了工单切换操作，则清空上个工单关键件缓存信息
        if ((!StringUtils.isEmpty(curWorkOrderNo)) && (!curWorkOrderNo.equals(selectWorkOrderNo))) {
            String bindKeyPartCacheKey = getCacheKey(reqBean, MesPcnExtConstWords.BIND_KEY_PART);
            redisMesPcn.deleteKey(bindKeyPartCacheKey);
        }

        WorkOrderExtModel orderModel = new WorkOrderExtModel();
        BeanUtils.copyProperties(workOrderExt, orderModel, MesPcnExtConstWords.ID);
        reqBean.getDataMap().put(MesPcnExtConstWords.WORK_ORDER, orderModel);
        reqBean.setWorkOrderNo(workOrderExt.getOrderNo());
        log.info("EpSnLineOffInitOrderStepService[execute] END 参数：{}", reqBean);
        return StepResult.getSuccessComplete();

    }

}
