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.base.IWorkCenterExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IProduceSnExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.ISxOutPutStatisticsTravelService;
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.model.WorkCenterExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkOrderExtModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOutPutStatisticsOqcRepository;
import cn.estsh.i3plus.ext.mes.pcn.pojo.repository.MesOutPutStatisticsRepository;
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.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("sxNoumenonPackageOutPutStatisticsStepService")
public class SxNoumenonPackageOutPutStatisticsStepService extends BaseStepService {

    @Autowired
    private IWorkCenterExtService workCenterExtService;

    @Autowired
    private MesOutPutStatisticsRepository outPutStatisticsRepository;

    @Autowired
    private MesOutPutStatisticsOqcRepository outPutStatisticsOqcRepository;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private IProduceSnExtService produceSnExtService;

    @Autowired
    private IPartExtService partExtService;

    @Autowired
    private ISxOutPutStatisticsTravelService outPutStatisticsTravelService;

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

        WorkCenterExtModel centerModel = (WorkCenterExtModel) reqBean.getDataMap().get(MesPcnExtConstWords.WORK_CENTER);
        if (null == centerModel || (StringUtils.isEmpty(centerModel.getShiftCode()) || StringUtils.isEmpty(centerModel.getWorkTime()))) {
            MesWorkCenterExt workCenterExt = workCenterExtService.getWorkCenterExtByWorkCenterCode(reqBean.getOrganizeCode(), reqBean.getWorkCenterCode());
            if (null == workCenterExt) {
                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);
                foundExThrowNoShowMsg();
            }
            if (StringUtils.isEmpty(workCenterExt.getShiftCode()) || StringUtils.isEmpty(workCenterExt.getWorkTime())) {
                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);
                foundExThrowNoShowMsg();
            }
            centerModel = new WorkCenterExtModel();
            BeanUtils.copyProperties(workCenterExt, centerModel, MesPcnExtConstWords.ID);
            reqBean.getDataMap().put(MesPcnExtConstWords.WORK_CENTER, centerModel);
        }

        WorkOrderExtModel orderModel = (WorkOrderExtModel) reqBean.getDataMap().get(MesPcnExtConstWords.WORK_ORDER);
        if (null == orderModel) {
            orderModel = getWorkOrderExtModel(reqBean);
        }
        if (null == orderModel) {
            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();
        }

        MesPartExt partExt = partExtService.getPartExt(reqBean.getOrganizeCode(), orderModel.getPartNo());
        if (null == partExt) {
            err = String.format(LocaleUtil.transferLanguage(reqBean.getUserInfo(), "物料编码[%s]无效"), orderModel.getPartNo());
            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);
        }

        MesOutPutStatistics outPutStatistics = outPutStatisticsRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID,
                        MesPcnExtConstWords.WORK_ORDER_NO, MesPcnExtConstWords.WORK_TIME, MesPcnExtConstWords.SHIFT_CODE},
                new Object[]{reqBean.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(),
                        reqBean.getWorkOrderNo(), centerModel.getWorkTime(), centerModel.getShiftCode()});
        if (null == outPutStatistics) {
            outPutStatistics = new MesOutPutStatistics();
            outPutStatistics.setWorkCenterCode(reqBean.getWorkCenterCode());
            outPutStatistics.setWorkOrderNo(reqBean.getWorkOrderNo());
            outPutStatistics.setPartNo(reqBean.getPartNo());
            outPutStatistics.setPartNameRdd(reqBean.getPartName());
            outPutStatistics.setShiftCode(centerModel.getShiftCode());
            outPutStatistics.setWorkTime(centerModel.getWorkTime());
            outPutStatistics.setOrderStartTime(orderModel.getStartTime());
            outPutStatistics.setQty(new Double(1));
            outPutStatistics.setPlanQty(orderModel.getQty());
            outPutStatistics.setPlanRemakeQty(orderModel.getPlanQty());
            if (MesPcnExtEnumUtil.PRODUCE_SN_TYPE.REMAKE_SN.getValue() == snModel.getSnType()) {
                outPutStatistics.setRemakeQty(new Double(1));
            } else {
                outPutStatistics.setRemakeQty(new Double(0));
            }
            outPutStatistics.setWorkOrderType(orderModel.getWorkOrderType());
            outPutStatistics.setOrganizeCode(reqBean.getOrganizeCode());
            outPutStatistics.setCategoryCode(centerModel.getCategoryCode());
            outPutStatistics.setCareerCode(centerModel.getCareerCode());
            outPutStatistics.setOutPutFlag(orderModel.getOutPutFlag());
            outPutStatistics.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
            ConvertBean.serviceModelInitialize(outPutStatistics, reqBean.getUserInfo());
            outPutStatistics.setStartTime(outPutStatistics.getModifyDatetime());
            outPutStatistics.setEndTime(outPutStatistics.getStartTime());

            MesOutPutStatisticsOqc outPutStatisticsOqc = new MesOutPutStatisticsOqc();
            BeanUtils.copyProperties(outPutStatistics, outPutStatisticsOqc);
            outPutStatisticsOqc.setCheckLimit(new Double(0));
            outPutStatisticsOqc.setSpotCheckQty(new Double(0));
            outPutStatisticsOqc.setCheckResult(MesPcnExtEnumUtil.OQC_TOTAL_STATUS.UNTREATED.getValue());
            outPutStatisticsOqc.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
            outPutStatisticsOqc.setPid(outPutStatisticsRepository.insert(outPutStatistics).getId());
            outPutStatisticsOqcRepository.insert(outPutStatisticsOqc);
        } else {
            outPutStatistics.setQty(MathOperation.add(outPutStatistics.getQty(), new Double(1)));
            if (MesPcnExtEnumUtil.PRODUCE_SN_TYPE.REMAKE_SN.getValue() == snModel.getSnType()) {
                outPutStatistics.setRemakeQty(MathOperation.add(outPutStatistics.getRemakeQty(), new Double(1)));
            }
            ConvertBean.serviceModelUpdate(outPutStatistics, reqBean.getUserInfo());
            outPutStatistics.setEndTime(outPutStatistics.getModifyDatetime());
            outPutStatistics.setSystemSyncStatus(MesPcnExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
            outPutStatisticsRepository.save(outPutStatistics);
        }
        outPutStatisticsTravelService.insertOutPutStatisticsTravel(outPutStatistics);

        return StepResult.getSuccessComplete();
    }

    private WorkOrderExtModel getWorkOrderExtModel(StationRequestBean reqBean) {
        WorkOrderExtModel orderModel = null;
        MesWorkOrderExt workOrderExt = workOrderExtService.getWorkOrderExtByOrderNo(reqBean.getOrganizeCode(), reqBean.getWorkOrderNo());
        if (null != workOrderExt) {
            orderModel = new WorkOrderExtModel();
            BeanUtils.copyProperties(workOrderExt, orderModel, MesPcnExtConstWords.ID);
        }
        return orderModel;
    }

    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;
    }

}

