From 7ff298c6834275b63b612af49651673689a39660 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期三, 27 十一月 2024 11:33:18 +0800
Subject: [PATCH] 更新

---
 WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs |   82 +++++++++++++++++++++++++++++++++++++---
 1 files changed, 75 insertions(+), 7 deletions(-)

diff --git a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
index f469b47..4ad7e51 100644
--- a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
+++ b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/RouterService.cs
@@ -12,7 +12,7 @@
 using WIDESEAWCS_QuartzJob.Repository;
 using WIDESEAWCS_QuartzJob.Service;
 
-namespace WIDESEAWCS_BasicInfoService
+namespace WIDESEAWCS_QuartzJob.Service
 {
     public class RouterService : ServiceBase<Dt_Router, IRouterRepository>, IRouterService
     {
@@ -24,26 +24,36 @@
             _deviceInfoRepository = deviceInfoRepository;
         }
 
-        //todo 鏂规硶闇�浼樺寲
+        /// <summary>
+        /// 鏍规嵁璧风偣/褰撳墠浣嶇疆銆佺粓鐐硅幏鍙栦笅涓�涓瓙鑺傜偣銆�
+        /// </summary>
+        /// <param name="startPosi">璧风偣/褰撳墠浣嶇疆銆�</param>
+        /// <param name="endPosi">缁堢偣銆�</param>
+        /// <returns>杩斿洖璺敱瀹炰綋闆嗗悎銆�</returns>
         public List<Dt_Router> QueryNextRoutes(string startPosi, string endPosi)
         {
+            //todo 鏂规硶闇�浼樺寲
             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 } });
                 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);
@@ -54,6 +64,7 @@
                 }
                 else
                 {
+                    //濡傛灉鏌ヨ涓嶅埌涓嬩竴涓矾鐢变俊鎭紝鍒欐姏鍑哄紓甯�
                     throw new Exception($"璇ヨ矾寰勬湭閰嶇疆鎴栭厤缃敊璇�,璇锋鏌ヨ澶囪矾鐢变俊鎭�,璧风偣:銆恵startPosi}銆�,缁堢偣:銆恵endPosi}銆�");
                 }
             }
@@ -65,101 +76,152 @@
             return routers;
         }
 
+        /// <summary>
+        /// 鏍规嵁璁惧缂栧彿鑾峰彇瀵瑰簲鐨勮矾鐢辩偣浣嶇紪鍙�(杈撻�佺嚎绔欏彴缂栧彿)淇℃伅
+        /// </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> outRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.Out).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;
         }
-
-        public List<dynamic> GetAllWholeRouters()
+        /// <summary>
+        /// 鑾峰彇璺敱琛ㄤ腑鎵�鏈夊畬鏁寸殑璺敱淇℃伅(鍓嶇璋冪敤灞曠ず鏁版嵁)銆�
+        /// </summary>
+        /// <returns>鍖垮悕瀵硅薄闆嗗悎銆�</returns>
+        public List<object> GetAllWholeRouters()
         {
-            List<dynamic> data = new List<dynamic>();
+            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)
                 {
+                    // 灏嗗瓙璺敱鍙嶈浆骞舵坊鍔犲埌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;
         }
 
-        public string GetPreviousRoutes(string startPosi, List<Dt_Router> allRouters, RouterInOutType routerType)
+        private string GetPreviousRoutes(string startPosi, List<Dt_Router> allRouters, RouterInOutType 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;
         }
 
+        /// <summary>
+        /// 娣诲姞瀹屾暣璺敱淇℃伅(鍓嶇璋冪敤閰嶇疆璺敱淇℃伅)銆�
+        /// </summary>
+        /// <param name="routersAddDTOs">璁惧璺敱閰嶇疆娣诲姞DTO</param>
+        /// <param name="routerType">璺敱绫诲瀷</param>
+        /// <returns>杩斿洖澶勭悊缁撴灉</returns>
         public WebResponseContent AddRouters(List<RoutersAddDTO> routersAddDTOs, int routerType)
         {
             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);
@@ -176,24 +238,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);
 
+                // 绉婚櫎閲嶅鐨勮矾鐢变俊鎭�
                 dt_Routers.ForEach(x =>
                 {
                     var t = routers.FirstOrDefault(v => v.StartPosi == x.StartPosi && v.NextPosi == x.NextPosi);
@@ -208,6 +275,7 @@
                     }
                 });
 
+                // 娣诲姞鏂扮殑璺敱淇℃伅
                 BaseDal.AddData(routers);
                 content = WebResponseContent.Instance.OK();
             }

--
Gitblit v1.9.3