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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IShiftExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxOrganizeExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxRepairWorkService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ISxWorkOrderExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.base.ep.IHappenStationService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.ep.*;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxHappenStationModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.SxWorkCellModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkOrderQueryDto;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpQmsBadCellDetailModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpQmsOutPutStatisticsModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.EpQmsRepirTaskModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.SxEpOqcManageModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesShift;
import cn.estsh.i3plus.pojo.mes.model.DefectModel;
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class EpQmsHttpRequestService implements IEpQmsHttpRequestService {

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private IShiftExtService shiftExtService;

    @Autowired
    private ISxRepairWorkService repairWorkService;

    @Autowired
    private IHappenStationService happenStationService;

    @Autowired
    private IEpBadCellDetailService badCellDetailService;

    @Autowired
    private IEpRepairTaskService repairTaskService;

    @Autowired
    private IEpOutPutStatisticsService outPutStatisticsService;

    @Autowired
    private ISxOrganizeExtService organizeExtService;

    @Override
    public SxEpOqcManageModel queryEpQmsOqcManageData(Map<String, Object> params) {
        checkNormalDataParams(params);
        return ((IEpQmsOqcManageService) SpringContextsUtil.getBean(
                (String) params.get(MesPcnExtConstWords.CLASS_NAME))).queryEpQmsOqcManageData(params);
    }

    private void checkNormalDataParams(Map<String, Object> params) {
        if (CollectionUtils.isEmpty(params) || (!params.containsKey(MesPcnExtConstWords.ORGANIZE_CODE)) || !params.containsKey(MesPcnExtConstWords.CLASS_NAME)) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("获取出货检验数据HTTP请求PCN服务缺失必要参数")
                    .build();
        }
    }

    @Override
    public ListPager queryEpQmsWindowWorkOrderExtListPage(Map<String, Object> params) {
        WorkOrderQueryDto workOrderQueryDto = JSONObject.toJavaObject(JSONObject.parseObject((String) params.get(MesPcnExtConstWords.MODEL)), WorkOrderQueryDto.class);
        Pager pager = JSONObject.toJavaObject(JSONObject.parseObject((String) params.get(MesPcnExtConstWords.PAGER)), Pager.class);
        return workOrderExtService.queryWindowWorkOrderExtListPage(workOrderQueryDto, pager);
    }

    @Override
    public List<MesShift> queryEpQmsSelectShiftList(Map<String, Object> params) {
        checkShiftParams(params);
        return shiftExtService.getShiftListbyCenterCode((String) params.get(MesPcnExtConstWords.ORGANIZE_CODE), (String) params.get(MesPcnExtConstWords.WORK_CENTER_CODE));
    }

    private void checkShiftParams(Map<String, Object> params) {
        if (CollectionUtils.isEmpty(params) || (!params.containsKey(MesPcnExtConstWords.ORGANIZE_CODE) || !params.containsKey(MesPcnExtConstWords.WORK_CENTER_CODE))) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("获取班次信息数据HTTP请求PCN服务缺失必要参数")
                    .build();
        }
    }

    @Override
    public List<MesShift> queryEpQmsShiftList(Map<String, Object> params) {
        if (CollectionUtils.isEmpty(params) || !params.containsKey(MesPcnExtConstWords.ORGANIZE_CODE)) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("获取班次信息数据HTTP请求PCN服务缺失必要参数")
                    .build();
        }
        return shiftExtService.getShiftList((String) params.get(MesPcnExtConstWords.ORGANIZE_CODE));
    }

    @Override
    public List<DefectModel> queryEpQmsDefectList(Map<String, Object> params) {
        return repairWorkService.getMesDefect((String) params.get(MesPcnExtConstWords.ORGANIZE_CODE));
    }

    @Override
    public ListPager<SxHappenStationModel> queryEpQmsWindowHappenStationListPage(Map<String, Object> params) {
        SxHappenStationModel happenStationModel = JSONObject.toJavaObject(JSONObject.parseObject((String) params.get(MesPcnExtConstWords.MODEL)), SxHappenStationModel.class);
        Pager pager = JSONObject.toJavaObject(JSONObject.parseObject((String) params.get(MesPcnExtConstWords.PAGER)), Pager.class);
        return happenStationService.queryByPager(happenStationModel, pager);
    }

    @Override
    public ListPager<SxWorkCellModel> queryEpQmsWindowWorkCellListPage(Map<String, Object> params) {
        SxWorkCellModel workCellModel = JSONObject.toJavaObject(JSONObject.parseObject((String) params.get(MesPcnExtConstWords.MODEL)), SxWorkCellModel.class);
        Pager pager = JSONObject.toJavaObject(JSONObject.parseObject((String) params.get(MesPcnExtConstWords.PAGER)), Pager.class);
        return organizeExtService.queryValidWorkCellListPage(workCellModel, pager);
    }

    @Override
    public List<EpQmsBadCellDetailModel> queryEpQmsBadCellDetailList(Map<String, Object> params) {
        EpQmsBadCellDetailModel badCellDetailModel = JSONObject.toJavaObject(JSONObject.parseObject((String) params.get(MesPcnExtConstWords.MODEL)), EpQmsBadCellDetailModel.class);
        List<String> partNoList = params.containsKey(MesPcnExtConstWords.PART_NO) ? JSONObject.parseObject((String) params.get(MesPcnExtConstWords.PART_NO), List.class) : null;
        return badCellDetailService.queryEpBadCellDetailList(badCellDetailModel, partNoList);
    }

    @Override
    public List<EpQmsRepirTaskModel> queryEpQmsRepairTaskList(Map<String, Object> params) {
        EpQmsRepirTaskModel repirTaskModel = JSONObject.toJavaObject(JSONObject.parseObject((String) params.get(MesPcnExtConstWords.MODEL)), EpQmsRepirTaskModel.class);
        List<String> partNoList = params.containsKey(MesPcnExtConstWords.PART_NO) ? JSONObject.parseObject((String) params.get(MesPcnExtConstWords.PART_NO), List.class) : null;
        return repairTaskService.queryEpQmsRepairTaskList(repirTaskModel, partNoList);
    }

    @Override
    public List<EpQmsOutPutStatisticsModel> queryEpQmsOutPutStatisticsList(Map<String, Object> params) {
        EpQmsOutPutStatisticsModel repirTaskModel = JSONObject.toJavaObject(JSONObject.parseObject((String) params.get(MesPcnExtConstWords.MODEL)), EpQmsOutPutStatisticsModel.class);
        List<String> partNoList = params.containsKey(MesPcnExtConstWords.PART_NO) ? JSONObject.parseObject((String) params.get(MesPcnExtConstWords.PART_NO), List.class) : null;
        return outPutStatisticsService.queryEpQmsOutPutStatisticsList(repirTaskModel, partNoList);
    }

}
