From 60477d72e953fc73744943db3c63f26d8eb94a19 Mon Sep 17 00:00:00 2001
From: kaiyu <404897439@qq.com>
Date: Mon, 22 Nov 2021 09:19:43 +0800
Subject: [PATCH] screen-api            增加获取柱状图表格数据的mock接口

---
 screen-api/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java |  119 +++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 67 insertions(+), 52 deletions(-)

diff --git a/screen-api/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java b/screen-api/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
index a67255f..1c1b45b 100644
--- a/screen-api/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
+++ b/screen-api/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
@@ -1,7 +1,6 @@
 package com.moral.api.service.impl;
 
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.moral.api.entity.Device;
 import com.moral.api.entity.Sensor;
@@ -15,20 +14,14 @@
 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.Arrays;
-import java.util.Collections;
-import java.util.Comparator;
+
 import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedHashMap;
+
 import java.util.List;
 import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
-import java.util.TreeMap;
-import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -42,7 +35,7 @@
 public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
 
     @Autowired
-    DeviceMapper deviceMapper;
+    private DeviceMapper deviceMapper;
 
     @Autowired
     private RedisTemplate redisTemplate;
@@ -50,14 +43,15 @@
     @Override
     public List<Device> getDevicesByMonitorPointId(Integer monitorPointId) {
         QueryWrapper<Device> wrapper = new QueryWrapper();
-        wrapper.eq("monitor_point_id",monitorPointId);
+        wrapper.eq("monitor_point_id", monitorPointId);
         wrapper.eq("is_delete", Constants.NOT_DELETE);
         return deviceMapper.selectList(wrapper);
     }
 
     @Override
     public Map<String, Object> getSensorsByMac(Map<String, Object> params) {
-        String[] macs = params.get("macs").toString().split(",");
+        //������mac
+        List<String> macs = (List<String>) params.remove("macs");
         List<Map<String, Object>> elementLists = new ArrayList<>();
 
         for (String mac : macs) {
@@ -86,73 +80,94 @@
     public List<Map<String, Object>> getTrendChartData(Map<String, Object> params) {
         Object type = params.get("type");
         //������mac
-        String[] macs = params.remove("macs").toString().split(",");
+        List<String> macs = (List<String>) params.remove("macs");
+
+        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
+        queryWrapper.select("mac", "name").in("mac", macs);
+        List<Device> devices = deviceMapper.selectList(queryWrapper);
+
         //������������
-        String[] times = params.remove("times").toString().split(",");
+        List<String> times = (List<String>) params.remove("times");
         //������code
         String sensorCode = params.get("sensorCode").toString();
         String end;
         String timeUnits;
         String dateFormat;
-
         //���������������,time=data
         List<Map<String, Object>> result = new ArrayList<>();
 
         for (String start : times) {
-            Map<String, Object> resultMap = new HashMap<>();
-
-            if ("day".equals(type)) {
+            if ("hour".equals(type)) {
                 end = DateUtils.getDateAddDay(start, 1);
-                timeUnits = "hourly";
-                dateFormat = "%k";
-            } else if ("month".equals(type)) {
+                String yearAndMonth = DateUtils.dateToDateString(DateUtils.getDate(start, DateUtils.yyyy_MM_dd_EN), DateUtils.yyyyMM_EN);
+                timeUnits = "hourly_" + yearAndMonth;
+                dateFormat = "%Y-%m-%d %H";
+            } else if ("day".equals(type)) {
                 end = DateUtils.getDateAddMonth(start, 1);
                 timeUnits = "daily";
-                dateFormat = "%e";
+                dateFormat = "%Y-%m-%d";
             } else {
                 end = DateUtils.getDateAddYear(start, 1);
                 timeUnits = "monthly";
-                dateFormat = "%c";
+                dateFormat = "%Y-%m";
             }
             params.put("timeUnits", timeUnits);
-            params.put("dateFormat", dateFormat);
             params.put("start", start);
             params.put("end", end);
             params.put("macs", macs);
+            params.put("dateFormat", dateFormat);
             //���������������������������������
             List<Map<String, Object>> list = deviceMapper.getTrendChartData(params);
 
-            //���time������
-            Map<String, List<Map<String, Object>>> data = list.parallelStream()
-                    .collect(Collectors.groupingBy(o -> o.get("time").toString()));
-
-            //TreeMap<String, List<Map<String, Object>>> data = new TreeMap<>(listMap);
-
-            for (Map.Entry<String, List<Map<String, Object>>> entry : data.entrySet()) {
-                List<Object> values = new ArrayList<>();
-                String time = entry.getKey();
-                List<Map<String, Object>> value = entry.getValue();
-                if (value.isEmpty()) {
-                    continue;
-                }
-                for (String mac : macs) {
-                    boolean flag = false;
-                    for (Map<String, Object> map : value) {
-                        if (mac.equals(map.get("mac"))) {
-                            Object o = map.get(sensorCode);
-                            values.add(o);
-                            flag = true;
-                            break;
+            for (String s : DateUtils.getTimeLag(start)) {
+                Map<String, Object> resultMap = new HashMap<>();
+                resultMap.put("time", s);
+                List<Map<String, Object>> deviceData = new ArrayList<>();
+                for (Device device : devices) {
+                    Map<String, Object> valueMap = new HashMap<>();
+                    valueMap.put("name", device.getName());
+                    valueMap.put("sensorValue", "");
+                    for (Map<String, Object> map : list) {
+                        Object time = map.get("time");
+                        Object sensorValue = map.get(sensorCode);
+                        Object mac = map.get("mac");
+                        if (s.equals(time) && device.getMac().equals(mac)) {
+                            valueMap.put("sensorValue", sensorValue);
                         }
                     }
-                    if (!flag) {
-                        values.add("");
-                    }
+                    deviceData.add(valueMap);
                 }
-                resultMap.put(time, values);
+                resultMap.put("deviceData", deviceData);
+                result.add(resultMap);
             }
-            result.add(resultMap);
         }
         return result;
     }
+
+    @Override
+    public Device getDeviceByMac(String mac) {
+        Map<String, Object> deviceMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac);
+        Device device = JSON.parseObject(JSON.toJSONString(deviceMap), Device.class);
+        //���map������organizationId���monitorPointId������versionId
+        Map<String, Object> organizationMap = (Map<String, Object>) deviceMap.get("organization");
+        Map<String, Object> monitorPointMap = (Map<String, Object>) deviceMap.get("monitorPoint");
+        Map<String, Object> versionMap = (Map<String, Object>) deviceMap.get("version");
+        device.setDeviceVersionId((Integer) versionMap.get("id"));
+        device.setOrganizationId((Integer) organizationMap.get("id"));
+        device.setMonitorPointId((Integer) monitorPointMap.get("id"));
+        //������������������������������������
+        if (ObjectUtils.isEmpty(device)) {
+            return getDeviceByMacFromDB(mac);
+        }
+        return device;
+    }
+
+    private Device getDeviceByMacFromDB(String mac) {
+        QueryWrapper<Device> wrapper = new QueryWrapper<>();
+        wrapper.eq("mac", mac);
+        wrapper.eq("is_delete", Constants.NOT_DELETE);
+        return deviceMapper.selectOne(wrapper);
+    }
+
+
 }

--
Gitblit v1.8.0