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 java.util.Map;

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

    private MesOutPutStatisticsRepository outPutStatisticsRepository;

    private ISxWorkOrderExtService workOrderExtService;

    private IShiftExtService shiftExtService;

    public BuQmsOqcByZjExecBusiService() {

        outPutStatisticsRepository = SpringContextsUtil.getBean(MesOutPutStatisticsRepository.class);

        workOrderExtService = SpringContextsUtil.getBean(SxWorkOrderExtService.class);

        shiftExtService = SpringContextsUtil.getBean(ShiftExtService.class);

    }

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

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

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

        log.info("BU-QMS出货检验公共方法 --- 组件出货检验 START --- 参数[{}][{}] --- 线程ID[{}]", organizeCode, idStr, Thread.currentThread().getName());

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

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

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

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

        //封装班次
        packShift(oqcDataModel);

        log.info("BU-QMS出货检验公共方法 --- 组件出货检验 --- 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, MesOutPutStatistics outPutStatisticsDb) {
        MesWorkOrder workOrder = workOrderExtService.getWorkOrderByOrderNo(organizeCode, outPutStatisticsDb.getWorkOrderNo());
        if (null == workOrder) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("组件抽检:产量统计信息ID[%s]数据对应的工单[%s]信息不存在", outPutStatisticsDb.getId(), outPutStatisticsDb.getWorkOrderNo())
                    .build();
        }
        return workOrder;
    }

    private void packOqcManageData(SxBuOqcDataModel oqcDataModel, MesWorkOrder workOrder, MesOutPutStatistics outPutStatisticsDb) {
        oqcDataModel.setWorkOrderNo(workOrder.getOrderNo());
        oqcDataModel.setWorkOrderType(workOrder.getWorkOrderType());
        oqcDataModel.setWorkOrderTypeName(MesPcnExtEnumUtil.WORK_ORDER_TYPE.valueOfDescription(workOrder.getWorkOrderType()));
        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());
        oqcDataModel.setWorkTime(outPutStatisticsDb.getWorkTime());
        oqcDataModel.setShiftCode(outPutStatisticsDb.getShiftCode());
    }

    private void packShift(SxBuOqcDataModel oqcDataModel) {
        MesShift shiftDb = shiftExtService.getShiftByShiftCodeAndCenterCode(oqcDataModel.getOrganizeCode(), oqcDataModel.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("组件抽检:产量统计信息ID[%s]数据对应的产量统计的班次[%s]与生产线[%s]的关联信息不存在",
                            oqcDataModel.getIdStr(), oqcDataModel.getShiftCode(), oqcDataModel.getWorkCenterCode())
                    .build();
        }
        oqcDataModel.setShiftName(shiftDb.getShiftName());
    }

}
