From 3faca3fc0bb178afd9cf17222bee14e1c8722f6b Mon Sep 17 00:00:00 2001
From: swb <jpy123456>
Date: Thu, 04 Jul 2024 17:02:26 +0800
Subject: [PATCH] fix:尘负荷排名补充提交

---
 screen-job/src/main/java/com/moral/api/service/impl/ForecastServiceImpl.java |   29 +++++++++++++++--------------
 1 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/screen-job/src/main/java/com/moral/api/service/impl/ForecastServiceImpl.java b/screen-job/src/main/java/com/moral/api/service/impl/ForecastServiceImpl.java
index 944dd08..d1bd8ab 100644
--- a/screen-job/src/main/java/com/moral/api/service/impl/ForecastServiceImpl.java
+++ b/screen-job/src/main/java/com/moral/api/service/impl/ForecastServiceImpl.java
@@ -22,12 +22,7 @@
 import org.springframework.stereotype.Service;
 import org.springframework.util.ObjectUtils;
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.OptionalDouble;
+import java.util.*;
 import java.util.function.Supplier;
 import java.util.stream.Collectors;
 import java.util.stream.DoubleStream;
@@ -57,9 +52,9 @@
     private CityAqiService cityAqiService;
 
     @Autowired
-    private ForecastService forecastService;
+    private ForecastMapper forecastMapper;
 
-    private static Map<String, Integer> weatherScore = new HashMap<>();
+    private static final Map<String, Integer> weatherScore = new HashMap<>();
 
     static {
         weatherScore.put("���", 100);
@@ -129,15 +124,14 @@
         //���������������������map
         Map<Date, List<Integer>> hours = DateUtils.getBeforeAndAfterHourDate(nextDay);
 
-        Forecast forecast = new Forecast();
+        List<Forecast> forecastList = new ArrayList<>();
+
         Map<String, Object> forecastMap = new HashMap<>();
         for (Object obj : cityCodes) {
             Integer cityCode = Integer.parseInt(obj.toString());
 
-            forecast.setCityCode(cityCode);
-
             //������
-            List<CityWeatherForecast> cityWeatherForecasts = cityForecast.get(cityCode);
+            List<CityWeatherForecast> cityWeatherForecasts = Objects.isNull(cityForecast.get(cityCode))?new ArrayList<>():cityForecast.get(cityCode);
 
             //������������������������������
             List<CityWeather> cityWeathers = cityHistoryWeather.get(cityCode);
@@ -170,6 +164,8 @@
             }
 
             for (CityWeatherForecast cityWeatherForecast : cityWeatherForecasts) {
+                Forecast forecast = new Forecast();
+                forecast.setCityCode(cityCode);
                 Date time = cityWeatherForecast.getTime();
                 Map<String, Object> value = JSONObject.parseObject(cityWeatherForecast.getValue(), Map.class);
                 String weather = value.get("text").toString();
@@ -193,6 +189,8 @@
                 int size = weatherMaps.size();
                 Double tempAvg = calculateAvg(weatherMaps, "temp");
                 Double o3Avg = calculateAvg(weatherMaps, "O3");
+                if(tempAvg == null || o3Avg == null)
+                    continue;
                 Double sum1 = calculateProduct(weatherMaps, "temp", "O3");
                 Double sum2 = calculateProduct(weatherMaps, "temp", "temp");
                 double b = (sum1 - size * tempAvg * o3Avg) / (sum2 - size * tempAvg * tempAvg);
@@ -205,11 +203,14 @@
                 if (!Double.isNaN(o3Forecast)) {
                     forecastMap.put("O3", AmendUtils.sciCal(o3Forecast, 0));
                     forecast.setValue(JSONObject.toJSONString(forecastMap));
-                    forecastService.save(forecast);
+                    forecastList.add(forecast);
                 }
             }
-
         }
+        if(forecastList.size()>0){
+            forecastMapper.insertForecast(forecastList);
+        }
+
     }
 
     private Double calculateAvg(List<Map<String, Object>> list, String param) {

--
Gitblit v1.8.0