李昊 4 years ago
parent
commit
86662f165a

+ 11 - 5
gx_api/GxPress/Api/GxPress.Api/AdminControllers/AdminDepartmentController.cs

@@ -1,9 +1,7 @@
 using System.Collections.Generic;
-using System.IO;
 using System.Threading.Tasks;
 using GxPress.Entity;
 using GxPress.Repository.Interface;
-using GxPress.Repository.Interface.DepartmentUser;
 using GxPress.Result.Department;
 using GxPress.Service.Interface.Department;
 using Microsoft.AspNetCore.Authorization;
@@ -21,7 +19,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;
@@ -33,7 +31,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);
         }
@@ -82,6 +80,14 @@ namespace GxPress.Api.AdminControllers
         {
             return await _departmentRepository.GetTreeAsync(id);
         }
-       
+        /// <summary>
+        ///  获取全部部门
+        /// </summary>
+        /// <returns></returns>
+         [HttpGet("list")]
+        public async Task<List<DepartmentTreeResult>> GetAllDepartmentAsync()
+        {
+            return await _departmentRepository.GetAllDepartmentAsync();
+        }
     }
 }

+ 7 - 0
gx_api/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> Children { get; set; }
     }
 }

+ 35 - 0
gx_api/GxPress/Repository/GxPress.Repository.Implement/DepartmentRepository.cs

@@ -254,7 +254,42 @@ namespace GxPress.Repository.Implement
             }
             return departments;
         }
+        /// <summary>
+        ///  获取全部部门
+        /// </summary>
+        /// <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)
+            {
+                return await GetDepartmentById(item.Id, department.ToList());
+            }
+            return department.ToList();
+        }
+        /// <summary>
+        /// 获取所用下级部门
+        /// </summary>
+        /// <param name="id"></param>
+        /// <param name="departments"></param>
+        /// <returns></returns>
+        public async Task<List<DepartmentTreeResult>> GetDepartmentById(int id, List<DepartmentTreeResult> departments)
+        {
+            var list = await _repository.GetAllAsync<DepartmentTreeResult>(Q.Where(nameof(Entity.Department.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);
+                }
 
+            }
+            return departments;
+        }
         /// <summary>
         /// 获取所用下级部门
         /// </summary>

+ 5 - 0
gx_api/GxPress/Repository/GxPress.Repository.Interface/IDepartmentRepository.cs

@@ -88,5 +88,10 @@ namespace GxPress.Repository.Interface
         Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments);
 
         Task<IEnumerable<Department>> GetAllAsync(SqlKata.Query query);
+        /// <summary>
+        ///  获取全部部门
+        /// </summary>
+        /// <returns></returns>
+        Task<List<DepartmentTreeResult>> GetAllDepartmentAsync();
     }
 }