From 37acb8358f5602a9013ee29c04a45e33483c2329 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期四, 09 四月 2026 15:21:40 +0800
Subject: [PATCH] fix: 修复PLC字符串解析和任务处理逻辑

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs |  432 ++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 321 insertions(+), 111 deletions(-)

diff --git a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
index 4bf89ae..b060189 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
@@ -1,9 +1,5 @@
 锘縰sing SqlSugar;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using WIDESEAWCS_Common;
 using WIDESEAWCS_Core;
 using WIDESEAWCS_Core.BaseServices;
 using WIDESEAWCS_Core.Enums;
@@ -11,6 +7,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 +18,7 @@
     {
         private readonly IDeviceProtocolRepository _deviceProtocolRepository;
         private readonly IDeviceInfoRepository _deviceInfoRepository;
+        private readonly ICacheService _cacheService;
 
         /// <summary>
         /// 璺敱閰嶇疆涓氬姟灞�
@@ -28,10 +26,142 @@
         /// <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>
+        /// 娓呴櫎鎵�鏈夎矾鐢辩紦瀛橈紙鍏ュ彛鍜屽嚭鍙g被鍨嬶級
+        /// </summary>
+        public void ClearRouterCache()
+        {
+            _cacheService.Remove("Router:AllRouters:In");
+            _cacheService.Remove("Router:AllRouters:Out");
+        }
+
+        /// <summary>
+        /// 鏍规嵁璁惧缂栧彿鏌ヨ缁忚繃璇ヨ澶囩殑鎵�鏈夎矾鐢憋紙鍚堝苟鍏ュ彛+鍑哄彛绫诲瀷锛�
+        /// </summary>
+        /// <param name="deviceCode">璁惧缂栧彿</param>
+        /// <returns>缁忚繃璇ヨ澶囩殑璺敱鍒楄〃</returns>
+        public List<Dt_Router> QueryRoutersByDeviceCode(string deviceCode)
+        {
+            // 浠庣紦瀛樿幏鍙栧叆鍙g被鍨嬪拰鍑哄彛绫诲瀷鐨勫叏閲忚矾鐢辨暟鎹�
+            List<Dt_Router> inRouters = GetAllRoutersFromCache(RouterInOutType.In.ObjToInt());
+            List<Dt_Router> outRouters = GetAllRoutersFromCache(RouterInOutType.Out.ObjToInt());
+            // 鍚堝苟鍚庣瓫閫夊嚭缁忚繃鎸囧畾璁惧鐨勮矾鐢憋紙ChildPosiDeviceCode鍖归厤锛�
+            return inRouters.Concat(outRouters)
+                .Where(x => x.ChildPosiDeviceCode == deviceCode)
+                .ToList();
+        }
+
+        /// <summary>
+        /// 鍒ゆ柇涓ょ偣涔嬮棿鏄惁瀛樺湪璺敱锛堝叏绫诲瀷锛�
+        /// </summary>
+        public bool ExistsRouter(string startPosi, string endPosi)
+        {
+            // 浠庣紦瀛樿幏鍙栧叆鍙g被鍨嬪拰鍑哄彛绫诲瀷鐨勫叏閲忚矾鐢辨暟鎹苟鍚堝苟
+            List<Dt_Router> inRouters = GetAllRoutersFromCache(RouterInOutType.In.ObjToInt());
+            List<Dt_Router> outRouters = GetAllRoutersFromCache(RouterInOutType.Out.ObjToInt());
+            var allRouters = inRouters.Concat(outRouters).ToList();
+            // 鍦ㄥ唴瀛樹腑鏌ユ壘浠庤捣鐐瑰埌缁堢偣鐨勮矾鐢�
+            var routes = FindRoutesInMemory(startPosi, endPosi, allRouters, null);
+            return routes.Count > 0;
+        }
+
+        /// <summary>
+        /// 鍒ゆ柇涓ょ偣涔嬮棿鏄惁瀛樺湪鎸囧畾绫诲瀷鐨勮矾鐢�
+        /// </summary>
+        public bool ExistsRouter(string startPosi, string endPosi, int routeType)
+        {
+            // 浠庣紦瀛樿幏鍙栨寚瀹氱被鍨嬬殑鍏ㄩ噺璺敱鏁版嵁
+            List<Dt_Router> allRouters = GetAllRoutersFromCache(routeType);
+            // 鍦ㄥ唴瀛樹腑鏌ユ壘浠庤捣鐐瑰埌缁堢偣鐨勮矾鐢�
+            var routes = FindRoutesInMemory(startPosi, endPosi, allRouters, routeType);
+            return routes.Count > 0;
+        }
+
+        /// <summary>
+        /// 鑾峰彇鍏ㄩ噺璺敱鏁伴噺锛堝叆鍙�+鍑哄彛鍚堣锛�
+        /// </summary>
+        public int GetRouterCount()
+        {
+            // 鍒嗗埆鑾峰彇鍏ュ彛绫诲瀷鍜屽嚭鍙g被鍨嬬殑璺敱鏁伴噺骞剁浉鍔�
+            int inCount = GetAllRoutersFromCache(RouterInOutType.In.ObjToInt()).Count;
+            int outCount = GetAllRoutersFromCache(RouterInOutType.Out.ObjToInt()).Count;
+            return inCount + outCount;
+        }
+
+        /// <summary>
+        /// 鑾峰彇鎸囧畾绫诲瀷璺敱鏁伴噺
+        /// </summary>
+        public int GetRouterCount(int routeType)
+        {
+            // 鑾峰彇鎸囧畾绫诲瀷鐨勫叏閲忚矾鐢辨暟鎹苟杩斿洖鏁伴噺
+            return GetAllRoutersFromCache(routeType).Count;
+        }
+
+        /// <summary>
+        /// 鎵归噺鍒犻櫎鎸囧畾ID鐨勮矾鐢憋紝鍒犻櫎鍚庡悓姝ユ竻闄ゅ搴旂被鍨嬬殑缂撳瓨
+        /// </summary>
+        /// <param name="routerIds">寰呭垹闄ょ殑璺敱ID鍒楄〃</param>
+        /// <returns>杩斿洖澶勭悊缁撴灉</returns>
+        public WebResponseContent DeleteRouters(List<long> routerIds)
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                if (routerIds == null || routerIds.Count == 0)
+                {
+                    return content = WebResponseContent.Instance.Error("寰呭垹闄ょ殑璺敱ID鍒楄〃涓嶈兘涓虹┖");
+                }
+
+                // 鏌ヨ寰呭垹闄よ矾鐢辩殑绫诲瀷锛堢敤浜庡悗缁竻闄ょ紦瀛橈級
+                var routersToDelete = BaseDal.QueryData(x => routerIds.Contains(x.Id));
+                if (routersToDelete.Count == 0)
+                {
+                    return content = WebResponseContent.Instance.Error("鏈壘鍒板緟鍒犻櫎鐨勮矾鐢�");
+                }
+
+                // 璁板綍娑夊強鐨勭被鍨嬶紙鍘婚噸锛�
+                var affectedTypes = routersToDelete.Select(x => x.InOutType).Distinct().ToList();
+
+                // 鎵ц鎵归噺鍒犻櫎
+                BaseDal.DeleteData(routersToDelete);
+
+                // 娓呴櫎鍙楀奖鍝嶇被鍨嬬殑缂撳瓨
+                foreach (var routeType in affectedTypes)
+                {
+                    string cacheKey = $"Router:AllRouters:{(routeType == RouterInOutType.In.ObjToInt() ? "In" : "Out")}";
+                    _cacheService.Remove(cacheKey);
+                }
+
+                content = WebResponseContent.Instance.OK();
+            }
+            catch (Exception ex)
+            {
+                content = WebResponseContent.Instance.Error(ex.Message);
+            }
+            return content;
         }
 
         /// <summary>
