package org.ta.pddserver.utils;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.ta.pddserver.model.pddjy.request.JYBaseRequestModel;
import org.ta.pddserver.model.pddlg.request.LGBaseModel;

import java.util.HashMap;
import java.util.Map;


public class PddHttpTool {
    private static final String url = "https://opengw-api.hutaojie.com/ark/router";
//    private static final String url = "https://ark-api.pinduoduo.com/ark/router";

    public static String postRequest(JYBaseRequestModel request) {

        MultiValueMap<String, String> params = ObjectCommonTool.objectToMultiValueMap(request);
        RestTemplate restTemplate = new RestTemplate();
        // 1. 设置请求头：指定 Content-Type
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        // 2. 封装请求实体（参数 + 头信息）
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);

        // 3. 发送 POST 请求，获取响应
        ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);

        // 4. 返回响应体（根据实际需求处理响应状态码、响应头）
        return response.getBody();
    }

    public static String sendJsonPost(Object requestObj) {
        // 自动序列化 requestObj 为 JSON，并设置 Content-Type: application/json
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> resp = restTemplate.postForEntity(url, requestObj, String.class);
        return resp.getBody();
    }
}
