From 28923b3d67c58b032bc4ff128aa1d5e62d73dba1 Mon Sep 17 00:00:00 2001
From: jinpengyong <jpy123456>
Date: Fri, 02 Feb 2024 10:07:58 +0800
Subject: [PATCH] fix:提交实时在线率接口

---
 screen-api/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java |  925 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 908 insertions(+), 17 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 f5994b4..c4f0ede 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,36 +1,37 @@
 package com.moral.api.service.impl;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.moral.api.entity.Device;
-import com.moral.api.entity.OrganizationUnitAlarm;
-import com.moral.api.entity.Sensor;
-import com.moral.api.entity.Version;
+import com.moral.api.config.Interceptor.UserHelper;
+import com.moral.api.dto.OnlineRatePageCond;
+import com.moral.api.entity.*;
 import com.moral.api.mapper.DeviceMapper;
+import com.moral.api.mapper.HistoryFiveMinutelyMapper;
+import com.moral.api.mapper.HistoryHourlyMapper;
 import com.moral.api.mapper.OrganizationUnitAlarmMapper;
-import com.moral.api.mapper.UnitConversionMapper;
+import com.moral.api.pojo.dust.OnlineRateLogsForm;
+import com.moral.api.pojo.vo.device.AppDeviceVo;
+import com.moral.api.pojo.vo.user.QxUser;
 import com.moral.api.service.DeviceService;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.moral.api.service.HistoryDailyService;
+import com.moral.api.service.HistoryHourlyService;
 import com.moral.api.service.SensorService;
 import com.moral.api.service.UnitConversionService;
+import com.moral.api.vo.OnlineRateVo;
 import com.moral.constant.Constants;
 import com.moral.constant.RedisConstants;
+import com.moral.constant.SeparateTableType;
 import com.moral.util.DateUtils;
-
-import org.apache.kafka.streams.state.internals.metrics.Sensors;
+import com.moral.util.MybatisPLUSUtils;
 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.HashMap;
-
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -58,11 +59,33 @@
     @Autowired
     UnitConversionService unitConversionService;
 
+    @Autowired
+    HistoryHourlyMapper historyHourlyMapper;
+
+    @Autowired
+    HistoryHourlyService historyHourlyService;
+
+    @Autowired
+    HistoryDailyService historyDailyService;
+
+    @Autowired
+    HistoryFiveMinutelyMapper historyFiveMinutelyMapper;
+
     @Override
     public List<Device> getDevicesByMonitorPointId(Integer monitorPointId) {
         QueryWrapper<Device> wrapper = new QueryWrapper();
         wrapper.eq("monitor_point_id", monitorPointId);
         wrapper.eq("is_delete", Constants.NOT_DELETE);
+        wrapper.orderByAsc("dev_num");
+        return deviceMapper.selectList(wrapper);
+    }
+
+    @Override
+    public List<Device> getDevicesByMonitorPointId(List<Integer> monitorPointId) {
+        QueryWrapper<Device> wrapper = new QueryWrapper();
+        wrapper.in("monitor_point_id", monitorPointId);
+        wrapper.eq("is_delete", Constants.NOT_DELETE);
+        wrapper.orderByAsc("dev_num");
         return deviceMapper.selectList(wrapper);
     }
 
@@ -166,7 +189,7 @@
                 for (Device device : devices) {
                     Map<String, Object> valueMap = new HashMap<>();
                     valueMap.put("name", device.getName());
-                    valueMap.put("sensorValue", "");
+                    valueMap.put("sensorValue", "0.0");
                     for (Map<String, Object> map : list) {
                         Object time = map.get("time");
                         Object sensorValue = map.get(sensorCode);
@@ -181,6 +204,246 @@
                 result.add(resultMap);
             }
         }
