From 6f4e852b84c577454a4876f83c7085bd360fe4fb Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Tue, 17 Aug 2021 17:07:02 +0800
Subject: [PATCH] 特殊设备数据insert

---
 screen-job/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java |  175 +++++++++++++++++++++++++++++++++-------------------------
 1 files changed, 100 insertions(+), 75 deletions(-)

diff --git a/screen-job/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java b/screen-job/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java
index ed9a04a..ae9d97a 100644
--- a/screen-job/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java
+++ b/screen-job/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java
@@ -1,7 +1,9 @@
 package com.moral.api.service.impl;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.ObjectUtils;
 
 import java.util.ArrayList;
@@ -17,10 +19,12 @@
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.moral.api.entity.Sensor;
 import com.moral.api.mapper.HistoryFiveMinutelyMapper;
+import com.moral.api.mapper.HistoryMinutelyMapper;
 import com.moral.api.service.HistoryFiveMinutelyService;
-import com.moral.api.service.HistoryMinutelyService;
 import com.moral.api.service.SensorService;
 import com.moral.constant.Constants;
+import com.moral.constant.RedisConstants;
+import com.moral.util.AmendUtils;
 import com.moral.util.DateUtils;
 
 @Service
@@ -30,10 +34,13 @@
     private HistoryFiveMinutelyMapper historyFiveMinutelyMapper;
 
     @Autowired
-    private HistoryMinutelyService historyMinutelyService;
+    private HistoryMinutelyMapper historyMinutelyMapper;
 
     @Autowired
     private SensorService sensorService;
+
+    @Autowired
+    private RedisTemplate redisTemplate;
 
     @Override
     public void createTable(String timeUnits) {
@@ -41,10 +48,14 @@
     }
 
     @Override
+    @Transactional
     public void insertHistoryFiveMinutely() {
         //������������������yyyy-MM-dd HH:mm
         String format = DateUtils.yyyy_MM_dd_HH_mm_EN;
         Date now = new Date();
+        now = DateUtils.getFiveMinuteDate(now);
+
+        //������������������������������
         Map<String, Object> params = new HashMap<>();
         //������������
         Date start = DateUtils.dataToTimeStampTime(DateUtils.getDateOfMin(now, -5), format);
@@ -57,12 +68,16 @@
         params.put("timeUnits", timeUnits);
 
         //������
-        QueryWrapper<Sensor> queryWrapper = new QueryWrapper<>();
-        queryWrapper.select("code").eq("is_delete", Constants.NOT_DELETE);
-        List<Object> sensorCodes = sensorService.listObjs(queryWrapper);
+        QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>();
+        sensorQueryWrapper.select("code", "lower", "upper").eq("is_delete", Constants.NOT_DELETE);
+        List<Sensor> sensors = sensorService.list(sensorQueryWrapper);
 
-        //���������������������5������������
-        List<Map<String, Object>> fiveMinutelyData = historyMinutelyService.getHistoryFiveMinutelyData(params);
+        //���������������������5������������������
+        List<Map<String, Object>> fiveMinutelyData = historyMinutelyMapper.getHistoryMinutelyData(params);
+        if (fiveMinutelyData.size() == 0) {
+            return;
+        }
+
         //���mac������
         Map<String, List<Map<String, Object>>> data = fiveMinutelyData.parallelStream()
                 .collect(Collectors.groupingBy(o -> (String) o.get("mac")));
@@ -72,88 +87,98 @@
 
         data.forEach((key, value) -> {
             Map<String, Object> dataMap = new HashMap<>();
-            Map<String, Object> jsonMap = new HashMap<>();
             dataMap.put("mac", key);
             dataMap.put("time", end);
-            sensorCodes.forEach(sensorCode -> {
-                OptionalDouble optionalDouble = value.parallelStream()
+            Map<String, Object> jsonMap = new HashMap<>();
+
+
+            Map<String, Object> map = new HashMap<>();
+            map.put("data", value);
+            map.put("type", "fiveMinutes");
+            for (Sensor sensor : sensors) {
+                String sensorCode = sensor.getCode();
+
+                //���������������
+                if (sensorCode.equals(Constants.SENSOR_CODE_WIND_DIR)) {
+                    if (sensor.getUpper() != null) {
+                        map.put("windDirUpper", sensor.getUpper());
+                    }
+                    if (sensor.getLower() != null) {
+                        map.put("windDirLower", sensor.getLower());
+                    }
+                }
+
+                //���������������
+                if (sensorCode.equals(Constants.SENSOR_CODE_WIND_SPEED)) {
+                    if (sensor.getUpper() != null) {
+                        map.put("windSpeedUpper", sensor.getUpper());
+                    }
+                    if (sensor.getLower() != null) {
+                        map.put("windSpeedLower", sensor.getLower());
+                    }
+                }
+            }
+
+            //���������������������������
+            Map<String, Object> windDirAvg = AmendUtils.getWindDirAvg(map);
+            if (!ObjectUtils.isEmpty(windDirAvg)) {
+                jsonMap.putAll(windDirAvg);
+            }
+
+            //������������������������������������
+            sensors.forEach(sensor -> {
+                String sensorCode = sensor.getCode();
+                Double upper = sensor.getUpper();
+                Double lower = sensor.getLower();
+                DoubleStream 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;
                             }
-                            return DoubleStream.of(Double.parseDouble(sensorValue.toString()));
-                        }).average();
-                if (optionalDouble.isPresent()) {
-                    jsonMap.put(sensorCode.toString(), Double.parseDouble(String.format("%.4f", optionalDouble.getAsDouble())));
+                            //������������������
+                            if (Constants.SENSOR_CODE_WIND_DIR.equals(sensorCode)) {
+                                return null;
+                            }
+                            //������������������������������������
+                            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);
+                        });
+
+                OptionalDouble average = optionalDouble.average();
+                if (average.isPresent()) {
+                    //���������������������
+                    double sciCal = AmendUtils.sciCal(average.getAsDouble(), 4);
+                    jsonMap.put(sensorCode, sciCal);
                 }
+
             });
             dataMap.put("value", JSONObject.toJSONString(jsonMap));
