package com.dxhy.common.util;

import cn.hutool.http.HttpRequest;
import lombok.extern.slf4j.Slf4j;

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

/**
 * @author ZSC-DXHY
 */
@Slf4j
public class HttpUtils {
    
    private static final String LOGGER_MSG = "(请求http访问)";
    
    /**
     * 执行post请求
     *
     * @param url
     * @param paramMap
     * @return
     * @throws IOException
     */
    public static String doPost(String url, Map<String, ?> paramMap) {
        Map<String, Object> requestMap = new HashMap<>(paramMap);
        long startTime = System.currentTimeMillis();
        String body = HttpRequest.post(url).form(requestMap).timeout(300000).execute().body();
        long endTime = System.currentTimeMillis();
        log.debug("{}以Map调用post请求url:{},耗时:{}", LOGGER_MSG, url, endTime - startTime);
        return body;
    }
    
    public static String doPost(String url, String request) {
        long startTime = System.currentTimeMillis();
        String body = HttpRequest.post(url).body(request).timeout(300000).execute().body();
        long endTime = System.currentTimeMillis();
        log.debug("{}以字符串调用post请求url:{},耗时:{}", LOGGER_MSG, url, endTime - startTime);
        return body;
    }
    
    public static String doPostWithHeader(String url, String data, Map<String, String> header) {
        long startTime = System.currentTimeMillis();
        String body = HttpRequest.post(url).addHeaders(header).body(data).timeout(300000).execute().body();
        long endTime = System.currentTimeMillis();
        log.debug("{}带head调用post请求url:{},耗时:{}", LOGGER_MSG, url, endTime - startTime);
//        log.debug("{}接口返回:{}", LOGGER_MSG, body);
        return body;
    }
    
    public static String doPostFormWithHeader(String url, Map<String, ?> paramMap, Map<String, String> header) {
        Map<String, Object> requestMap = new HashMap<>(paramMap);
        long startTime = System.currentTimeMillis();
        String body = HttpRequest.post(url).addHeaders(header).form(requestMap).timeout(300000).execute().body();
        long endTime = System.currentTimeMillis();
        log.debug("{}带head和form调用post请求url:{},耗时:{}", LOGGER_MSG, url, endTime - startTime);
        return body;
    }
    
    public static String doGetWithHeader(String url, Map<String, String> header) {
        long startTime = System.currentTimeMillis();
        String body = HttpRequest.get(url).addHeaders(header).timeout(300000).execute().body();
        long endTime = System.currentTimeMillis();
        log.debug("{}带head调用get请求url:{},耗时:{}", LOGGER_MSG, url, endTime - startTime);
        return body;
    }

    public static String doGetWithHeaderParams(String url, Map<String, String> header, Map<String, Object> params) {
        long startTime = System.currentTimeMillis();
        String body = HttpRequest.get(url).addHeaders(header).form(params).timeout(300000).execute().body();
        long endTime = System.currentTimeMillis();
        log.debug("{}带head调用get请求url:{},耗时:{}", LOGGER_MSG, url, endTime - startTime);
        return body;
    }
    
    public static String doGet(String url, String request) {
        long startTime = System.currentTimeMillis();
        String body = HttpRequest.get(url).body(request).timeout(300000).execute().body();
        long endTime = System.currentTimeMillis();
        log.debug("{}以字符串调用get请求url:{},耗时:{}", LOGGER_MSG, url, endTime - startTime);
        return body;
    }


    public static String doGet(String url) {
        long startTime = System.currentTimeMillis();
        String body = HttpRequest.get(url).timeout(300000).execute().body();
        long endTime = System.currentTimeMillis();
        log.debug("{}以字符串调用get请求url:{},耗时:{}", LOGGER_MSG, url, endTime - startTime);
        return body;
    }

    public static String doPost(String url) {
        long startTime = System.currentTimeMillis();
        String body = HttpRequest.post(url).timeout(300000).execute().body();
        long endTime = System.currentTimeMillis();
        log.debug("{}以字符串调用post请求url:{},耗时:{}", LOGGER_MSG, url, endTime - startTime);
        return body;
    }
    
}
