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 |  292 ++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 242 insertions(+), 50 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 aae96af..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
@@ -4,26 +4,20 @@
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.core.metadata.OrderItem;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.moral.api.config.Interceptor.UserHelper;
-import com.moral.api.config.mybatis.MybatisPlusConfig;
 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.HistoryFiveMinutelyService;
 import com.moral.api.service.HistoryHourlyService;
-import com.moral.api.service.HistoryMonthlyService;
 import com.moral.api.service.SensorService;
 import com.moral.api.service.UnitConversionService;
 import com.moral.api.vo.OnlineRateVo;
@@ -31,18 +25,11 @@
 import com.moral.constant.RedisConstants;
 import com.moral.constant.SeparateTableType;
 import com.moral.util.DateUtils;
-
 import com.moral.util.MybatisPLUSUtils;
-import com.sun.org.apache.regexp.internal.RE;
-
-import org.apache.commons.collections4.CollectionUtils;
-import org.apache.kafka.streams.state.internals.metrics.Sensors;
 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.text.DecimalFormat;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -565,22 +552,32 @@
         int nh = 1000 * 60 * 60;
         String startTime = onlineRatePageCond.getStartTime();
         String endTime = onlineRatePageCond.getEndTime();
-        Date start = DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
-        Date end = DateUtils.getDate(endTime, DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
+        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; //������������������������
 
-        List<OnlineRateVo> OnlineRateVoList = deviceMapper.getList(onlineRatePageCond.getOrganizationId());
+        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_mm_ss_EN);
+                Date start1 = DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN);
                 if (!ObjectUtils.isEmpty(valueByMacAndTime)){
-                    for (HistoryHourly historyHourly : 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;
@@ -607,18 +604,30 @@
                         }
                         start1 = DateUtils.getDateAddHour(start1,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)));
+                        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");
                     }
-//               it.setEndTime(DateUtils.dateToDateString(dates1.get(dates1.size()-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(valueByMacAndTime.size());
-                    double number = (double) valueByMacAndTime.size() / hour * 100;
+                    onlineRateVo.setOnlineTime(collects.size());
+                    double number = (double) collects.size() / hour * 100;
                     String result = String.format("%.2f", number);
-                    onlineRateVo.setOnlineRate(result+"%");
+                    onlineRateVo.setOnlineRate(result);
                 }
             }
 
@@ -676,7 +685,10 @@
                 }
             });
         }*/
-        return OnlineRateVoList;
+        if (!ObjectUtils.isEmpty(OnlineRateVoList)){
+            collectList = OnlineRateVoList.stream().collect(Collectors.groupingBy(OnlineRateVo::getHourState));
+        }
+        return ObjectUtils.isEmpty(onlineRatePageCond.getHourState())?OnlineRateVoList:collectList.get(onlineRatePageCond.getHourState());
     }
 
     /**
@@ -698,7 +710,7 @@
                 new Comparator<String>() {
                     @Override
                     public int compare(String o1, String o2) {
-                        return o2.compareTo(o1);
+                        return o1.compareTo(o2);
                     }
                 }
         );
@@ -713,8 +725,15 @@
             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)){
-                for (HistoryHourly historyHourly : 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());
                 }
@@ -741,14 +760,22 @@
                 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)){
-//                        long l = EndDates.get(i).getTime() - StartDates.get(i).getTime();
-//                        onlineRateLogsForm.setMun(Long.toString(l/nh));
-//                        StartDates.remove(i);
-//                    }else {
-//                        onlineRateLogsForm.setMun("-");
-//                    }
+//                    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);
                 }
@@ -770,14 +797,20 @@
             String result = String.format("%.2f", number);
             rsMap.put("pieChart1",endNumber);
             rsMap.put("pieChart2",dates.size());
-            rsMap.put("code","���"+map.size()+"���������"+"������"+dates.size()+"���������"+"���������"+result);
+//            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)){
-                for (HistoryDaily historyDaily : 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());
                 }
@@ -813,10 +846,11 @@
                         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)));
                         }
-                        onlineRateLogsForm.setStartTime(DateUtils.dateToDateString(StartDates.get(i)));
-                        long l = EndDates.get(i).getTime() - StartDates.get(i).getTime();
-                        onlineRateLogsForm.setMun(Long.toString(l/nd));
                     }
 //                    if (!ObjectUtils.isEmpty(StartDates)){
 //                        long l = EndDates.get(i).getTime() - StartDates.get(i).getTime();
@@ -847,13 +881,19 @@
             String result = String.format("%.2f", number);
             rsMap.put("pieChart1",endNumber);
             rsMap.put("pieChart2",dates.size());
-            rsMap.put("code","���"+map.size()+"������"+"������"+dates.size()+"������"+"���������"+result);
+//            rsMap.put("code","���"+map.size()+"������"+"������"+dates.size()+"������"+"���������"+(result.equals("NaN")?"0":result));
+            rsMap.put("sum",map.size());
         }else {
-            Date start = DateUtils.getDate(startTime,"yyyy-MM-dd HH:mm:00");
-            Date end = DateUtils.getDate(endTime, "yyyy-MM-dd HH:mm:00");
+            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)){
-                for (HistoryMinutely historyMinutely : 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());
                 }
@@ -881,7 +921,20 @@
                 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)));
+//                    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));
@@ -905,14 +958,95 @@
             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);
-        }
+            rsMap.put("code","���"+map.size()+"���������"+"������"+dates.size()+"���������"+"���������"+(result.equals("NaN")?"0":result));
 
-        return rsMap;
+        }else if (type.equals("day")){
+
+        }else {
+
+        }
+        return null;
     }
 
     private Device getDeviceUnitAlramInforByMacFromDb(String mac){
@@ -982,4 +1116,62 @@
     }
 
 
+    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