+            //������redis
+            redisTemplate.opsForHash().put(RedisConstants.DATA_FIVE_MINUTES, key, jsonMap);
             insertData.add(dataMap);
         });
 
         //5���������������
-        String insertTimeUnits = DateUtils.dateToDateString(new Date());
+        String insertTimeUnits = DateUtils.dateToDateString(now, DateUtils.yyyyMM_EN);
+        //���������������
         historyFiveMinutelyMapper.insertHistoryFiveMinutely(insertData, insertTimeUnits);
-    }
-
-    public static void main(String[] args) {
-        //������������
-        List<Map<String, Object>> list = new ArrayList<>();
-        Map<String, Object> map1 = new HashMap<>();
-        map1.put("mac", "p5dnd1234567");
-        map1.put("value", "{\"a0001\": 10, \"a0002\": 8, \"a0001-Flag\": \"N\", \"a0002-Flag\": \"N\"}");
-        Map<String, Object> map2 = new HashMap<>();
-        map2.put("mac", "p5dnd123456789");
-        map2.put("value", "{\"a0001\": 12, \"a0002\": 12, \"a0001-Flag\": \"N\", \"a0002-Flag\": \"N\"}");
-        Map<String, Object> map3 = new HashMap<>();
-        map3.put("mac", "p5dnd1234567");
-        map3.put("value", "{\"a0001\": 6, \"a0002\": 20, \"a0001-Flag\": \"N\", \"a0002-Flag\": \"N\", \"a0003\": 14}");
-        Map<String, Object> map4 = new HashMap<>();
-        map4.put("mac", "p5dnd1234567");
-        map4.put("value", "{\"a0001\": 4, \"a0002\": 16, \"a0001-Flag\": \"N\", \"a0002-Flag\": \"N\", \"a0003\": 16}");
-        list.add(map1);
-        list.add(map2);
-        list.add(map3);
-        list.add(map4);
-
-        //���������mac������
-        Map<String, List<Map<String, Object>>> data = list.parallelStream().collect(Collectors.groupingBy(o -> (String) o.get("mac")));
-        System.out.println(data);
-
-        //������������
-        List<Object> sensors = new ArrayList<>();
-        sensors.add("a0001");
-        sensors.add("a0002");
-        sensors.add("a0003");
-
-        //������������������������
-        List<Map<String, Object>> insertData = new ArrayList<>();
-
-        data.forEach((key, value) -> {
-            Map<String, Object> dataMap = new HashMap<>();
-            Map<String, Object> jsonMap = new HashMap<>();
-            dataMap.put("mac", key);
-            sensors.forEach(sensorCode -> {
-                OptionalDouble optionalDouble = value.parallelStream()
-                        .flatMapToDouble(v -> {
-                            Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class);
-                            Object sensorValue = dataValue.get(sensorCode.toString());
-                            if (ObjectUtils.isEmpty(sensorValue)) {
-                                return null;
-                            }
-                            return DoubleStream.of(Double.parseDouble(sensorValue.toString()));
-                        }).average();
-                if (optionalDouble.isPresent()) {
-                    jsonMap.put(sensorCode.toString(), Double.parseDouble(String.format("%.4f", optionalDouble.getAsDouble())));
-                }
-            });
-            dataMap.put("value", JSONObject.toJSONString(jsonMap));
-            insertData.add(dataMap);
-        });
-
-
-        System.out.println("666==" + insertData);
     }
 }

--
Gitblit v1.8.0