+        return result;
+    }
+
+    @Override
+    public List<Map<String, Object>> getTrendChartDataV2(Map<String, Object> params) {
+        Object type = params.get("type");
+        //������mac
+        List<String> macs = (List<String>) params.remove("macs");
+
+        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
+        queryWrapper.select("mac", "name").in("mac", macs);
+        queryWrapper.orderByAsc("dev_num","name","id");
+        List<Device> devices = deviceMapper.selectList(queryWrapper);
+        Map<String,Object> devicesInfo = new HashMap<>();
+        for (Device device:devices) {
+            devicesInfo.put(device.getMac(),device.getName());
+        }
+
+        //������������
+        List<String> times = (List<String>) params.remove("times");
+        String startTime = times.get(0);
+        String endTime = times.get(1);
+        //������code
+        String sensorCode = params.get("sensorCode").toString();
+        String end;
+        String timeUnits;
+        String dateFormat;
+        //���������������,time=data
+        List<Map<String, Object>> result = new ArrayList<>();
+        List<Map<String, Object>> list = new ArrayList<>();
+        if ("hour".equals(type)) {
+            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN);
+            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN);
+            List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH);
+            for (String mac:macs) {
+                Map<String, Object> mapParams = new HashMap<>();
+                mapParams.put("startDate",startDate);
+                mapParams.put("endDate",endDate);
+                mapParams.put("mac",mac);
+                List<HistoryHourly> historyHourlies = multiTableQuery(mapParams, tableNames);
+                historyHourlies = historyHourlies.stream().distinct().collect(Collectors.toList());
+                List<HistoryHourly> distinctHistoryHourlies = new ArrayList<>();
+                Map<String, Object> disMap = new HashMap<>();
+                for (HistoryHourly historyHourly:historyHourlies) {
+                    Date time = historyHourly.getTime();
+                    String timeStr = DateUtils.dateToDateString(time,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
+                    if (!disMap.containsKey(timeStr)){
+                        distinctHistoryHourlies.add(historyHourly);
+                        disMap.put(timeStr,true);
+                    }
+                }
+                for (HistoryHourly historyHourly:distinctHistoryHourlies) {
+                    Map<String,Object> historyHourlyMap = new HashMap<>();
+                    historyHourlyMap.put("mac",mac);
+                    JSONObject  value= JSONObject.parseObject(historyHourly.getValue());
+                    Double sensorValue = Objects.nonNull(value)&&Objects.nonNull(value.get(sensorCode))?Double.parseDouble(value.get(sensorCode).toString()):0d;
+                    historyHourlyMap.put(sensorCode,sensorValue);
+                    Date time = historyHourly.getTime();
+                    String timeStr = DateUtils.dateToDateString(time, DateUtils.yyyy_MM_dd_HH_EN);
+                    historyHourlyMap.put("time",timeStr);
+                    list.add(historyHourlyMap);
+                }
+            }
+            Date middleDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN);
+            while (DateUtils.compareDateStr(endTime,DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.yyyy_MM_dd_HH_EN)<=0){
+                for (String mac:macs) {
+                    boolean flag = true;
+                    for (Map map:list) {
+                        if (map.get("time").equals(DateUtils.dateToDateString(middleDate, DateUtils.yyyy_MM_dd_HH_EN)) && map.get("mac").toString().equals(mac.toString())){
+                            flag = false;
+                        }
+                    }
+                    if (flag){
+                        Map<String,Object> historyHourlyMap = new HashMap<>();
+                        historyHourlyMap.put("mac",mac);
+                        historyHourlyMap.put(sensorCode,"0.0");
+                        historyHourlyMap.put("time",DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN));
+                        list.add(historyHourlyMap);
+                    }
+                }
+                middleDate = DateUtils.addHours(middleDate,1);
+            }
+        } else if ("day".equals(type)) {
+            end = DateUtils.getDateAddDay(endTime,1);
+            timeUnits = "daily";
+            dateFormat = "%Y-%m-%d";
+            params.put("timeUnits", timeUnits);
+            params.put("start", startTime);
+            params.put("end", end);
+            params.put("macs", macs);
+            params.put("dateFormat", dateFormat);
+            //���������������������������������
+            list = deviceMapper.getTrendChartData(params);
+            Date middleDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN);
+            while (DateUtils.compareDateStr(endTime,DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_EN),DateUtils.yyyy_MM_dd_EN)<=0){
+                for (String mac:macs) {
+                    boolean flag = true;
+                    for (Map map:list) {
+                        if (map.get("time").equals(DateUtils.dateToDateString(middleDate, DateUtils.yyyy_MM_dd_EN)) && map.get("mac").toString().equals(mac.toString())){
+                            flag = false;
+                        }
+                    }
+                    if (flag){
+                        Map<String,Object> historyDailyMap = new HashMap<>();
+                        historyDailyMap.put("mac",mac);
+                        historyDailyMap.put(sensorCode,"0.0");
+                        historyDailyMap.put("time",DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_EN));
+                        list.add(historyDailyMap);
+                    }
+                }
+                middleDate = DateUtils.addDays(middleDate,1);
+            }
+        } else if ("month".equals(type)){
+            end = DateUtils.getDateAddMonth(endTime, 1);
+            timeUnits = "monthly";
+            dateFormat = "%Y-%m";
+            params.put("timeUnits", timeUnits);
+            params.put("start", startTime);
+            params.put("end", end);
+            params.put("macs", macs);
+            params.put("dateFormat", dateFormat);
+            //���������������������������������
+            list = deviceMapper.getTrendChartData(params);
+            Date middleDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_EN);
+            while (DateUtils.compareDateStr(endTime,DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN),DateUtils.yyyy_MM_EN)<=0){
+                for (String mac:macs) {
+                    boolean flag = true;
+                    for (Map map:list) {
+                        if (map.get("time").equals(DateUtils.dateToDateString(middleDate, DateUtils.yyyy_MM_EN)) && map.get("mac").toString().equals(mac.toString())){
+                            flag = false;
+                        }
+                    }
+                    if (flag){
+                        Map<String,Object> historyMonthlyMap = new HashMap<>();
+                        historyMonthlyMap.put("mac",mac);
+                        historyMonthlyMap.put(sensorCode,"0.0");
+                        historyMonthlyMap.put("time",DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN));
+                        list.add(historyMonthlyMap);
+                    }
+                }
+                middleDate = DateUtils.addMonths(middleDate,1);
+            }
+        }else {
+            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
+            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
+            List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH);
+            for (String mac:macs) {
+                Map<String, Object> mapParams = new HashMap<>();
+                mapParams.put("startDate",startDate);
+                mapParams.put("endDate",endDate);
+                mapParams.put("mac",mac);
+                List<HistoryFiveMinutely> HistoryFiveMinutelys = FiveMinuteTableQuery(mapParams, tableNames);
+                HistoryFiveMinutelys = HistoryFiveMinutelys.stream().distinct().collect(Collectors.toList());
+                List<HistoryFiveMinutely> distinctHistoryHourlies = new ArrayList<>();
+                Map<String, Object> disMap = new HashMap<>();
+                for (HistoryFiveMinutely historyFiveMinutely:HistoryFiveMinutelys) {
+                    Date time = historyFiveMinutely.getTime();
+                    String timeStr = DateUtils.dateToDateString(time,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
+                    if (!disMap.containsKey(timeStr)){
+                        distinctHistoryHourlies.add(historyFiveMinutely);
+                        disMap.put(timeStr,true);
+                    }
+                }
+                for (HistoryFiveMinutely historyFiveMinutely:distinctHistoryHourlies) {
+                    Map<String,Object> historyHourlyMap = new HashMap<>();
+                    historyHourlyMap.put("mac",mac);
+                    JSONObject value = JSONObject.parseObject(historyFiveMinutely.getValue());
+                    if (value.get(sensorCode)==null){
+                        historyHourlyMap.put(sensorCode,0.0);
+                    }else {
+                        Double sensorValue = Double.parseDouble(value.get(sensorCode).toString());
+                        historyHourlyMap.put(sensorCode,sensorValue);
+                    }
+//                    Double sensorValue = Double.parseDouble(value.get(sensorCode).toString());
+//                    historyHourlyMap.put(sensorCode,sensorValue);
+                    Date time = historyFiveMinutely.getTime();
+                    String timeStr = DateUtils.dateToDateString(time, DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
+                    historyHourlyMap.put("time",timeStr);
+                    list.add(historyHourlyMap);
+                }
+            }
+        }
+        for (Map map:list) {
+            String time = map.get("time").toString();
+            Map<String,Object> deviceMap = new HashMap<>();
+            deviceMap.put("name",devicesInfo.get(map.get("mac")));
+            deviceMap.put("sensorValue",map.get(sensorCode));
+            if (result.size()>0){
+                boolean flag = true;
+                for (Map resultMap:result) {
+                    if (resultMap.get("time").toString().equals(time)){
+                        List<Map<String,Object>> deviceData = new ArrayList<>();
+                        deviceData = (List<Map<String,Object>>)resultMap.get("deviceData");
+                        deviceData.add(deviceMap);
+                        Collections.sort(deviceData, (map1,map2) -> {
+                            String name1 = map1.get("name").toString();//name1���������list������������������������
+                            String name2 = map2.get("name").toString(); //name1���������list���������������������������name
+                            return name1.compareTo(name2);
+                        });
+                        resultMap.put("deviceData",deviceData);
+                        //result.add(resultMap);
+                        flag = false;
+                        break;
+                    }
+                }
+                if (flag){
+                    List<Map<String,Object>> deviceData = new ArrayList<>();
+                    deviceData.add(deviceMap);
+                    Collections.sort(deviceData, (map1,map2) -> {
+                        String name1 = map1.get("name").toString();//name1���������list������������������������
+                        String name2 = map2.get("name").toString(); //name1���������list���������������������������name
+                        return name1.compareTo(name2);
+                    });
+                    Map<String,Object> resultMap = new HashMap<>();
+                    resultMap.put("deviceData",deviceData);
+                    resultMap.put("time",time);
+                    result.add(resultMap);
+                }
+            }else {
+                List<Map<String,Object>> deviceData = new ArrayList<>();
+                deviceData.add(deviceMap);
+                Collections.sort(deviceData, (map1,map2) -> {
+                    String name1 = map1.get("name").toString();//name1���������list������������������������
+                    String name2 = map2.get("name").toString(); //name1���������list���������������������������name
+                    return name1.compareTo(name2);
+                });
+                Map<String,Object> resultMap = new HashMap<>();
+                resultMap.put("deviceData",deviceData);
+                resultMap.put("time",time);
+                result.add(resultMap);
+            }
+        }
+        Collections.sort(result, new Comparator<Map<String, Object>>() {
+            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
+                String id1 = (String) o1.get("time");
+                String id2 = (String) o2.get("time");
+                return id1.compareTo(id2);
+            }
+
+        });
         return result;
     }
 
