/**
 * Copyright
 * All right reserved.
 * 项目名称:
 * 创建日期: 2022/11/9
 */
package org.springblade.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.log.exception.ServiceException;

import java.util.Map;

/**
 * 创建日期: 2022/11/9
 * Title: 文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author DSH
 * @mender：（文件的修改者，文件创建者之外的人）
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class HttpRequestUtil {

	/**
	 * 功能: GET请求
	 * 作者: DSH
	 * 创建日期: 2022/11/09
	 * @param interfaceName 接口名称
	 * @param url 接口地址
	 * @param headers 请求头
	 * @param paramMap 接口入参
	 * @param nTimeOut 超时时间，单位：毫秒
	 * @return: com.alibaba.fastjson.JSONObject
	 */
	public static JSONObject doHttpGetRequest(String interfaceName, String url, Map<String, String> headers, Map<String, Object> paramMap, int nTimeOut) {
		log.info("【{}】接口地址：{}，参数：{}", interfaceName, url, paramMap);
		HttpResponse response = HttpRequest.get(url)
			.form(paramMap)
			.addHeaders(headers)
			.timeout(nTimeOut)
			.execute();
		if (response.getStatus() == HttpStatus.HTTP_OK) {
			String body = response.body();
			log.info("【{}】接口响应：{}", interfaceName, body);
			if (StringUtils.isNotBlank(body)) {
				JSONObject result = JSONObject.parseObject(body);
				if (result.getIntValue("code") != HttpStatus.HTTP_OK) {
					throw new ServiceException(result.getString("msg"));
				}
				return result;
			}
		} else {
			log.error("【{}】请求异常，Http状态码：{}", interfaceName, response.getStatus());
			throw new ServiceException("请求异常，Http状态码："+ response.getStatus());
		}
		return null;
	}

	/**
	 * 功能: POST请求
	 * 作者: DSH
	 * 创建日期: 2022/11/09
	 * @param interfaceName 接口名称
	 * @param url 接口地址
	 * @param headers 请求头
	 * @param paramMap 接口入参
	 * @param nTimeOut 超时时间，单位：毫秒
	 * @return: com.alibaba.fastjson.JSONObject
	 */
	public static JSONObject doHttpPostRequest(String interfaceName, String url, Map<String, String> headers, Map<String, Object> paramMap, int nTimeOut) {
		log.info("【{}】接口地址：{}，参数：{}", interfaceName, url, paramMap);
		HttpResponse response = HttpRequest.post(url)
			//.contentType("application/json;charset=utf-8")
			.contentType(ContentType.JSON.toString(CharsetUtil.CHARSET_UTF_8))
			.body(paramMap == null ? null : JSONObject.toJSONString(paramMap))
			.addHeaders(headers)
			.timeout(nTimeOut)
			.execute();
		if (response.getStatus() == HttpStatus.HTTP_OK) {
			String body = response.body();
			log.info("【{}】接口响应：{}", interfaceName, body);
			if (StringUtils.isNotBlank(body)) {
				JSONObject result = JSONObject.parseObject(body);
				if (result.getIntValue("code") != HttpStatus.HTTP_OK) {
					throw new ServiceException(result.getString("msg"));
				}
				return result;
			}
		} else {
			log.error("【{}】请求异常，Http状态码：{}", interfaceName, response.getStatus());
			throw new ServiceException("请求异常，Http状态码："+ response.getStatus());
		}
		return null;
	}

	/**
	 *  POST请求 ，按JSON字符串传入BODY
	 * @param interfaceName
	 * @param url
	 * @param headers
	 * @param paramMap
	 * @param nTimeOut
	 * @return
	 */
	public static JSONObject doHttpPostRequest(String interfaceName, String url, Map<String, String> headers, String paramMap, int nTimeOut) {
		log.info("【{}】接口地址：{}，参数：{}", interfaceName, url, paramMap);
		HttpResponse response = HttpRequest.post(url)
//			.contentType("application/json;charset=utf-8")
			.contentType(ContentType.JSON.toString(CharsetUtil.CHARSET_UTF_8))
			.body(paramMap)
			.addHeaders(headers)
			.timeout(nTimeOut)
			.execute();
		if (response.getStatus() == HttpStatus.HTTP_OK) {
			String body = response.body();
			log.info("【{}】接口响应：{}", interfaceName, body);
			if (StringUtils.isNotBlank(body)) {
				JSONObject result = JSONObject.parseObject(body);
//				if (result.getIntValue("code") != HttpStatus.HTTP_OK) {
//					throw new ServiceException(result.getString("msg"));
//				}
				return result;
			}
		} else {
			log.error("【{}】请求异常，Http状态码：{}", interfaceName, response.getStatus());
			throw new ServiceException("请求异常，Http状态码："+ response.getStatus());
		}
		return null;
	}


	/**
	 * 功能: POST请求
	 * 作者: DSH
	 * 创建日期: 2022/11/09
	 * @param interfaceName 接口名称
	 * @param url 接口地址
	 * @param headers 请求头
	 * @param paramMap 接口入参
	 * @param nTimeOut 超时时间，单位：毫秒
	 * @return: com.alibaba.fastjson.JSONObject
	 */
	public static JSONObject doHttpPostSubmitForm(String interfaceName, String url, Map<String, String> headers, Map<String, Object> paramMap, int nTimeOut) {
		log.info("【{}】接口地址：{}，参数：{}", interfaceName, url, paramMap);
		HttpResponse response = HttpRequest.post(url)
			.form(paramMap)
			.addHeaders(headers)
			.timeout(nTimeOut)
			.execute();
		if (response.getStatus() == HttpStatus.HTTP_OK) {
			String body = response.body();
			log.info("【{}】接口响应：{}", interfaceName, body);
			if (StringUtils.isNotBlank(body)) {
				JSONObject result = JSONObject.parseObject(body);
				if (result.getIntValue("code") != HttpStatus.HTTP_OK) {
					throw new ServiceException(result.getString("msg"));
				}
				return result;
			}
		} else {
			log.error("【{}】请求异常，Http状态码：{}", interfaceName, response.getStatus());
			throw new ServiceException("请求异常，Http状态码："+ response.getStatus());
		}
		return null;
	}

}
