package thyyxxk.webserver.http.drg;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dtflys.forest.Forest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.envionment.ApiUrl;
import thyyxxk.webserver.config.envionment.SystemConfig;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.utils.ResultVoUtil;

@Service
@RequiredArgsConstructor
@Slf4j
public class DrgWebApi {
    private final ApiUrl apiUrl;
    private final SystemConfig config;


    public JSONObject drgQuality(JSONObject obj) {
        if (StrUtil.isBlank(apiUrl.getDrgWebApi())) {
            return new JSONObject();
        }
        return Forest.post(apiUrl.getDrgWebApi() + "/drg_web/drgGroupThird/V2/setListDrg.action")
                .connectTimeout(3000)
                .readTimeout(6000)
                .contentTypeJson()
                .addBody(obj)
                .execute(JSONObject.class);
    }

    public JSONObject etlClient(JSONObject obj) {
        if (StrUtil.isBlank(apiUrl.getDrgWebApi())) {
            return new JSONObject();
        }
        return Forest.post(apiUrl.getDrgWebApi() + "/etlClient/callHisData.action")
                .connectTimeout(3000)
                .readTimeout(6000)
                .contentTypeJson()
                .addBody(obj)
                .execute(JSONObject.class);
    }

    public JSONObject getDrgCaseQualityControlGroup(JSONObject obj) {
        if (StrUtil.isBlank(apiUrl.getDrgWebApi())) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", 404);
            return jsonObject;
        }
        return Forest.post(apiUrl.getDrgWebApi() + "/drg_web/drgGroupThird/getAllDrgResults.action")
                .connectTimeout(3000)
                .readTimeout(6000)
                .contentTypeJson()
                .addBody(obj)
                .execute(JSONObject.class);
    }

    public ResultVo<JSONArray> powersiPreDischarge(String visitId) {
        if (StrUtil.isBlank(apiUrl.getDrgWebApi())) {
            return ResultVoUtil.success();
        }
        String url = apiUrl.getDrgWebApi() + "/drg_web/api/json/call.action";
        JSONObject params = new JSONObject();
        params.put("function_id", "apiHnsService1001");
        params.put("hospital_id", config.getInstitutionId());
        params.put("scene_type", "6");
        params.put("visit_id", visitId);
        JSONObject response;
        try {
            response = Forest.post(url)
                    .contentTypeJson()
                    .addBody(params)
                    .execute(JSONObject.class);
        } catch (Exception e) {
            return ResultVoUtil.success();
        }
        log.info("创智出院预审：【{}】，【{}】", visitId, response);
        if (null == response) {
            return ResultVoUtil.success();
        }
        Integer retcode = response.getInteger("return_code");
        if (null != retcode && retcode == 1) {
            JSONArray retdata = response.getJSONArray("return_data");
            if (null == retdata || retdata.isEmpty()) {
                return ResultVoUtil.success();
            }
            return ResultVoUtil.fail(ExceptionEnum.PRE_DISCHARGE_ERROR, retdata);
        }
        return ResultVoUtil.success();
    }

    public ResultVo<String> frontsheetQualityCheck(JSONObject jsonParams) {
        if (StrUtil.isBlank(apiUrl.getDrgWebApi())) {
            return ResultVoUtil.success();
        }
        String url = apiUrl.getDrgWebApi() + "/drg_web/drgGroupThird/V2/groupAndQuality.action";
        String result = Forest.post(url)
                .contentTypeJson()
                .addBody(jsonParams)
                .execute(String.class);

        result = apiUrl.getDrgWebApi() + result;
        String url2 = apiUrl.getDrgWebApi() + "/drg_web/drgGroupThird/V2/drgGroupAndQuality.action";

        String result2 = Forest.post(url2)
                .contentTypeJson()
                .addBody(jsonParams)
                .execute(String.class);

        log.info("病案质控：\n参数：{}\n结果：{}", jsonParams, result2);
        return ResultVoUtil.success(result);
    }

    public ResultVo<String> getDrgIntelligentGrouping(JSONObject data) {
        if (StrUtil.isBlank(apiUrl.getDrgWebApi())) {
            return ResultVoUtil.success();
        }
        String url = apiUrl.getDrgWebApi() + "/drg_web/localHelp/drg_dagns/list.action";
        String res = Forest.post(url)
                .contentTypeJson()
                .addBody(data)
                .execute(String.class);
        return ResultVoUtil.success(apiUrl.getDrgWebApi() + res);
    }


}
