package com.ruoyi.common.pay.xy.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @author wjh
 * 2025/1/8
 */
@Slf4j
public class HttpUtil {

    public static String postData(String url, Map<String, Object> params) throws UnsupportedEncodingException {
        String json = JSONObject.toJSONString(params);
        return postData(url, json, StandardCharsets.UTF_8);
    }

    public static String postData(String url, String json, Charset responseCharset) throws UnsupportedEncodingException {
        log.info("请求地址：" + url);
        HttpPost httpPost = new HttpPost(url);
        setTimeOut(httpPost);
        log.info("连接成功");
        StringEntity requestEntity = new StringEntity(json, "UTF-8");
        requestEntity.setContentEncoding("UTF-8");
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setEntity(requestEntity);
        log.info("请求报文:" + json);
        return execute(httpPost, responseCharset);
    }

    private static void setTimeOut(HttpRequestBase httpRequest) {
        //设置超时时间 请求超时时间 30s
        RequestConfig config = RequestConfig.custom()
                // 设置连接超时时间(单位毫秒)
                .setConnectTimeout(30000)
                // 设置请求超时时间(单位毫秒)
                .setConnectionRequestTimeout(30000)
                // socket读写超时时间(单位毫秒)
                .setSocketTimeout(30000).build();
        httpRequest.setConfig(config);
    }

    private static String execute(HttpRequestBase httpRequest, Charset responseCharset) {
        log.info("开始请求————————");
        log.info("请求中------------------");
        long start = System.currentTimeMillis();
        String result = "";
        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build();
             CloseableHttpResponse response = httpClient.execute(httpRequest)) {
            long end = System.currentTimeMillis();
            log.info("请求结束————————");
            log.info("响应时间：" + (end - start));
            if (response != null) {
                // 从响应模型中获取响应实体
                HttpEntity responseEntity = response.getEntity();
                log.info("响应状态为:" + response.getStatusLine());
                if (responseEntity != null) {
                    log.info("响应内容长度为:" + responseEntity.getContentLength());
                    result = EntityUtils.toString(responseEntity, responseCharset);
                    log.info("响应内容为:" + result);
                }
            }
        } catch (SocketTimeoutException s) {
            log.error("请求超时", s);
        } catch (Exception e) {
            log.error("请求处理异常", e);
        }
        return result;
    }
}
