From bfd4fd8e4a05a681ec10a47992294cf752a764c4 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期一, 02 三月 2026 15:10:58 +0800
Subject: [PATCH] 添加Redis服务与缓存增强

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs |   78 ++++++++++++++++++++++-----------------
 1 files changed, 44 insertions(+), 34 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
index 3cdd283..c63f06e 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
@@ -11,6 +11,7 @@
 using WIDESEAWCS_DTO.BasicInfo;
 using WIDESEAWCS_QuartzJob.Models;
 using WIDESEAWCS_QuartzJob.Repository;
+using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
 
 namespace WIDESEAWCS_QuartzJob.Service
 {
@@ -21,6 +22,7 @@
     {
         private readonly IDeviceProtocolRepository _deviceProtocolRepository;
         private readonly IDeviceInfoRepository _deviceInfoRepository;
+        private readonly ICacheService _cacheService;
 
         /// <summary>
         /// 璺敱閰嶇疆涓氬姟灞�
@@ -28,10 +30,11 @@
         /// <param name="BaseDal"></param>
         /// <param name="deviceProtocolRepository"></param>
         /// <param name="deviceInfoRepository"></param>
-        public RouterService(IRouterRepository BaseDal, IDeviceProtocolRepository deviceProtocolRepository, IDeviceInfoRepository deviceInfoRepository) : base(BaseDal)
+        public RouterService(IRouterRepository BaseDal, IDeviceProtocolRepository deviceProtocolRepository, IDeviceInfoRepository deviceInfoRepository, ICacheService cacheService) : base(BaseDal)
         {
             _deviceProtocolRepository = deviceProtocolRepository;
             _deviceInfoRepository = deviceInfoRepository;
+            _cacheService = cacheService;
         }
 
         /// <summary>
@@ -47,10 +50,10 @@
             {
                 // 涓�娆℃�ф煡璇㈡墍鏈夎矾鐢辨暟鎹埌鍐呭瓨
                 List<Dt_Router> allRouters = BaseDal.QueryData(x => true);
-                
+
                 // 鍦ㄥ唴瀛樹腑杩涜璺緞鎼滅储
                 routers = FindRoutesInMemory(startPosi, endPosi, allRouters, null);
-                
+
                 if (routers.Count == 0)
                 {
                     throw new Exception($"璇ヨ矾寰勬湭閰嶇疆鎴栭厤缃敊璇�,璇锋鏌ヨ澶囪矾鐢变俊鎭�,璧风偣:銆恵startPosi}銆�,缁堢偣:銆恵endPosi}銆�");
@@ -77,10 +80,10 @@
             {
                 // 涓�娆℃�ф煡璇㈡寚瀹氱被鍨嬬殑鎵�鏈夎矾鐢辨暟鎹埌鍐呭瓨
                 List<Dt_Router> allRouters = BaseDal.QueryData(x => x.InOutType == routeType);
-                
+
                 // 鍦ㄥ唴瀛樹腑杩涜璺緞鎼滅储
                 routers = FindRoutesInMemory(startPosi, endPosi, allRouters, routeType);
-                
+
                 if (routers.Count == 0)
                 {
                     throw new Exception($"璇ヨ矾寰勬湭閰嶇疆鎴栭厤缃敊璇�,璇锋鏌ヨ澶囪矾鐢变俊鎭�,璧风偣:銆恵startPosi}銆�,缁堢偣:銆恵endPosi}銆�");
@@ -110,7 +113,7 @@
             var routersByNext = allRouters
                 .GroupBy(r => r.NextPosi)
                 .ToDictionary(g => g.Key, g => g.ToList());
-            
+
             var routersByChild = allRouters
                 .GroupBy(r => r.ChildPosi)
                 .ToDictionary(g => g.Key, g => g.ToList());
@@ -127,7 +130,7 @@
 
             // 浣跨敤闃熷垪杩涜骞垮害浼樺厛鎼滅储
             Queue<(Dt_Router router, List<Dt_Router> path)> queue = new Queue<(Dt_Router, List<Dt_Router>)>();
-            
+
             // 灏嗘墍鏈夌粓鐐硅矾鐢卞姞鍏ラ槦鍒�
             foreach (var endRouter in endRouters)
             {
@@ -186,9 +189,9 @@
             try
             {
                 // 鏌ヨ浠庤捣鐐瑰嚭鍙戠殑鎵�鏈夎矾鐢�
-                List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi, 
+                List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi,
                     new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
-                
+
                 // 杩斿洖绗竴涓矾鐢�
                 return routes.FirstOrDefault();
             }
@@ -210,9 +213,9 @@
             try
             {
                 // 鏌ヨ浠庤捣鐐瑰嚭鍙戠殑鎸囧畾绫诲瀷璺敱
-                List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi && x.InOutType == routeType, 
+                List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi && x.InOutType == routeType,
                     new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
-                
+
                 // 杩斿洖绗竴涓矾鐢�
                 return routes.FirstOrDefault();
             }
@@ -235,9 +238,9 @@
             try
             {
                 // 鏌ヨ浠庤捣鐐瑰嚭鍙戠殑鎸囧畾绫诲瀷璺敱
-                List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi && x.InOutType == routeType, 
+                List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi && x.InOutType == routeType,
                     new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
-                
+
                 if (routes.Count == 0)
                     return null;
 
@@ -278,7 +281,7 @@
             List<Dt_Router> path = new List<Dt_Router>();
             string currentPosi = startPosi;
             HashSet<string> visitedPositions = new HashSet<string>();
-            
+
             try
             {
                 while (currentPosi != endPosi)
@@ -290,21 +293,21 @@
                     visitedPositions.Add(currentPosi);
 
                     Dt_Router nextRoute = QueryNextRoute(currentPosi, endPosi, routeType);
-                    
+
                     if (nextRoute == null)
                     {
                         break;
                     }
-                    
+
                     path.Add(nextRoute);
                     currentPosi = nextRoute.NextPosi;
-                    
+
                     if (path.Count > 1000)
                     {
                         break;
                     }
                 }
-                
+
                 if (currentPosi != endPosi)
                 {
                     return new List<Dt_Router>();
@@ -314,7 +317,7 @@
             {
                 return new List<Dt_Router>();
             }
-            
+
             return path;
         }
 
@@ -327,24 +330,31 @@
         {
             // 鍒涘缓涓�涓瓧绗︿覆鍒楄〃锛岀敤浜庡瓨鍌ㄦ墍鏈変綅缃�
             List<string> positions = new List<string>();
-            try
-            {
-                // 鏌ヨ鎵�鏈夎繘鍏ヨ矾鐢卞櫒鐨勪綅缃�
-                List<string> inRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.In.ObjToInt()).GroupBy(x => x.StartPosi).Select(x => x.Key).ToList();
-
-                // 鏌ヨ鎵�鏈夌寮�璺敱鍣ㄧ殑浣嶇疆
-                List<string> outRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.Out.ObjToInt()).GroupBy(x => x.ChildPosi).Select(x => x.Key).ToList();
-
-                // 灏嗚繘鍏ュ拰绂诲紑璺敱鍣ㄧ殑浣嶇疆娣诲姞鍒板垪琛ㄤ腑
-                positions.AddRange(inRouterPositions);
-                positions.AddRange(outRouterPositions);
-                // 杩斿洖鍘婚噸鍚庣殑浣嶇疆鍒楄〃
-                return positions.GroupBy(x => x).Select(x => x.Key).ToList();
-            }
-            catch
+            var device = _cacheService.Get<List<string>>($"DevicePositions:{deviceCode}");
+            if (device.IsNullOrEmpty())
             {
 
+                try
+                {
+                    // 鏌ヨ鎵�鏈夎繘鍏ヨ矾鐢卞櫒鐨勪綅缃�
+                    List<string> inRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.In.ObjToInt()).GroupBy(x => x.StartPosi).Select(x => x.Key).ToList();
+
+                    // 鏌ヨ鎵�鏈夌寮�璺敱鍣ㄧ殑浣嶇疆
+                    List<string> outRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.Out.ObjToInt()).GroupBy(x => x.ChildPosi).Select(x => x.Key).ToList();
+
+                    // 灏嗚繘鍏ュ拰绂诲紑璺敱鍣ㄧ殑浣嶇疆娣诲姞鍒板垪琛ㄤ腑
+                    positions.AddRange(inRouterPositions);
+                    positions.AddRange(outRouterPositions);
+                    // 杩斿洖鍘婚噸鍚庣殑浣嶇疆鍒楄〃
+                    return positions.GroupBy(x => x).Select(x => x.Key).ToList();
+                }
+                catch
+                {
+
+                }
             }
+            else 
+                positions = device;
             // 杩斿洖浣嶇疆鍒楄〃
             return positions;
         }

--
Gitblit v1.9.3