From c96aba58f5bf3991e18cb0adaa71e7bec339cb4b Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Thu, 22 Dec 2022 16:00:16 +0800
Subject: [PATCH] 国控站数据维护问题3

---
 screen-job/src/main/java/com/moral/api/service/impl/HistoryDailyServiceImpl.java |  107 +++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 82 insertions(+), 25 deletions(-)

diff --git a/screen-job/src/main/java/com/moral/api/service/impl/HistoryDailyServiceImpl.java b/screen-job/src/main/java/com/moral/api/service/impl/HistoryDailyServiceImpl.java
index fb62517..2dc5a04 100644
--- a/screen-job/src/main/java/com/moral/api/service/impl/HistoryDailyServiceImpl.java
+++ b/screen-job/src/main/java/com/moral/api/service/impl/HistoryDailyServiceImpl.java
@@ -3,7 +3,6 @@
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.moral.api.entity.HistoryDaily;
-import com.moral.api.entity.HistoryHourly;
 import com.moral.api.entity.Sensor;
 import com.moral.api.mapper.HistoryDailyMapper;
 import com.moral.api.service.HistoryDailyService;
@@ -59,13 +58,17 @@
         Date end = DateUtils.dataToTimeStampTime(now, format);
         //������
         QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>();
-        sensorQueryWrapper.select("code").eq("is_delete", Constants.NOT_DELETE);
-        List<Object> sensorCodes = sensorService.listObjs(sensorQueryWrapper);
+        sensorQueryWrapper.select("code", "lower", "upper").eq("is_delete", Constants.NOT_DELETE);
+        List<Sensor> sensors = sensorService.list(sensorQueryWrapper);
 
         //������������������������������
-        QueryWrapper<HistoryHourly> historyHourlyQueryWrapper = new QueryWrapper<>();
-        historyHourlyQueryWrapper.ge("time", DateUtils.dateToDateString(start)).le("time", DateUtils.dateToDateString(end));
-        List<Map<String, Object>> dailyData = historyHourlyService.listMaps(historyHourlyQueryWrapper);
+        Map<String, Object> prop = new HashMap<>();
+        String timeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN);
+        prop.put("timeUnits", timeUnits);
+        prop.put("start", DateUtils.dateToDateString(start));
+        prop.put("end", DateUtils.dateToDateString(end));
+        List<Map<String, Object>> dailyData = historyHourlyService.selectDailyData(prop);
+
         if (dailyData.size() == 0) {
             return;
         }
@@ -76,47 +79,101 @@
         //���������������������������
         List<Map<String, Object>> insertData = new ArrayList<>();
 
-        data.forEach((key, value) -> {
+        data.forEach((mac, value) -> {
             Map<String, Object> dataMap = new HashMap<>();
             Map<String, Object> jsonMap = new HashMap<>();
-            dataMap.put("mac", key);
+            dataMap.put("mac", mac);
             dataMap.put("time", start);
 
-            //���������������������������������������������������
-            List<Map<String, Object>> tempValue = new ArrayList<>(value);
+            Map<String, Object> params = new HashMap<>();
+            params.put("data", value);
+            params.put("type", "day");
+            for (Sensor sensor : sensors) {
+                String sensorCode = sensor.getCode();
 
-            value.removeIf(map -> ((Date) map.get("time")).getTime() == start.getTime());
-            //������8������������������������������������
-            Object o3AvgOfDay = AmendUtils.getO3AvgOfDay(value);
-            if (o3AvgOfDay != null) {
-                jsonMap.put(Constants.SENSOR_CODE_O3, o3AvgOfDay);
+                //O3���������
+                if (sensorCode.equals(Constants.SENSOR_CODE_O3)) {
+                    if (sensor.getUpper() != null) {
+                        params.put("o3Upper", sensor.getUpper());
+                    }
+                    if (sensor.getLower() != null) {
+                        params.put("o3Lower", sensor.getLower());
+                    }
+                }
+
+                //���������������
+                if (sensorCode.equals(Constants.SENSOR_CODE_WIND_DIR)) {
+                    if (sensor.getUpper() != null) {
+                        params.put("windDirUpper", sensor.getUpper());
+                    }
+                    if (sensor.getLower() != null) {
+                        params.put("windDirLower", sensor.getLower());
+                    }
+                }
+
+                //���������������
+                if (sensorCode.equals(Constants.SENSOR_CODE_WIND_SPEED)) {
+                    if (sensor.getUpper() != null) {
+                        params.put("windSpeedUpper", sensor.getUpper());
+                    }
+                    if (sensor.getLower() != null) {
+                        params.put("windSpeedLower", sensor.getLower());
+                    }
+                }
             }
-            //������������������������������������
-            tempValue.removeIf(map -> ((Date) map.get("time")).getTime() == end.getTime());
+            //������8������������������������������������
+            Map<String, Object> o3AvgOfDay = AmendUtils.getO3AvgOfDay(params);
+            if (!ObjectUtils.isEmpty(o3AvgOfDay)) {
+                jsonMap.putAll(o3AvgOfDay);
+            }
 
             //���������������������������
-            Object windDirAvg = AmendUtils.getWindDirAvg(value);
-            if (windDirAvg != null) {
-                jsonMap.put(Constants.SENSOR_CODE_WIND_DIR, windDirAvg);
+            Map<String, Object> windDirAvg = AmendUtils.getWindDirAvg(params);
+            if (!ObjectUtils.isEmpty(windDirAvg)) {
+                jsonMap.putAll(windDirAvg);
             }
 
-            sensorCodes.forEach(sensorCode -> {
-                OptionalDouble optionalDouble = tempValue.parallelStream()
+            sensors.forEach(sensor -> {
+                String sensorCode = sensor.getCode();
+                Double upper = sensor.getUpper();
+                Double lower = sensor.getLower();
+                OptionalDouble optionalDouble = value.parallelStream()
                         .flatMapToDouble(v -> {
                             Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class);
-                            Object sensorValue = dataValue.get(sensorCode.toString());
+                            Object sensorValue = dataValue.get(sensorCode);
+                            //������������������������
+                            Object flag = dataValue.get(sensorCode + "-" + Constants.MARKER_BIT_KEY);
+                            if (!Constants.MARKER_BIT_TRUE.equals(flag)) {
+                                return null;
+                            }
+
                             if (ObjectUtils.isEmpty(sensorValue)) {
                                 return null;
                             }
+
+                            //O3������������
                             if (sensorCode.equals(Constants.SENSOR_CODE_O3)) {
                                 return null;
                             }
-                            return DoubleStream.of(Double.parseDouble(sensorValue.toString()));
+
+                            //������������������������������������
+                            double aDouble = Double.parseDouble(sensorValue.toString());
+                            if (!ObjectUtils.isEmpty(upper)) {
+                                if (aDouble > upper) {
+                                    return null;
+                                }
+                            }
+                            if (!ObjectUtils.isEmpty(lower)) {
+                                if (aDouble < lower) {
+                                    return null;
+                                }
+                            }
+                            return DoubleStream.of(aDouble);
                         }).average();
                 if (optionalDouble.isPresent()) {
                     //���������������������
                     double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 4);
-                    jsonMap.put(sensorCode.toString(), sciCal);
+                    jsonMap.put(sensorCode, sciCal);
                 }
             });
             dataMap.put("value", JSONObject.toJSONString(jsonMap));

--
Gitblit v1.8.0