From 2b776c4a2222d9bc36f53a449bda2b808c7b43b9 Mon Sep 17 00:00:00 2001
From: xiangpei <xiangpei@timesnew.cn>
Date: 星期六, 03 八月 2024 14:38:53 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 ycl-server/src/main/java/com/ycl/calculate/VideoOnlineCalculation.java |  136 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 136 insertions(+), 0 deletions(-)

diff --git a/ycl-server/src/main/java/com/ycl/calculate/VideoOnlineCalculation.java b/ycl-server/src/main/java/com/ycl/calculate/VideoOnlineCalculation.java
new file mode 100644
index 0000000..7b82051
--- /dev/null
+++ b/ycl-server/src/main/java/com/ycl/calculate/VideoOnlineCalculation.java
@@ -0,0 +1,136 @@
+package com.ycl.calculate;
+
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.ycl.platform.domain.entity.CheckIndexVideo;
+import com.ycl.platform.domain.entity.TMonitor;
+import com.ycl.platform.domain.result.HK.SnapshotDelayMonitorResult;
+import com.ycl.platform.domain.result.UY.QueryVqdResult;
+import com.ycl.platform.mapper.CheckIndexVideoMapper;
+import com.ycl.platform.service.ICheckIndexFaceService;
+import com.ycl.platform.service.ICheckIndexVideoService;
+import com.ycl.platform.service.ITMonitorService;
+import constant.ApiConstants;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+import utils.DateUtils;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+/**
+ * 璁$畻瑙嗛璁惧鐐逛綅鍦ㄧ嚎鐜囥�侀噸鐐圭偣浣嶅湪绾跨巼
+ * 鍥惧儚鐩戞祴璇婃柇缁撴灉
+ * 鑾峰彇鍒嗙渷鍘呫�佸尯鍩熺殑map<k,v> k涓篸eptId鎴栬�匬rovince_deptId
+ * 寰幆map璁$畻鏁版嵁涓婁紶鍙婃椂鎬�
+ * 鏇存柊鎴栨柊澧�
+ */
+@Component
+//TODO锛氶噸鐐规寚鎸ュ浘鍍忓湪绾跨巼
+public class VideoOnlineCalculation extends IndexCalculationServe implements CalculationStrategy<QueryVqdResult> {
+    @Autowired
+    private CheckIndexVideoMapper checkIndexVideoMapper;
+    @Autowired
+    private ITMonitorService monitorService;
+    @Autowired
+    private ICheckIndexVideoService checkIndexVideoService;
+
+    //鍖哄煙瑙嗛鍦ㄧ嚎鐜囩殑鍐呴儴绫�
+    private static class AreaStats {
+        int totalSites = 0;
+        int onlineSites = 0;
+        int importantSites = 0;
+        int importantOnlineSites = 0;
+    }
+
+    @Override
+    public void calculate(List<QueryVqdResult> list) {
+        if (CollectionUtils.isEmpty(list)) {
+            return;
+        }
+
+        //鑾峰緱鍥芥爣鐮佷负key鐨勮澶噈ap
+        Map<String, TMonitor> monitorMap = monitorService.list(new QueryWrapper<TMonitor>()
+                        .in("serial_number", list.stream().map(QueryVqdResult::getDeviceId).collect(Collectors.toList())))
+                .stream().collect(Collectors.toMap(TMonitor::getSerialNumber, Function.identity()));
+        //鑾峰彇鐪佸巺鍥芥爣鐮侀泦鍚�
+        List<String> provinceIds = getProvince();
+        //閲嶇偣鐐逛綅闆嗗悎
+        List<String> important = getImportant();
+
+        Map<String, AreaStats> areaStatsMap = new HashMap<>();
+        for (QueryVqdResult result : list) {
+            TMonitor monitor = monitorMap.get(result.getDeviceId());
+            if (monitor == null) continue;
+
+            String deptId = monitor.getDeptId().toString();
+            updateAreaStats(areaStatsMap, deptId, result, important);
+
+            // 澶勭悊鐪佸巺鏁版嵁
+            if (!CollectionUtils.isEmpty(provinceIds) && provinceIds.contains(monitor.getSerialNumber())) {
+                String provinceKey = "Province_" + deptId;
+                updateAreaStats(areaStatsMap, provinceKey, result, important);
+            }
+        }
+
+        // 鏌ヨ鏄惁index琛ㄥ凡缁忓瓨鍦ㄤ粖鏃ユ暟鎹�
+        List<CheckIndexVideo> checkIndexVideoList = checkIndexVideoMapper.selectToday(DateUtils.getDate());
+        List<CheckIndexVideo> checkIndexVideos = new ArrayList<>();
+        areaStatsMap.forEach((deptId, stats) -> {
+            if (stats.totalSites > 0) {
+                CheckIndexVideo CheckIndexVideo = createOrUpdateCheckIndexFace(deptId, stats, checkIndexVideoList);
+                if (CheckIndexVideo != null) {
+                    checkIndexVideos.add(CheckIndexVideo);
+                }
+            }
+        });
+
+        checkIndexVideoService.saveOrUpdateBatch(checkIndexVideos);
+    }
+
+    /**
+     * 绱鎬荤偣浣嶆暟銆佸湪绾跨偣浣嶆暟銆侀噸鐐圭偣浣嶆暟銆侀噸鐐圭偣浣嶅湪绾挎暟
+     */
+    private void updateAreaStats(Map<String, AreaStats> areaStatsMap, String key, QueryVqdResult result, List<String> important) {
+        //杩斿洖瀵硅薄鐨勫紩鐢紝濡傛灉涓嶅瓨鍦ㄤ細鏀惧叆鏂扮殑key,value
+        AreaStats stats = areaStatsMap.computeIfAbsent(key, k -> new AreaStats());
+        stats.totalSites++;
+        if(important.contains(result.getDeviceId())){
+            stats.importantSites++;
+        }
+        if (ApiConstants.UY_OnlineSite_Online.equals(result.getOnlineStatus())){
+            stats.onlineSites++;
+            if(important.contains(result.getDeviceId())){
+                stats.importantOnlineSites++;
+            }
+        }
+    }
+
+    /**
+     * 瑙嗛鐐逛綅鍦ㄧ嚎鐜�
+     */
+    private CheckIndexVideo createOrUpdateCheckIndexFace(String key, AreaStats stats, List<CheckIndexVideo> checkIndexVideoList) {
+        CheckIndexVideo checkIndexVideo = getCheckIndex(key, checkIndexVideoList, CheckIndexVideo.class);
+        if (checkIndexVideo == null) {
+            return null;
+        }
+        //璋冪敤鐐逛綅鍦ㄧ嚎鐜囪绠楁柟娉�
+        Map<String, Object> param = new HashMap<>();
+        param.put("totalSites", stats.totalSites);
+        param.put("onlineSites", stats.onlineSites);
+        BigDecimal siteOnline = siteOnline(param);
+        checkIndexVideo.setSiteOnline(siteOnline);
+        //璋冪敤鐐逛綅鍦ㄧ嚎鐜囪绠楁柟娉� 璁$畻閲嶇偣鐐逛綅鍦ㄧ嚎鐜�
+        Map<String, Object> importantParam = new HashMap<>();
+        param.put("totalSites", stats.importantSites);
+        param.put("onlineSites", stats.importantOnlineSites);
+        BigDecimal importantSiteOnline = siteOnline(importantParam);
+        checkIndexVideo.setKeySiteOnline(importantSiteOnline);
+        return checkIndexVideo;
+    }
+}

--
Gitblit v1.8.0