From f052b63c0428d4f3f723e9c729f19a62bfe3172e Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Thu, 20 Jan 2022 09:38:31 +0800
Subject: [PATCH] job定时任务修改入库逻辑

---
 screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java |   30 +++++++++++++++++++-----------
 1 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java b/screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java
index 5a68e10..18ddf7c 100644
--- a/screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java
+++ b/screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java
@@ -69,8 +69,19 @@
             return;
         }
         //���city_code������
-        Map<String, List<Map<String, Object>>> data = monthlyData.parallelStream().collect(Collectors.groupingBy(o -> o.get("city_code").toString()));
+        Map<String, List<Map<String, Object>>> data = monthlyData.stream()
+                .collect(Collectors.groupingBy(o -> o.get("city_code").toString()));
 
+        //������������������������������������������������
+        QueryWrapper<CityAqiMonthly> queryWrapper = new QueryWrapper<>();
+        queryWrapper.select("city_code", "value")
+                .eq("time", DateUtils.dateToDateString(lastLastMonth));
+        //������������������
+        List<CityAqiMonthly> lastCityAqiMonthly = cityAqiMonthlyMapper.selectList(queryWrapper);
+        Map<Integer, List<CityAqiMonthly>> lastMonthData = lastCityAqiMonthly.stream()
+                .collect(Collectors.groupingBy(CityAqiMonthly::getCityCode));
+
+        List<CityAqiMonthly> cityAqiMonthlyList = new ArrayList<>();
         CityAqiMonthly cityAqiMonthly = new CityAqiMonthly();
         data.forEach((cityCode, value) -> {
             Map<String, Object> jsonMap = new HashMap<>();
@@ -102,7 +113,7 @@
             }
 
             sensors.forEach(sensor -> {
-                OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> {
+                OptionalDouble optionalDouble = value.stream().flatMapToDouble(v -> {
                     Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class);
                     double aDouble = Double.parseDouble(dataValue.get(sensor).toString());
                     return DoubleStream.of(aDouble);
@@ -118,21 +129,18 @@
             jsonMap.put("compositeIndex", compositeIndex);
 
             //���������������������������
-            QueryWrapper<CityAqiMonthly> queryWrapper = new QueryWrapper<>();
-            queryWrapper.select("value")
-                    .eq("city_code", cityCode)
-                    .eq("time", DateUtils.dateToDateString(lastLastMonth));
-            //���������������������
-            CityAqiMonthly lastCityAqiMonthly = cityAqiMonthlyMapper.selectOne(queryWrapper);
-            if (lastCityAqiMonthly != null) {
-                Map<String, Object> map = JSONObject.parseObject(lastCityAqiMonthly.getValue(), Map.class);
+            List<CityAqiMonthly> cityAqiMonthlies = lastMonthData.get(Integer.parseInt(cityCode));
+            if (!ObjectUtils.isEmpty(cityAqiMonthlies)) {
+                CityAqiMonthly monthly = cityAqiMonthlies.get(0);
+                Map<String, Object> map = JSONObject.parseObject(monthly.getValue(), Map.class);
                 double lastCompositeIndex = Double.parseDouble(map.get("compositeIndex").toString());
                 DecimalFormat decimalFormat = new DecimalFormat("0.00%");
                 String format = decimalFormat.format((compositeIndex - lastCompositeIndex) / lastCompositeIndex);
                 jsonMap.put("monthContrast", format);
             }
             cityAqiMonthly.setValue(JSONObject.toJSONString(jsonMap));
-            cityAqiMonthlyMapper.insert(cityAqiMonthly);
+            cityAqiMonthlyList.add(cityAqiMonthly);
         });
+        cityAqiMonthlyMapper.insertCityAqiMonthly(cityAqiMonthlyList);
     }
 }

--
Gitblit v1.8.0