package com.docus.server.collection.rpc.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.docus.server.collection.rpc.MzZyyRocCurrWebCommonRpc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 梅州中医院 roc curr-web 公共 api 接口实现
 *
 * @author YongBin Wen
 * @date 2025/2/8 10:04
 */
@Service
@Slf4j
public class MzZyyRocCurrWebCommonRpcImpl implements MzZyyRocCurrWebCommonRpc {
    @Value("${mzzyy.url.roc.curr-web: http://199.168.91.110:7800/roc/curr-web}")
    private String rocCurrWebUrl;
    @Value("${mzzyy.roc-domain: WZHBA}")
    private String rocDomain;
    @Value("${mzzyy.roc-key: b91b0ac7-665f-4874-a282-2f5511a44263}")
    private String rocKey;

    @Override
    public JSONObject queryDept(String deptCode, String deptType) {
        final String path = "/api/v1/common/dept/query";
        final String url = rocCurrWebUrl + path;
        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("deptCode", deptCode);
        paramMap.put("deptType", deptType);
        try {
            String result = HttpUtil.createGet(url)
                    .header("Content-Type", "application/json")
                    .header("domain", rocDomain)
                    .header("key", rocKey)
                    .form(paramMap)
                    .timeout(60 * 1000)
                    .execute().body();
            return JSONObject.parseObject(result);
        } catch (Exception ex) {
            log.error(url + " 请求出错了，" + ex.getMessage(), ex);
            return null;
        }
    }

    @Override
    public JSONObject getInPatient(String inpatientNo) {
        final String path = "/api/v1/common/in-patient/get/" + inpatientNo;
        final String url = rocCurrWebUrl + path;
        try {
            String result = HttpUtil.createGet(url)
                    .header("Content-Type", "application/json")
                    .header("domain", rocDomain)
                    .header("key", rocKey)
                    .timeout(60 * 1000)
                    .execute().body();
            return JSONObject.parseObject(result);
        } catch (Exception ex) {
            log.error(url + " 请求出错了，" + ex.getMessage(), ex);
            return null;
        }
    }
}
