package com.cm.common.utils.http;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @ClassName: RestRequestUtil
 * @Description: RestTemplate请求
 * @Author: WangGeng
 * @Date: 2019-07-02 22:30
 * @Version: 1.0
 **/
public class RestRequestUtil {

    private static final Logger LOG = LoggerFactory.getLogger(RestRequestUtil.class);
    public static String METHOD_GET = "get";
    public static String METHOD_POST = "post";
    public static String SUCCESS = "success";
    public static String CODE = "code";
    public static String STATE = "state";
    public static String DATA = "data";
    public static String RESULT = "result";

    /**
     * GET请求
     *
     * @param uri
     * @param requestParams
     * @return
     */
    public static String getRestRequest(String uri, Map<String, Object> requestParams) {
        RestTemplate restTemplate = getRestTemplate();
        try {
            return getResponse(restTemplate.getForEntity(uri, String.class, requestParams));
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * POST提交JSON格式数据请求
     *
     * @param uri
     * @param requestParams
     * @param requestBody
     * @return
     */
    public static String postRestRequestJson(String uri, Map<String, Object> requestParams, Map<String, Object> requestBody) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(requestBody, httpHeaders);
        RestTemplate restTemplate = getRestTemplate();
        try {
            return getResponse(restTemplate.postForEntity(uri, httpEntity, String.class, requestParams));
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * POST提交JSON格式请求
     *
     * @param uri
     * @param requestParams
     * @param requestBody
     * @return
     */
    public static String postRestRequestForm(String uri, Map<String, Object> requestParams, MultiValueMap<String, Object> requestBody) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(requestBody, httpHeaders);
        RestTemplate restTemplate = getRestTemplate();
        try {
            return getResponse(restTemplate.postForEntity(uri, httpEntity, String.class, requestParams));
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            return null;
        }
    }

    /**
     * 获取restTemplate
     *
     * @return
     */
    private static RestTemplate getRestTemplate() {
        SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory();
        simpleClientHttpRequestFactory.setConnectTimeout(20 * 1000);
        simpleClientHttpRequestFactory.setReadTimeout(60 * 1000);
        return new RestTemplate(simpleClientHttpRequestFactory);
    }

    /**
     * @param responseEntity
     * @return
     */
    private static String getResponse(ResponseEntity<String> responseEntity) {
        LOG.debug(">>>> 请求状态：" + responseEntity.getStatusCodeValue());
        if (HttpStatus.OK.value() == responseEntity.getStatusCodeValue()) {
            return responseEntity.getBody();
        }
        return null;
    }
}
