sgj
2026-03-23 fff68633c2fb6aad6b6b42077bb8d5833b92d4d2
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.fzzy.igds.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzzy.igds.constant.RedisConst;
import com.fzzy.igds.domain.WeatherCity;
import com.fzzy.igds.domain.WeatherConf;
import com.fzzy.igds.domain.WeatherInfo;
import com.fzzy.igds.mapper.WeatherCityMapper;
import com.fzzy.igds.mapper.WeatherConfMapper;
import com.fzzy.igds.mapper.WeatherInfoMapper;
import com.fzzy.igds.utils.ContextUtil;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
 
/**
 * @Description
 * @Author CZT
 * @Date 2026/01/09 11:55
 */
@Slf4j
@Service
public class WeatherService {
 
    @Resource
    private WeatherInfoMapper weatherInfoMapper;
    @Resource
    private WeatherConfMapper weatherConfMapper;
    @Resource
    private WeatherCityMapper weatherCityMapper;
    @Resource
    private RedisCache redisCache;
 
    /**
     * 查询配置信息
     *
     * @param companyId
     * @param deptId
     * @return
     */
    public List<WeatherConf> getConfData(String companyId, String deptId) {
        if (StringUtils.isEmpty(companyId)) {
            companyId = ContextUtil.getCompanyId();
        }
        if (StringUtils.isEmpty(deptId)) {
            deptId = ContextUtil.subDeptId(null);
        }
 
        QueryWrapper<WeatherConf> queryWrapper = new QueryWrapper<>();
 
        queryWrapper.eq("company_id", companyId);
 
        queryWrapper.eq("dept_id", deptId);
 
        return weatherConfMapper.selectList(queryWrapper);
    }
 
    /**
     * 查询配置信息
     *
     * @return
     */
    public List<WeatherConf> getConfData() {
        QueryWrapper<WeatherConf> queryWrapper = new QueryWrapper<>();
        return weatherConfMapper.selectList(queryWrapper);
    }
 
    /**
     * 保存配置信息
     *
     * @param data
     * @return
     */
    public String saveConf(WeatherConf data) {
        if (StringUtils.isEmpty(data.getId())) {
            data.setId(ContextUtil.generateId());
            data.setCompanyId(ContextUtil.getCompanyId());
            data.setDeptId(ContextUtil.subDeptId(null));
        }
        if(StringUtils.isBlank(data.getCreateBy())){
            data.setCreateBy(ContextUtil.getLoginUserName());
            data.setCreateTime(new Date());
        }
        if(StringUtils.isBlank(data.getUpdateBy())){
            data.setUpdateBy(ContextUtil.getLoginUserName());
            data.setUpdateTime(new Date());
        }
        weatherConfMapper.insert(data);
        return null;
    }
 
    /**
     * 删除配置信息
     *
     * @param data
     * @return
     */
    public String delConf(WeatherConf data) {
        weatherConfMapper.deleteById(data);
        return null;
    }
 
    /**
     * 保存气息信息
     *
     * @param data
     */
    private void addWeatherInfo(WeatherInfo data) {
 
        if(StringUtils.isBlank(data.getId())){
            data.setId(ContextUtil.UUID());
        }
        if(StringUtils.isBlank(data.getCreateBy())){
            data.setCreateBy(ContextUtil.getLoginUserName());
            data.setCreateTime(new Date());
        }
        if(StringUtils.isBlank(data.getUpdateBy())){
            data.setUpdateBy(ContextUtil.getLoginUserName());
            data.setUpdateTime(new Date());
        }
        weatherInfoMapper.insert(data);
    }
 
    /**
     * 分页查询数据
     * @param page
     * @param param
     */
    public void getInfoData(Page<WeatherInfo> page, Map<String, Object> param) {
 
        QueryWrapper<WeatherInfo> queryWrapper = new QueryWrapper<>();
 
        queryWrapper.eq("company_id", ContextUtil.getCompanyId());
        queryWrapper.eq("dept_id", ContextUtil.subDeptId(null));
 
        weatherInfoMapper.selectPage(page, queryWrapper);
    }
 
