learshaw 2 mesiacov pred
rodič
commit
39ea5b51c9

+ 70 - 0
ems/ems-cloud/ems-server/src/main/java/com/ruoyi/ems/config/WeatherConfig.java

@@ -0,0 +1,70 @@
+/*
+ * 文 件 名:  WeatherConfig
+ * 版    权:  华设设计集团股份有限公司
+ * 描    述:  <描述>
+ * 修 改 人:  lvwenbin
+ * 修改时间:  2025/5/27
+ * 跟踪单号:  <跟踪单号>
+ * 修改单号:  <修改单号>
+ * 修改内容:  <修改内容>
+ */
+package com.ruoyi.ems.config;
+
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * 天气采集配
+ * <功能详细描述>
+ *
+ * @author lvwenbin
+ * @version [版本号, 2025/5/27]
+ * @see [相关类/方法]
+ * @since [产品/模块版本]
+ */
+@Configuration
+public class WeatherConfig {
+    @Value("${weather.adcode}")
+    private String adcode;
+
+    @Value("${weather.api-key}")
+    private String apiKey;
+
+    @Value("${weather.rt.api-addr}")
+    private String weatherRtAddr;
+
+    @Value("${weather.forecast.api-addr}")
+    private String weatherForecastAddr;
+
+    public String getAdcode() {
+        return adcode;
+    }
+
+    public void setAdcode(String adcode) {
+        this.adcode = adcode;
+    }
+
+    public String getApiKey() {
+        return apiKey;
+    }
+
+    public void setApiKey(String apiKey) {
+        this.apiKey = apiKey;
+    }
+
+    public String getWeatherRtAddr() {
+        return weatherRtAddr;
+    }
+
+    public void setWeatherRtAddr(String weatherRtAddr) {
+        this.weatherRtAddr = weatherRtAddr;
+    }
+
+    public String getWeatherForecastAddr() {
+        return weatherForecastAddr;
+    }
+
+    public void setWeatherForecastAddr(String weatherForecastAddr) {
+        this.weatherForecastAddr = weatherForecastAddr;
+    }
+}

+ 66 - 2
ems/ems-cloud/ems-server/src/main/java/com/ruoyi/ems/task/TaskService.java

@@ -10,7 +10,18 @@
  */
 package com.ruoyi.ems.task;
 
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
+import com.huashe.common.domain.model.WeatherForecast;
+import com.huashe.common.domain.model.WeatherRt;
+import com.huashe.common.exception.Assert;
+import com.huashe.common.utils.HttpUtils;
+import com.ruoyi.ems.config.WeatherConfig;
 import com.ruoyi.ems.service.IWeatherService;
+import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.client.utils.URIBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Value;
@@ -20,6 +31,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 
 import javax.annotation.Resource;
+import java.util.List;
 
 /**
  * 调度任务
@@ -39,6 +51,9 @@ public class TaskService {
     private String weatherAdcode;
 
     @Resource
+    private WeatherConfig weatherConfig;
+
+    @Resource
     private IWeatherService weatherService;
 
     /**
@@ -48,7 +63,29 @@ public class TaskService {
     @Scheduled(cron = "${weather.rt.cron}")
     public void collectRt() {
         log.debug("start collect weather rt.");
-        weatherService.collectRt(weatherAdcode);
+
+        try {
+            URIBuilder uriBuilder = new URIBuilder(weatherConfig.getWeatherRtAddr());
+            uriBuilder.addParameter("adcode", weatherConfig.getAdcode())
+                .addParameter("apiKey", weatherConfig.getApiKey());
+            String res = HttpUtils.doGet(uriBuilder);
+            log.debug("get res:\r\n{}", res);
+
+            JSONObject jsonObject = JSON.parseObject(res);
+
+            String code = jsonObject.getString("code");
+            Assert.isTrue(StringUtils.equals(code, "0"), -1, jsonObject.getString("message"));
+
+            String data = jsonObject.getString("data");
+
+            if (data != null) {
+                WeatherRt weatherRt = JSONObject.parseObject(data, WeatherRt.class);
+                weatherService.mergeRt(weatherRt);
+            }
+        }
+        catch (Exception e) {
+            log.error("collectRt fail!", e);
+        }
     }
 
     /**
@@ -58,6 +95,33 @@ public class TaskService {
     @Scheduled(cron = "${weather.forecast.cron}")
     public void collectForecast() {
         log.debug("start collect weather forecast.");
-        weatherService.collectForecast(weatherAdcode);
+
+        try {
+            URIBuilder uriBuilder = new URIBuilder(weatherConfig.getWeatherForecastAddr());
+            uriBuilder.addParameter("adcode", weatherConfig.getAdcode())
+                .addParameter("apiKey", weatherConfig.getApiKey());
+            String res = HttpUtils.doGet(uriBuilder);
+            log.debug("get res:\r\n{}", res);
+
+            JSONObject jsonObject = JSON.parseObject(res);
+
+            String code = jsonObject.getString("code");
+            Assert.isTrue(StringUtils.equals(code, "0"), -1, jsonObject.getString("message"));
+
+            JSONArray data = jsonObject.getJSONArray("data");
+            Assert.notNull(data, -1, "weather list is null.");
+
+            if (!data.isEmpty()) {
+                List<WeatherForecast> forecastList = JSON.parseArray(data.toString(), WeatherForecast.class);
+
+                if (CollectionUtils.isNotEmpty(forecastList)) {
+                    weatherService.deleteForecastByAdcode(weatherConfig.getAdcode());
+                    weatherService.insertForecastBacth(forecastList);
+                }
+            }
+        }
+        catch (Exception e) {
+            log.error("collectForecast fail!", e);
+        }
     }
 }

+ 1 - 1
ems/ems-core/src/main/java/com/ruoyi/ems/mapper/AdmWeatherForecastMapper.java

@@ -45,5 +45,5 @@ public interface AdmWeatherForecastMapper {
     /**
      * 根据区划代码删除
      */