@@ -45,12 +175,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 +207,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 +242,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 +259,7 @@
 
             // 浣跨敤闃熷垪杩涜骞垮害浼樺厛鎼滅储
             Queue<(Dt_Router router, List<Dt_Router> path)> queue = new Queue<(Dt_Router, List<Dt_Router>)>();
-            
+
             // 灏嗘墍鏈夌粓鐐硅矾鐢卞姞鍏ラ槦鍒�
             foreach (var endRouter in endRouters)
             {
@@ -185,10 +317,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 +343,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 +370,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 +384,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 +393,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 +452,7 @@
             List<Dt_Router> path = new List<Dt_Router>();
             string currentPosi = startPosi;
             HashSet<string> visitedPositions = new HashSet<string>();
-            
+
             try
             {
                 while (currentPosi != endPosi)
@@ -290,21 +464,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 +488,7 @@
             {
                 return new List<Dt_Router>();
             }
-            
+
             return path;
         }
 
@@ -327,27 +501,38 @@
         {
             // 鍒涘缓涓�涓瓧绗︿覆鍒楄〃锛岀敤浜庡瓨鍌ㄦ墍鏈変綅缃�
             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 (Exception ex)
+                {
+                    ConsoleHelper.WriteErrorLine($"RouterService.QueryAllPositions 鏌ヨ澶辫触: {ex.Message}");
+                }
+                finally
+                {
+                    _cacheService.TryAdd($"{RedisPrefix.System}:{RedisName.DevicePositions}:{deviceCode}", positions);
+                }
             }
-            catch
-            {
-
-            }
+            else
+                positions = device;
             // 杩斿洖浣嶇疆鍒楄〃
             return positions;
         }
