From 1f11310ba04770a4efe4657d39872590575828f1 Mon Sep 17 00:00:00 2001
From: zxl <763096477@qq.com>
Date: 星期四, 10 四月 2025 17:17:24 +0800
Subject: [PATCH] 设备未知状态,新增,导出页新增统计一个月的离线数,与在线率

---
 ycl-server/src/main/java/com/ycl/platform/service/impl/TMonitorServiceImpl.java |  276 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 264 insertions(+), 12 deletions(-)

diff --git a/ycl-server/src/main/java/com/ycl/platform/service/impl/TMonitorServiceImpl.java b/ycl-server/src/main/java/com/ycl/platform/service/impl/TMonitorServiceImpl.java
index 3865624..3127208 100644
--- a/ycl-server/src/main/java/com/ycl/platform/service/impl/TMonitorServiceImpl.java
+++ b/ycl-server/src/main/java/com/ycl/platform/service/impl/TMonitorServiceImpl.java
@@ -3,14 +3,11 @@
 import annotation.DataScope;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.mongodb.ExplainVerbosity;
 import com.mongodb.client.AggregateIterable;
 import com.mongodb.client.MongoCollection;
 import com.mongodb.client.MongoDatabase;
 import com.ycl.platform.domain.entity.TMonitor;
-import com.ycl.platform.domain.excel.TMonitorExp;
-import com.ycl.platform.domain.excel.VideoDailyExp;
-import com.ycl.platform.domain.excel.VideoTotalExp;
+import com.ycl.platform.domain.excel.*;
 import com.ycl.platform.domain.form.VideoExportForm;
 import com.ycl.platform.domain.query.DashboardQuery;
 import com.ycl.platform.domain.query.DataCenterQuery;
@@ -37,7 +34,6 @@
 import com.ycl.platform.service.ITMonitorService;
 import com.ycl.system.Result;
 import com.ycl.system.entity.SysDictData;
-import com.ycl.system.mapper.SysConfigMapper;
 import com.ycl.system.mapper.SysDictDataMapper;
 import com.ycl.system.page.PageUtil;
 import com.ycl.system.service.ISysConfigService;
@@ -45,13 +41,12 @@
 import com.ycl.utils.StringUtils;
 import com.ycl.utils.poi.ExcelUtil;
 import constant.ApiConstants;
-import constant.CheckConstants;
 import enumeration.general.AreaDeptEnum;
 import jakarta.servlet.http.HttpServletResponse;
-import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.Validate;
 import org.bson.Document;
-import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.mongodb.core.MongoTemplate;
 import org.springframework.data.mongodb.core.query.Criteria;
@@ -68,17 +63,13 @@
 import java.lang.reflect.InvocationTargetException;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
-import java.text.DecimalFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.time.LocalDate;
 import java.time.YearMonth;
-import java.time.ZoneId;
 import java.time.format.DateTimeFormatter;
-import java.time.temporal.TemporalAdjusters;
 import java.util.*;
 import java.util.concurrent.*;
-import java.util.function.BinaryOperator;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
@@ -89,6 +80,7 @@
  * @date 2024-03-04
  */
 @Service
