package shop.alien.util.httpsend.spring;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;
import java.util.Set;

/**
 * post请求
 *
 * @author ssk
 * @version 1.0
 * @date 2022/7/11 14:01
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class PostMethod {

    private final RestTemplate restTemplate;

    /**
     * 发送post请求
     *
     * @param url    请求地址
     * @param params 请求参数
     * @return String
     */
    public String post(String url, String params) {
        log.info("restTemplate:" + restTemplate + "url:" + url + "\t content:" + params);
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        HttpEntity<String> request = new HttpEntity<>(params, headers);
        ResponseEntity<String> postForEntity = null;
        try {
            postForEntity = restTemplate.postForEntity(url, request, String.class);
        } catch (Exception e) {
            log.error("类:PostMethod 方法:post", e);
        }
        if (postForEntity != null) {
            if (postForEntity.getStatusCode() == HttpStatus.OK) {
                log.info("postForEntity.getBody()\t" + postForEntity.getBody());
                return postForEntity.getBody();
            } else {
                log.error("http状态：" + postForEntity.getStatusCode());
                return null;
            }
        } else {
            return null;
        }

    }

    /**
     * post请求
     *
     * @param url       请求地址
     * @param params    请求参数
     * @param headerMap 请求头
     * @return String
     */
    public String post(String url, String params, Map<String, String> headerMap) {
        log.info("restTemplate:" + restTemplate + "url:" + url + "\t content:" + params);
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        //设置Header
        if (headerMap.size() > 0) {
            Set<String> set = headerMap.keySet();
            for (String next : set) {
                System.out.println("key为：" + next + ",value为：" + headerMap.get(next));
                headers.set(next, headerMap.get(next));
            }
        }
        HttpEntity<String> request = new HttpEntity<>(params, headers);
        ResponseEntity<String> postForEntity = null;
        try {
            postForEntity = restTemplate.postForEntity(url, request, String.class);
        } catch (Exception e) {
            log.error("类:PostMethod 方法:post", e);
        }
        if (postForEntity != null) {
            if (postForEntity.getStatusCode() == HttpStatus.OK) {
                log.info("postForEntity.getBody()\t" + postForEntity.getBody());
                return postForEntity.getBody();
            } else {
                log.error("http状态：" + postForEntity.getStatusCode());
                return null;
            }
        } else {
            return null;
        }

    }

}