+
         /// <summary>
         /// 鑾峰彇璺敱琛ㄤ腑鎵�鏈夊畬鏁寸殑璺敱淇℃伅(鍓嶇璋冪敤灞曠ず鏁版嵁)銆�
         /// </summary>
@@ -355,42 +540,34 @@
         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();
 
             // 閬嶅巻鎵�鏈夌粨鏉熺殑璺敱
             foreach (var item in dt_Routers)
             {
-                // 鑾峰彇褰撳墠璺敱鐨勫瓙璺敱
-                string routes = $"{item.NextPosi},";
-                // 鑾峰彇褰撳墠璺敱鐨勭埗璺敱
-                string str = GetPreviousRoutes(item.StartPosi, allRouters, item.InOutType);
-                // 濡傛灉鐖惰矾鐢变笉涓虹┖
-                if (!string.IsNullOrEmpty(str))
+                // 鑾峰彇鎵�鏈夊彲鑳界殑瀹屾暣璺緞
+                List<List<string>> allPaths = GetAllPaths(item.StartPosi, item.NextPosi, allRouters, item.InOutType);
+
+                // 涓烘瘡鏉$嫭绔嬭矾寰勭敓鎴愮粨鏋�
+                foreach (var path in allPaths)
                 {
-                    // 鍘绘帀鏈�鍚庝竴涓�楀彿
-                    if (str.EndsWith(","))
-                        str = str.Substring(0, str.Length - 1);
-                    // 灏嗙埗璺敱娣诲姞鍒板瓙璺敱涓�
-                    routes += str;
-                }
-                // 濡傛灉褰撳墠璺敱鏄叆鍙�
-                if (item.InOutType == RouterInOutType.In.ObjToInt())
-                {
-                    // 灏嗗瓙璺敱鍙嶈浆骞舵坊鍔犲埌data涓�
-                    List<string> itemRouters = routes.Split(",").Reverse().ToList();
-                    object obj = new { type = RouterInOutType.In, routes = itemRouters };
-                    data.Add(obj);
-                }
-                // 濡傛灉褰撳墠璺敱鏄嚭鍙�
-                else
-                {
-                    // 灏嗗瓙璺敱鍙嶈浆骞舵坊鍔犲埌data涓�
-                    List<string> itemRouters = routes.Split(",").Reverse().ToList();
-                    object obj = new { type = RouterInOutType.Out, routes = itemRouters };
-                    data.Add(obj);
+                    // 濡傛灉褰撳墠璺敱鏄叆鍙�
+                    if (item.InOutType == RouterInOutType.In.ObjToInt())
+                    {
+                        object obj = new { type = RouterInOutType.In, routes = path };
+                        data.Add(obj);
+                    }
+                    // 濡傛灉褰撳墠璺敱鏄嚭鍙�
+                    else
+                    {
+                        object obj = new { type = RouterInOutType.Out, routes = path };
+                        data.Add(obj);
+                    }
                 }
             }
 
