From 38ab8fbd4355070def9e3b39f362731dd4dea6d1 Mon Sep 17 00:00:00 2001
From: CZT <czt18638530771@163.com>
Date: 星期三, 30 八月 2023 20:54:12 +0800
Subject: [PATCH] 优化设备状态解析

---
 igds-core/src/main/java/com/ld/igds/common/impl/CoreDeviceServiceImpl.java |  112 ++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 85 insertions(+), 27 deletions(-)

diff --git a/igds-core/src/main/java/com/ld/igds/common/impl/CoreDeviceServiceImpl.java b/igds-core/src/main/java/com/ld/igds/common/impl/CoreDeviceServiceImpl.java
index f96082f..6b53fb2 100644
--- a/igds-core/src/main/java/com/ld/igds/common/impl/CoreDeviceServiceImpl.java
+++ b/igds-core/src/main/java/com/ld/igds/common/impl/CoreDeviceServiceImpl.java
@@ -1,15 +1,15 @@
 package com.ld.igds.common.impl;
 
 import java.util.*;
-import java.util.stream.Collectors;
 
 import com.ld.igds.common.CoreDeviceService;
 import com.ld.igds.common.dto.DepotSerData;
 import com.ld.igds.common.mapper.DeviceMapper;
 import com.ld.igds.constant.Constant;
 import com.ld.igds.constant.DeviceStatus;
-
 import com.ld.igds.constant.DeviceType;
+
+import com.ld.igds.models.Depot;
 import lombok.extern.slf4j.Slf4j;
 
 import org.apache.commons.lang3.StringUtils;
@@ -32,37 +32,63 @@
     private DeviceMapper deviceMapper;
 
 
-    @Override
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+	@Override
     public void setCacheAllDevice(List<Device> listAll, String companyId) {
-        // 棣栧厛鎸夌収鍒嗘満鍒嗙粍锛岀劧鍚庡瓨鏇存柊缂撳瓨銆�
-        Map<String, List<Device>> map = listAll.stream().collect(Collectors.groupingBy(Device::getSerId));
 
-        if (null == map || map.isEmpty()) {
-            log.error("瀛楀吀淇℃伅锛氭墍鏈夎澶囨寜鐓у垎鏈哄垎缁勪繚瀛樼紦瀛樺け璐ワ紝娌℃湁鍒嗙粍鎴愬姛=={}", companyId);
-            return;
-        }
+        if (null == listAll || listAll.isEmpty()) return;
 
-        for (String serId : map.keySet()) {
-            updateCacheDeviceBySerId(map.get(serId), companyId, serId);
-        }
+        //鎸夌収鍒嗘満鍒嗙被
+        Map<String, List<Device>> mapSer = new HashMap<>();
 
-        this.setCacheDepotDeviceSer(listAll, companyId);
-    }
+        //浠撳簱-璁惧-鍒嗘満瀵硅薄鍒嗙被
+        Map<String, DepotSerData> mapDepot = new HashMap<>();
 
-    @SuppressWarnings({"unchecked", "rawtypes"})
-    private void setCacheDepotDeviceSer(List<Device> listAll, String companyId) {
-        // 澶勭悊浠撳簱涓庡垎鏈虹殑鍏崇郴缂撳瓨
-        Map<String, DepotSerData> tempMap = new HashMap<>();
-        String key;
-        for (Device item : listAll) {
-            key = companyId + "-" + item.getDepotId() + "-" + item.getSerId();
-            if (tempMap.get(key) == null) {
-                tempMap.put(key, new DepotSerData(companyId, item.getDepotId(), item.getSerId()));
+
+        //鎶婅澶囨寜鐓у垎鏈鸿繘琛屽垎缁勶紝鍚屼簨鍚ц澶囨坊鍔犲埌缂撳瓨鍒楄〃涓�
+        String deviceKey, depotDataKey;
+        for (Device device : listAll) {
+
+            //灏佽浠撳簱-璁惧-鍒嗘満瀵硅薄鍏崇郴
+            depotDataKey = companyId + "-" + device.getDepotId() + "-" + device.getSerId();
+            if (mapDepot.get(depotDataKey) == null) {
+                mapDepot.put(depotDataKey, new DepotSerData(companyId, device.getDepotId(), device.getSerId()));
             }
+
+
+            //鍗曚釜璁惧杩涜缂撳瓨
+            deviceKey = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE, device.getId());
+            redisUtil.set(deviceKey, device);
+
+            //鎸夌収鍒嗘満鍒嗙被
+            if (null == mapSer.get(device.getSerId())) mapSer.put(device.getSerId(), new ArrayList<Device>());
+            mapSer.get(device.getSerId()).add(device);
+
         }
 
-        redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST), new ArrayList(tempMap.values()));
+        //浠ュ垎鏈轰负鍗曚綅缂撳瓨璁惧
+        for (String serId : mapSer.keySet()) {
+            updateCacheDeviceBySerId(mapSer.get(serId), companyId, serId);
+        }
+
+
+        redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST), new ArrayList(mapDepot.values()));
     }
+
+
+//    private void setCacheDepotDeviceSer(List<Device> listAll, String companyId) {
+//        // 澶勭悊浠撳簱涓庡垎鏈虹殑鍏崇郴缂撳瓨
+//        Map<String, DepotSerData> tempMap = new HashMap<>();
+//        String key;
+//        for (Device item : listAll) {
+//            key = companyId + "-" + item.getDepotId() + "-" + item.getSerId();
+//            if (tempMap.get(key) == null) {
+//                tempMap.put(key, new DepotSerData(companyId, item.getDepotId(), item.getSerId()));
+//            }
+//        }
+//
+//        redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST), new ArrayList(tempMap.values()));
+//    }
 
     @Override
     public void updateCacheDeviceBySerId(List<Device> listBySer, String companyId, String serId) {
@@ -85,6 +111,38 @@
             return null;
         }
         return list;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public List<Device> getCacheDeviceByDepotId(String companyId, String depotId) {
+        String patten = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_LIST);
+
+        Set<String> keys = redisUtil.keys(patten);
+        if (null == keys) return null;
+
+        List<Device> result = new ArrayList<>();
+        List<Device> list = new ArrayList<>();
+        for (String key : keys) {
+            list.addAll((List<Device>) redisUtil.get(key));
+        }
+
+        if (list.isEmpty()) {
+            return null;
+        }
+
+        for (Device device : list) {
+            if(depotId.equals(device.getDepotId())){
+                result.add(device);
+            }
+        }
+        return result;
+    }
+
+    @Override
+    public Device getCacheDeviceById(String companyId, String id) {
+        String key = RedisConst.buildDeviceKey(companyId, RedisConst.KEY_DEVICE, id);
+        return (Device) redisUtil.get(key);
     }
 
     @Override
@@ -173,7 +231,7 @@
             }
             log.debug("瑙f瀽璁惧鐘舵��--涓婚�氶亾={}---鍏宠仈閫氶亾={}--瑙f瀽鍚庣姸鎬�={}", curDevice.getPassCode(), curDevice.getLink(), curDevice.getStatus());
         }
-        
+
         // 鏇存柊缂撳瓨
         this.updateCacheDeviceBySerId(listCache, companyId, serId);
     }
@@ -279,7 +337,7 @@
     }
 
     @Override
-    public List<Device> getDeviceByTimId(String companyId,String timId){
-        return deviceMapper.queryByTimId(companyId,timId);
+    public List<Device> getDeviceByTimId(String companyId, String timId) {
+        return deviceMapper.queryByTimId(companyId, timId);
     }
 }

--
Gitblit v1.9.3