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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IShiftExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu.IBuQmsOqcManageService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.base.ShiftExtService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.base.SxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesOutPutStatistics;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.SxBuOqcDataModel;
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.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesShift;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @Description 出货检验 手动检验 成品 被代理产品
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
public class BuQmsOqcBySnManualExecBusiService implements IBuQmsOqcManageService {

    private ISxWorkOrderExtService workOrderExtService;

    private IShiftExtService shiftExtService;

    private MesOutPutStatisticsRepository outPutStatisticsRepository;

    public BuQmsOqcBySnManualExecBusiService() {

        workOrderExtService = SpringContextsUtil.getBean(SxWorkOrderExtService.class);

        shiftExtService = SpringContextsUtil.getBean(ShiftExtService.class);

        outPutStatisticsRepository = SpringContextsUtil.getBean(MesOutPutStatisticsRepository.class);

    }

    @Override
    public SxBuOqcDataModel exec(Map<String, Object> params) {

        String organizeCode = (String) params.get(MesPcnExtConstWords.ORGANIZE_CODE);

        String idStr = (String) params.get(MesPcnExtConstWords.VALUE);

        String dataType = (String) params.get(MesPcnExtConstWords.DATA_TYPE);

        log.info("BU-QMS出货检验手动检验公共方法 --- 成品出货检验 --- 获取工单关联信息 START --- --- 线程ID[{}]", Thread.currentThread().getName());

        //产量
        MesOutPutStatistics outPutStatisticsDb = getOutPutStatistics(Long.parseLong(idStr));

        //工单
        MesWorkOrder workOrder = getWorkOrder(organizeCode, outPutStatisticsDb.getWorkOrderNo());

        //初始化
        SxBuOqcDataModel oqcDataModel = new SxBuOqcDataModel(organizeCode);

        //数据组合
        packOqcManageData(oqcDataModel, workOrder, outPutStatisticsDb);

        log.info("BU-QMS出货检验手动检验公共方法 --- 成品出货检验 --- 获取工单关联信息 SUCCESS --- result[{}] --- 线程ID[{}]", JSONObject.toJSONString(oqcDataModel), Thread.currentThread().getName());

        if (StringUtils.isEmpty(dataType) || CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValueStr() == dataType) return oqcDataModel;

        log.info("BU-QMS出货检验手动检验公共方法 --- 成品出货检验 --- 获取条码组信息 START --- 线程ID[{}]", Thread.currentThread().getName());

        //条码组信息必要参数
        params.put(MesPcnExtConstWords.MODEL, oqcDataModel);

        //条码组信息
        oqcDataModel = new BuQmsOqcBySnExecAmountService().exec(params);

        log.info("BU-QMS出货检验手动检验公共方法 --- 成品出货检验 --- 获取条码组信息 SUCCESS --- result[{}] --- 线程ID[{}]", JSONObject.toJSONString(oqcDataModel), Thread.currentThread().getName());

        return oqcDataModel;
    }

    private MesOutPutStatistics getOutPutStatistics(Long id) {
        MesOutPutStatistics outPutStatisticsDb = outPutStatisticsRepository.getById(id);
        if (null == outPutStatisticsDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("手动检验:传参ID[%s]数据对应的产量统计信息不存在", id)
                    .build();
        }
        return outPutStatisticsDb;
    }

    private MesWorkOrder getWorkOrder(String organizeCode, String workOrderNo) {
        MesWorkOrder workOrder = workOrderExtService.getWorkOrderByOrderNo(organizeCode, workOrderNo);
        if (null == workOrder) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("手动检验:生产工单[%s]信息不存在", workOrderNo)
                    .build();
        }
        return workOrder;
    }

    private void packOqcManageData(SxBuOqcDataModel oqcDataModel, MesWorkOrder workOrder, MesOutPutStatistics outPutStatisticsDb) {
        oqcDataModel.setWorkTime(outPutStatisticsDb.getWorkTime());
        oqcDataModel.setShiftCode(outPutStatisticsDb.getShiftCode());

        MesShift shiftDb = shiftExtService.getShiftByShiftCodeAndCenterCode(oqcDataModel.getOrganizeCode(), workOrder.getWorkCenterCode(), oqcDataModel.getShiftCode());
        if (null == shiftDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("手动检验:生产工单[%s]对应的生产线[%s]与班次[%s]的关联信息不存在", workOrder.getOrderNo(), workOrder.getWorkCenterCode(), oqcDataModel.getShiftCode())
                    .build();
        }
        oqcDataModel.setShiftName(shiftDb.getShiftName());
        oqcDataModel.setWorkOrderType(workOrder.getWorkOrderType());
        oqcDataModel.setWorkOrderTypeName(MesPcnExtEnumUtil.WORK_ORDER_TYPE.valueOfDescription(workOrder.getWorkOrderType()));
        oqcDataModel.setWorkOrderNo(workOrder.getOrderNo());
        oqcDataModel.setPartNo(workOrder.getPartNo());
        oqcDataModel.setPartNameRdd(workOrder.getPartNameRdd());
        oqcDataModel.setWorkCenterCode(workOrder.getWorkCenterCode());
        oqcDataModel.setPlanQty(workOrder.getQty());
        oqcDataModel.setCompleteQty(outPutStatisticsDb.getQty());
        oqcDataModel.setIdStr(outPutStatisticsDb.getId().toString());
    }

}
