Browse Source

易客云获取当天天气修改

lvy 5 ngày trước cách đây
mục cha
commit
42ef60c950

+ 7 - 2
blade-common/src/main/java/org/springblade/common/utils/YiKeYunApiUtils.java

@@ -100,8 +100,13 @@ public class YiKeYunApiUtils {
      * @param cityId
      * @return
      */
-    public static Map<String, Map<String, String>> getHistoryWeather(String cityId, Integer year, Integer month) {
-        String getUrl = String.format("http://gfeljm.tianqiapi.com/free/history?appid=%s&appsecret=%s&cityid=%s&year=%d&month=%d", API_YIKEYUN_APPID, API_YIKEYUN_APPSECRET, cityId, year, month);
+    public static Map<String, Map<String, String>> getHistoryWeather(String cityId, Integer year, Integer month, String date1) {
+        String getUrl;
+        if (date1 != null && date1.matches("\\d{4}-\\d{2}-\\d{2}")) {
+            getUrl = String.format("http://gfeljm.tianqiapi.com/free/history?appid=%s&appsecret=%s&cityid=%s&date=%s", API_YIKEYUN_APPID, API_YIKEYUN_APPSECRET, cityId, date1);
+        } else {
+            getUrl = String.format("http://gfeljm.tianqiapi.com/free/history?appid=%s&appsecret=%s&cityid=%s&year=%d&month=%d", API_YIKEYUN_APPID, API_YIKEYUN_APPSECRET, cityId, year, month);
+        }
         Map<String, Map<String, String>> map = new HashMap<>();
         try {
             URL url = new URL(getUrl);

+ 16 - 15
blade-service/blade-business/src/main/java/org/springblade/business/service/impl/WeatherInfoServiceImpl.java

@@ -120,7 +120,7 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
     /**
      * 获取当前系统所有项目下所有合同段的当天天气
      */
-    @Scheduled(cron = "0 18 10 * * ?")
+    @Scheduled(cron = "0 10 0,6,12,18,23 * * ?")
     public void syncWeatherInfo() {
         if (!SystemUtils.isLinux()) {
             return;
@@ -128,12 +128,13 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
         //获取所有合同段的定位信息
         List<ProjectContractArea> areaList = this.projectContractAreaClient.queryAllContractArea();
         Map<String, Map<String, String>> cachedWeatherMap = new HashMap<>();
+        String date = DateUtil.format(new Date(), "yyyy-MM-dd");
         for (ProjectContractArea area : areaList) {
             try {
                 //校验当前区域是否已经获取当天日期(手动补填或自动获取)
                 Map<String, Object> queryMap = new HashMap<>();
                 queryMap.put("contractAreaId", area.getId());
-                queryMap.put("recordTime", DateUtil.format(new Date(), "yyyy-MM-dd"));
+                queryMap.put("recordTime", date);
                 //查询
                 long count = this.count(Condition.getQueryWrapper(queryMap, WeatherInfo.class));
                 if (count > 0) {
@@ -146,30 +147,30 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
                     String nums = stringRedisTemplate.opsForValue().get("blade-business::contractArea:yiKeYun:todayWeatherNums");
                     if (nums == null || Integer.parseInt(nums) <= 19000) {
                         // 重试三次
-                        weatherMap = YiKeYunApiUtils.getTodayWeatherByAdcodeTry(area.getCity_code() + "000000");
-                        if (weatherMap != null) {
+//                        weatherMap = YiKeYunApiUtils.getTodayWeatherByAdcodeTry(area.getCity_code() + "000000");
+                        Map<String, Map<String, String>> map = getHistoryWeather(area, date, date);
+                        if (map != null && !map.isEmpty()) {
+                            weatherMap = map.values().stream().findFirst().orElse( null);
+                        }
+                        if (weatherMap != null && !weatherMap.isEmpty()) {
                             cachedWeatherMap.put(area.getCity_code(), weatherMap);
                             if (weatherMap.get("nums") != null) {
                                 stringRedisTemplate.opsForValue().set("blade-business::contractArea:yiKeYun:todayWeatherNums", weatherMap.get("nums"), getSecondsUntilMidnight(), TimeUnit.SECONDS);
                             }
                         }
                     }
-                    //获取天气信息(百度天气)
-//                    if (weatherMap == null) {
-//                        weatherMap = BaiduApiUtil.getTodayWeather(area.getCity_code());
-//                    }
                 }
-                if (weatherMap != null) {
+                if (weatherMap != null && !weatherMap.isEmpty()) {
                     //计算平均气温
                     BigDecimal aver = (new BigDecimal(weatherMap.get("high")).add(new BigDecimal(weatherMap.get("low")))).divide(new BigDecimal("2"), 1, BigDecimal.ROUND_HALF_UP);
                     WeatherInfo newWeather = new WeatherInfo(String.valueOf(area.getId()), weatherMap.get("weather"), aver.toString(), weatherMap.get("high"), weatherMap.get("low"), weatherMap.get("windLevel"));
                     this.save(newWeather);
                     log.info("今日的天气已经同步完成!contractAreaId:" + area.getId() + ",syncTime:" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                 } else {
-                    log.info("获取今日的天气失败!contractAreaId:" + area.getId() + ",syncTime:" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
+                    log.warn("获取今日的天气失败!contractAreaId:" + area.getId() + ",syncTime:" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
                 }
             } catch (Exception e) {
-                e.printStackTrace();
+                log.error("获取今日的天气失败!contractAreaId:" + area.getId() + ",syncTime:" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"), e);
             }
         }
         System.out.println("同步当天天气完成");
@@ -264,7 +265,7 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
                         //判断集合中是否存在当前天气,如果不存在则获取
                         if (weatherMap == null || weatherMap.get(plainTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"))) == null) {
                             String month = plainTime.format(DateTimeFormatter.ofPattern("yyyyMM"));
-                            weatherMap = this.getHistoryWeather(projectContractArea, month);
+                            weatherMap = this.getHistoryWeather(projectContractArea, month, null);
                             if (weatherMap == null) {
                                 weatherMap = this.getWeather(county.toString(), month);
                                 if (weatherMap == null || weatherMap.get(plainTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"))) == null) {
@@ -361,7 +362,7 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
             Map<String, List<LocalDate>> map = missDate.stream().collect(Collectors.groupingBy(l -> l.format(DateTimeFormatter.ofPattern("yyyyMM"))));
             for (String s : map.keySet()) {
                 //获取整月天气
-                weatherMap = this.getHistoryWeather(area, s);
+                weatherMap = this.getHistoryWeather(area, s, null);
                 if (weatherMap == null) {
                     weatherMap = this.getWeather(county.toString(), s);
                     if (weatherMap == null || weatherMap.size() == 0) {
@@ -426,7 +427,7 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
         System.out.println("9999");
     }
 
-    public Map<String, Map<String, String>> getHistoryWeather(ProjectContractArea area, String month) {
+    public Map<String, Map<String, String>> getHistoryWeather(ProjectContractArea area, String month, String date) {
         String nums = stringRedisTemplate.opsForValue().get("blade-business::contractArea:yiKeYun:historyWeatherNums");
         if (nums != null && Integer.parseInt(nums) > 19000) {
             log.info("获取历史天气失败:易客云获取历史天气的api次数已用完。");
@@ -455,7 +456,7 @@ public class WeatherInfoServiceImpl extends ServiceImpl<WeatherInfoMapper, Weath
             cityId = jdbcTemplate.queryForObject("select id from m_yikeyun_weather_city where cityZh like '" + county + "%'", String.class);
         }
         if (cityId != null) {
-            Map<String, Map<String, String>> historyWeather = YiKeYunApiUtils.getHistoryWeather(cityId, Integer.parseInt(month.substring(0, 4)), Integer.parseInt(month.substring(4, 6)));
+            Map<String, Map<String, String>> historyWeather = YiKeYunApiUtils.getHistoryWeather(cityId, Integer.parseInt(month.substring(0, 4)), Integer.parseInt(month.substring(4, 6)), date);
             if (historyWeather != null) {
                 Map<String, String> map = historyWeather.get("nums");
                 if (map != null && map.get("nums") != null) {