李昊 4 years ago
parent
commit
912e057023

+ 12 - 3
GxPress/Api/GxPress.Api/AdminControllers/AdminDepartmentController.cs

@@ -20,7 +20,7 @@ namespace GxPress.Api.AdminControllers
     {
         private readonly IDepartmentRepository _departmentRepository;
         private readonly IDepartmentService _departmentService;
-        public AdminDepartmentController(IDepartmentRepository departmentRepository,IDepartmentService departmentService)
+        public AdminDepartmentController(IDepartmentRepository departmentRepository, IDepartmentService departmentService)
         {
             _departmentRepository = departmentRepository;
             _departmentService = departmentService;
@@ -32,7 +32,7 @@ namespace GxPress.Api.AdminControllers
         /// <param name="request"></param>
         /// <returns></returns>
         [HttpPost]
-        public async Task<Department> Add([FromBody]Department request)
+        public async Task<Department> Add([FromBody] Department request)
         {
             return await _departmentRepository.AddAsync(request);
         }
@@ -81,6 +81,15 @@ namespace GxPress.Api.AdminControllers
         {
             return await _departmentRepository.GetTreeAsync(id);
         }
-       
+        /// <summary>
+        /// 树形结构展示,懒加载
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        [HttpGet("list")]
+        public async Task<List<DepartmentTreeResult>> GetTree()
+        {
+            return await _departmentRepository.GetTreeAsync();
+        }
     }
 }

+ 7 - 0
GxPress/Model/GxPress.Result/Department/DepartmentTreeResult.cs

@@ -1,3 +1,5 @@
+using System.Collections.Generic;
+
 namespace GxPress.Result.Department
 {
     /// <summary>
@@ -19,5 +21,10 @@ namespace GxPress.Result.Department
         /// 是否有下级节点
         /// </summary>
         public bool HasChildren { get; set; }
+        /// <summary>
+        /// 子集
+        /// </summary>
+        /// <value></value>
+        public List<DepartmentTreeResult> ChildrenItems { get; set; }
     }
 }

+ 40 - 0
GxPress/Repository/GxPress.Repository.Implement/DepartmentRepository.cs

@@ -207,7 +207,47 @@ namespace GxPress.Repository.Implement
 
             return list;
         }
+        /// <summary>
+        /// 获取树列表
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        public async Task<List<DepartmentTreeResult>> GetTreeAsync()
+        {
+            var departments = await GetAllAsync(Q.Where(nameof(Department.Id), ">", 0));
 
+            var departmentParent = departments.Where(n => n.ParentId == 0);
+            var list = _mapper.Map<List<DepartmentTreeResult>>(departmentParent);
+            foreach (var item in list)
+            {
+                item.HasChildren = departments.Any(n => n.ParentId == item.Id);
+                if (item.HasChildren)
+                {
+                    var departmentChildren = departments.Where(n => n.ParentId == item.Id);
+                    var departmentChildrenTree = departmentChildren.Select(n => _mapper.Map<DepartmentTreeResult>(n));
+                    item.ChildrenItems = GetTreeAsync(departments.ToList(), item.Id, departmentChildrenTree.ToList());
+                }
+            }
+            return list;
+        }
+        public List<DepartmentTreeResult> GetTreeAsync(List<Department> departments, int id, List<DepartmentTreeResult> items)
+        {
+            foreach (var item in items)
+            {
+                item.HasChildren = departments.Any(n => n.ParentId == item.Id);
+                if (item.HasChildren)
+                {
+                    var departmentParent = departments.Where(n => n.ParentId == item.Id);
+                    item.ChildrenItems = _mapper.Map<List<DepartmentTreeResult>>(departmentParent);
+                    foreach (var childrenItem in item.ChildrenItems)
+                    {
+                        GetTreeAsync(departments, childrenItem.Id, items);
+                    }
+                }
+                return items;
+            }
+            return items;
+        }
         /// <summary>
         /// 根据部门ID获取成员以及下级部门
         /// </summary>

+ 6 - 0
GxPress/Repository/GxPress.Repository.Interface/IDepartmentRepository.cs

@@ -88,5 +88,11 @@ namespace GxPress.Repository.Interface
         Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments);
 
         Task<IEnumerable<Department>> GetAllAsync(SqlKata.Query query);
+          /// <summary>
+        /// 获取树列表
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        Task<List<DepartmentTreeResult>> GetTreeAsync();
     }
 }