-    void deleteByAdcode(@Param("adcode") String adcode);
+    int deleteByAdcode(@Param("adcode") String adcode);
 }

+ 13 - 8
ems/ems-core/src/main/java/com/ruoyi/ems/service/IWeatherService.java

@@ -34,6 +34,12 @@ public interface IWeatherService {
     WeatherRt getWeatherRt(String adcode);
 
     /**
+     * 合并实时天气数据
+     * @param weatherRt 实时天气数据
+     */
+    void mergeRt(WeatherRt weatherRt);
+
+    /**
      * 获取历史天气数据
      *
      * @param adcode    行政区划代码
@@ -52,16 +58,15 @@ public interface IWeatherService {
     List<WeatherForecast> getWeatherForecast(String adcode);
 
     /**
-     * 按小时采集
-     *
-     * @return 小时实时天气
+     * 根据code删除预报
+     * @param adcode 行政区划代码
+     * @return 影响行数
      */
-    void collectRt(String adcode);
+    int deleteForecastByAdcode(String adcode);
 
     /**
-     * 按小时采集
-     *
-     * @return 小时实时天气
+     * 插入预报数据
+     * @param forecasts 预报数据列表
      */
-    void collectForecast(String adcode);
+    void insertForecastBacth(List<WeatherForecast> forecasts);
 }

+ 9 - 67
ems/ems-core/src/main/java/com/ruoyi/ems/service/impl/WeatherServiceImpl.java

@@ -10,22 +10,13 @@
  */
 package com.ruoyi.ems.service.impl;
 
-import com.alibaba.fastjson2.JSON;
-import com.alibaba.fastjson2.JSONArray;
-import com.alibaba.fastjson2.JSONObject;
 import com.huashe.common.domain.model.WeatherForecast;
 import com.huashe.common.domain.model.WeatherRt;
-import com.huashe.common.exception.Assert;
-import com.huashe.common.utils.HttpUtils;
 import com.ruoyi.ems.mapper.AdmWeatherForecastMapper;
 import com.ruoyi.ems.mapper.AdmWeatherRtMapper;
 import com.ruoyi.ems.service.IWeatherService;
-import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.http.client.utils.URIBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
@@ -50,21 +41,17 @@ public class WeatherServiceImpl implements IWeatherService {
     @Resource
     private AdmWeatherForecastMapper weatherForecastMapper;
 
-    @Value("${weather.api-key}")
-    private String apiKey;
-
-    @Value("${weather.rt.api-addr}")
-    private String weatherRtAddr;
-
-    @Value("${weather.forecast.api-addr}")
-    private String weatherForecastAddr;
-
     @Override
     public WeatherRt getWeatherRt(String adcode) {
         return weatherRtMapper.getNewByAdcode(adcode);
     }
 
     @Override
+    public void mergeRt(WeatherRt weatherRt) {
+        weatherRtMapper.merge(weatherRt);
+    }
+
+    @Override
     public List<WeatherRt> getWeatherHis(String adcode, String startTime, String endTime) {
         return weatherRtMapper.getByTime(adcode, startTime, endTime);
     }
@@ -75,57 +62,12 @@ public class WeatherServiceImpl implements IWeatherService {
     }
 
     @Override
-    public void collectRt(String adcode) {
-        try {
-            URIBuilder uriBuilder = new URIBuilder(weatherRtAddr);
-            uriBuilder.addParameter("adcode", adcode).addParameter("apiKey", apiKey);
-            String res = HttpUtils.doGet(uriBuilder);
-            log.debug("get res:\r\n{}", res);
-
-            JSONObject jsonObject = JSON.parseObject(res);
-
-            String code = jsonObject.getString("code");
-            Assert.isTrue(StringUtils.equals(code, "0"), -1, jsonObject.getString("message"));
-
-            String data = jsonObject.getString("data");
-
-            if (data != null) {
-                WeatherRt weatherRt = JSONObject.parseObject(data, WeatherRt.class);
-                weatherRtMapper.merge(weatherRt);
-            }
-        }
-        catch (Exception e) {
-            log.error("collectRt fail!", e);
-        }
+    public int deleteForecastByAdcode(String adcode) {
+        return weatherForecastMapper.deleteByAdcode(adcode);
     }
 
     @Override
-    public void collectForecast(String adcode) {
-        try {
-            URIBuilder uriBuilder = new URIBuilder(weatherForecastAddr);
-            uriBuilder.addParameter("adcode", adcode).addParameter("apiKey", apiKey);
-            String res = HttpUtils.doGet(uriBuilder);
-            log.debug("get res:\r\n{}", res);
-
-            JSONObject jsonObject = JSON.parseObject(res);
-
-            String code = jsonObject.getString("code");
-            Assert.isTrue(StringUtils.equals(code, "0"), -1, jsonObject.getString("message"));
-
-            JSONArray data = jsonObject.getJSONArray("data");
-            Assert.notNull(data, -1, "weather list is null.");
-
-            if (!data.isEmpty()) {
-                List<WeatherForecast> forecastList = JSON.parseArray(data.toString(), WeatherForecast.class);
-
-                if (CollectionUtils.isNotEmpty(forecastList)) {
-                    weatherForecastMapper.deleteByAdcode(adcode);
-                    weatherForecastMapper.insertBatch(forecastList);
-                }
-            }
-        }
-        catch (Exception e) {
-            log.error("collectForecast fail!", e);
-        }
+    public void insertForecastBacth(List<WeatherForecast> forecasts) {
+        weatherForecastMapper.insertBatch(forecasts);
     }
 }