工业互联网平台2.0版本后端代码
houzhongjian
2025-05-29 41499fd3c28216c1526a72b10fa98eb8ffee78cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.iailab.module.ai.service.model.tool;
 
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;
 
import java.time.LocalDateTime;
import java.util.function.Function;
 
import static cn.hutool.core.date.DatePattern.NORM_DATETIME_PATTERN;
 
/**
 * 工具:查询指定城市的天气信息
 *
 * @author Iailab
 */
@Component("weather_query")
public class WeatherQueryToolFunction
        implements Function<WeatherQueryToolFunction.Request, WeatherQueryToolFunction.Response> {
 
    private static final String[] WEATHER_CONDITIONS = { "晴朗", "多云", "阴天", "小雨", "大雨", "雷雨", "小雪", "大雪" };
 
    @Data
    @JsonClassDescription("查询指定城市的天气信息")
    public static class Request {
 
        /**
         * 城市名称
         */
        @JsonProperty(required = true, value = "city")
        @JsonPropertyDescription("城市名称,例如:北京、上海、广州")
        private String city;
 
    }
 
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Response {
 
        /**
         * 城市名称
         */
        private String city;
 
        /**
         * 天气信息
         */
        private WeatherInfo weatherInfo;
 
        @Data
        @AllArgsConstructor
        @NoArgsConstructor
        public static class WeatherInfo {
 
            /**
             * 温度(摄氏度)
             */
            private Integer temperature;
 
            /**
             * 天气状况
             */
            private String condition;
 
            /**
             * 湿度百分比
             */
            private Integer humidity;
 
            /**
             * 风速(km/h)
             */
            private Integer windSpeed;
 
            /**
             * 查询时间
             */
            private String queryTime;
 
        }
 
    }
 
    @Override
    public Response apply(Request request) {
        // 检查城市名称是否为空
        if (StrUtil.isBlank(request.getCity())) {
            return new Response("未知城市", null);
        }
 
        // 获取天气数据
        String city = request.getCity();
        Response.WeatherInfo weatherInfo = generateMockWeatherInfo();
        return new Response(city, weatherInfo);
    }
 
    /**
     * 生成模拟的天气数据
     * 在实际应用中,应替换为真实 API 调用
     */
    private Response.WeatherInfo generateMockWeatherInfo() {
        int temperature = RandomUtil.randomInt(-5, 30);
        int humidity = RandomUtil.randomInt(1, 100);
        int windSpeed = RandomUtil.randomInt(1, 30);
        String condition = RandomUtil.randomEle(WEATHER_CONDITIONS);
        return new Response.WeatherInfo(temperature, condition, humidity, windSpeed,
                LocalDateTimeUtil.format(LocalDateTime.now(), NORM_DATETIME_PATTERN));
    }
 
}