package com.jeeplus.centerservice.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.jeeplus.sys.utils.Global;
import com.jeeplus.sys.utils.StringUtils;
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 RestTemplateService {

    @Autowired
    private RestTemplate restTemplate;

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

    /**
     * 访问get接口 - CCPM 系统
     * @param path 接口路径
     * @param token token
     * @param paramMap 请求参数-路径后
     * @return
     */
    public Object getCCPM(String path, String token, Map<String, Object> paramMap) {
        return httpRequest(HttpMethod.GET, token, paramMap, null, Global.getConfig("CCPM_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) {
        Object res = null;
        try {
            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=3cb71cc227e348ea883e0f009579088f");
            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);
            res = responseEntity.getBody();
        } catch (Exception e) {
            System.out.println("远程调用失败");
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 将参数拼接到请求路径中
     * @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;
    }
}
