package com.heyu.api.utils;

import com.alibaba.fastjson.JSONObject;
import com.heyu.api.data.utils.HttpUtils;
import com.heyu.api.tencent.resp.ThirdIdCardResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ThirdUtils {


    public static ThirdIdCardResp getIdCard(String idcardNumber, String realName) {
        BufferedReader in = null;
        try {
            String host = "https://kzidcardv1.market.alicloudapi.com";
            String path = "/api/id_card/check";
            String method = "GET";
            String appcode = "72579fb099fd490d8b7cf6f7eec637b8";
            Map<String, String> headers = new HashMap<String, String>();
            //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
            headers.put("Authorization", "APPCODE " + appcode);
            Map<String, String> querys = new HashMap<String, String>();
            querys.put("name", realName);
            querys.put("idcard", idcardNumber);


            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);

            //获取response的body
            String result = EntityUtils.toString(response.getEntity());


            return JSONObject.parseObject(result, ThirdIdCardResp.class);

        } catch (Exception e) {

            log.error("ThirdUtils.getIdCard error idcardNumber: " + idcardNumber + ", realName:" + realName, e);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                log.error("ThirdUtils.getIdCard error2 idcardNumber: " + idcardNumber + ", realName:" + realName, e2);
            }
        }
        return null;

    }
}