    /**
     * 分页查询数据
     * @param page
     * @param param
     */
    public void pageCity(Page<WeatherCity> page, Map<String, Object> param) {
 
        QueryWrapper<WeatherCity> queryWrapper = new QueryWrapper<>();
 
        weatherCityMapper.selectPage(page, queryWrapper);
    }
 
    /**
     * 判断气象信息是否需要保存
     *
     * @param weather
     */
    public void updateCacheAndSave(WeatherInfo weather) {
        if (null == weather.getCompanyId()) {
            weather.setCompanyId(ContextUtil.getCompanyId());
        }
        try {
            // 更新缓存
            updateCacheWeather(weather);
            // 判断是不是需要保存
            if (this.isSave(weather.getUpdateTime())) {
                addWeatherInfo(weather);
            }
 
        } catch (Exception e) {
            String msg = e.getMessage();
            if (msg.indexOf("PRIMARY") > 0) {
                log.error("持久化气象信息异常,主键冲突,请注意更新时间,出现异常是否合理");
            } else {
                log.error("更新保存气象信息异常:", e);
            }
        }
 
    }
 
 
    /**
     * 根据组织编码设置缓存
     *
     * @param weather
     */
    public void updateCacheWeather(WeatherInfo weather) {
        if (null == weather.getCompanyId()) {
            weather.setCompanyId(ContextUtil.getCompanyId());
        }
        String key = RedisConst.buildKey(weather.getCompanyId(), RedisConst.KEY_WEATHER_INFO);
        log.debug("气象信息更新到缓存中,info={}", weather.toString());
        redisCache.setCacheObject(key, weather);
    }
 
    /**
     * 根据cityId设置缓存
     *
     * @param weather
     * @param cityId
     */
    public void setCacheWeatherByCityId(WeatherInfo weather, String cityId) {
        String key = RedisConst.buildKey(RedisConst.KEY_WEATHER_INFO, cityId);
 
        log.debug("气象信息更新到缓存中,info={}", weather.toString());
        redisCache.setCacheObject(key, weather, 60 * 30, TimeUnit.MICROSECONDS);
    }
 
    /**
     * 根据cityId获取缓存信息
     *
     * @param cityId
     * @return
     */
    public WeatherInfo getCacheWeatherByCityId(String cityId) {
        String key = RedisConst.buildKey(RedisConst.KEY_WEATHER_INFO, cityId);
        WeatherInfo info = (WeatherInfo) redisCache.getCacheObject(key);
        if (null == info) {
            log.error("缓存中没有获取到气象信息!!");
        }
        return info;
    }
 
    /**
     * 根据companyId获取缓存信息
     *
     * @param companyId
     * @return
     */
    public WeatherInfo getCacheWeather(String companyId) {
        String key = RedisConst
                .buildKey(companyId, RedisConst.KEY_WEATHER_INFO);
        WeatherInfo info = (WeatherInfo) redisCache.getCacheObject(key);
        if (null == info) {
            log.error("缓存中没有获取到气象信息!!");
        }
        return info;
    }
 
    /**
     * 创建连接
     *
     * @param address
     * @param port
     */
    public void onCreate(String address, Integer port) {
        log.debug("气象站自动连接通知暂不处理……");
    }
 
    /**
     * 销毁链接
     *
     * @param address
     * @param port
     */
    public void onDestroy(String address, Integer port) {
        log.debug("气象站自动断开通知暂不处理……");
    }
 
    /**
     * 判断当前气象信息是否需要持久化,默认系统只保存在上午8点 和下午3点的气象信。
     *
     * @param updateTime
     * @return
     */
    public static boolean isSave(Date updateTime) {
 
        String tag = DateFormatUtils.format(updateTime, "HHmm");
        String tag1 = "0800";
        String tag2 = "1500";
 
        if (tag.equals(tag1) || tag.equals(tag2)) {
            return true;
        }
 
        return false;
    }
}