From 994d4b65f78e5121680de31b14161003620d746e Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Wed, 16 Nov 2022 10:45:22 +0800
Subject: [PATCH] 修改张家港国控站数据

---
 screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java |  212 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 205 insertions(+), 7 deletions(-)

diff --git a/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java b/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java
index 8f5f0b5..b6f156f 100644
--- a/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java
+++ b/screen-job/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java
@@ -2,6 +2,7 @@
 
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.moral.api.config.mybatis.MybatisPlusConfig;
 import com.moral.api.entity.HistoryHourly;
 import com.moral.api.entity.Sensor;
 import com.moral.api.mapper.HistoryHourlyMapper;
@@ -10,8 +11,11 @@
 import com.moral.api.service.SensorService;
 import com.moral.constant.Constants;
 import com.moral.constant.RedisConstants;
+import com.moral.constant.SeparateTableType;
 import com.moral.util.AmendUtils;
 import com.moral.util.DateUtils;
+
+import com.moral.util.MybatisPLUSUtils;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
@@ -26,8 +30,10 @@
 import java.util.OptionalDouble;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Supplier;
 import java.util.stream.Collectors;
 import java.util.stream.DoubleStream;
+import java.util.stream.Stream;
 
 /**
  * <p>
@@ -55,6 +61,11 @@
     @Override
     public void createTable(String timeUnits) {
         historyHourlyMapper.createTable(timeUnits);
+    }
+
+    @Override
+    public void createTableComplete(String timeUnits) {
+        historyHourlyMapper.createTableComplete(timeUnits);
     }
 
     @Override
@@ -116,14 +127,14 @@
                 .collect(Collectors.groupingBy(o -> (String) o.get("mac")));
 
         //���������������������������
-        List<HistoryHourly> insertData = new ArrayList<>();
+        List<Map<String, Object>> insertData = new ArrayList<>();
 
         data.forEach((key, value) -> {
-            HistoryHourly historyHourly = new HistoryHourly();
-            historyHourly.setMac(key);
-            historyHourly.setTime(end);
-            Map<String, Object> jsonMap = new HashMap<>();
+            Map<String, Object> historyHourly = new HashMap<>();
+            historyHourly.put("mac", key);
+            historyHourly.put("time", end);
 
+            Map<String, Object> jsonMap = new HashMap<>();
             Map<String, Object> map = new HashMap<>();
             map.put("data", value);
             map.put("type", "hour");
@@ -211,8 +222,8 @@
                     }
                 }
             });
-            historyHourly.setValue(JSONObject.toJSONString(jsonMap));
-            historyHourly.setVersion((Integer) value.get(0).get("version"));
+            historyHourly.put("version", value.get(0).get("version"));
+            historyHourly.put("value", JSONObject.toJSONString(jsonMap));
             insertData.add(historyHourly);
         });
 
@@ -224,4 +235,191 @@
     public List<Map<String, Object>> selectDailyData(Map<String, Object> params) {
         return historyHourlyMapper.selectDailyData(params);
     }
+
+    /**
+     * @Description: ���������������������������mac���������
+     * @Param: [mac, startDate, endDate]
+     * @return: java.util.List<com.moral.api.entity.HistoryHourly>
+     * @Author: ���������
+     * @Date: 2021/9/23
+     */
+    @Override
+    public List<HistoryHourly> getValueByMacAndTime(String mac, Date startDate, Date endDate) {
+        QueryWrapper<HistoryHourly> wrapper = new QueryWrapper<>();
+        wrapper.eq("mac", mac);
+        wrapper.between("time", startDate, endDate);
+        List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH);
+        List<HistoryHourly> datas = multiTableQuery(wrapper, tableNames);
+        return datas;
+    }
+
+    @Override
+    public void insertHistoryHourlyComplete() {
+        //������������������yyyy-MM-dd HH:mm
+        String format = DateUtils.yyyy_MM_dd_HH_EN;
+        Date now = new Date();
+
+        //������������������������������
+        Map<String, Object> params = new HashMap<>();
+        //������������
+        Date start = DateUtils.dataToTimeStampTime(DateUtils.addHours(now, -1), format);
+        //������������
+        Date end = DateUtils.dataToTimeStampTime(now, format);
+        params.put("start", start);
+        params.put("end", end);
+        //������������������������������
+        String timeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN);
+        params.put("timeUnits", timeUnits);
+
+        //������
+        QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>();
+        sensorQueryWrapper.select("code", "lower", "upper").eq("is_delete", Constants.NOT_DELETE);
+        List<Sensor> sensors = sensorService.list(sensorQueryWrapper);
+
+        //������������������������������������������
+        List<Map<String, Object>> hourlyData = historyMinutelyMapper.getHourlyData(params);
+        if (ObjectUtils.isEmpty(hourlyData)) {
+            return;
+        }
+
+        //���mac������
+        Map<String, List<Map<String, Object>>> data = hourlyData.parallelStream()
+                .collect(Collectors.groupingBy(o -> (String) o.get("mac")));
+
+        //���������������������������
+        List<Map<String, Object>> insertData = new ArrayList<>();
+
+        data.forEach((key, value) -> {
+            Map<String, Object> dataMap = new HashMap<>();
+            dataMap.put("mac", key);
+            dataMap.put("time", start);
+            Map<String, Object> jsonMap = new HashMap<>();
+
+
+            Map<String, Object> map = new HashMap<>();
+            map.put("data", value);
+            map.put("type", "hourlyComplete");
+            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());
+                    }
+                }
+            }
+            Supplier<Stream<Map<String, Object>>> streamSupplier = () -> value.stream();
+            //������������������������������������
+            sensors.forEach(sensor -> {
+                Double avg = calculatedValue(streamSupplier, sensor, "avg");
+                if (Constants.SENSOR_CODE_WIND_DIR.equals(sensor.getCode())) {
+                    //���������������������������
+                    Map<String, Object> windDirAvg = AmendUtils.getWindDirAvg(map);
+                    if (!ObjectUtils.isEmpty(windDirAvg)) {
+                        avg = (Double) windDirAvg.get(Constants.SENSOR_CODE_WIND_DIR);
+                    }
+                }
+                Double min = calculatedValue(streamSupplier, sensor, "min");
+                Double max = calculatedValue(streamSupplier, sensor, "max");
+                List<Double> doubles = new ArrayList<>(3);
+                if (avg != null && min != null && max != null) {
+                    doubles.add(avg);
+                    doubles.add(min);
+                    doubles.add(max);
+                    jsonMap.put(sensor.getCode(), doubles);
+                }
+            });
+            dataMap.put("value", JSONObject.toJSONString(jsonMap));
+            insertData.add(dataMap);
+        });
+
+        //������
+        String insertTimeUnits = DateUtils.dateToDateString(start, DateUtils.yyyyMM_EN);
+        //���������������
+        historyHourlyMapper.insertHistoryHourlyComplete(insertData, insertTimeUnits);
+    }
+
+    /**
+     * @Description: ������������������������������������������������wrapper���������������
+     * @Param: [wrapper, tableNames]
+     * @return: java.util.List<com.moral.api.entity.HistoryHourly>
+     * @Author: ���������
+     * @Date: 2021/9/23
+     */
+    private List<HistoryHourly> multiTableQuery(QueryWrapper<HistoryHourly> wrapper, List<String> tableNames) {
+        List<HistoryHourly> result = new ArrayList<>();
+        for (String tableName : tableNames) {
+            MybatisPlusConfig.tableName.set(tableName);
+            List<HistoryHourly> datas = historyHourlyMapper.selectList(wrapper);
+            result.addAll(datas);
+        }
+        MybatisPlusConfig.tableName.remove();
+        return result;
+    }
+
+    //���������������������������������������
+    private Double calculatedValue(Supplier<Stream<Map<String, Object>>> supplier, Sensor sensor, String type) {
+        String sensorCode = sensor.getCode();
+        Double upper = sensor.getUpper();
+        Double lower = sensor.getLower();
+        DoubleStream doubleStream = supplier.get()
+                .flatMapToDouble(v -> {
+                    Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class);
+                    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;
+                    }
+                    //������������������
+                    if ("avg".equals(type) && 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 = null;
+        if ("avg".equals(type)) {
+            average = doubleStream.average();
+        } else if ("min".equals(type)) {
+            average = doubleStream.min();
+        } else if ("max".equals(type)) {
+            average = doubleStream.max();
+        }
+
+        if (average.isPresent()) {
+            //���������������������
+            return AmendUtils.sciCal(average.getAsDouble(), 4);
+        }
+        return null;
+    }
 }

--
Gitblit v1.8.0