From 94ad631d316da04c46266ddb1fc6e63e6f8f2fae Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期二, 17 三月 2026 17:34:15 +0800
Subject: [PATCH] feat: 同步协议处理、前端交互与业务联调改动

---
 Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs |  122 +++++++++++++++++++++++++++++-----------
 1 files changed, 89 insertions(+), 33 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..8be09ba 100644
--- a/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
+++ b/Code/WCS/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
@@ -11,6 +11,8 @@
 using WIDESEAWCS_DTO.BasicInfo;
 using WIDESEAWCS_QuartzJob.Models;
 using WIDESEAWCS_QuartzJob.Repository;
+using WIDESEAWCS_Common;
+using ICacheService = WIDESEAWCS_Core.Caches.ICacheService;
 
 namespace WIDESEAWCS_QuartzJob.Service
 {
@@ -21,6 +23,7 @@
     {
         private readonly IDeviceProtocolRepository _deviceProtocolRepository;
         private readonly IDeviceInfoRepository _deviceInfoRepository;
+        private readonly ICacheService _cacheService;
 
         /// <summary>
         /// 璺敱閰嶇疆涓氬姟灞�
@@ -28,10 +31,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 +51,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 +81,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 +114,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 +131,7 @@
 
             // 浣跨敤闃熷垪杩涜骞垮害浼樺厛鎼滅储
             Queue<(Dt_Router router, List<Dt_Router> path)> queue = new Queue<(Dt_Router, List<Dt_Router>)>();
-            
+
             // 灏嗘墍鏈夌粓鐐硅矾鐢卞姞鍏ラ槦鍒�
             foreach (var endRouter in endRouters)
             {
@@ -186,9 +190,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 +214,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 +239,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;
 
@@ -267,6 +271,48 @@
         }
 
         /// <summary>
+        /// 鏍规嵁璧风偣銆佺粓鐐规柟鍚戝拰璺敱绫诲瀷鑾峰彇涓嬩竴涓崟涓瓙鑺傜偣璺敱锛堟櫤鑳介�夋嫨鏈濆悜缁堢偣鐨勮矾鐢憋級
+        /// </summary>
+        /// <param name="startPosi">璧风偣/褰撳墠浣嶇疆</param>
+        /// <param name="endPosi">缁堢偣浣嶇疆锛堢敤浜庢柟鍚戝垽鏂級</param>
+        /// <returns>杩斿洖涓嬩竴涓矾鐢辫妭鐐癸紝浼樺厛杩斿洖鏈濆悜缁堢偣鐨勮矾鐢憋紝濡傛灉娌℃湁鍒欒繑鍥瀗ull</returns>
+        public Dt_Router QueryNextRoute(string startPosi, string endPosi)
+        {
+            try
+            {
+                // 鏌ヨ浠庤捣鐐瑰嚭鍙戠殑鎸囧畾绫诲瀷璺敱
+                List<Dt_Router> routes = BaseDal.QueryData(x => x.StartPosi == startPosi,
+                    new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
+
+                if (routes.Count == 0)
+                    return null;
+
+                // 浼樺厛閫夋嫨鐩存帴鎸囧悜缁堢偣鐨勮矾鐢�
+                Dt_Router directRoute = routes.FirstOrDefault(x => x.NextPosi == endPosi || x.ChildPosi == endPosi);
+                if (directRoute != null)
+                    return directRoute;
+
+                //// 濡傛灉娌℃湁鐩存帴璺敱锛屼娇鐢ㄦ煡鎵剧畻娉曟壘鍒版湞鍚戠粓鐐圭殑璺敱
+                //List<Dt_Router> allRouters = BaseDal.QueryData(x => x.InOutType == routeType);
+                //foreach (var route in routes)
+                //{
+                //    // 妫�鏌ヤ粠杩欎釜璺敱鐨勪笅涓�涓綅缃槸鍚﹁兘鍒拌揪缁堢偣
+                //    var pathToEnd = FindRoutesInMemory(route.NextPosi, endPosi, allRouters, routeType);
+                //    if (pathToEnd.Count > 0)
+                //        return route;
+                //}
+
+                // 濡傛灉閮戒笉鑳藉埌杈剧粓鐐癸紝杩斿洖绗竴涓矾鐢�
+                return routes.FirstOrDefault();
+            }
+            catch (Exception ex)
+            {
+                // 璁板綍閿欒淇℃伅
+                return null;
+            }
+        }
+
+        /// <summary>
         /// 鑾峰彇浠庤捣鐐瑰埌缁堢偣鐨勫畬鏁磋矾寰勶紙鎸夐『搴忚繑鍥炴瘡涓瓙鑺傜偣璺敱锛�
         /// </summary>
         /// <param name="startPosi">璧风偣浣嶇疆</param>
@@ -278,7 +324,7 @@
             List<Dt_Router> path = new List<Dt_Router>();
             string currentPosi = startPosi;
             HashSet<string> visitedPositions = new HashSet<string>();
-            
+
             try
             {
                 while (currentPosi != endPosi)
@@ -290,21 +336,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 +360,7 @@
             {
                 return new List<Dt_Router>();
             }
-            
+
             return path;
         }
 
@@ -327,24 +373,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;
         }

--
Gitblit v1.9.3