package com.hzya.frame.plugin.lets.util;

import cn.hutool.core.lang.Assert;

import com.hzya.frame.plugin.lets.constant.OverallConstant;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

/**
 * U8C推送工具类
 */
@Component
public class PushDataByU8cUtil {
    /**
     * 发起post请求，传递json类型
     *
     * @param url  地址
     * @param data 请求参数
     * @return 返回消息
     */
    public  String pushU8CByPK(String url, String data) throws Exception {
        Assert.notNull(url, "请求地址不能为空");
        Assert.notNull(data, "请求参数不能为空(需要JSON格式)");

        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

        try {
            // 设置连接超时和读取超时
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(300000) // 连接超时时间为60秒
                    .setSocketTimeout(300000) // 读取超时时间为60秒
                    .build();

            HttpPost post = new HttpPost(OverallConstant.getOverAllValue("u8cApiUrl") + url);
            post.setHeader("Content-Type", "application/json;charset=UTF-8");
            post.setHeader("usercode", OverallConstant.getOverAllValue("u8cApiUsercodePK"));
            post.setHeader("password", OverallConstant.getOverAllValue("u8cApiPasswordPK"));
            post.setHeader("trantype", OverallConstant.getOverAllValue("u8cApiTrantypePK"));
            post.setHeader("system", OverallConstant.getOverAllValue("u8cApiSystemPK"));
            post.setHeader("needStackTrace", OverallConstant.getOverAllValue("u8cApiNeedStackTracePK"));
            post.setConfig(requestConfig);
            //post.setHeader("charset", "utf-8");

            ByteArrayEntity entity = new ByteArrayEntity(data.getBytes("UTF-8"));
            entity.setContentType("application/json");
            post.setEntity(entity);

            HttpResponse response = closeableHttpClient.execute(post);
            // 处理返回结果乱码问题
            if (response != null && response.getEntity() != null) {
                String resultStr = EntityUtils.toString(response.getEntity(), "UTF-8");
                return resultStr;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Assert.state(false, "拉取/推送失败(pushU8CByPK)，失败原因:{} 请求地址：{}", e.getMessage(), url);
        } finally {
            try {
                if (closeableHttpClient != null) {
                    closeableHttpClient.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 发起post请求，传递json类型
     *
     * u8c code模式。
     *
     * @param url  地址
     * @param data 请求参数
     * @return 返回消息
     */
    public String pushU8CByCode(String url, String data) throws Exception {
        Assert.notNull(url, "请求地址不能为空");
        Assert.notNull(data, "请求参数不能为空(需要JSON格式)");

        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

        try {
            // 设置连接超时和读取超时
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(300000) // 连接超时时间为60秒
                    .setSocketTimeout(300000) // 读取超时时间为60秒
                    .build();

            HttpPost post = new HttpPost(OverallConstant.getOverAllValue("u8cApiUrl") + url);
            post.setHeader("Content-Type", "application/json;charset=UTF-8");
            post.setHeader("usercode", OverallConstant.getOverAllValue("u8cApiUsercodeCode"));
            post.setHeader("password", OverallConstant.getOverAllValue("u8cApiPasswordCode"));
            post.setHeader("trantype", OverallConstant.getOverAllValue("u8cApiTrantypeCode"));
            post.setHeader("system", OverallConstant.getOverAllValue("u8cApiSystemCode"));
            post.setHeader("needStackTrace", OverallConstant.getOverAllValue("u8cApiNeedStackTraceCode"));
            post.setConfig(requestConfig);
            //post.setHeader("charset", "utf-8");

            ByteArrayEntity entity = new ByteArrayEntity(data.getBytes("UTF-8"));
            entity.setContentType("application/json");
            post.setEntity(entity);

            HttpResponse response = closeableHttpClient.execute(post);
            // 处理返回结果乱码问题
            if (response != null && response.getEntity() != null) {
                String resultStr = EntityUtils.toString(response.getEntity(), "UTF-8");
                return resultStr;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Assert.state(false, "拉取/推送失败(pushU8CByCode)，失败原因:{} 请求地址：{}", e.getMessage(), url);
        } finally {
            try {
                if (closeableHttpClient != null) {
                    closeableHttpClient.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
