package com.zjrs.ggfw.sggfw.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yinhai.bcpcs.auth.SignUtils;
import com.yinhai.bcpcs.exceptions.ClientException;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.*;

import static org.apache.http.impl.client.HttpClients.createDefault;

/**
 * 广东省公共服务接口
 * @author liangxiudong
 * @since 2021-03-01
 */
public class SggfwInterface {

    private static final Logger LOG = LoggerFactory.getLogger(SggfwInterface.class);
    //请求接口
    public static Map  qqjk(Map<String, Object> p,SggfwConfig config,String jkmc) throws ClientException, IOException {
        Date date=new Date();
        String category = "1";
        JSONObject param = new JSONObject();
        if(jkmc.equals("ExportPdfQuery")||jkmc.equals("ylz_authenticateTerminal")||jkmc.equals("NB_JMYL_GGYW_001")){
            category="0";
            param.put("input", new Object[]{p});
        }else{
            param.put("input", p);
        }
        // 创建一个post对象
        HttpPost post =new HttpPost(config.getUrl()+"/proxyService" + "/" + config.getKey() + "/" + jkmc);
        List<NameValuePair> kvList = new ArrayList();
        kvList.add(new BasicNameValuePair("category",category));
        kvList.add(new BasicNameValuePair("param", param.toJSONString()));
        kvList.add(new BasicNameValuePair("signature", SignUtils.sign(param.toJSONString(), config.getKeystore())));
        // 包装成一个Entity对象
        StringEntity entity = new UrlEncodedFormEntity(kvList,"utf-8");
        // 设置请求的内容
        post.setEntity(entity);
        // 执行post请求
        LOG.info(jkmc+"接口请求开始时间："+date);
        LOG.info(jkmc+"接口请求开始 url："+config.getUrl()+"/proxyService" + "/" + config.getKey() + "/" + jkmc);
        LOG.info(jkmc+"接口请求param："+param.toJSONString());

        CloseableHttpClient httpClient = createDefault();
        RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(30000)
                .setSocketTimeout(30000).setConnectTimeout(30000).build();
        post.setConfig(requestConfig);
        CloseableHttpResponse response = httpClient.execute(post);
        String string = EntityUtils.toString(response.getEntity());
        LOG.info(jkmc+"接口请求response："+string);
        response.close();
        httpClient.close();

        HashMap hashMap = JSON.parseObject(string, HashMap.class);
        hashMap = JSON.parseObject(hashMap.get("invokeData").toString(), HashMap.class);
        Date date2=new Date();
        LOG.info(jkmc+"接口请求时间："+(date2.getTime()-date.getTime())/1000+" 秒");
        return hashMap;
    }
}
