From db32739ab1886582c42299a23d56f02f1b27d275 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期五, 27 三月 2026 12:34:04 +0800
Subject: [PATCH] feat(RouterService): 改造 AddRouters 写入后同步更新缓存

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs |  177 +++++++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 130 insertions(+), 47 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..8ebe3d7 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
@@ -4,6 +4,8 @@
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using WIDESEAWCS_Common;
+using WIDESEAWCS_Common.TaskEnum;
 using WIDESEAWCS_Core;
 using WIDESEAWCS_Core.BaseServices;
 using WIDESEAWCS_Core.Enums;
@@ -11,6 +13,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 +24,7 @@
     {
         private readonly IDeviceProtocolRepository _deviceProtocolRepository;
         private readonly IDeviceInfoRepository _deviceInfoRepository;
+        private readonly ICacheService _cacheService;
 
         /// <summary>
         /// 璺敱閰嶇疆涓氬姟灞�
@@ -28,10 +32,27 @@
         /// <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>
+        /// 浠庣紦瀛樿幏鍙栨寚瀹氱被鍨嬬殑鍏ㄩ噺璺敱鏁版嵁锛岀紦瀛樹笉瀛樺湪鏃惰嚜鍔ㄤ粠鏁版嵁搴撳姞杞藉苟鍐欏叆缂撳瓨
+        /// </summary>
+        /// <param name="routeType">璺敱绫诲瀷锛堝叆鍙�/鍑哄彛锛�</param>
+        /// <returns>璇ョ被鍨嬬殑鍏ㄩ儴璺敱鍒楄〃</returns>
+        private List<Dt_Router> GetAllRoutersFromCache(int routeType)
+        {
+            // 鏍规嵁璺敱绫诲瀷鏋勫缓缂撳瓨Key锛孖n绫诲瀷瀵瑰簲"In"锛孫ut绫诲瀷瀵瑰簲"Out"
+            string cacheKey = $"Router:AllRouters:{(routeType == RouterInOutType.In.ObjToInt() ? "In" : "Out")}";
+            // 閫氳繃缂撳瓨鏈嶅姟鑾峰彇鏁版嵁锛岀紦瀛樻湭鍛戒腑鏃惰皟鐢ㄥ伐鍘傛柟娉曚粠鏁版嵁搴撴煡璇㈠苟鍐欏叆缂撳瓨
+            return _cacheService.GetOrAdd(
+                cacheKey,
+                _ => BaseDal.QueryData(x => x.InOutType == routeType)
+            );
         }
 
         /// <summary>
@@ -45,12 +66,14 @@
             List<Dt_Router> routers = new List<Dt_Router>();
             try
             {
-                // 涓�娆℃�ф煡璇㈡墍鏈夎矾鐢辨暟鎹埌鍐呭瓨
-                List<Dt_Router> allRouters = BaseDal.QueryData(x => true);
-                
+                // 浠庣紦瀛樺姞杞藉叆鍙g被鍨嬪拰鍑哄彛绫诲瀷鐨勮矾鐢辨暟鎹苟鍚堝苟锛堝垱寤烘柊鍒楄〃锛岄伩鍏嶄慨鏀圭紦瀛樺紩鐢級
+                List<Dt_Router> allRouters = GetAllRoutersFromCache(RouterInOutType.In.ObjToInt())
+                    .Concat(GetAllRoutersFromCache(RouterInOutType.Out.ObjToInt()))
+                    .ToList();
+
                 // 鍦ㄥ唴瀛樹腑杩涜璺緞鎼滅储
                 routers = FindRoutesInMemory(startPosi, endPosi, allRouters, null);
-                
+
                 if (routers.Count == 0)
                 {
                     throw new Exception($"璇ヨ矾寰勬湭閰嶇疆鎴栭厤缃敊璇�,璇锋鏌ヨ澶囪矾鐢变俊鎭�,璧风偣:銆恵startPosi}銆�,缁堢偣:銆恵endPosi}銆�");
@@ -75,12 +98,12 @@
             List<Dt_Router> routers = new List<Dt_Router>();
             try
             {
-                // 涓�娆℃�ф煡璇㈡寚瀹氱被鍨嬬殑鎵�鏈夎矾鐢辨暟鎹埌鍐呭瓨
-                List<Dt_Router> allRouters = BaseDal.QueryData(x => x.InOutType == routeType);
-                
+                // 浠庣紦瀛樺姞杞芥寚瀹氱被鍨嬬殑鎵�鏈夎矾鐢辨暟鎹�
+                List<Dt_Router> allRouters = GetAllRoutersFromCache(routeType);
+
                 // 鍦ㄥ唴瀛樹腑杩涜璺緞鎼滅储
                 routers = FindRoutesInMemory(startPosi, endPosi, allRouters, routeType);
-                
+
                 if (routers.Count == 0)
                 {
                     throw new Exception($"璇ヨ矾寰勬湭閰嶇疆鎴栭厤缃敊璇�,璇锋鏌ヨ澶囪矾鐢变俊鎭�,璧风偣:銆恵startPosi}銆�,缁堢偣:銆恵endPosi}銆�");
@@ -110,7 +133,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 +150,7 @@
 
             // 浣跨敤闃熷垪杩涜骞垮害浼樺厛鎼滅储
             Queue<(Dt_Router router, List<Dt_Router> path)> queue = new Queue<(Dt_Router, List<Dt_Router>)>();
-            
+
             // 灏嗘墍鏈夌粓鐐硅矾鐢卞姞鍏ラ槦鍒�
             foreach (var endRouter in endRouters)
             {
@@ -185,10 +208,12 @@
         {
             try
             {
-                // 鏌ヨ浠庤捣鐐瑰嚭鍙戠殑鎵�鏈夎矾鐢�
-                List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi, 
-                    new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
-                
+                // 浠庣紦瀛樿幏鍙栧叆鍙g被鍨嬬殑鎵�鏈夎矾鐢辨暟鎹紝鍩轰簬璧风偣绛涢�夊悗鎸塈sEnd闄嶅簭鎺掑簭
+                List<Dt_Router> routes = GetAllRoutersFromCache(RouterInOutType.In.ObjToInt())
+                    .Where(x => x.StartPosi == startPosi)
+                    .ToList();
+                routes = routes.OrderByDescending(x => x.IsEnd).ToList();
+
                 // 杩斿洖绗竴涓矾鐢�
                 return routes.FirstOrDefault();
             }
@@ -209,10 +234,12 @@
         {
             try
             {
-                // 鏌ヨ浠庤捣鐐瑰嚭鍙戠殑鎸囧畾绫诲瀷璺敱
-                List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi && x.InOutType == routeType, 
-                    new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
-                
+                // 浠庣紦瀛樿幏鍙栨寚瀹氱被鍨嬬殑鎵�鏈夎矾鐢辨暟鎹紝绛涢�夎捣鐐瑰悗鎸塈sEnd闄嶅簭鎺掑垪
+                List<Dt_Router> routes = GetAllRoutersFromCache(routeType)
+                    .Where(x => x.StartPosi == startPosi)
+                    .ToList();
+                routes = routes.OrderByDescending(x => x.IsEnd).ToList();
+
                 // 杩斿洖绗竴涓矾鐢�
                 return routes.FirstOrDefault();
             }
@@ -234,10 +261,12 @@
         {
             try
             {
-                // 鏌ヨ浠庤捣鐐瑰嚭鍙戠殑鎸囧畾绫诲瀷璺敱
-                List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi && x.InOutType == routeType, 
-                    new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
-                
+                // 浠庣紦瀛樿幏鍙栨寚瀹氱被鍨嬬殑鎵�鏈夎矾鐢辨暟鎹紝绛涢�夎捣鐐瑰悗鎸塈sEnd闄嶅簭鎺掑垪
+                List<Dt_Router> routes = GetAllRoutersFromCache(routeType)
+                    .Where(x => x.StartPosi == startPosi)
+                    .ToList();
+                routes = routes.OrderByDescending(x => x.IsEnd).ToList();
+
                 if (routes.Count == 0)
                     return null;
 
@@ -246,8 +275,8 @@
                 if (directRoute != null)
                     return directRoute;
 
-                // 濡傛灉娌℃湁鐩存帴璺敱锛屼娇鐢ㄦ煡鎵剧畻娉曟壘鍒版湞鍚戠粓鐐圭殑璺敱
-                List<Dt_Router> allRouters = BaseDal.QueryData(x => x.InOutType == routeType);
+                // 濡傛灉娌℃湁鐩存帴璺敱锛屼娇鐢ㄧ紦瀛樹腑鐨勫叏閲忚矾鐢辨暟鎹煡鎵炬湞鍚戠粓鐐圭殑璺敱
+                List<Dt_Router> allRouters = GetAllRoutersFromCache(routeType);
                 foreach (var route in routes)
                 {
                     // 妫�鏌ヤ粠杩欎釜璺敱鐨勪笅涓�涓綅缃槸鍚﹁兘鍒拌揪缁堢偣
@@ -255,6 +284,42 @@
                     if (pathToEnd.Count > 0)
                         return route;
                 }
+
+                // 濡傛灉閮戒笉鑳藉埌杈剧粓鐐癸紝杩斿洖绗竴涓矾鐢�
+                return routes.FirstOrDefault();
+            }
+            catch (Exception ex)
+            {
+                // 璁板綍閿欒淇℃伅
+                return null;
+            }
+        }
+
+        /// <summary>
+        /// 鏍规嵁璧风偣銆佺粓鐐规柟鍚戝拰璺敱绫诲瀷鑾峰彇涓嬩竴涓崟涓瓙鑺傜偣璺敱锛堟櫤鑳介�夋嫨鏈濆悜缁堢偣鐨勮矾鐢憋級
+        /// </summary>
+        /// <param name="startPosi">璧风偣/褰撳墠浣嶇疆</param>
+        /// <param name="endPosi">缁堢偣浣嶇疆锛堢敤浜庢柟鍚戝垽鏂級</param>
+        /// <returns>杩斿洖涓嬩竴涓矾鐢辫妭鐐癸紝浼樺厛杩斿洖鏈濆悜缁堢偣鐨勮矾鐢憋紝濡傛灉娌℃湁鍒欒繑鍥瀗ull</returns>
+        public Dt_Router QueryNextRoute(string startPosi, string endPosi)
+        {
+            try
+            {
+                // 浠庣紦瀛樿幏鍙栧叆鍙e拰鍑哄彛绫诲瀷鐨勬墍鏈夎矾鐢辨暟鎹紝绛涢�夎捣鐐瑰悗鎸塈sEnd闄嶅簭鎺掑簭
+                List<Dt_Router> inRoutes = GetAllRoutersFromCache(RouterInOutType.In.ObjToInt());
+                List<Dt_Router> outRoutes = GetAllRoutersFromCache(RouterInOutType.Out.ObjToInt());
+                List<Dt_Router> routes = inRoutes.Concat(outRoutes)
+                    .Where(x => x.StartPosi == startPosi)
+                    .ToList();
+                routes = routes.OrderByDescending(x => x.IsEnd).ToList();
+
+                if (routes.Count == 0)
+                    return null;
+
+                // 浼樺厛閫夋嫨鐩存帴鎸囧悜缁堢偣鐨勮矾鐢�
+                Dt_Router directRoute = routes.FirstOrDefault(x => x.NextPosi == endPosi || x.ChildPosi == endPosi);
+                if (directRoute != null)
+                    return directRoute;
 
                 // 濡傛灉閮戒笉鑳藉埌杈剧粓鐐癸紝杩斿洖绗竴涓矾鐢�
                 return routes.FirstOrDefault();
@@ -278,7 +343,7 @@
             List<Dt_Router> path = new List<Dt_Router>();
             string currentPosi = startPosi;
             HashSet<string> visitedPositions = new HashSet<string>();
-            
+
             try
             {
                 while (currentPosi != endPosi)
@@ -290,21 +355,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 +379,7 @@
             {
                 return new List<Dt_Router>();
             }
-            
+
             return path;
         }
 
@@ -327,24 +392,34 @@
         {
             // 鍒涘缓涓�涓瓧绗︿覆鍒楄〃锛岀敤浜庡瓨鍌ㄦ墍鏈変綅缃�
             List<string> positions = new List<string>();
-            try
+            var device = _cacheService.Get<List<string>>($"{RedisPrefix.System}:{RedisName.DevicePositions}:{deviceCode}");
+            if (device.IsNullOrEmpty())
             {
-                // 鏌ヨ鎵�鏈夎繘鍏ヨ矾鐢卞櫒鐨勪綅缃�
-                List<string> inRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.In.ObjToInt()).GroupBy(x => x.StartPosi).Select(x => x.Key).ToList();
+                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();
+                    // 鏌ヨ鎵�鏈夌寮�璺敱鍣ㄧ殑浣嶇疆
+                    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();
+                    // 灏嗚繘鍏ュ拰绂诲紑璺敱鍣ㄧ殑浣嶇疆娣诲姞鍒板垪琛ㄤ腑
+                    positions.AddRange(inRouterPositions);
+                    positions.AddRange(outRouterPositions);
+                    // 杩斿洖鍘婚噸鍚庣殑浣嶇疆鍒楄〃
+                    return positions.GroupBy(x => x).Select(x => x.Key).ToList();
+                }
+                catch
+                {
+
+                }
+                finally
+                {
+                    _cacheService.TryAdd($"{RedisPrefix.System}:{RedisName.DevicePositions}:{deviceCode}", positions);
+                }
             }
-            catch
-            {
-
-            }
+            else 
+                positions = device;
             // 杩斿洖浣嶇疆鍒楄〃
             return positions;
         }
@@ -355,8 +430,10 @@
         public List<object> GetAllWholeRouters()
         {
             List<object> data = new List<object>();
-            // 鏌ヨ鎵�鏈夎矾鐢�
-            List<Dt_Router> allRouters = BaseDal.QueryData(x => true);
+            // 浠庣紦瀛樺姞杞藉叆鍙g被鍨嬪拰鍑哄彛绫诲瀷鐨勫叏閲忚矾鐢辨暟鎹苟鍚堝苟
+            List<Dt_Router> inRouters = GetAllRoutersFromCache(RouterInOutType.In.ObjToInt());
+            List<Dt_Router> outRouters = GetAllRoutersFromCache(RouterInOutType.Out.ObjToInt());
+            List<Dt_Router> allRouters = inRouters.Concat(outRouters).ToList();
             // 鏌ヨ鎵�鏈夌粨鏉熺殑璺敱锛屽苟鎸塈d鎺掑簭
             List<Dt_Router> dt_Routers = allRouters.Where(x => x.IsEnd).OrderBy(x => x.Id).ToList();
 
@@ -527,6 +604,12 @@
 
                 // 娣诲姞鏂扮殑璺敱淇℃伅
                 BaseDal.AddData(routers);
+
+                // 閲嶆柊鏌ヨ鍏ㄩ噺璺敱锛堟鏃舵墠鍖呭惈鏂板鐨勮矾鐢憋級锛屽啀鍐欏叆缂撳瓨
+                List<Dt_Router> updatedRouters = BaseDal.QueryData(x => x.InOutType == routerType);
+                string cacheKey = $"Router:AllRouters:{(routerType == RouterInOutType.In.ObjToInt() ? "In" : "Out")}";
+                _cacheService.AddOrUpdate(cacheKey, updatedRouters);
+
                 content = WebResponseContent.Instance.OK();
             }
             catch (Exception ex)

--
Gitblit v1.9.3