+@Slf4j
 public class TMonitorServiceImpl extends ServiceImpl<TMonitorMapper, TMonitor> implements ITMonitorService {
     @Autowired
     private TMonitorMapper tMonitorMapper;
@@ -570,6 +562,8 @@
      */
     @Override
     public void exportVideoOnline(HttpServletResponse response, VideoExportForm exportForm) throws IOException, NoSuchFieldException, IllegalAccessException {
+        log.error("寮�濮嬪鍑烘暟鎹�");
+        log.error("浼犲叆鐨勬湀浠�:{}",exportForm.getMonth());
         //榛樿鏌ユ墍鏈夐儴闂�
         if (CollectionUtils.isEmpty(exportForm.getDeptIds())) {
             List<Integer> deptIds = new ArrayList<>();
@@ -585,6 +579,7 @@
         Query query = getQuery(exportForm);
         //鏈堜唤姣忔棩鍦ㄧ嚎鏁版嵁
         List<TMonitorResult> onlineResult = mongoTemplate.find(query, TMonitorResult.class);
+        log.error("鏈堜唤鍦ㄧ嚎鏁版嵁:{}鏉℃暟",onlineResult.size());
         // 浣跨敤 Collectors.toMap 鍘婚噸锛屼繚鐣欐瘡涓� No 鐨勭涓�涓亣鍒扮殑鍏冪礌
         Map<String, TMonitorResult> uniqueResultsMap = onlineResult.stream()
                 .collect(Collectors.toMap(
@@ -594,6 +589,7 @@
                 ));
         // 灏� Map 杞崲涓� List
         List<TMonitorResult> tMonitorResults = new ArrayList<>(uniqueResultsMap.values());
+        log.error("鍘婚噸鍚庡ぇ灏�:{}",tMonitorResults.size());
         //鑾峰彇鍔ㄦ�佸垪鏁版嵁
         List<Integer> pointIds = tMonitorResults.stream().map(TMonitorResult::getPointId).collect(Collectors.toList());
         List<DynamicColumnVO> dynamics = dynamicColumnMapper.getDynamicsByIds("t_yw_point", pointIds);
@@ -605,6 +601,8 @@
                 tMonitorResult.setDynamicColumnList(map.get(pointId));
             }
         }
+        //瀛樻斁鍖哄煙 涓� 璁惧鍒楄〃 map key涓� 鍖哄煙
+        Map<Integer, List<VideoDailyExp>> map = new HashMap<>();
         List<CompletableFuture<List<VideoDailyExp>>> futures = new ArrayList<>();
         for (Integer deptId : exportForm.getDeptIds()) {
             CompletableFuture<List<VideoDailyExp>> future = CompletableFuture.supplyAsync(() -> {
@@ -628,6 +626,8 @@
                     VideoDailyExp videoDailyExp = new VideoDailyExp();
                     videoDailyExp.setSerialNumber(result.getNo());
                     videoDailyExp.setDeviceName(result.getName());
+                    videoDailyExp.setType(result.getMonitorType());
+
                     videoDailyExp.setArea(areaDeptEnum == null ? "鏈煡" : areaDeptEnum.getName());
 
                     StringBuilder tag = new StringBuilder("" +
@@ -663,19 +663,267 @@
                 );
                 mysheet.add(excelExp);
 
+
                 return videoDailyExps;
             }, threadPoolTaskExecutor);
             futures.add(future);
+            map.put(deptId,future.join());
         }
         // 鑾峰彇鍏ㄩ噺鏁版嵁
         List<VideoDailyExp> totalExps = futures.stream()
                 .map(CompletableFuture::join)
                 .flatMap(List::stream)
                 .collect(Collectors.toList());
+        log.error("鎵撳嵃鍏ㄩ噺鏁版嵁:{}",totalExps);
         ExcelExp excelExp = new ExcelExp("鍏ㄩ噺", totalExps, VideoDailyExp.class);
         mysheet.add(excelExp);
+        //娣诲姞鏂扮殑sheet 绂荤嚎鏁扮粺璁¤〃
+        List<VideoTypeOffOnlineExp> videoTypeOffOnlineExps = new ArrayList<>();
+        //鎻掑叆excel鏃剁‘淇濇暟鎹嚭鐜板湪鏈�鍚�
+        List<VideoTypeOffOnlineExp> allVideoTypeOffOnlineExps = new ArrayList<>();
+
+        //鍦ㄧ嚎鐜囩粺璁¤〃
+        List<VideoOnlineRateExp> videoOnlineRateExps = new ArrayList<>();
+        //鎻掑叆excel鏃剁‘淇濇暟鎹嚭鐜板湪鏈�鍚�
+        List<VideoOnlineRateExp> allVideoOnlineRateExps = new ArrayList<>();
+
+        for (Integer deptId : map.keySet()){
+            List<VideoDailyExp>  list = map.get(deptId);
+            AreaDeptEnum areaDeptEnum = AreaDeptEnum.fromDept(deptId);
+            // 娣诲姞绂荤嚎琛ㄨ褰�
+            //璁惧绫诲瀷 1浜鸿劯 2杞﹁締 3瑙嗛
+            //浜鸿劯
+            log.error("浼犲叆閮ㄩ棬闆嗗悎澶у皬:{} + 閮ㄩ棬id:{}" ,list.size(),deptId);
+            VideoTypeOffOnlineExp faceVideoTypeOffOnlineExp = this.getListOfflineCountInfo(list,"1",areaDeptEnum,false);
+            //鍗″彛
+            VideoTypeOffOnlineExp carVideoTypeOffOnlineExp =this.getListOfflineCountInfo(list,"2",areaDeptEnum,false);
+            //瑙嗛
+            VideoTypeOffOnlineExp videoTypeOffOnlineExp = this.getListOfflineCountInfo(list,"3",areaDeptEnum,false);
+
+            VideoOnlineRateExp faceVideoOnlineRateExp = this.getListOnLineCountInfo(list,"1",areaDeptEnum,false);
+            //鍗″彛
+            VideoOnlineRateExp carVideoOnlineRateExp =this.getListOnLineCountInfo(list,"2",areaDeptEnum,false);
+            //瑙嗛
+            VideoOnlineRateExp VideoOnlineRateExp = this.getListOnLineCountInfo(list,"3",areaDeptEnum,false);
+
+
+            //灏嗚鍖哄煙绫讳笁绉嶈澶囩被鍨嬬殑 淇℃伅 鏀惧叆 excel瀵硅薄鍐�
+            //鏀惧叆褰撳墠鍖哄煙鐨勪汉鑴歌澶囩浉鍏宠缁�
+            videoTypeOffOnlineExps.add(faceVideoTypeOffOnlineExp);
+            //鏀惧叆褰撳墠鍖哄煙鐨勮溅杈嗚澶囩浉鍏宠缁�
+            videoTypeOffOnlineExps.add(carVideoTypeOffOnlineExp);
+            //鏀惧叆褰撳墠鍖哄煙鐨勮棰戣澶囩浉鍏宠缁�
+            videoTypeOffOnlineExps.add(videoTypeOffOnlineExp);
+
+            videoOnlineRateExps.add(faceVideoOnlineRateExp);
+            videoOnlineRateExps.add(carVideoOnlineRateExp);
+            videoOnlineRateExps.add(VideoOnlineRateExp);
+
+
+            VideoTypeOffOnlineExp ALLfaceVideoTypeOffOnlineExp = this.getListOfflineCountInfo(list,"1",areaDeptEnum,true);
+            //鍗″彛
+            VideoTypeOffOnlineExp ALLcarVideoTypeOffOnlineExp =this.getListOfflineCountInfo(list,"2",areaDeptEnum,true);
+            //瑙嗛
+            VideoTypeOffOnlineExp ALLvideoTypeOffOnlineExp = this.getListOfflineCountInfo(list,"3",areaDeptEnum,true);
+
+            VideoOnlineRateExp ALLfaceVideoOnlineRateExp = this.getListOnLineCountInfo(list,"1",areaDeptEnum,true);
+            //鍗″彛
+            VideoOnlineRateExp ALLcarVideoOnlineRateExp =this.getListOnLineCountInfo(list,"2",areaDeptEnum,true);
+            //瑙嗛
+            VideoOnlineRateExp ALLVideoOnlineRateExp = this.getListOnLineCountInfo(list,"3",areaDeptEnum,true);
+
+            //娣诲姞鍚堣鏁版嵁
+            allVideoTypeOffOnlineExps.add(ALLfaceVideoTypeOffOnlineExp);
+            //鏀惧叆褰撳墠鍖哄煙鐨勮溅杈嗚澶囩浉鍏宠缁�
+            allVideoTypeOffOnlineExps.add(ALLcarVideoTypeOffOnlineExp);
+            //鏀惧叆褰撳墠鍖哄煙鐨勮棰戣澶囩浉鍏宠缁�
+            allVideoTypeOffOnlineExps.add(ALLvideoTypeOffOnlineExp);
+
+            allVideoOnlineRateExps.add(ALLfaceVideoOnlineRateExp);
+            allVideoOnlineRateExps.add(ALLcarVideoOnlineRateExp);
+            allVideoOnlineRateExps.add(ALLVideoOnlineRateExp);
+
+        }
+        videoTypeOffOnlineExps.addAll(allVideoTypeOffOnlineExps);
+        videoOnlineRateExps.addAll(allVideoOnlineRateExps);
+        log.error("鎵撳嵃璁$畻绂荤嚎鐨勪俊鎭�:{}" ,videoTypeOffOnlineExps );
+        log.error("鎵撳嵃璁″湪绾跨殑淇℃伅:{}" ,videoOnlineRateExps );
+        //娣诲姞鍚堣鏁版嵁
+
+        ExcelExp excelTypeOffLineExp = new ExcelExp("绂荤嚎鏁扮粺璁�", videoTypeOffOnlineExps, VideoTypeOffOnlineExp.class);
+        mysheet.add(excelTypeOffLineExp);
+
+        //娣诲姞鍦ㄧ嚎鐜囪〃
+        ExcelExp excelOnlineRateExp = new ExcelExp("鍦ㄧ嚎鐜囩粺璁�",videoOnlineRateExps, VideoOnlineRateExp.class);
+        mysheet.add(excelOnlineRateExp);
+
         ExcelUtilManySheet<List<ExcelExp>> util = new ExcelUtilManySheet<>(mysheet);
         util.exportExcelManySheet(response, mysheet);
+        log.error("瀵煎嚭缁撴潫");
+    }
+
+    /**
+     * 璁$畻绂荤嚎璁惧excel瀵硅薄淇℃伅
+     * @param videoDailyExps 浼犲叆鐨勮澶囬泦鍚堜俊鎭�
+     * @param type 璁惧绫诲瀷 1浜鸿劯 2杞﹁締 3瑙嗛
+     * @param areaDeptEnum 鍖哄煙
+     * @return
+     */
+    public VideoTypeOffOnlineExp getListOfflineCountInfo(List<VideoDailyExp> videoDailyExps,
+                                                         String type,
+                                                         AreaDeptEnum areaDeptEnum,
+                                                         boolean isTotal){
+        VideoTypeOffOnlineExp videoTypeOffOnlineExp = new VideoTypeOffOnlineExp();
+
+        List<VideoDailyExp> list = videoDailyExps.stream()
+                .filter(device ->device.getType() != null && device.getType().contains(type)).collect(Collectors.toList());
+        log.error("绛涢�夊畬璁惧绫诲瀷 锛歿} 鍚庨泦鍚堢殑澶у皬锛歿}",type,list.size());
+        //绂荤嚎鏁伴噺
+        try {
+            //璁剧疆绂荤嚎鏁伴噺 浠ュ強姣忔棩绂荤嚎鏁伴噺
+            setVideoTypeOffOnlineExpCountAndDays(list,videoTypeOffOnlineExp,isTotal);
+        } catch (Exception e) {
+            log.error(e.getMessage());
+        }
+        //璁惧绫诲瀷
+        if(isTotal){
+            if ("1".equals(type)){
+                videoTypeOffOnlineExp.setType("浜鸿劯鍚堣");
+            }else if ("2".equals(type)){
+                videoTypeOffOnlineExp.setType("鍗″彛鍚堣");
+            }else if ("3".equals(type)){
+                videoTypeOffOnlineExp.setType("瑙嗛鍚堣");
+            }
+        }else {
+            if ("1".equals(type)){
+                videoTypeOffOnlineExp.setType("浜鸿劯");
+            }else if ("2".equals(type)){
+                videoTypeOffOnlineExp.setType("鍗″彛");
+            }else if ("3".equals(type)){
+                videoTypeOffOnlineExp.setType("瑙嗛");
+            }
+        }
+        //淇敼鍖哄煙
+        videoTypeOffOnlineExp.setArea(areaDeptEnum == null ? "鏈煡" : areaDeptEnum.getName());
+        return videoTypeOffOnlineExp;
+    }
+
+    private void setVideoTypeOffOnlineExpCountAndDays(List<VideoDailyExp> videoDailyExps,VideoTypeOffOnlineExp videoTypeOffOnlineExp,boolean isTotal)throws NoSuchFieldException, IllegalAccessException {
+        //寰幆涓�涓湀
+        //绂荤嚎鎬绘暟
+        long AllOffLineCount = 0;
+        log.error("浼犲叆闆嗗悎澶у皬:{}", videoDailyExps.size());
+        for (VideoDailyExp videoDailyExp : videoDailyExps) {
+            if (videoDailyExp.isAllOfflineByMonth()) {
+                AllOffLineCount++;
+            }
+        }
+        //鏄悎璁℃暟鎹笉闇�瑕佷笅鏂规暟鎹�
+//        if (!isTotal) {
+            for (int i = 1; i <= 31; i++) {
+                //姣忔棩绂荤嚎鏁�
+                long count = 0;
+                String fieldName = "day" + i;
+                for (VideoDailyExp videoDailyExp : videoDailyExps) {
+                    // 鏋勯�犲瓧娈靛悕
+                    Field field = videoDailyExp.getClass().getDeclaredField(fieldName);
+                    // 纭繚瀛楁鏄鏈夌殑鍙互璁块棶
+                    field.setAccessible(true);
+                    // 鑾峰彇瀛楁鍊�
+                    String value = (String) field.get(videoDailyExp);
+                    if ("绂荤嚎".equals(value)) {
+                        count++;
+                    }
+                }
+                //鍙嶅皠娣诲姞鍒板璞″睘鎬т腑
+                Field field = videoTypeOffOnlineExp.getClass().getDeclaredField(fieldName);
+                field.setAccessible(true);
+                String rateStr = count + "";
+                if (!"0".equals(rateStr)) {
+                    field.set(videoTypeOffOnlineExp, rateStr);
+                }
+
+            }
+//        }
+        videoTypeOffOnlineExp.setOfflineCount(String.valueOf(AllOffLineCount));
+    }
+
+    /**
+     * 璁$畻鍦ㄧ嚎璁惧excel瀵硅薄淇℃伅
+     * @param videoDailyExps 浼犲叆鐨勮澶囬泦鍚堜俊鎭�
+     * @param type 璁惧绫诲瀷 1浜鸿劯 2杞﹁締 3瑙嗛
+     * @param areaDeptEnum 鍖哄煙
+     * @return
+     */
+    public VideoOnlineRateExp getListOnLineCountInfo(List<VideoDailyExp> videoDailyExps,
+                                                         String type,
+                                                         AreaDeptEnum areaDeptEnum,
+                                                         boolean isTotal){
+        VideoOnlineRateExp videoOnlineRateExp = new VideoOnlineRateExp();
+
+        List<VideoDailyExp> list = videoDailyExps.stream()
+                .filter(device ->device.getType() != null && device.getType().contains(type)).collect(Collectors.toList());
+        log.error("绛涢�夊畬璁惧绫诲瀷 锛歿} 鍚庨泦鍚堢殑澶у皬锛歿}",type,list.size());
+        //绂荤嚎鏁伴噺
+        try {
+            //璁剧疆绂荤嚎鏁伴噺 浠ュ強姣忔棩绂荤嚎鏁伴噺
+            setVideoOnlineRateExpCountAndDays(list,videoOnlineRateExp,isTotal);
+        } catch (Exception e) {
+            log.error(e.getMessage());
+        }
+        //璁惧绫诲瀷
+        if(isTotal) {
+            if ("1".equals(type)) {
+                videoOnlineRateExp.setType("浜鸿劯鍚堣");
+            } else if ("2".equals(type)) {
+                videoOnlineRateExp.setType("鍗″彛鍚堣");
+            } else if ("3".equals(type)) {
+                videoOnlineRateExp.setType("瑙嗛鍚堣");
+            }
+        }else {
+            if ("1".equals(type)) {
+                videoOnlineRateExp.setType("浜鸿劯");
+            } else if ("2".equals(type)) {
+                videoOnlineRateExp.setType("鍗″彛");
+            } else if ("3".equals(type)) {
+                videoOnlineRateExp.setType("瑙嗛");
+            }
+        }
+        //淇敼鍖哄煙
+        videoOnlineRateExp.setArea(areaDeptEnum == null ? "鏈煡" : areaDeptEnum.getName());
+        return videoOnlineRateExp;
+    }
+
+
+    private void setVideoOnlineRateExpCountAndDays(List<VideoDailyExp> videoDailyExps,VideoOnlineRateExp videoOnlineRateExp,boolean isTotal)throws NoSuchFieldException, IllegalAccessException {
+
+        //寰幆涓�涓湀
+        for (int i = 1;i <= 31 ;i++){
+            //鍦ㄧ嚎鐜囪绠�
+            long count = 0;
+            String fieldName = "day" + i;
+            for(VideoDailyExp videoDailyExp: videoDailyExps){
+                // 鏋勯�犲瓧娈靛悕
+                Field field = videoDailyExp.getClass().getDeclaredField(fieldName);
+                // 纭繚瀛楁鏄鏈夌殑鍙互璁块棶
+                field.setAccessible(true);
+                // 鑾峰彇瀛楁鍊�
+                String value = (String) field.get(videoDailyExp);
+                if ("鍦ㄧ嚎".equals(value)) {
+                    count ++;
+                }
+            }
+            //姣忔棩鍦ㄧ嚎鐜�
+            double rate = (double) count / videoDailyExps.size();
+            //鍙嶅皠娣诲姞鍒板璞″睘鎬т腑
+            Field field = videoOnlineRateExp.getClass().getDeclaredField(fieldName);
+            field.setAccessible(true);
+            String rateStr = String.format("%.2f", rate * 100) +"%";
+            if (!"0.00%".equals(rateStr)){
+                field.set(videoOnlineRateExp, rateStr);
+            }
+
+        }
     }
 
 
