package com.prolog.rdc.tms.util;

import java.nio.charset.StandardCharsets;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.prolog.framework.authority.util.HystrixVarUtil;
import com.prolog.framework.core.exception.ParameterException;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 *
 * Http请求
 *
 * @className :HttpUtils
 * @author :fanzw
 * @creationTime :2022/06/01
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/06/01
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Slf4j
@Configuration
public class HttpUtils {

	// token地址
	private static String tokenUrl;

	@Value("${prolog.interface.tokenUrl}")
	private void setTokenUrl(String tokenUrl) {
		HttpUtils.tokenUrl = tokenUrl;
	}

	// 运单地址
	private static String interfaceUser;

	@Value("${prolog.interface.interfaceUser}")
	private void setInterfaceUser(String interfaceUser) {
		HttpUtils.interfaceUser = interfaceUser;
	}

	/**
	 * 接口用户登录
	 */
	public static void HttpRestClient() {
		// 构造请求对象
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
		// 建立连接的超时时间
		requestFactory.setConnectTimeout(200 * 1000);
		// 传递数据的超时时间
		requestFactory.setReadTimeout(200 * 1000);

		// 请求客户端
		RestTemplate client = new RestTemplate(requestFactory);
		// 设置编码格式(默认的是ISO-8859-1)为UTF_8
		client.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));

		// 返回xml转json处理(非必须)
		client.getMessageConverters().clear();
		client.getMessageConverters().add(new FastJsonHttpMessageConverter4());

		// 设置请求类型
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		// 获取TOKEN
		JSONObject token = getToken();
		if (!token.getBoolean("success")) {
			throw new ParameterException("获取TOKEN失败：" + token.getString("message"));
		}

		if (!HystrixRequestContext.isCurrentThreadInitialized()) {
			HystrixRequestContext.initializeContext();
		}

		HystrixVarUtil.setUserKey(interfaceUser);
	}

	/**
	 * 获取token
	 * 
	 * @param tokenUrl
	 * @return
	 */
	public static JSONObject getToken() {
		// 构造请求对象
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
		// 建立连接的超时时间
		requestFactory.setConnectTimeout(100 * 1000);
		// 传递数据的超时时间
		requestFactory.setReadTimeout(100 * 1000);

		// 请求客户端
		RestTemplate client = new RestTemplate(requestFactory);
		// 设置编码格式(默认的是ISO-8859-1)为UTF_8
		client.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));

		// 设置请求类型
		HttpHeaders headers = new HttpHeaders();

		// 设置请求方式 post请求
		HttpMethod method = HttpMethod.POST;

		// 设置请求体
		HttpEntity<String> requestEntity = new HttpEntity<String>(headers);

		// 执行HTTP请求
		ResponseEntity<JSONObject> response = client.exchange(tokenUrl, method, requestEntity, JSONObject.class);

		return response.getBody();
	}

	/**
	 * 公共post json请求
	 *
	 * @param url
	 * @param request
	 * @return
	 */
	public static JSONObject postJsonRequest(String url, String request) {
		// 构造请求对象
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
		// 建立连接的超时时间
		requestFactory.setConnectTimeout(200 * 1000);
		// 传递数据的超时时间
		requestFactory.setReadTimeout(200 * 1000);

		// 请求客户端
		RestTemplate client = new RestTemplate(requestFactory);
		// 设置编码格式(默认的是ISO-8859-1)为UTF_8
		client.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));

		// 设置请求类型
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);

		// 设置请求方式 post请求
		HttpMethod method = HttpMethod.POST;

		// 设置请求体
		HttpEntity<String> requestEntity = new HttpEntity<String>(request, headers);

		// 执行HTTP请求
		ResponseEntity<JSONObject> response = client.exchange(url, method, requestEntity, JSONObject.class);

		return response.getBody();
	}

}