package com.diagbot.facade;

import com.diagbot.client.QcServiceClient;
import com.diagbot.dto.OutputInfo;
import com.diagbot.dto.Response;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.util.SpringContextUtil;
import com.diagbot.util.StringUtil;
import com.diagbot.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.URI;

/**
 * @Description:
 * @author: gaodm
 * @time: 2024/7/8 11:33
 */
@Component
public class QcClientFacade {

    @Autowired
    private QcServiceClient qcServiceClient;
    @Autowired
    private SysHospitalSetFacade sysHospitalSetFacade;

    public Response<OutputInfo> extract(QueryVo queryVo) {
        URI uri = null;
        String code = "active_profiles_" + SpringContextUtil.getActiveProfile();
        String url = sysHospitalSetFacade.getValue(Long.valueOf(queryVo.getHospitalId()), code);
        if (StringUtil.isBlank(url)) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "配置表内找不到评分接口地址数据！");
        }
        url = "http://127.0.0.1:6660";
        try {
            uri = new URI(url);
        } catch (Exception e) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR);
        }

        return qcServiceClient.extract(uri, queryVo);
    }

    public Response<OutputInfo> analyseField(QueryVo queryVo) {
        URI uri = null;
        String code = "active_profiles_" + SpringContextUtil.getActiveProfile();
        String url = sysHospitalSetFacade.getValue(Long.valueOf(queryVo.getHospitalId()), code);
        if (StringUtil.isBlank(url)) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "配置表内找不到评分接口地址数据！");
        }
        try {
//            String url = "http://127.0.0.1:6010";
            uri = new URI(url);
        } catch (Exception e) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR);
        }

        return qcServiceClient.analyseField(uri, queryVo);
    }
}
