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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IProdPackExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnTravelExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesPartExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesProduceSnExtRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.ConvertBeanExt;
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.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.tool.MathOperation;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
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("sxNoumenonSnOnlineCreateSnStepService")
public class SxNoumenonSnOnlineCreateSnStepService extends BaseStepService {

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private MesProduceSnExtRepository produceSnExtRepository;

    @Autowired
    private MesPartExtRepository partExtRepository;

    @Autowired
    private IProdPackExtService prodPackExtService;

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private IProduceSnTravelExtService produceSnTravelExtService;

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

        StationResultBean resultBean = new StationResultBean();
        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);
        }

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

        MesWorkOrderExt workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo());
        if (null == workOrderExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]无效,不能操作"), reqBean.getWorkOrderNo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        //if (MathOperation.compareTo(workOrderExt.getBoundQty(), workOrderExt.getQty()) >= 0) {
        //    err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]已达到绑定数量,不能操作"), reqBean.getWorkOrderNo());
        //    sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
        //    return StepResult.getNonComplete(err);
        //}

        if (MesPcnExtEnumUtil.WORK_ORDER_STATUS.OPEN.getValue() != workOrderExt.getWorkOrderStatus()) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "工单[%s]状态为[%s],不能操作"),
                    reqBean.getWorkOrderNo(), MesPcnExtEnumUtil.WORK_ORDER_STATUS.valueOfDescription(workOrderExt.getWorkOrderStatus()));
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            return StepResult.getNonComplete(err);
        }

        //MesPartExt partExt = partExtRepository.getByProperty(
        //        new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, MesPcnExtConstWords.PART_NO},
        //        new Object[]{reqBean.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue(), reqBean.getPartNo()});

        //MesProdPackExt prodPackDb = prodPackExtService.getProdPackExt(reqBean.getOrganizeCode(), partExt.getPartNo(), partExt.getPptCode());

        MesProduceSnExt produceSnExt = new MesProduceSnExt();
        produceSnExt.setSerialNumber(reqBean.getSerialNumber());
        produceSnExt.setWorkOrderNo(reqBean.getWorkOrderNo());
        produceSnExt.setLastWorkOrderNo(reqBean.getWorkOrderNo());
        produceSnExt.setPartNo(reqBean.getPartNo());
        produceSnExt.setPartNameRdd(reqBean.getPartName());
        //produceSnExt.setProcessLabelTemplate(partExt.getProcessLabelTemplate());
        //produceSnExt.setQualityAssureTemplate(partExt.getQualityAssureTemplate());
        //produceSnExt.setBigProcessLabelTemplate(partExt.getBigProcessLabelTemplate());
        //produceSnExt.setProcessLabelTemplate(prodPackDb == null ? "" : prodPackDb.getProcessLabelTemplate());
        //produceSnExt.setQualityAssureTemplate(prodPackDb == null ? "" : prodPackDb.getQualityAssureTemplate());
        //produceSnExt.setBigProcessLabelTemplate(prodPackDb == null ? "" : prodPackDb.getBigProcessLabelTemplate());
        produceSnExt.setCustCode(workOrderExt.getCustCode());
        produceSnExt.setPrintStatus(MesPcnExtEnumUtil.PRODUCE_SN_PRINT_STATUS.UNPRINT.getValue());
        produceSnExt.setPrintCount(0);
        produceSnExt.setSnStatus(MesPcnExtEnumUtil.PRODUCE_SN_STATUS.CREATE.getValue());
        produceSnExt.setQcStatus(MesPcnExtEnumUtil.PRODUCE_SN_QC_STATUS.QUALIFIED.getValue());
        produceSnExt.setSnType(MesPcnExtEnumUtil.ENCODE_CODE_TYPE.STANDARD_SERIAL_SN.getValue());
        produceSnExt.setQty(1d);
        produceSnExt.setWorkCenterCode(workOrderExt.getWorkCenterCode());
        produceSnExt.setOrganizeCode(workOrderExt.getOrganizeCode());
        ConvertBean.serviceModelInitialize(produceSnExt, reqBean.getUserInfo());
        produceSnExt.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        new ConvertBeanExt(produceSnExt).convertBean(produceSnExt);
        //produceSnExtRepository.insert(produceSnExt);
        produceSnExtService.insertProduceSnExt(produceSnExt);

        // 当前执行管理码
        //String curManageCode = getCacheKey(reqBean, MesPcnExtConstWords.CUR_MANAGE_CODE);

        return StepResult.getSuccessComplete();
    }



}
