李昊 4 年 前
コミット
ef7a7a6384

+ 4 - 1
gx_api/GxPress/Model/GxPress.Result/Department/DepartmentTreeResult.cs

@@ -16,7 +16,10 @@ namespace GxPress.Result.Department
         /// 部门名称
         /// </summary>
         public string Name { get; set; }
-
+        /// <summary>
+        /// 是否有下级节点
+        /// </summary>
+        public int ParentId { get; set; }
         /// <summary>
         /// 是否有下级节点
         /// </summary>

+ 49 - 12
gx_api/GxPress/Repository/GxPress.Repository.Implement/DepartmentRepository.cs

@@ -270,12 +270,14 @@ namespace GxPress.Repository.Implement
         /// <returns></returns>
         public async Task<List<DepartmentTreeResult>> GetAllDepartmentAsync()
         {
-            var department = await _repository.GetAllAsync<DepartmentTreeResult>(Q.Where(nameof(Department.ParentId), 0));
-            foreach (var item in department)
+            var DepartmentTreeResults = new List<DepartmentTreeResult>();
+            var department = await _repository.GetAllAsync<DepartmentTreeResult>(Q.Where(nameof(Department.Id), ">", 0));
+            DepartmentTreeResults.AddRange(department.Where(n => n.ParentId == 0));
+            foreach (var item in DepartmentTreeResults)
             {
-                return await GetDepartmentById(item.Id, department.ToList());
+                await GetDepartmentById(item.Id, DepartmentTreeResults, department.ToList());
             }
-            return department.ToList();
+            return DepartmentTreeResults;
         }
         /// <summary>
         /// 获取所用下级部门
@@ -283,23 +285,58 @@ namespace GxPress.Repository.Implement
         /// <param name="id"></param>
         /// <param name="departments"></param>
         /// <returns></returns>
-        public async Task<List<DepartmentTreeResult>> GetDepartmentById(int id, List<DepartmentTreeResult> departments)
+        public async Task<List<DepartmentTreeResult>> GetDepartmentById(int id, List<DepartmentTreeResult> departments, List<DepartmentTreeResult> allDepartments)
         {
-            var list = await _repository.GetAllAsync<DepartmentTreeResult>(Q.Where(nameof(Entity.Department.ParentId), id));
+            GetDepartmentTreeResult(id, departments, allDepartments);
+            var list = allDepartments.Where(n => n.ParentId == id);
             if (list.Any())
             {
-                departments.FindLast(n => n.Id == id).HasChildren = true;
-                departments.FindLast(n => n.Id == id).Children = list.ToList();
-                var result = new List<DepartmentTreeResult>();
                 foreach (var item in list)
                 {
-                    // result.Add(item);
-                    await GetDepartmentById(item.Id, departments);
+                    GetDepartmentTreeResult(item.Id, departments, allDepartments);
                 }
-
             }
             return departments;
         }
+
+        public List<DepartmentTreeResult> GetDepartmentTreeResult(int id, List<DepartmentTreeResult> data, List<DepartmentTreeResult> allDepartments)
+        {
+            var list = allDepartments.Where(n => n.ParentId == id).ToList();
+            if (list == null || list.Count == 0)
+                return data;
+            foreach (var item in data)
+            {
+                if (data.Any(n => n.Id == id))
+                {
+                    data.FirstOrDefault(n => n.Id == id).Children = list;
+                    data.FirstOrDefault(n => n.Id == id).HasChildren = true;
+                    break;
+                }
+                else
+                {
+                    if (data.Any(n => n.HasChildren == true))
+                    {
+                        foreach (var children in data.Where(n => n.HasChildren == true))
+                        {
+                            if (children.Children.Any(n => n.Id == id))
+                            {
+                                foreach (var dd in children.Children)
+                                {
+                                    if (dd.Id == id)
+                                    {
+                                        dd.Children = list;
+                                        dd.HasChildren = true;
+                                        break;
+                                    }
+                                }
+
+                            }
+                        }
+                    }
+                }
+            }
+            return data;
+        }
         /// <summary>
         /// 获取所用下级部门
         /// </summary>