package com.xinelu.common.utils.http;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @Author mengkuiliang
 * @Description Http请求Service
 * @Date 2023-09-22 022 11:40
 * @Param
 * @return
 **/
@Service
@Slf4j
public class HttpService {

    @Resource
    private RestTemplate restTemplate;

    /**
     * @return java.lang.Object
     * @Author mengkuiliang
     * @Description get请求
     * @Date 2022-10-09 16:48
     * @Param [url, headerMap, resp]
     **/
    public Object get(String url, Map<String, Object> headerMap, Class<?> resp) {
//        log.info("get请求：{}", url);
        HttpHeaders httpHeaders = new HttpHeaders();
        if (headerMap != null) {
            for (Map.Entry<String, Object> stringStringEntry : headerMap.entrySet()) {
                httpHeaders.add(stringStringEntry.getKey(), String.valueOf(stringStringEntry.getValue()));
            }
        }
        HttpEntity httpEntity = new HttpEntity(httpHeaders);
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        ResponseEntity<?> result = restTemplate.exchange(url, HttpMethod.GET, httpEntity, resp);
        return result.getBody();
    }

    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author mengkuiliang
     * @Description post请求
     * @Date 2022-10-09 16:50
     * @Param [url, headerMap, jsonObject]
     **/
    public JSONObject post(String url, Map<String, Object> headerMap, JSONObject jsonObject) {
//        if (headerMap != null) {
//            log.info("post请求：{} 请求头：{} 参数：{}", url, new JSONObject(headerMap).toJSONString(), jsonObject.toJSONString());
//        } else {
//            log.info("post请求：{} 参数：{}", url, jsonObject.toJSONString());
//        }
        HttpHeaders httpHeaders = new HttpHeaders();
        if (headerMap != null) {
            for (Map.Entry<String, Object> stringStringEntry : headerMap.entrySet()) {
                httpHeaders.add(stringStringEntry.getKey(), String.valueOf(stringStringEntry.getValue()));
            }
        }
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpEntity httpEntity = new HttpEntity(jsonObject, httpHeaders);
        return restTemplate.postForObject(url, httpEntity, JSONObject.class);
    }

    /**
     * @return com.alibaba.fastjson.JSONObject
     * @Author mengkuiliang
     * @Description post请求
     * @Date 2022-10-09 16:50
     * @Param [url, headerMap, jsonObject]
     **/
    public JSONObject post(String url, Map<String, Object> headerMap, String paramStr) {
//        if (headerMap != null) {
//            log.info("post请求：{} 请求头：{} 参数：{}", url, new JSONObject(headerMap).toJSONString(), paramStr);
//        } else {
//            log.info("post请求：{} 参数：{}", url, paramStr);
//        }
        HttpHeaders httpHeaders = new HttpHeaders();
        if (headerMap != null) {
            for (Map.Entry<String, Object> stringStringEntry : headerMap.entrySet()) {
                httpHeaders.add(stringStringEntry.getKey(), String.valueOf(stringStringEntry.getValue()));
            }
        }
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        HttpEntity httpEntity = new HttpEntity(JSONObject.parseObject(paramStr), httpHeaders);
        return restTemplate.postForObject(url, httpEntity, JSONObject.class);
    }

}
