From 1b92eb61fee576a51c58e34ede99fc199dcb3f13 Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Wed, 19 Oct 2022 14:40:46 +0800
Subject: [PATCH] 上传浑南区空气质量日报功能

---
 screen-api/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java |  177 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 157 insertions(+), 20 deletions(-)

diff --git a/screen-api/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java b/screen-api/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java
index dac6205..4ddfbcb 100644
--- a/screen-api/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java
+++ b/screen-api/src/main/java/com/moral/api/service/impl/HistoryFiveMinutelyServiceImpl.java
@@ -1,32 +1,35 @@
 package com.moral.api.service.impl;
 
 import com.alibaba.fastjson.JSONArray;
-import com.moral.api.entity.Device;
-import com.moral.api.entity.HistoryFiveMinutely;
-import com.moral.api.entity.MonitorPoint;
+import com.alibaba.fastjson.JSON;
+import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
+import com.baomidou.mybatisplus.core.conditions.segments.NormalSegmentList;
+import com.moral.api.config.mybatis.MybatisPlusConfig;
+import com.moral.api.entity.*;
 import com.moral.api.mapper.HistoryFiveMinutelyMapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.moral.api.pojo.dto.Wind.WindData;
 import com.moral.api.pojo.dto.historyFiveMinutely.DeviceAndFiveMinuteDataDTO;
 import com.moral.api.pojo.form.device.MonitorPointQueryForm;
 import com.moral.api.pojo.form.historyFiveMinutely.QueryDeviceAndFiveMinuteDataForm;
+import com.moral.api.service.DeviceService;
 import com.moral.api.service.HistoryFiveMinutelyService;
 import com.moral.api.service.MonitorPointService;
+import com.moral.api.utils.UnitConvertUtils;
+import com.moral.constant.Constants;
 import com.moral.constant.RedisConstants;
+import com.moral.constant.SeparateTableType;
 import com.moral.util.DateUtils;
-
+import com.moral.util.MybatisPLUSUtils;
 import io.lettuce.core.GeoCoordinates;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.ObjectUtils;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * <p>
@@ -41,12 +44,12 @@
 
     @Autowired
     MonitorPointService monitorPointService;
-
     @Autowired
     RedisTemplate redisTemplate;
-
     @Autowired
-    private HistoryFiveMinutelyMapper historyFiveMinutelyMapper;
+    HistoryFiveMinutelyMapper historyFiveMinutelyMapper;
+    @Autowired
+    DeviceService deviceService;
 
     @Override
     public List<DeviceAndFiveMinuteDataDTO> queryDeviceAndFiveMinuteData(QueryDeviceAndFiveMinuteDataForm form) {
@@ -59,26 +62,58 @@
         List<Device> devices = new ArrayList<>();
         for (MonitorPoint monitorPoint : monitorPoints) {
             List<Device> monitorPointDevices = monitorPoint.getDevices();
-            if (!ObjectUtils.isEmpty(monitorPointDevices))
-                devices.addAll(monitorPointDevices);
+            if (!ObjectUtils.isEmpty(monitorPointDevices)) {
+                //������������������
+                for (Device device : monitorPointDevices) {
+                    if (!device.getState().equals(Constants.DEVICE_STATE_OFFLINE))
+                        devices.add(device);
+                }
+            }
+
         }
         //���������������������������������
         List<DeviceAndFiveMinuteDataDTO> dtos = new ArrayList<>();
         for (Device device : devices) {
             DeviceAndFiveMinuteDataDTO dto = new DeviceAndFiveMinuteDataDTO();
             String mac = device.getMac();
-            Map<String, Object> sensorValues = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DATA_FIVE_MINUTES, mac);
-            Map<String, Object> value = new HashMap<>();
+            Map<String, Object> sensorValues = queryLastDataByMac(mac);
+            String dbDataStr = JSON.toJSONString(sensorValues);
+            sensorValues = JSON.parseObject(dbDataStr, HashMap.class);
+            Map<String, Object> sensorValue = new HashMap<>();
             if (sensorValues != null && sensorValues.get(sensorCode) != null)
-                value.put(sensorCode, sensorValues.get(sensorCode));
+                sensorValue.put(sensorCode, sensorValues.get(sensorCode));
             else
-                value.put(sensorCode, null);
+                sensorValue.put(sensorCode, null);
             dto.setDevice(device);
-            dto.setSensorValue(value);
+            dto.setSensorValue(sensorValue);
             dtos.add(dto);
         }
         return dtos;
     }
