package com.jeeplus.sys.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.Map;
import java.util.Objects;

@Component
public class RestTemplateService2 {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 访问get接口 - 人力资源 系统
     * @param path 接口路径
     * @param token token
     * @param paramMap 请求参数-路径后
     * @return
     */
    public Object getHuman(String path, String token, Map<String, Object> paramMap) {
        return httpRequest(HttpMethod.GET, token, paramMap, null, Global.getConfig("HUMAN_PATH"), path);
    }

    /**
     * 访问post接口 - 人力资源 系统
     * @param path 接口路径
     * @param token token
     * @param paramMap 请求参数-路径后
     * @param bodyMap 请求参数-请求体
     * @return
     */
    public Object postHuman(String path, String token, Map<String, Object> paramMap, Map<String, Object> bodyMap) {
        return httpRequest(HttpMethod.POST, token, paramMap, bodyMap, Global.getConfig("HUMAN_PATH"), path);
    }

    /**
     * 访问post接口 - CAS 系统
     * @param path 接口路径
     * @param token token
     * @param paramMap 请求参数-路径后
     * @param bodyMap 请求参数-请求体
     * @return
     */
    public Object postCas(String path, String token, Map<String, Object> paramMap, Map<String, Object> bodyMap) {
        return httpRequest(HttpMethod.POST, token, paramMap, bodyMap, Global.getConfig("CAS_PATH"), path);
    }

    /**
     * 访问get接口 - CAS 系统
     * @param path 接口路径
     * @param token token
     * @param paramMap 请求参数-路径后
     * @return
     */
    public Object getCas(String path, String token, Map<String, Object> paramMap) {
        return httpRequest(HttpMethod.GET, token, paramMap, null, Global.getConfig("CAS_PATH"), path);
    }

    /**
     * 访问远程接口方法
     * @param method 请求类型
     * @param token token
     * @param paramMap 请求参数-路径后
     * @param bodyMap 请求参数-请求体
     * @param hostAddress 服务host地址
     * @param path 接口路径
     * @return
     */
    public Object httpRequest(HttpMethod method, String token, Map<String, Object> paramMap, Map<String, Object> bodyMap, String hostAddress, String path) {
        JSONObject jsonObject = new JSONObject();
        if (CollectionUtil.isNotEmpty(bodyMap)) {
            jsonObject = new JSONObject(bodyMap); // 另一端接口需要使用@RequestBody来接收此参数
        }
        String url = getUrl(hostAddress, path, paramMap);
        HttpHeaders httpHeaders = new HttpHeaders();
//        httpHeaders.add("token",token);
        httpHeaders.add("Accept", MediaType.ALL_VALUE);
        httpHeaders.add("cookie", "jeeplus.session.id=0635611b0f5a4401836262c7d23ae98e");
        httpHeaders.setContentType(new MediaType("application","json", Charset.forName("UTF-8")));
        HttpEntity<Object> entity = new HttpEntity<>(jsonObject,httpHeaders);
        ResponseEntity<Object> responseEntity = restTemplate.exchange(url, method, entity, Object.class);
        return responseEntity.getBody();
    }

    /**
     * 将参数拼接到请求路径中
     * @param hostAddress 服务host地址
     * @param path 接口路径
     * @param map 请求参数
     * @return
     */
    public String getUrl(String hostAddress, String path, Map<String, Object> map) {
        String url = hostAddress + path;
        StringBuilder paramStr = new StringBuilder();
        if (CollectionUtil.isNotEmpty(map)) {
            for (String key : map.keySet()) {
                Object o = map.get(key);
                if (Objects.nonNull(o)) {
//                    String value = URLEncoder.encode(o.toString(), "utf-8");
                    paramStr.append("&").append(key).append("=").append(o);
                }
            }
        }
        if (StringUtils.isNotBlank(paramStr.toString())){
            String param = paramStr.substring(1).toString(); // 去掉第一个&
            url = url + "?" + param;
        }
        return url;
    }
}
