From e585442b151615cb5cd57ee1d817e88848b5f5a1 Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Mon, 21 Mar 2022 09:36:20 +0800
Subject: [PATCH] city_aqi小时数据第三方接口变更

---
 screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java |   51 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 38 insertions(+), 13 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..1745e77 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
@@ -2,6 +2,7 @@
 
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.moral.api.entity.CityAqiDaily;
 import com.moral.api.entity.CityAqiMonthly;
 import com.moral.api.mapper.CityAqiMonthlyMapper;
@@ -57,6 +58,20 @@
         //���������������������1���
         Date end = DateUtils.addMonths(start, 1);
 
+        //���������1������������������������������������
+        //������1���������������������������������������������������������
+        int day = DateUtils.getDay(new Date());
+        UpdateWrapper<CityAqiMonthly> cityAqiMonthlyUpdateWrapper = new UpdateWrapper<>();
+        if (day == 1) {
+            cityAqiMonthlyUpdateWrapper.eq("time", start);
+        } else {
+            start = end;
+            end = DateUtils.addMonths(start, 1);
+            lastLastMonth = DateUtils.addMonths(start, -1);
+            cityAqiMonthlyUpdateWrapper.eq("time", start);
+        }
+        cityAqiMonthlyMapper.delete(cityAqiMonthlyUpdateWrapper);
+
 
         //������������������aqi������������
         QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>();
@@ -69,13 +84,26 @@
             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()));
 
-        CityAqiMonthly cityAqiMonthly = new CityAqiMonthly();
+        //������������������������������������������������
+        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<>();
+
+        Date finalStart = start;
         data.forEach((cityCode, value) -> {
+            CityAqiMonthly cityAqiMonthly = new CityAqiMonthly();
             Map<String, Object> jsonMap = new HashMap<>();
             cityAqiMonthly.setCityCode(Integer.parseInt(cityCode));
-            cityAqiMonthly.setTime(start);
+            cityAqiMonthly.setTime(finalStart);
 
             Map<String, Object> params = new HashMap<>();
             List<Map<String, Object>> temp = new ArrayList<>();
@@ -102,7 +130,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 +146,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