@@ -242,6 +505,550 @@
         return device;
     }
 
+    /**
+     * ������������
+     *
+     * @param mac
+     * @return
+     */
+    @Override
+    public List<AppDeviceVo> getFuzzySearch(String mac) {
+        QxUser user = UserHelper.getCurrentUser();
+        Integer organizationId = user.getOrganizationId();
+//        Integer organizationId = 24;
+        List<AppDeviceVo> devices;
+        if (organizationId!=24){
+             devices = deviceMapper.getDevices(mac,organizationId);
+        }else {
+             devices = deviceMapper.getDevices(mac,null);
+        }
+        return devices;
+    }
+
+
+    /**
+     * ������������������������
+     *
+     * @param id
+     * @return
+     */
+    @Override
+    public List<Device> getOrganizationDevice(Integer id) {
+        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
+        wrapper.eq(Device::getMonitorPointId,id);
+        wrapper.eq(Device::getIsDelete,Constants.NOT_DELETE);
+        wrapper.orderByAsc(Device::getCreateTime);
+        List<Device> devices = deviceMapper.selectList(wrapper);
+        return devices;
+    }
+
+    /**
+     * ���������
+     * @param onlineRatePageCond
+     * @return
+     */
+    @Override
+    public List<OnlineRateVo> getPage(OnlineRatePageCond onlineRatePageCond) {
+        int nh = 1000 * 60 * 60;
+        String startTime = onlineRatePageCond.getStartTime();
+        String endTime = onlineRatePageCond.getEndTime();
+        Date start = DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN);
+        Date end = DateUtils.getDate(endTime, DateUtils.yyyy_MM_dd_HH_EN);
+        long diff = end.getTime() - start.getTime(); //������������
+        long hour = (diff / nh)+1; //������������������������
+
+        Map<String, List<OnlineRateVo>> collectList=new HashMap<>();
+        List<OnlineRateVo> OnlineRateVoList = deviceMapper.getLists(onlineRatePageCond.getOrganizationId(),onlineRatePageCond.getState());
+        if (!ObjectUtils.isEmpty(OnlineRateVoList)){
+
+            for (OnlineRateVo onlineRateVo : OnlineRateVoList) {
+                List<HistoryHourly> valueByMacAndTime = historyHourlyService.getValueByMacAndTime(onlineRateVo.getMac(), start, end);
+                ArrayList<Date> dates = new ArrayList<>();
+                //������������
+                ArrayList<Date> dates1 = new ArrayList<>();
+                //������������
+                ArrayList<Date> dates2 = new ArrayList<>();
+                ArrayList<Integer> list = new ArrayList<>();
+                Date start1 = DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN);
+                if (!ObjectUtils.isEmpty(valueByMacAndTime)){
+                    //������������
+                    ArrayList<HistoryHourly> collects = valueByMacAndTime.stream().collect(
+                            Collectors.collectingAndThen(Collectors.toCollection(
+                                    () -> new TreeSet<>(Comparator.comparing(p -> p.getTime()))), ArrayList::new)
+                    );
+
+                    for (HistoryHourly historyHourly : collects) {
+                        dates.add(historyHourly.getTime());
+                    }
+                    boolean flag= true;
+                    int i = 0;
+                    while (DateUtils.isTimeBeforE(end,start1)){
+                        if (dates.contains(start1)){
+                            if (flag){
+                                list.add(i);
+                            }else {
+                                i++;
+                                list.add(i);
+                                dates2.add(start1);
+                                flag= true;
+                            }
+                        }else {
+                            if (flag){
+                                i++;
+                                list.add(i);
+                                dates1.add(start1);
+                                flag=false;
+                            }else {
+                                list.add(i);
+                            }
+                        }
+                        start1 = DateUtils.getDateAddHour(start1,1);
+                    }
+                    if (!ObjectUtils.isEmpty(dates1)){
+                        if (dates1.size()>dates2.size()){
+                            onlineRateVo.setEndTime(DateUtils.dateToDateString(dates1.get(dates1.size()-1)));
+                            onlineRateVo.setStartTime("-");
+                            onlineRateVo.setHourState("0");
+                        }else {
+                            onlineRateVo.setStartTime(DateUtils.dateToDateString(dates2.get(dates2.size()-1)));
+                            onlineRateVo.setEndTime(DateUtils.dateToDateString(dates1.get(dates1.size()-1)));
+                            onlineRateVo.setHourState("1");
+                        }
+                    }else {
+                        onlineRateVo.setHourState("1");
+                    }
+//                    if (!ObjectUtils.isEmpty(dates2)){
+//                        onlineRateVo.setStartTime(DateUtils.dateToDateString(dates2.get(dates2.size()-1)));
+//                    }
+//                    if (!ObjectUtils.isEmpty(dates1)){
+//                        onlineRateVo.setEndTime(DateUtils.dateToDateString(dates1.get(dates1.size()-1)));
+//                    }
+                    onlineRateVo.setNum(dates1.size()+"");
+                    onlineRateVo.setOnlineTime(collects.size());
+                    double number = (double) collects.size() / hour * 100;
+                    String result = String.format("%.2f", number);
+                    onlineRateVo.setOnlineRate(result);
+                }
+            }
+
+        }
+     /*   Page<OnlineRateVo> page = deviceMapper.getPage(onlineRatePageCond.getPage().convertPage(), onlineRatePageCond.getOrganizationId());
+        if(CollectionUtils.isNotEmpty(page.getRecords())){
+            page.getRecords().forEach(it->{
+                Date start1 = DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
+                List<HistoryHourly> valueByMacAndTime = historyHourlyService.getValueByMacAndTime(it.getMac(), start, end);
+                ArrayList<Date> dates = new ArrayList<>();
+                ArrayList<Date> dates1 = new ArrayList<>();
+                ArrayList<Date> dates2 = new ArrayList<>();
+
+                ArrayList<Integer> list = new ArrayList<>();
+                if (!ObjectUtils.isEmpty(valueByMacAndTime)){
+                    for (HistoryHourly historyHourly : valueByMacAndTime) {
+                        dates.add(historyHourly.getTime());
+                    }
+                    boolean flag= true;
+                    int i = 0;
+                    while (DateUtils.isTimeBeforE(end,start1)){
+                    if (dates.contains(start1)){
+                        if (flag){
+                          list.add(i);
+                        }else {
+                          i++;
+                          list.add(i);
+                          dates2.add(start1);
+                          flag= true;
+                        }
+                    }else {
+                        if (flag){
+                            i++;
+                            list.add(i);
+                            dates1.add(start1);
+                            flag=false;
+                        }else {
+                          list.add(i);
+                        }
+                    }
+                        start1 = DateUtils.getDateAddHour(start1,1);
+                    }
+                    if (!ObjectUtils.isEmpty(dates2)){
+                        it.setStartTime(DateUtils.dateToDateString(dates2.get(dates2.size()-1)));
+                    }
+                    if (!ObjectUtils.isEmpty(dates1)){
+                        it.setEndTime(DateUtils.dateToDateString(dates1.get(dates1.size()-1)));
+                    }
+//                    it.setEndTime(DateUtils.dateToDateString(dates1.get(dates1.size()-1)));
+                    it.setNum(dates1.size()+"");
+                    it.setOnlineTime(valueByMacAndTime.size());
+                    double number = (double) valueByMacAndTime.size() / hour * 100;
+                    String result = String.format("%.2f", number);
+                    it.setOnlineRate(result+"%");
+                }
+            });
+        }*/
+        if (!ObjectUtils.isEmpty(OnlineRateVoList)){
+            collectList = OnlineRateVoList.stream().collect(Collectors.groupingBy(OnlineRateVo::getHourState));
+        }
+        return ObjectUtils.isEmpty(onlineRatePageCond.getHourState())?OnlineRateVoList:collectList.get(onlineRatePageCond.getHourState());
+    }
+
+    /**
+     * ���������������������
+     *
+     * @param mac
+     * @param startTime
+     * @param endTime
+     * @param type
+     * @return
+     */
+    @Override
+    public Map<String,Object> detail(String mac, String startTime, String endTime, String type) {
+        int nh = 1000 * 60 * 60;
+        long nd = 1000 * 24 * 60 * 60;
+        long nm = 1000 * 60;
+        HashMap<String, Object> rsMap = new HashMap<>();
+        Map<String, Object> map = new TreeMap<>(
+                new Comparator<String>() {
+                    @Override
+                    public int compare(String o1, String o2) {
+                        return o1.compareTo(o2);
+                    }
+                }
+        );
+//        HashMap<String, Object> map = new HashMap<>();
+        //������������
+        ArrayList<Date> EndDates = new ArrayList<>();
+        //������������
+        ArrayList<Date> StartDates = new ArrayList<>();
+        ArrayList<Date> dates = new ArrayList<>();
+
+        if (type.equals("hour")){
+            Date start = DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN);
+            Date end = DateUtils.getDate(endTime, DateUtils.yyyy_MM_dd_HH_EN);
+            List<HistoryHourly> valueByMacAndTime = historyHourlyService.getValueByMacAndTime(mac, start, end);
+
+            if (!ObjectUtils.isEmpty(valueByMacAndTime)){
+
+                //������������
+                ArrayList<HistoryHourly> collect = valueByMacAndTime.stream().collect(
+                        Collectors.collectingAndThen(Collectors.toCollection(
+                                () -> new TreeSet<>(Comparator.comparing(p -> p.getTime()))), ArrayList::new)
+                );
+                for (HistoryHourly historyHourly : collect) {
+                    map.put(DateUtils.dateToDateString(historyHourly.getTime()),1);
+                    dates.add(historyHourly.getTime());
+                }
+                boolean flag= true;
+                while (DateUtils.isTimeBeforE(end,start)){
+                    if (dates.contains(start)){
+                        if (!flag){
+                            StartDates.add(start);
+                            flag= true;
+                        }
+                    }else {
+                        if (flag){
+                            EndDates.add(start);
+                            flag=false;
+                        }
+                        map.put(DateUtils.dateToDateString(start),0);
+                    }
+                    start = DateUtils.getDateAddHour(start,1);
+                }
+            }
+            //������
+            ArrayList<OnlineRateLogsForm> OrfList = new ArrayList<>();
+            if (!ObjectUtils.isEmpty(EndDates)){
+                for (int i = 0; i < EndDates.size(); i++) {
+                    OnlineRateLogsForm onlineRateLogsForm = new OnlineRateLogsForm();
+                    onlineRateLogsForm.setEndTime(DateUtils.dateToDateString(EndDates.get(i)));
+//                    onlineRateLogsForm.setStartTime(ObjectUtils.isEmpty(StartDates)?"-":DateUtils.dateToDateString(StartDates.get(i)));
+                    if (ObjectUtils.isEmpty(StartDates)){
+                        onlineRateLogsForm.setStartTime("-");
+                        onlineRateLogsForm.setMun("-");
+                    }else {
+                        if (EndDates.size()>StartDates.size()&& i==EndDates.size()-1){
+                            onlineRateLogsForm.setStartTime("-");
+                            onlineRateLogsForm.setMun("-");
+                        }else {
+                            onlineRateLogsForm.setStartTime(DateUtils.dateToDateString(StartDates.get(i)));
+                            long l = EndDates.get(i).getTime() - StartDates.get(i).getTime();
+                            onlineRateLogsForm.setMun(Long.toString(Math.abs(l/nh)));
+
+                        }
+                    }
+
+                    onlineRateLogsForm.setDate("������");
+                    OrfList.add(onlineRateLogsForm);
+                }
+            }
+            //���������
+            ArrayList<String> list1 = new ArrayList<>();
+            ArrayList<Integer> list2 = new ArrayList<>();
+            Set<String> strings = map.keySet();
+            for (String string : strings) {
+                list1.add(string);
+                list2.add(Integer.parseInt(map.get(string).toString()));
+            }
+            rsMap.put("lineChart1",list1);
+            rsMap.put("lineChart2",list2);
+            rsMap.put("tabulation",OrfList);
+            //���������������������
+            int endNumber = map.size() - dates.size();
+            double number = (double) dates.size() / map.size() * 100;
+            String result = String.format("%.2f", number);
+            rsMap.put("pieChart1",endNumber);
+            rsMap.put("pieChart2",dates.size());
+//            rsMap.put("code","���"+map.size()+"���������"+"������"+dates.size()+"���������"+"���������"+(result.equals("NaN")?"0":result));
+            rsMap.put("sum",map.size());
+
+        } else if (type.equals("day")){
+            Date start = DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_EN);
+            Date end = DateUtils.getDate(endTime, DateUtils.yyyy_MM_dd_EN);
+            List<HistoryDaily> historyDailys = historyDailyService.getHistoryDailyByMacAndTimeSlot(mac, start, end);
+            if (!ObjectUtils.isEmpty(historyDailys)){
+                //������������
+                ArrayList<HistoryDaily> collects = historyDailys.stream().collect(
+                        Collectors.collectingAndThen(Collectors.toCollection(
+                                () -> new TreeSet<>(Comparator.comparing(p -> p.getTime()))), ArrayList::new)
+                );
+                for (HistoryDaily historyDaily : collects) {
+                    map.put(DateUtils.dateToDateString(historyDaily.getTime()),1);
+                    dates.add(historyDaily.getTime());
+                }
+                boolean flag= true;
+                while (DateUtils.isTimeBeforE(end,start)){
+                    if (dates.contains(start)){
+                        if (!flag){
+                            StartDates.add(start);
+                            flag= true;
+                        }
+                    }else {
+                        if (flag){
+                            EndDates.add(start);
+                            flag=false;
+                        }
+                        map.put(DateUtils.dateToDateString(start),0);
+                    }
+//                    start = DateUtils.getDateAddHour(start,24);
+                    start = DateUtils.getDateOfDay(start,1);
+                }
+            }
+            //������
+            ArrayList<OnlineRateLogsForm> OrfList = new ArrayList<>();
+            if (!ObjectUtils.isEmpty(EndDates)){
+                for (int i = 0; i < EndDates.size(); i++) {
+                    OnlineRateLogsForm onlineRateLogsForm = new OnlineRateLogsForm();
+                    onlineRateLogsForm.setEndTime(DateUtils.dateToDateString(EndDates.get(i)));
+//                    onlineRateLogsForm.setStartTime(ObjectUtils.isEmpty(StartDates)?"-":DateUtils.dateToDateString(StartDates.get(i)));
+                    if (ObjectUtils.isEmpty(StartDates)){
+                        onlineRateLogsForm.setStartTime("-");
+                        onlineRateLogsForm.setMun("-");
+                    }else {
+                        if (EndDates.size()>StartDates.size()&& i==EndDates.size()-1){
+                            onlineRateLogsForm.setStartTime("-");
+                            onlineRateLogsForm.setMun("-");
+                        }else {
+                            onlineRateLogsForm.setStartTime(DateUtils.dateToDateString(StartDates.get(i)));
+                            long l = EndDates.get(i).getTime() - StartDates.get(i).getTime();
+                            onlineRateLogsForm.setMun(Long.toString(Math.abs(l/nd)));
+                        }
+                    }
+//                    if (!ObjectUtils.isEmpty(StartDates)){
+//                        long l = EndDates.get(i).getTime() - StartDates.get(i).getTime();
+//                        onlineRateLogsForm.setMun(Long.toString(l/nd));
+//                    }else {
+//                        onlineRateLogsForm.setMun("-");
+//                    }
+                    onlineRateLogsForm.setDate("���");
+                    OrfList.add(onlineRateLogsForm);
+                }
+            }
+            //���������
+            ArrayList<String> list1 = new ArrayList<>();
+            ArrayList<Integer> list2 = new ArrayList<>();
+            Set<String> strings = map.keySet();
+            for (String string : strings) {
+                list1.add(string);
+                list2.add(Integer.parseInt(map.get(string).toString()));
+            }
+
+
+            rsMap.put("lineChart1",list1);
+            rsMap.put("lineChart2",list2);
+            rsMap.put("tabulation",OrfList);
+            //���������������
+            int endNumber = map.size() - dates.size();
+            double number = (double) dates.size() / map.size() * 100;
+            String result = String.format("%.2f", number);
+            rsMap.put("pieChart1",endNumber);
+            rsMap.put("pieChart2",dates.size());
+//            rsMap.put("code","���"+map.size()+"������"+"������"+dates.size()+"������"+"���������"+(result.equals("NaN")?"0":result));
+            rsMap.put("sum",map.size());
+        }else {
+            Date start = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_mm_EN);
+            Date end = DateUtils.getDate(endTime, DateUtils.yyyy_MM_dd_HH_mm_EN);
+            List<HistoryMinutely> historyMinutelys = historyHourlyService.getHistoryMinutely(mac, start, end);
+            if (!ObjectUtils.isEmpty(historyMinutelys)){
+                //������������
+                ArrayList<HistoryMinutely> collects = historyMinutelys.stream().collect(
+                        Collectors.collectingAndThen(Collectors.toCollection(
+                                () -> new TreeSet<>(Comparator.comparing(p -> p.getTime()))), ArrayList::new)
+                );
+                for (HistoryMinutely historyMinutely : collects) {
+                    map.put(DateUtils.dateToDateString(historyMinutely.getTime()),1);
+                    dates.add(historyMinutely.getTime());
+                }
+                boolean flag= true;
+                while (DateUtils.isTimeBeforE(end,start)){
+                    if (dates.contains(start)){
+                        if (!flag){
+                            StartDates.add(start);
+                            flag= true;
+                        }
+                    }else {
+                        if (flag){
+                            EndDates.add(start);
+                            flag=false;
+                        }
+                        map.put(DateUtils.dateToDateString(start),0);
+                    }
+//                    start = DateUtils.getDateAddHour(start,24);
+                    start = DateUtils.getDateOfMin(start,1);
+                }
+            }
+            //������
+            ArrayList<OnlineRateLogsForm> OrfList = new ArrayList<>();
+            if (!ObjectUtils.isEmpty(EndDates)){
+                for (int i = 0; i < EndDates.size(); i++) {
+                    OnlineRateLogsForm onlineRateLogsForm = new OnlineRateLogsForm();
+                    onlineRateLogsForm.setEndTime(DateUtils.dateToDateString(EndDates.get(i)));
+//                    onlineRateLogsForm.setStartTime(ObjectUtils.isEmpty(StartDates)?"-":DateUtils.dateToDateString(StartDates.get(i)));
+                    if (ObjectUtils.isEmpty(StartDates)){
+                        onlineRateLogsForm.setStartTime("-");
+                        onlineRateLogsForm.setMun("-");
+                    }else {
+                        if (EndDates.size()>StartDates.size()&& i==EndDates.size()-1){
+                            onlineRateLogsForm.setStartTime("-");
+                            onlineRateLogsForm.setMun("-");
+                        }else {
+                            onlineRateLogsForm.setStartTime(DateUtils.dateToDateString(StartDates.get(i)));
+                            long l = EndDates.get(i).getTime() - StartDates.get(i).getTime();
+                            onlineRateLogsForm.setMun(Long.toString(Math.abs(l/nm)));
+                        }
+                    }
+//                    if (!ObjectUtils.isEmpty(StartDates)){
+//                        long l = EndDates.get(i).getTime() - StartDates.get(i).getTime();
+//                        onlineRateLogsForm.setMun(Long.toString(l/nm));
+//                    }else {
+//                        onlineRateLogsForm.setMun("-");
+//                    }
+                    onlineRateLogsForm.setDate("������");
+                    OrfList.add(onlineRateLogsForm);
+                }
+            }
+            //���������
+            ArrayList<String> list1 = new ArrayList<>();
+            ArrayList<Integer> list2 = new ArrayList<>();
+            Set<String> strings = map.keySet();
+            for (String string : strings) {
+                list1.add(string);
+                list2.add(Integer.parseInt(map.get(string).toString()));
+            }
+            rsMap.put("lineChart1",list1);
+            rsMap.put("lineChart2",list2);
+            rsMap.put("tabulation",OrfList);
+            //���������������
+            int endNumber = map.size() - dates.size();
+//            double number = (double) dates.size() / map.size() * 100;
+//            String result = String.format("%.2f", number);
+            rsMap.put("pieChart1",endNumber);
+            rsMap.put("pieChart2",dates.size());
+//            rsMap.put("code","���"+map.size()+"���������"+"������"+dates.size()+"���������"+"���������"+(result.equals("NaN")?"0":result));
+            rsMap.put("sum",map.size());
+        }
+        return rsMap;
+    }
+
+    @Override
+    public Map<String,Object> getStart(Integer organizationId) {
+        HashMap<String, Object> map = new HashMap<>();
+        List<OnlineRateVo> lists = deviceMapper.getLists(organizationId, null);
+        if (!ObjectUtils.isEmpty(lists)){
+            Map<String, List<OnlineRateVo>> collect = lists.stream().collect(Collectors.groupingBy(OnlineRateVo::getState));
+            int size1 = lists.size();
+            int size = collect.get("0").size();
+            int i = size1 - size;
+            //������
+            map.put("sum",lists.size());
+            //������
+            map.put("online",i);
+            double number = (double) i / size1 * 100;
+            String result = String.format("%.2f", number);
+            map.put("onlineRate",result);
+        }
+        return map;
+    }
+
+    @Override
+    public Map<String, Object> detailV1(String mac, String startTime, String endTime, String type) {
+        HashMap<String, Object> rsMap = new HashMap<>();
+        Map<String, Object> map = new TreeMap<>(
+                new Comparator<String>() {
+                    @Override
+                    public int compare(String o1, String o2) {
+                        return o1.compareTo(o2);
+                    }
+                }
+        );
+        //������������
+        ArrayList<Date> EndDates = new ArrayList<>();
+        //������������
+        ArrayList<Date> StartDates = new ArrayList<>();
+        ArrayList<Date> dates = new ArrayList<>();
+        if (type.equals("hour")){
+            Date start = DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN);
+            Date end = DateUtils.getDate(endTime, DateUtils.yyyy_MM_dd_HH_EN);
+            List<HistoryHourly> valueByMacAndTime = historyHourlyService.getValueByMacAndTime(mac, start, end);
+            if (!ObjectUtils.isEmpty(valueByMacAndTime)){
+                //������������
+                ArrayList<HistoryHourly> collect = valueByMacAndTime.stream().collect(
+                        Collectors.collectingAndThen(Collectors.toCollection(
+                                () -> new TreeSet<>(Comparator.comparing(p -> p.getTime()))), ArrayList::new)
+                );
+                for (HistoryHourly historyHourly : collect) {
+                    map.put(DateUtils.dateToDateString(historyHourly.getTime()),1);
+                    dates.add(historyHourly.getTime());
+                }
+             setCode(end,start,dates,StartDates,EndDates,map,type);
+            }
+            //������
+            List<OnlineRateLogsForm> OrfList = getList(EndDates, StartDates, type);
+            //���������
+            ArrayList<String> list1 = new ArrayList<>();
+            ArrayList<Integer> list2 = new ArrayList<>();
+            Set<String> strings = map.keySet();
+            for (String string : strings) {
+                list1.add(string);
+                list2.add(Integer.parseInt(map.get(string).toString()));
+            }
+            rsMap.put("lineChart1",list1);
+            rsMap.put("lineChart2",list2);
+            rsMap.put("tabulation",OrfList);
+            //���������������������
+            int endNumber = map.size() - dates.size();
+            double number = (double) dates.size() / map.size() * 100;
+            String result = String.format("%.2f", number);
+            rsMap.put("pieChart1",endNumber);
+            rsMap.put("pieChart2",dates.size());
+            rsMap.put("code","���"+map.size()+"���������"+"������"+dates.size()+"���������"+"���������"+(result.equals("NaN")?"0":result));
+
+        }else if (type.equals("day")){
+
+        }else {
+
+        }
+        return null;
+    }
+
     private Device getDeviceUnitAlramInforByMacFromDb(String mac){
         QueryWrapper<Device> wrapper = new QueryWrapper<>();
         wrapper.eq("mac",mac);
@@ -281,6 +1088,90 @@
         return deviceMapper.selectOne(wrapper);
     }
 
