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

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.IMissingPartService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IOqcManageService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu.IBuOutPutStatisticsService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu.IBuQmsHttpRequestService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.bu.IBuQmsOqcManageService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MissPartConfirmModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.OqcLastCheckModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.WorkOrderQueryDto;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.BuOutPutStatisticsModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.BuOutPutStatisticsQtyModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.SxBuOqcDataModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.bu.SxPartCategoryModel;
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.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 BuQmsHttpRequestService implements IBuQmsHttpRequestService {

    @Autowired
    private IOqcManageService oqcManageService;

    @Autowired
    private IPartExtService partExtService;

    @Autowired
    private IBuOutPutStatisticsService outPutStatisticsService;

    @Autowired
    private ISxWorkOrderExtService workOrderExtService;

    @Autowired
    private IMissingPartService missingPartService;

    @Override
    public SxBuOqcDataModel queryBuQmsOqcManageData(Map<String, Object> params) {
        checkNormalDataParams(params);
        return ((IBuQmsOqcManageService) SpringContextsUtil.getBean(
                (String) params.get(MesPcnExtConstWords.CLASS_NAME))).exec(params);
    }

    private void checkNormalDataParams(Map<String, Object> params) {
        if (CollectionUtils.isEmpty(params) ||
                (!params.containsKey(MesPcnExtConstWords.ORGANIZE_CODE) || !params.containsKey(MesPcnExtConstWords.VALUE)) || !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 MissPartConfirmModel getMissingPartAndWorkOrderBySn(Map<String, Object> params) {
        return missingPartService.getMissingPartAndWorkOrderBySn((String) params.get(MesPcnExtConstWords.SERIAL_NUMBER), (String) params.get(MesPcnExtConstWords.ORGANIZE_CODE) );
    }

    @Override
    public List<BuOutPutStatisticsModel> queryOutPutStatisticsList(Map<String, Object> params) {
        OqcLastCheckModel oqcLastCheckModel = JSONObject.toJavaObject(JSONObject.parseObject((String) params.get(MesPcnExtConstWords.MODEL)), OqcLastCheckModel.class);
        return outPutStatisticsService.queryOutPutStatisticsModelList(oqcLastCheckModel, (String) params.get(MesPcnExtConstWords.ORGANIZE_CODE) );
    }

    @Override
    public List<SxPartCategoryModel> queryPartCategoryList(Map<String, Object> params) {
        return partExtService.queryPartCategoryList((String) params.get(MesPcnExtConstWords.ORGANIZE_CODE) );
    }

    @Override
    public List<BuOutPutStatisticsQtyModel> queryOutPutStatisticsQtyList(Map<String, Object> params) {
        BuOutPutStatisticsQtyModel model = JSONObject.toJavaObject(JSONObject.parseObject((String) params.get(MesPcnExtConstWords.MODEL)), BuOutPutStatisticsQtyModel.class);
        return outPutStatisticsService.queryOutPutStatisticsQtyList(model);
    }

    @Override
    public ListPager queryBuQmsWindowWorkOrderExtListPage(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.queryBuWindowWorkOrderExtListPage(workOrderQueryDto, pager);
    }
}
