package com.gyee.runeconomy.service.EarlyWarning;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class ApiClient {
    private final RestTemplate restTemplate = new RestTemplate();

    public String getDataFromApi(String begin, String end, String deviceId, String modelId, String deviceType, String alarmIds, String timeType) {
        try {
            // 构建 URL
            String baseUrl = "http://10.220.1.5:6015/alarm/history/findCtFeatureStatByWtId";
            String charset = StandardCharsets.UTF_8.name();  // 获取字符集名称
            String url = String.format("%s?begin=%s&end=%s&deviceid=%s&modelId=%s&deviceType=%s&alarmIds=%s&timeType=%s",
                    baseUrl,
                    URLEncoder.encode(begin, charset),
                    URLEncoder.encode(end, charset),
                    URLEncoder.encode(deviceId, charset),
                    URLEncoder.encode(modelId, charset),
                    URLEncoder.encode(deviceType, charset),
                    URLEncoder.encode(alarmIds, charset),
                    URLEncoder.encode(timeType, charset)
            );

            // 发送 GET 请求
            URI uri = new URI(url);
            ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);

            // 返回响应内容
            return response.getBody();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getgzFromApi(String begin, String end, String alarmType,String stationid,String deviceid, String description,String modelId,
                               String deviceType,String components, String subcomponents,String alarmId, String isclose, String pageNum,String pageSize) {
        try {
            // 构建 URL
            String baseUrl = "http://10.220.1.5:6015/alarm/history/findAlarmlist";
            String charset = StandardCharsets.UTF_8.name();  // 获取字符集名称
            String url = String.format("%s?begin=%s&end=%s&alarmType=%s&stationid=%s&deviceid=%s&description=%s&modelId=%s&deviceType=%s&components=%s&subcomponents=%s&alarmId=%s&isclose=%s&pageNum=%s&pageSize=%s",
                    baseUrl,
                    URLEncoder.encode(begin, charset),
                    URLEncoder.encode(end, charset),
                    URLEncoder.encode(alarmType, charset),
                    URLEncoder.encode(stationid, charset),
                    URLEncoder.encode(deviceid, charset),
                    URLEncoder.encode(description, charset),
                    URLEncoder.encode(modelId, charset),
                    URLEncoder.encode(deviceType, charset),
                    URLEncoder.encode(components, charset),
                    URLEncoder.encode(subcomponents, charset),
                    URLEncoder.encode(alarmId, charset),
                    URLEncoder.encode(isclose, charset),
                    URLEncoder.encode(pageNum, charset),
                    URLEncoder.encode(pageSize, charset)
            );

            // 发送 GET 请求
            URI uri = new URI(url);
            ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);

            // 返回响应内容
            return response.getBody();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