+    /**
+     * @Description: ������������������������������������������������wrapper���������������
+     * @Param: [wrapper, tableNames]
+     * @return: java.util.List<com.moral.api.entity.HistoryHourly>
+     * @Author: ���������
+     * @Date: 2021/9/23
+     */
+    private List<HistoryHourly> multiTableQuery(Map<String, Object> params, List<String> tableNames) {
+        List<HistoryHourly> result = new ArrayList<>();
+        for (String tableName : tableNames) {
+            params.put("table",tableName);
+            List<HistoryHourly> datas = historyHourlyMapper.listResult(params);
+            result.addAll(datas);
+        }
+        return result;
+    }
+
+    private List<HistoryFiveMinutely> FiveMinuteTableQuery(Map<String, Object> params, List<String> tableNames) {
+        List<HistoryFiveMinutely> result = new ArrayList<>();
+        for (String tableName : tableNames) {
+            params.put("table",tableName);
+            List<HistoryFiveMinutely> datas = historyFiveMinutelyMapper.listResult(params);
+            result.addAll(datas);
+        }
+        return result;
+    }
 
 
+    private void setCode(Date end,Date start,ArrayList<Date> dates,ArrayList<Date> StartDates,ArrayList<Date> EndDates,Map<String,Object> map,String type){
+        boolean flag= true;
+        while (DateUtils.isTimeBeforE(end,start)){
+            if (dates.contains(start)){
+                if (!flag){
+                    StartDates.add(start);
+                    flag= true;
+                }
+            }else {
+                if (flag){
+                    EndDates.add(start);
+                    flag=false;
+                }
+                map.put(DateUtils.dateToDateString(start),0);
+            }
+            if (type.equals("hour")){
+                start = DateUtils.getDateAddHour(start,1);
+            }else if (type.equals("day")){
+                start = DateUtils.getDateOfDay(start,1);
+            }else {
+                start = DateUtils.getDateOfMin(start,1);
+            }
+        }
+    }
+
+    private List<OnlineRateLogsForm>  getList(ArrayList<Date> EndDates,ArrayList<Date> StartDates,String type){
+        int nh = 1000 * 60 * 60;
+        long nd = 1000 * 24 * 60 * 60;
+        long nm = 1000 * 60;
+        ArrayList<OnlineRateLogsForm> OrfList = new ArrayList<>();
+        if (!ObjectUtils.isEmpty(EndDates)){
+            for (int i = 0; i < EndDates.size(); i++) {
+                OnlineRateLogsForm onlineRateLogsForm = new OnlineRateLogsForm();
+                onlineRateLogsForm.setEndTime(DateUtils.dateToDateString(EndDates.get(i)));
+                if (ObjectUtils.isEmpty(StartDates)){
+                    onlineRateLogsForm.setStartTime("-");
+                    onlineRateLogsForm.setMun("-");
+                }else {
+                    if (EndDates.size()>StartDates.size()&& i==EndDates.size()-1){
+                        onlineRateLogsForm.setStartTime("-");
+                        onlineRateLogsForm.setMun("-");
+                    }else {
+                        onlineRateLogsForm.setStartTime(DateUtils.dateToDateString(StartDates.get(i)));
+                        long l = EndDates.get(i).getTime() - StartDates.get(i).getTime();
+                        if (type.equals("hour")){
+                            onlineRateLogsForm.setMun(Long.toString(Math.abs(l/nh)));
+                        }else if (type.equals("day")){
+                            onlineRateLogsForm.setMun(Long.toString(Math.abs(l/nd)));
+                        }else {
+                            onlineRateLogsForm.setMun(Long.toString(Math.abs(l/nm)));
+                        }
+                    }
+                }
+                OrfList.add(onlineRateLogsForm);
+            }
+        }
+        return OrfList;
+    }
 }

--
Gitblit v1.8.0