@@ -826,6 +1074,7 @@
                         Function.identity(),  // valueMapper锛岀洿鎺ヤ娇鐢ㄥ璞℃湰韬�
                         (existing, replacement) -> existing // mergeFunction锛屽鏋滄湁閲嶅锛屼繚鐣欑涓�涓�
                 ));
+
         // 灏� Map 杞崲涓� List
         List<TMonitorResult> tMonitorResults = new ArrayList<>(uniqueResultsMap.values());
         //鑾峰彇鍔ㄦ�佸垪鏁版嵁
@@ -1387,6 +1636,7 @@
     private void setOnlineDaily(VideoDailyExp videoDailyExp, TMonitorResult result, List<TMonitorResult> onlines) throws NoSuchFieldException, IllegalAccessException {
         //涓�涓澶囧綋鏈堝湪绾挎儏鍐�
         List<TMonitorResult> onlineResult = onlines.stream().filter(online -> online.getNo().equals(result.getNo())).collect(Collectors.toList());
+        videoDailyExp.setOnlineStateList(onlineResult);
         for (TMonitorResult monitorResult : onlineResult) {
             int dayOfMonth = monitorResult.getMongoCreateTime().getDayOfMonth();
             String online = "";
@@ -1402,6 +1652,8 @@
             field.setAccessible(true);
             field.set(videoDailyExp, online);
         }
+
+
     }
 
     //璁剧疆姣忔棩褰曞儚鏁版嵁

--
Gitblit v1.8.0