package com.dsk.common.utils.http;


import com.dsk.acc.openapi.api.AccClient;
import com.dsk.acc.openapi.client.Config;
import com.dsk.acc.openapi.client.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

/**
 * @author lc
 * @date 2022/2/18 11:41
 */
@Slf4j
@Component
public class DskAccClientUtil {

    private static String accessKeyId;
    private static String accessSecret;


    /**
     * <b>application/json请求</b>
     * @param path 请求路径
     * @param bodyMap 参数体
     * @return
     * */
    public static Map<String,Object> requestBody(String path, Map<String, Object> bodyMap){
        return request(path, bodyMap, "json");
    }

    /**
     * <b>application/x-www-form-urlencoded请求</b>
     * @param path 请求路径
     * @param bodyMap 参数体
     * @return
     */
    public static Map<String,Object> requestForm(String path, Map<String, Object> bodyMap){
        return request(path, bodyMap, "form");
    }

    /**
     * <b>开放平台统一请求</b>
     * @param path 请求路径
     * @param bodyMap 参数体
     * @param reqBodyType 请求格式
     * @return
     */
    public static Map<String,Object> request(String path, Map<String, Object> bodyMap, String reqBodyType){
//        try {
            //测试沙盒版本
//            AccClient.init(new Config(accessKeyId, accessSecret).setEndpoint("sandbox.openapi.jiansheku.com"));
            //后期线上版本
            AccClient.init(new Config(accessKeyId, accessSecret));
            Map<String, ?> res = AccClient.request(reqBodyType, path, bodyMap);
            if(!res.containsKey("headers") || !res.containsKey("body")) {
                throw new RuntimeException(String.format("请求无返回:path=%s",path));
            }
            Object resBody = res.get("body");
            if(resBody == null) {
                return null;
            }
            return CommonUtils.assertAsMap(resBody);
//        } catch (Exception e) {
//            log.error("大司空开放平台请求异常 request() ",e);
//            throw new RuntimeException(String.format("请求异常:path=%s,err=%s",path,e.getMessage()));
//        }
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    @Value("${dsk-acc.open.accessKeyId}")
    public void setAccessKeyId(String accessKeyId) {
        DskAccClientUtil.accessKeyId = accessKeyId;
    }

    public String getAccessSecret() {
        return accessSecret;
    }

    @Value("${dsk-acc.open.accessSecret}")
    public void setAccessSecret(String accessSecret) {
        DskAccClientUtil.accessSecret = accessSecret;
    }


    public static void main(String[] args) {

        DskAccClientUtil.accessKeyId="kUN0ZtZg2H4b1e6z6smXlzo7Enta0bKt";
        DskAccClientUtil.accessSecret="LXmIIEZ9vVkRAqAKFDlYCavtkfveIDPi";
//        Map<String, Object> map =new HashMap<>();
//        map.put("companyId",339447);

        Map<String, Object> jsonParam = new HashMap<String, Object>();
        jsonParam.put("companyName", "重庆大司空信息科技有限公司");
        jsonParam.put("creditCode", "91350203MA2YG0RP5A");
       // Map<String,Object> res = DskAccClientUtil.requestBody(DskAccClientUtil.HTTPURLMAP.get("涉金融领域非法集资企业"), jsonParam);
//        Map<String, Object> result = requestForm("/zh/jsk/enterprise/ownership", map);
   //     System.out.println(JSONUtil.toJsonStr(res));
//        List<CompanyInfo> list = multiCriteriaQuery("浙江恒欣设计集团股份有限公司");
//        System.out.println(list);



    }
}