@@ -398,42 +575,61 @@
             return data;
         }
 
-        private string GetPreviousRoutes(string startPosi, List<Dt_Router> allRouters, int routerType)
+        /// <summary>
+        /// 鑾峰彇浠庤捣鐐瑰埌缁堢偣鐨勬墍鏈夊畬鏁磋矾寰�
+        /// </summary>
+        /// <param name="startPosi">褰撳墠璧峰浣嶇疆</param>
+        /// <param name="endPosi">缁堢偣浣嶇疆</param>
+        /// <param name="allRouters">鎵�鏈夎矾鐢辨暟鎹�</param>
+        /// <param name="routerType">璺敱绫诲瀷</param>
+        /// <returns>鎵�鏈夊畬鏁磋矾寰勫垪琛紝姣忔潯璺緞鏄粠璧风偣鍒扮粓鐐圭殑浣嶇疆鍒楄〃</returns>
+        private List<List<string>> GetAllPaths(string startPosi, string endPosi, List<Dt_Router> allRouters, int routerType)
         {
-            // 瀹氫箟涓�涓┖瀛楃涓瞨outers
-            string routers = string.Empty;
-            // 鍒ゆ柇startPosi鏄惁涓虹┖
-            if (!string.IsNullOrEmpty(startPosi))
+            List<List<string>> result = new List<List<string>>();
+
+            // 浠庣粓鐐瑰紑濮嬪弽鍚戞煡鎵炬墍鏈夎矾寰�
+            List<List<string>> reversePaths = new List<List<string>>();
+            BuildReversePaths(startPosi, new List<string> { endPosi, startPosi }, allRouters, routerType, reversePaths);
+
+            // 灏嗗弽鍚戣矾寰勮浆涓烘鍚戯紙浠庤捣鐐瑰埌缁堢偣锛�
+            foreach (var reversePath in reversePaths)
             {
-                // 鍒ゆ柇routers鏄惁浠ラ�楀彿缁撳熬
-                if (!routers.EndsWith(","))
-                    // 濡傛灉涓嶆槸锛屽垯灏唖tartPosi娣诲姞鍒皉outers涓紝骞跺湪鍚庨潰鍔犱笂閫楀彿
-                    routers += $"{startPosi},";
-                else
-                    // 濡傛灉鏄紝鍒欏皢startPosi娣诲姞鍒皉outers涓�
-                    routers += $"{startPosi}";
+                result.Add(reversePath.AsEnumerable().Reverse().ToList());
             }
-            // 浠巃llRouters涓瓫閫夊嚭NextPosi绛変簬startPosi涓擨nOutType绛変簬routerType鐨勫厓绱狅紝骞惰浆鎹负List
-            List<Dt_Router> preRouters = allRouters.Where(x => x.NextPosi == startPosi && x.InOutType == routerType).ToList();
-            // 閬嶅巻preRouters涓殑姣忎釜鍏冪礌
-            foreach (var item in preRouters)
+
+            return result;
+        }
+
+        /// <summary>
+        /// 閫掑綊鏋勫缓鍙嶅悜璺緞锛堜粠缁堢偣鍚戣捣鐐规煡鎵撅級
+        /// </summary>
+        /// <param name="currentStartPosi">褰撳墠鑺傜偣鐨勮捣濮嬩綅缃�</param>
+        /// <param name="currentPath">褰撳墠宸叉瀯寤虹殑璺緞锛堝弽鍚戯紝浠庣粓鐐瑰紑濮嬶級</param>
+        /// <param name="allRouters">鎵�鏈夎矾鐢辨暟鎹�</param>
+        /// <param name="routerType">璺敱绫诲瀷</param>
+        /// <param name="result">鏀堕泦鎵�鏈夊畬鏁磋矾寰�</param>
+        private void BuildReversePaths(string currentStartPosi, List<string> currentPath, List<Dt_Router> allRouters, int routerType, List<List<string>> result)
+        {
+            // 鏌ユ壘褰撳墠鑺傜偣鐨勫墠缃妭鐐癸紙NextPosi绛変簬褰撳墠StartPosi鐨勮矾鐢憋級
+            List<Dt_Router> preRouters = allRouters.Where(x => x.NextPosi == currentStartPosi && x.InOutType == routerType).ToList();
+
+            // 濡傛灉娌℃湁鍓嶇疆鑺傜偣锛岃鏄庡凡缁忓埌杈捐矾寰勮捣鐐癸紝淇濆瓨褰撳墠璺緞
+            if (preRouters.Count == 0)
             {
-                // 璋冪敤GetPreviousRoutes鏂规硶锛屼紶鍏tem.StartPosi銆乤llRouters鍜宺outerType锛屽苟灏嗚繑鍥炲�艰祴缁檚tr
-                string str = GetPreviousRoutes(item.StartPosi, allRouters, routerType);
-                // 鍒ゆ柇str鏄惁涓虹┖
-                if (!string.IsNullOrEmpty(str))
-                {
-                    // 鍒ゆ柇routers鏄惁浠ラ�楀彿缁撳熬
-                    if (routers.EndsWith(","))
-                        // 濡傛灉鏄紝鍒欏皢str娣诲姞鍒皉outers涓�
-                        routers += $"{str}";
-                    else
-                        // 濡傛灉涓嶆槸锛屽垯灏唖tr娣诲姞鍒皉outers涓紝骞跺湪鍚庨潰鍔犱笂閫楀彿
-                        routers += $"{str},";
-                }
+                result.Add(new List<string>(currentPath));
+                return;
             }
-            // 杩斿洖routers
-            return routers;
+
+            // 瀵规瘡涓墠缃妭鐐癸紝鍒涘缓鏂扮殑璺緞鍒嗘敮
+            foreach (var preRouter in preRouters)
+            {
+                // 鍒涘缓鏂扮殑璺緞鍓湰
+                List<string> newPath = new List<string>(currentPath);
+                newPath.Add(preRouter.StartPosi);
+
+                // 閫掑綊鏌ユ壘鍓嶇疆鑺傜偣
+                BuildReversePaths(preRouter.StartPosi, newPath, allRouters, routerType, result);
+            }
         }
 
         /// <summary>
@@ -518,6 +714,20 @@
 
                 // 娣诲姞鏂扮殑璺敱淇℃伅
                 BaseDal.AddData(routers);
+
+                // 閲嶆柊鏌ヨ鍏ㄩ噺璺敱锛堟鏃舵墠鍖呭惈鏂板鐨勮矾鐢憋級锛屽啀鍐欏叆缂撳瓨
+                List<Dt_Router> updatedRouters = BaseDal.QueryData(x => x.InOutType == routerType);
+                string cacheKey = $"Router:AllRouters:{(routerType == RouterInOutType.In.ObjToInt() ? "In" : "Out")}";
+
+                try
+                {
+                    _cacheService.AddOrUpdate(cacheKey, updatedRouters);
+                }
+                catch
+                {
+                    // 缂撳瓨鏇存柊澶辫触鏃堕潤榛樺拷鐣ワ紝涓嬫鏌ヨ浼氫粠DB鑷姩閲嶅缓缂撳瓨
+                }
+
                 content = WebResponseContent.Instance.OK();
             }
             catch (Exception ex)
@@ -527,4 +737,4 @@
             return content;
         }
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.9.3