lihao 4 years ago
parent
commit
f99ff89fe8

+ 2 - 0
gx_api/GxPress/Model/GxPress.Request/Department/DepartmentUserRequest.cs

@@ -21,6 +21,8 @@ namespace GxPress.Request.Department
         /// <value></value>
         public int UserId { get; set; }
 
+        public IEnumerable<int> DepartmentIds{get;set;}
+
     }
     /// <summary>
     /// 添加部门用户

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

@@ -219,7 +219,16 @@ namespace GxPress.Repository.Implement
         {
             var result = new DepartmentUserResult();
             var departments = await GetListAsync(request);
-            result.Departments = departments.ToList();
+            var departmentResults = await GetParentDepartmentResults(request.DepartmentIds.ToList());
+            var departmentList = new List<DepartmentResult>();
+            foreach (var item in departments)
+            {
+                if (departmentResults.Any(n => n.Id.Equals(item.Id)))
+                    departmentList.Add(item);
+            }
+            // if (departmentList.Count == 0)
+            //     return result;
+            result.Departments = departmentList;
             //获取用户ID
             var departmentUsers = await departmentUserRepository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), request.DepartmentId).Select(nameof(Entity.DepartmentUser.UserId)));
             //判断是否包含该用户
@@ -483,5 +492,30 @@ namespace GxPress.Repository.Implement
             }
             return departmentIdsList;
         }
+
+        public async Task<IEnumerable<DepartmentResult>> GetParentDepartmentResults(List<int> departmentIds)
+        {
+            var departmentResults = new List<DepartmentResult>();
+            foreach (var item in departmentIds)
+            {
+                await GetParentDepartmentResults(item, departmentResults);
+            }
+            return departmentResults;
+        }
+
+
+        public async Task<IEnumerable<DepartmentResult>> GetParentDepartmentResults(int departmentId, List<DepartmentResult> departmentResults)
+        {
+            var department = await _repository.GetAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.Id), departmentId));
+            if (department == null)
+                return departmentResults;
+            departmentResults.Add(department);
+            var parentDepartment = await _repository.GetAsync<DepartmentResult>(Q.Where(nameof(Entity.Department.Id), department.ParentId));
+            if (parentDepartment == null)
+                return departmentResults;
+            departmentResults.Add(parentDepartment);
+            await GetParentDepartmentResults(parentDepartment.Id, departmentResults);
+            return departmentResults;
+        }
     }
 }

+ 2 - 1
gx_api/GxPress/Service/GxPress.Service.Implement/Department/DepartmentService.cs

@@ -10,7 +10,6 @@ using GxPress.Repository.Interface.DepartmentUser;
 using System.Linq;
 using Datory;
 using GxPress.EnumConst;
-
 namespace GxPress.Service.Implement.Department
 {
 
@@ -41,6 +40,8 @@ namespace GxPress.Service.Implement.Department
         /// <returns></returns>
         public async Task<DepartmentUserResult> GetDepartmentUserResultAsync(DepartmentUserRequest request)
         {
+            //获取所在的部门
+            request.DepartmentIds = await departmentUserRepository.GetDepartmentIdsAsync(request.UserId);
             var result = await _departmentRepository.GetDepartmentUserResultAsync(request);
             if (result.Departments != null)
             {