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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IPartExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IPackageExtService;
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.pojo.bean.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ProduceSnExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.SxProductOffLineRepository;
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.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 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;

/**
 * @Description : 工步业务-半成品异常下线缺件-补件报工工步
 * @Reference :
 * @Author : wangjie
 * @CreateDate : 2019-07-11
 * @Modify:
 **/
@Slf4j
@Service("sxFrePartReportWorkStepService")
public class SxFrePartReportWorkStepService extends BaseStepService {

    @Autowired
    private IPartExtService partExtService;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private IPackageExtService packageExtService;

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private SxProductOffLineRepository productOffLineRepository;

    @Autowired
    private IProduceSnRepairService produceSnRepairService;

    @Override
    public StepResult execute(StationRequestBean reqBean) {
        log.info("SxFrePartReportWorkStepService[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);
            foundExThrowNoShowMsg();
        }

        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);
            foundExThrowNoShowMsg();
        }

        if (StringUtils.isEmpty(reqBean.getSerialNumber())) {
            err = LocaleUtil.transferLanguage(reqBean.getUserInfo(), "入参缺少产品条码");
            this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        ProduceSnExtModel snModel = (ProduceSnExtModel) reqBean.getDataMap().get(MesPcnExtConstWords.PRODUCE_SN);
        if (null == snModel || !snModel.getSerialNumber().equals(reqBean.getSerialNumber())) {
            snModel = getProduceSnExtModel(reqBean);
        }
        if (null == snModel) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "条码[%s]无效"), reqBean.getSerialNumber());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        MesPackageDetail packageDetail = packageExtService.getPackageDetailBySn(reqBean.getOrganizeCode(), reqBean.getSerialNumber());
        if (null == packageDetail) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "条码[%s]不存在包装明细信息"), reqBean.getSerialNumber());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        MesPackageExt pack = packageExtService.getPackageByPackNo(reqBean.getOrganizeCode(), packageDetail.getPackageNo());
        if (null == pack) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "条码[%s]不存在包装信息"), reqBean.getSerialNumber());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
            foundExThrowNoShowMsg();
        }

        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);
            foundExThrowNoShowMsg();
        }

        if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.ATTEMPT_ORDER.getValue() == workOrderExt.getWorkOrderType()) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "生产工单号[%s]类型为[试制工单],不执行报工"), reqBean.getWorkOrderNo());
            sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);
            return StepResult.getSuccessComplete();
        }

        MesPartExt partExt = partExtService.getPartExt(reqBean.getOrganizeCode(), reqBean.getPartNo());

        SxProductOffLine productOffLine = new SxProductOffLine();
        productOffLine.setWorkOrderNo(reqBean.getWorkOrderNo());
        productOffLine.setPackageNo(pack.getPackageNo());
        productOffLine.setSerialNumber(reqBean.getSerialNumber());
        productOffLine.setPartNo(snModel.getPartNo());
        productOffLine.setPartName(snModel.getPartNameRdd());
        productOffLine.setQty(new Double(1));
        productOffLine.setWorkCenterCode(reqBean.getWorkCenterCode());
        productOffLine.setErpWarehouse(workOrderExt.getErpWarehouse());

        if (MesPcnExtEnumUtil.WORK_ORDER_TYPE.REWORK_ORDER.getValue() != workOrderExt.getWorkOrderType()) {
            productOffLine.setSnType(snModel.getSnType());
            productOffLine.setLotNo(pack.getLotNo());
            productOffLine.setShiftCode(snModel.getShiftCode());
        } else {
            productOffLine.setSnType(MesPcnExtEnumUtil.PRODUCE_SN_TYPE.REWORK_SN.getValue());
            MesProduceSnRepair produceSnRepair = produceSnRepairService.getProduceSnRepairBySnAndOrderNo(reqBean.getOrganizeCode(), reqBean.getSerialNumber(), reqBean.getWorkOrderNo());
            if (null == produceSnRepair) {
                err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]未关联返工工单[%s]"), reqBean.getSerialNumber(), reqBean.getWorkOrderNo());
                this.sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.EXP_TEXT);
                foundExThrowNoShowMsg();
            }
            productOffLine.setLotNo(produceSnRepair.getLotNo());
            productOffLine.setShiftCode(produceSnRepair.getShiftCode());
        }
        if (null != partExt) {
            productOffLine.setUnit(partExt.getUnit());
            productOffLine.setProdType(getProdType(partExt.getCategoryCode1()));
        }
        productOffLine.setSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        productOffLine.setOrganizeCode(reqBean.getOrganizeCode());
        ConvertBean.serviceModelInitialize(productOffLine, reqBean.getUserInfo());
        productOffLineRepository.insert(productOffLine);

        err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "产品条码[%s]关联包装条码[%s]已报工"),
                reqBean.getSerialNumber(), pack.getPackageNo());
        sendMessage(reqBean, resultBean, err, MesPcnEnumUtil.STATION_BUSI_TYPE.MESSAGE, MesPcnEnumUtil.STATION_DATA_TYPE.TEXT);

        return StepResult.getSuccessComplete();

    }

    private Integer getProdType(String categoryCode1) {
        if (StringUtils.isEmpty(categoryCode1)) {
            return null;
        } else {
            try {
                return Integer.valueOf(categoryCode1);
            } catch (NumberFormatException e) {
                return null;
            }
        }
    }

    private ProduceSnExtModel getProduceSnExtModel(StationRequestBean reqBean) {
        ProduceSnExtModel snModel = null;
        MesProduceSnExt produceSnExt = produceSnExtService.getProduceSnExtBySn(reqBean.getOrganizeCode(), reqBean.getSerialNumber());
        if (null != produceSnExt) {
            snModel = new ProduceSnExtModel();
            BeanUtils.copyProperties(produceSnExt, snModel, MesPcnExtConstWords.ID);
        }
        return snModel;
    }

}
