swb
2024-07-04 3faca3fc0bb178afd9cf17222bee14e1c8722f6b
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;
@@ -130,15 +125,13 @@
        Map<Date, List<Integer>> hours = DateUtils.getBeforeAndAfterHourDate(nextDay);
        List<Forecast> forecastList = new ArrayList<>();
        Forecast forecast = new Forecast();
        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);
@@ -171,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();
@@ -194,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);
@@ -210,7 +207,10 @@
                }
            }
        }
        forecastMapper.insertForecast(forecastList);
        if(forecastList.size()>0){
            forecastMapper.insertForecast(forecastList);
        }
    }
    private Double calculateAvg(List<Map<String, Object>> list, String param) {