+
+    @Override
+    public Map<String, Object> queryLastDataByMac(String mac) {
+        //������������������
+        Map<String, Object> sensorValues = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DATA_FIVE_MINUTES, mac);
+        //������������������������������������
+        if (sensorValues != null)
+            return sensorValues;
+        QueryWrapper<HistoryFiveMinutely> queryWrapper = new QueryWrapper<>();
+        queryWrapper.eq("mac", mac);
+        queryWrapper.orderByDesc("time");
+        queryWrapper.last("limit 0,1");
+        //���������������������
+        Date date = new Date();
+        List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(date, date, SeparateTableType.MONTH);
+        List<HistoryFiveMinutely> datas = multiTableQuery(queryWrapper, tableNames);
+        if (ObjectUtils.isEmpty(datas))
+            return null;
+        HistoryFiveMinutely historyFiveMinutely = datas.get(0);
+        sensorValues = JSON.parseObject(historyFiveMinutely.getValue(), Map.class);
+        sensorValues.put("dataTime", DateUtils.dateToDateString(historyFiveMinutely.getTime(), "yyyy-MM-dd HH:mm:ss"));
+        return sensorValues;
+    }
+
 
     @Override
     public List<Object> getAreaWindData(Map<String, Object> params) {
@@ -87,6 +122,71 @@
         //������������
         List<WindData> windData = historyFiveMinutelyMapper.getAreaWindData(params);
         return getWindData(windData);
+    }
+
+    //���������������������������
+    @Override
+    public Map<String, Object> getPopDataByMac(String mac) {
+        Map<String, Object> datas = queryLastDataByMac(mac);
+        //������������������
+        Map<String, Object> result = new HashMap<>();
+        //������������������code������
+        List<String> sensorCodes = Arrays.asList("a34004", "a34002", "a21005", "a21004", "a21026", "a05024", "a99054");
+        //������������������
+        Device device = deviceService.getDeviceUnitAlramInforByMac(mac);
+        //���������������������������
+        for (Sensor sensor : device.getVersion().getSensors()) {
+            if (!sensorCodes.contains(sensor.getCode()))
+                continue;
+            Object dataO = datas.get(sensor.getCode());
+            if (dataO != null) {
+                String data = String.valueOf(dataO);
+                //������������
+                if (!sensor.getShowUnitKey().equals(sensor.getUnitKey()) )
+                    data = UnitConvertUtils.calculate(data, sensor.getFormula());
+                //������������
+                data = data +" "+ sensor.getShowUnit();
+                result.put(sensor.getCode(), data);
+            }else{
+                result.put(sensor.getCode(),"-");
+            }
+        }
+        //������������������
+        result.put("name",device.getName());
+        //������������
+        result.put("dataTime",datas.get("dataTime"));
+        //������
+        return orderSixParam(result);
+    }
+
+    //���������������������������������
+    private Map<String,Object> orderSixParam(Map<String,Object> data){
+        LinkedHashMap result = new LinkedHashMap();
+        Object PM2_5 = data.get("a34004");
+        Object PM10 = data.get("a34002");
+        Object SO2 = data.get("a21026");
+        Object NO2 = data.get("a21004");
+        Object CO = data.get("a21005");
+        Object O3 = data.get("a05024");
+        Object name = data.get("name");
+        Object time = data.get("dataTime");
+        if(name!=null)
+            result.put("������",name);
+        if(PM2_5!=null)
+            result.put("PM2.5",PM2_5);
+        if(PM10!=null)
+            result.put("PM10",PM10);
+        if(SO2!=null)
+            result.put("���������������SO������",SO2);
+        if(NO2!=null)
+            result.put("���������������NO������",NO2);
+        if(CO!=null)
+            result.put("���������������CO���",CO);
+        if(O3!=null)
+            result.put("���������O������",O3);
+        if(time!=null)
+            result.put("������",time);
+        return result;
     }
 
 
@@ -272,4 +372,41 @@
         list.add(laLaMap);
         return list;
     }
+
+    /**
+     * @Description: ���������������������������mac���������
+     * @Param: [mac, startDate, endDate]
+     * @return: java.util.List<com.moral.api.entity.HistoryHourly>
+     * @Author: ���������
+     * @Date: 2021/9/23
+     */
+    public List<HistoryFiveMinutely> getValueByMacAndTime(String mac, Date startDate, Date endDate) {
+        QueryWrapper<HistoryFiveMinutely> wrapper = new QueryWrapper<>();
+        wrapper.eq("mac", mac);
+        wrapper.between("time", startDate, endDate);
+        List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH);
+        List<HistoryFiveMinutely> datas = multiTableQuery(wrapper, tableNames);
+        return datas;
+    }
+
+
+    /**
+     * @Description: ������������������������������������������������wrapper���������������
+     * @Param: [wrapper, tableNames]
+     * @return: java.util.List<com.moral.api.entity.HistoryHourly>
+     * @Author: ���������
+     * @Date: 2021/9/23
+     */
+    private List<HistoryFiveMinutely> multiTableQuery(QueryWrapper<HistoryFiveMinutely> wrapper, List<String> tableNames) {
+        List<HistoryFiveMinutely> result = new ArrayList<>();
+        for (String tableName : tableNames) {
+            MybatisPlusConfig.tableName.set(tableName);
+            List<HistoryFiveMinutely> datas = historyFiveMinutelyMapper.selectList(wrapper);
+            result.addAll(datas);
+        }
+        MybatisPlusConfig.tableName.remove();
+        return result;
+    }
+
+
 }

--
Gitblit v1.8.0