From 17e4c7e3e7b3ef60d9da6de3b2a39a14a53c38a0 Mon Sep 17 00:00:00 2001
From: z8018 <1282578289@qq.com>
Date: 星期三, 12 三月 2025 14:11:33 +0800
Subject: [PATCH] 1

---
 WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs |  132 ++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 121 insertions(+), 11 deletions(-)

diff --git a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
index b389354..423ae6d 100644
--- a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
+++ b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
@@ -7,17 +7,27 @@
 using WIDESEAWCS_Core;
 using WIDESEAWCS_Core.BaseServices;
 using WIDESEAWCS_Core.Enums;
+using WIDESEAWCS_Core.Helper;
 using WIDESEAWCS_DTO.BasicInfo;
 using WIDESEAWCS_QuartzJob.Models;
 using WIDESEAWCS_QuartzJob.Repository;
-using WIDESEAWCS_QuartzJob.Service;
 
-namespace WIDESEAWCS_BasicInfoService
+namespace WIDESEAWCS_QuartzJob.Service
 {
+    /// <summary>
+    /// 璺敱閰嶇疆涓氬姟灞�
+    /// </summary>
     public class RouterService : ServiceBase<Dt_Router, IRouterRepository>, IRouterService
     {
         private readonly IDeviceProtocolRepository _deviceProtocolRepository;
         private readonly IDeviceInfoRepository _deviceInfoRepository;
+
+        /// <summary>
+        /// 璺敱閰嶇疆涓氬姟灞�
+        /// </summary>
+        /// <param name="BaseDal"></param>
+        /// <param name="deviceProtocolRepository"></param>
+        /// <param name="deviceInfoRepository"></param>
         public RouterService(IRouterRepository BaseDal, IDeviceProtocolRepository deviceProtocolRepository, IDeviceInfoRepository deviceInfoRepository) : base(BaseDal)
         {
             _deviceProtocolRepository = deviceProtocolRepository;
@@ -36,20 +46,24 @@
             List<Dt_Router> routers = new List<Dt_Router>();
             try
             {
-                List<Dt_Router> dt_Routers = BaseDal.QueryData(x => x.NextPosi == endPosi || x.ChildPosi == endPosi, new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
+                //鏌ヨ涓嬩竴涓矾鐢变俊鎭�
+                List<Dt_Router> dt_Routers = BaseDal.QueryData(x => (x.NextPosi == endPosi || x.ChildPosi == endPosi), new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
                 if (dt_Routers.Count > 0)
                 {
                     foreach (var item in dt_Routers)
                     {
+                        //濡傛灉涓嬩竴涓矾鐢辩殑璧风偣鍜岀粓鐐归兘鍖归厤锛屽垯娣诲姞鍒拌矾鐢卞垪琛ㄤ腑
                         if (item.StartPosi == startPosi && !routers.Any(x => x.Id == item.Id))
                         {
                             routers.Add(item);
                         }
                         else
                         {
+                            //鍚﹀垯锛岄�掑綊鏌ヨ涓嬩竴涓矾鐢辩殑璧风偣
                             List<Dt_Router> tempRouters = QueryNextRoutes(startPosi, item.StartPosi);
                             foreach (var router in tempRouters)
                             {
+                                //濡傛灉涓嬩竴涓矾鐢辩殑璧风偣鍜岀粓鐐归兘鍖归厤锛屽垯娣诲姞鍒拌矾鐢卞垪琛ㄤ腑
                                 if (router.StartPosi == startPosi && !routers.Any(x => x.Id == router.Id))
                                 {
                                     routers.Add(router);
@@ -60,6 +74,60 @@
                 }
                 else
                 {
+                    //濡傛灉鏌ヨ涓嶅埌涓嬩竴涓矾鐢变俊鎭紝鍒欐姏鍑哄紓甯�
+                    throw new Exception($"璇ヨ矾寰勬湭閰嶇疆鎴栭厤缃敊璇�,璇锋鏌ヨ澶囪矾鐢变俊鎭�,璧风偣:銆恵startPosi}銆�,缁堢偣:銆恵endPosi}銆�");
+                }
+            }
+            catch (Exception ex)
+            {
+                //throw new Exception(ex.Message);
+                //璁板綍閿欒淇℃伅
+            }
+            return routers;
+        }
+
+        /// <summary>
+        /// 鏍规嵁璧风偣/褰撳墠浣嶇疆銆佺粓鐐硅幏鍙栦笅涓�涓瓙鑺傜偣銆�
+        /// </summary>
+        /// <param name="startPosi">璧风偣/褰撳墠浣嶇疆銆�</param>
+        /// <param name="endPosi">缁堢偣銆�</param>
+        /// <param name="routeType">璺敱绫诲瀷銆�</param>
+        /// <returns>杩斿洖璺敱瀹炰綋闆嗗悎銆�</returns>
+        public List<Dt_Router> QueryNextRoutes(string startPosi, string endPosi, int routeType)
+        {
+            //todo 鏂规硶闇�浼樺寲
+            List<Dt_Router> routers = new List<Dt_Router>();
+            try
+            {
+                //鏌ヨ涓嬩竴涓矾鐢变俊鎭�
+                List<Dt_Router> dt_Routers = BaseDal.QueryData(x => (x.NextPosi == endPosi || x.ChildPosi == endPosi) && x.InOutType == routeType, new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
+                if (dt_Routers.Count > 0)
+                {
+                    foreach (var item in dt_Routers)
+                    {
+                        //濡傛灉涓嬩竴涓矾鐢辩殑璧风偣鍜岀粓鐐归兘鍖归厤锛屽垯娣诲姞鍒拌矾鐢卞垪琛ㄤ腑
+                        if (item.StartPosi == startPosi && !routers.Any(x => x.Id == item.Id))
+                        {
+                            routers.Add(item);
+                        }
+                        else
+                        {
+                            //鍚﹀垯锛岄�掑綊鏌ヨ涓嬩竴涓矾鐢辩殑璧风偣
+                            List<Dt_Router> tempRouters = QueryNextRoutes(startPosi, item.StartPosi, routeType);
+                            foreach (var router in tempRouters)
+                            {
+                                //濡傛灉涓嬩竴涓矾鐢辩殑璧风偣鍜岀粓鐐归兘鍖归厤锛屽垯娣诲姞鍒拌矾鐢卞垪琛ㄤ腑
+                                if (router.StartPosi == startPosi && !routers.Any(x => x.Id == router.Id))
+                                {
+                                    routers.Add(router);
+                                }
+                            }
+                        }
+                    }
+                }
+                else
+                {
+                    //濡傛灉鏌ヨ涓嶅埌涓嬩竴涓矾鐢变俊鎭紝鍒欐姏鍑哄紓甯�
                     throw new Exception($"璇ヨ矾寰勬湭閰嶇疆鎴栭厤缃敊璇�,璇锋鏌ヨ澶囪矾鐢变俊鎭�,璧风偣:銆恵startPosi}銆�,缁堢偣:銆恵endPosi}銆�");
                 }
             }
@@ -76,26 +144,32 @@
         /// </summary>
         /// <param name="deviceCode">璁惧缂栧彿</param>
         /// <returns>杩斿洖璺敱鐐逛綅缂栧彿(杈撻�佺嚎绔欏彴缂栧彿)闆嗗悎</returns>
+        // 鏍规嵁璁惧缂栫爜鏌ヨ鎵�鏈変綅缃�
         public List<string> QueryAllPositions(string deviceCode)
         {
+            // 鍒涘缓涓�涓瓧绗︿覆鍒楄〃锛岀敤浜庡瓨鍌ㄦ墍鏈変綅缃�
             List<string> positions = new List<string>();
             try
             {
-                List<string> inRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.In).GroupBy(x => x.StartPosi).Select(x => x.Key).ToList();
+                // 鏌ヨ鎵�鏈夎繘鍏ヨ矾鐢卞櫒鐨勪綅缃�
+                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).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();
             }
             catch
             {
 
             }
+            // 杩斿洖浣嶇疆鍒楄〃
             return positions;
         }
-
         /// <summary>
         /// 鑾峰彇璺敱琛ㄤ腑鎵�鏈夊畬鏁寸殑璺敱淇℃伅(鍓嶇璋冪敤灞曠ず鏁版嵁)銆�
         /// </summary>
@@ -103,58 +177,84 @@
         public List<object> GetAllWholeRouters()
         {
             List<object> data = new List<object>();
+            // 鏌ヨ鎵�鏈夎矾鐢�
             List<Dt_Router> allRouters = BaseDal.QueryData(x => true);
+            // 鏌ヨ鎵�鏈夌粨鏉熺殑璺敱锛屽苟鎸塈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.ChildPosi},";
+                // 鑾峰彇褰撳墠璺敱鐨勭埗璺敱
                 string str = GetPreviousRoutes(item.StartPosi, allRouters, item.InOutType);
+                // 濡傛灉鐖惰矾鐢变笉涓虹┖
                 if (!string.IsNullOrEmpty(str))
                 {
+                    // 鍘绘帀鏈�鍚庝竴涓�楀彿
                     if (str.EndsWith(","))
                         str = str.Substring(0, str.Length - 1);
+                    // 灏嗙埗璺敱娣诲姞鍒板瓙璺敱涓�
                     routes += str;
                 }
-                if (item.InOutType == RouterInOutType.In)
+                // 濡傛灉褰撳墠璺敱鏄叆鍙�
+                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);
                 }
             }
 
+            // 杩斿洖data
             return data;
         }
 
-        private string GetPreviousRoutes(string startPosi, List<Dt_Router> allRouters, RouterInOutType routerType)
+        private string GetPreviousRoutes(string startPosi, List<Dt_Router> allRouters, int routerType)
         {
+            // 瀹氫箟涓�涓┖瀛楃涓瞨outers
             string routers = string.Empty;
+            // 鍒ゆ柇startPosi鏄惁涓虹┖
             if (!string.IsNullOrEmpty(startPosi))
             {
+                // 鍒ゆ柇routers鏄惁浠ラ�楀彿缁撳熬
                 if (!routers.EndsWith(","))
+                    // 濡傛灉涓嶆槸锛屽垯灏唖tartPosi娣诲姞鍒皉outers涓紝骞跺湪鍚庨潰鍔犱笂閫楀彿
                     routers += $"{startPosi},";
                 else
+                    // 濡傛灉鏄紝鍒欏皢startPosi娣诲姞鍒皉outers涓�
                     routers += $"{startPosi}";
             }
+            // 浠巃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)
             {
+                // 璋冪敤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},";
                 }
             }
+            // 杩斿洖routers
             return routers;
         }
 
@@ -169,18 +269,22 @@
             WebResponseContent content = new WebResponseContent();
             try
             {
+                // 妫�鏌ュ瓙浣嶇疆缂栧彿鏄惁閲嶅
                 if (routersAddDTOs.GroupBy(x => x.ChildPositionCode).Where(x => !string.IsNullOrEmpty(x.Key)).Select(x => x.Count()).Any(x => x > 1))
                 {
                     return content = WebResponseContent.Instance.Error("瀛愪綅缃紪鍙烽噸澶�");
                 }
 
+                // 妫�鏌ユ牴浣嶇疆缂栧彿鏄惁閲嶅
                 if (routersAddDTOs.GroupBy(x => x.PositionCode).Select(x => x.Count()).Any(x => x > 1))
                 {
                     return content = WebResponseContent.Instance.Error("鏍逛綅缃紪鍙烽噸澶�");
                 }
+                // 鏌ヨ璁惧淇℃伅
                 List<dynamic> deviceCode = _deviceInfoRepository.QueryTabs<Dt_DeviceInfo, Dt_DeviceProtocol, dynamic>((a, b) => new object[] { JoinType.Inner, a.Id == b.DeviceId }, (a, b) => new { b.DeviceChildCode, a.DeviceCode }, (a, b) => true, x => true).Distinct().ToList();
 
                 List<Dt_Router> routers = new List<Dt_Router>();
+                // 閬嶅巻routersAddDTOs锛岀敓鎴怐t_Router瀵硅薄
                 for (int i = 0; i < routersAddDTOs.Count - 1; i++)
                 {
                     dynamic obj = deviceCode.FirstOrDefault(x => x.DeviceChildCode == routersAddDTOs[i + 1].PositionCode || x.DeviceChildCode == routersAddDTOs[i + 1].ChildPositionCode);
@@ -189,7 +293,7 @@
                         ChildPosi = routersAddDTOs[i + 1].PositionCode,
                         ChildPosiDeviceCode = obj.DeviceCode,
                         Depth = 1,
-                        InOutType = (RouterInOutType)routerType,
+                        InOutType = routerType,
                         NextPosi = routersAddDTOs[i + 1].PositionCode,
                         SrmColumn = string.IsNullOrEmpty(routersAddDTOs[i].SCColumn) ? int.TryParse(routersAddDTOs[i + 1].SCColumn, out int col) ? col : null : int.TryParse(routersAddDTOs[i].SCColumn, out int col2) ? col2 : null,
                         SrmLayer = string.IsNullOrEmpty(routersAddDTOs[i].SCLayer) ? int.TryParse(routersAddDTOs[i + 1].SCLayer, out int lay) ? lay : null : int.TryParse(routersAddDTOs[i].SCLayer, out int lay2) ? lay2 : null,
@@ -197,24 +301,29 @@
                         StartPosi = routersAddDTOs[i].PositionCode,
                         IsEnd = false
                     };
+                    // 濡傛灉鏄渶鍚庝竴涓厓绱狅紝璁剧疆缁堢偣浣嶇疆缂栧彿
                     if (i == routersAddDTOs.Count - 2)
                     {
                         if (routerType == (int)RouterInOutType.Out)
                             router.ChildPosi = routersAddDTOs[i + 1].ChildPositionCode;
-                         router.IsEnd = true;
+                        router.IsEnd = true;
                     }
                     routers.Add(router);
                 }
+                // 妫�鏌ヨ捣鐐逛綅缃紪鍙蜂笌瀛愪綅缃紪鍙锋槸鍚︾浉鍚�
                 if (routers.Any(x => x.StartPosi == x.ChildPosi))
                 {
                     return content = WebResponseContent.Instance.Error("杈撳叆鏁版嵁璧风偣浣嶇疆缂栧彿涓庡瓙浣嶇疆缂栧彿鐩稿悓");
                 }
+                // 妫�鏌ヨ捣鐐逛綅缃紪鍙蜂笌缁堢偣浣嶇疆缂栧彿鏄惁鐩稿悓
                 if (routers.Any(x => x.StartPosi == x.NextPosi))
                 {
                     return content = WebResponseContent.Instance.Error("杈撳叆鏁版嵁璧风偣浣嶇疆缂栧彿涓庣粓鐐逛綅缃紪鍙风浉鍚�");
                 }
-                List<Dt_Router> dt_Routers = BaseDal.QueryData(x => x.InOutType == (RouterInOutType)routerType);
+                // 鏌ヨ鏁版嵁搴撲腑宸叉湁鐨勮矾鐢变俊鎭�
+                List<Dt_Router> dt_Routers = BaseDal.QueryData(x => x.InOutType == routerType);
 
+                // 绉婚櫎閲嶅鐨勮矾鐢变俊鎭�
                 dt_Routers.ForEach(x =>
                 {
                     var t = routers.FirstOrDefault(v => v.StartPosi == x.StartPosi && v.NextPosi == x.NextPosi);
@@ -229,6 +338,7 @@
                     }
                 });
 
+                // 娣诲姞鏂扮殑璺敱淇℃伅
                 BaseDal.AddData(routers);
                 content = WebResponseContent.Instance.OK();
             }

--
Gitblit v1.9.3