李昊 4 年之前
父节点
当前提交
7af2f995f3

+ 7 - 0
gx_api/GxPress/Api/GxPress.Api/AppControllers/DepartmentController.cs

@@ -5,6 +5,8 @@ using GxPress.Result.Department;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Mvc;
 using GxPress.Service.Interface.Department;
+using System.Collections.Generic;
+
 namespace GxPress.Api.AppControllers
 {
     [Route("api/app/department")]
@@ -30,5 +32,10 @@ namespace GxPress.Api.AppControllers
         {
             return await departmentService.GetDepartmentUserResultAsync(request);
         }
+        [HttpGet("{userId}")]
+        public async Task<IEnumerable<Entity.Department>> GetUserInDepartmentAsync(int userId)
+        {
+            return await departmentService.GetUserInDepartmentAsync(userId);
+        }
     }
 }

+ 7 - 0
gx_api/GxPress/Model/GxPress.Request/Department/DepartmentRequest.cs

@@ -0,0 +1,7 @@
+namespace GxPress.Request.Department
+{
+    public class DepartmentRequest
+    {
+        
+    }
+}

+ 15 - 2
gx_api/GxPress/Model/GxPress.Result/Department/DepartmentResult.cs

@@ -1,3 +1,5 @@
+using System.Collections.Generic;
+
 namespace GxPress.Result.Department
 {
     /// <summary>
@@ -32,8 +34,19 @@ namespace GxPress.Result.Department
         /// <value></value>
         public int UserCount { get; set; }
 
-        public int TypeId{get;set;}
+        public int TypeId { get; set; }
+
+        public int TypeValue { get; set; }
+    }
+
+    public class DepartmentUserInsResult
+    {
+        public List<DepartmentUserInResult> Result { get; set; }
+    }
+    public class DepartmentUserInResult
+    {
+        public int DepartmentId { get; set; }
+        public List<Entity.Department> Item { get; set; }
 
-        public int TypeValue{get;set;}
     }
 }

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

@@ -356,6 +356,24 @@ namespace GxPress.Repository.Implement
             return departments;
         }
         /// <summary>
+        /// 获取所用下级部门
+        /// </summary>
+        /// <param name="id"></param>
+        /// <param name="departments"></param>
+        /// <returns></returns>
+        public async Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments, List<Department> allDepartments)
+        {
+            var departmentList = allDepartments.Where(n => n.ParentId == Pid);
+            if (departmentList.Count() == 0)
+                return departments;
+            foreach (var item in departmentList)
+            {
+                departments.Add(item);
+                await GetDepartmentByPid(item.Id, departments, allDepartments);
+            }
+            return departments;
+        }
+        /// <summary>
         /// 根据部门ID获取全名称
         /// </summary>
         /// <param name="departmentId"></param>
@@ -381,5 +399,89 @@ namespace GxPress.Repository.Implement
         {
             return await _repository.GetAllAsync(query);
         }
+
+
+        public async Task<IEnumerable<Entity.Department>> GetUserInDepartmentAsync(List<int> departmentIds)
+        {
+            //
+            var departmentIdsList = new List<int>();
+            var result = new DepartmentUserInsResult();
+            result.Result = new List<DepartmentUserInResult>();
+            var allDepartment = await GetAllAsync(Q.Where(nameof(Entity.Department.Id), ">", 0));
+            foreach (var departmentId in departmentIds)
+            {
+                result.Result.Add(new DepartmentUserInResult
+                {
+                    DepartmentId = departmentId,
+                    Item = await GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
+                });
+            }
+            if (departmentIds.Count() == 1)
+                departmentIdsList.AddRange(departmentIds);
+            for (int i = 0; i < result.Result.Count; i++)
+            {
+                for (int k = i + 1; k < result.Result.Count; k++)
+                {
+                    if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
+                    {
+                        departmentIdsList.Add(result.Result[i].DepartmentId);
+                    }
+                    else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
+                    {
+                        departmentIdsList.Add(result.Result[k].DepartmentId);
+                        result.Result[i].DepartmentId = result.Result[k].DepartmentId;
+                        result.Result[i].Item = result.Result[k].Item;
+                    }
+                    else
+                    {
+                        //departmentIdsList.Add(result.Result[i].DepartmentId);
+                        departmentIdsList.Add(result.Result[k].DepartmentId);
+                    }
+                }
+            }
+            departmentIdsList = await GetDepartmentDistinct(allDepartment.ToList(), departmentIdsList.Distinct().ToList());
+            return await GetAllAsync(Q.WhereIn(nameof(Entity.Department.Id), departmentIdsList));
+            //
+        }
+
+        public async Task<List<int>> GetDepartmentDistinct(List<Entity.Department> allDepartment, List<int> departmentIds)
+        {
+            //
+            var departmentIdsList = new List<int>();
+            var result = new DepartmentUserInsResult();
+            result.Result = new List<DepartmentUserInResult>();
+            foreach (var departmentId in departmentIds)
+            {
+                result.Result.Add(new DepartmentUserInResult
+                {
+                    DepartmentId = departmentId,
+                    Item = await GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
+                });
+            }
+            if (departmentIds.Count() == 1)
+                departmentIdsList.AddRange(departmentIds);
+            for (int i = 0; i < result.Result.Count; i++)
+            {
+                for (int k = i + 1; k < result.Result.Count; k++)
+                {
+                    if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
+                    {
+                        departmentIdsList.Add(result.Result[i].DepartmentId);
+                    }
+                    else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
+                    {
+                        departmentIdsList.Add(result.Result[k].DepartmentId);
+                        result.Result[i].DepartmentId = result.Result[k].DepartmentId;
+                        result.Result[i].Item = result.Result[k].Item;
+                    }
+                    else
+                    {
+                        //departmentIdsList.Add(result.Result[i].DepartmentId);
+                        departmentIdsList.Add(result.Result[i].DepartmentId);
+                    }
+                }
+            }
+            return departmentIdsList;
+        }
     }
 }

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

@@ -93,5 +93,13 @@ namespace GxPress.Repository.Interface
         /// </summary>
         /// <returns></returns>
         Task<List<DepartmentTreeResult>> GetAllDepartmentAsync();
+        /// <summary>
+        /// 获取所用下级部门
+        /// </summary>
+        /// <param name="id"></param>
+        /// <param name="departments"></param>
+        /// <returns></returns>
+        Task<List<Department>> GetDepartmentByPid(int Pid, List<Department> departments, List<Department> allDepartments);
+        Task<IEnumerable<Entity.Department>> GetUserInDepartmentAsync(List<int> departmentIds);
     }
 }

+ 86 - 0
gx_api/GxPress/Service/GxPress.Service.Implement/Department/DepartmentService.cs

@@ -57,5 +57,91 @@ namespace GxPress.Service.Implement.Department
             }
             return result;
         }
+
+        public async Task<IEnumerable<Entity.Department>> GetUserInDepartmentAsync(int userId)
+        {
+            //获取用户所在部门
+            var departmentIds = await departmentUserRepository.GetDepartmentIdsAsync(userId);
+            departmentIds = departmentIds.Distinct();
+            //
+            var departmentIdsList = new List<int>();
+            var result = new DepartmentUserInsResult();
+            result.Result = new List<DepartmentUserInResult>();
+            var allDepartment = await _departmentRepository.GetAllAsync(Q.Where(nameof(Entity.Department.Id), ">", 0));
+            foreach (var departmentId in departmentIds)
+            {
+                result.Result.Add(new DepartmentUserInResult
+                {
+                    DepartmentId = departmentId,
+                    Item = await _departmentRepository.GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
+                });
+            }
+            if (departmentIds.Count() == 1)
+                departmentIdsList.AddRange(departmentIds);
+            for (int i = 0; i < result.Result.Count; i++)
+            {
+                for (int k = i + 1; k < result.Result.Count; k++)
+                {
+                    if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
+                    {
+                        departmentIdsList.Add(result.Result[i].DepartmentId);
+                    }
+                    else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
+                    {
+                        departmentIdsList.Add(result.Result[k].DepartmentId);
+                        result.Result[i].DepartmentId = result.Result[k].DepartmentId;
+                        result.Result[i].Item = result.Result[k].Item;
+                    }
+                    else
+                    {
+                        //departmentIdsList.Add(result.Result[i].DepartmentId);
+                        departmentIdsList.Add(result.Result[k].DepartmentId);
+                    }
+                }
+            }
+            departmentIdsList = await GetDepartmentDistinct(allDepartment.ToList(), departmentIdsList.Distinct().ToList());
+            return await _departmentRepository.GetAllAsync(Q.WhereIn(nameof(Entity.Department.Id), departmentIdsList));
+            //
+        }
+
+        public async Task<List<int>> GetDepartmentDistinct(List<Entity.Department> allDepartment, List<int> departmentIds)
+        {
+            //
+            var departmentIdsList = new List<int>();
+            var result = new DepartmentUserInsResult();
+            result.Result = new List<DepartmentUserInResult>();
+            foreach (var departmentId in departmentIds)
+            {
+                result.Result.Add(new DepartmentUserInResult
+                {
+                    DepartmentId = departmentId,
+                    Item = await _departmentRepository.GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
+                });
+            }
+            if (departmentIds.Count() == 1)
+                departmentIdsList.AddRange(departmentIds);
+            for (int i = 0; i < result.Result.Count; i++)
+            {
+                for (int k = i + 1; k < result.Result.Count; k++)
+                {
+                    if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
+                    {
+                        departmentIdsList.Add(result.Result[i].DepartmentId);
+                    }
+                    else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
+                    {
+                        departmentIdsList.Add(result.Result[k].DepartmentId);
+                        result.Result[i].DepartmentId = result.Result[k].DepartmentId;
+                        result.Result[i].Item = result.Result[k].Item;
+                    }
+                    else
+                    {
+                        //departmentIdsList.Add(result.Result[i].DepartmentId);
+                        departmentIdsList.Add(result.Result[i].DepartmentId);
+                    }
+                }
+            }
+            return departmentIdsList;
+        }
     }
 }

+ 15 - 15
gx_api/GxPress/Service/GxPress.Service.Implement/UserService.cs

@@ -26,6 +26,7 @@ using GxPress.Repository.Interface.DepartmentUser;
 using GxPress.Repository.Interface.Friends;
 using GxPress.EnumConst;
 using GxPress.Result.Department;
+using GxPress.Service.Interface.Department;
 
 namespace GxPress.Service.Implement
 {
@@ -377,26 +378,25 @@ namespace GxPress.Service.Implement
             var user = await _userRepository.GetAsync(userId);
             //单位通讯录
             var userLinkResult = new List<UserLinkModelResult>();
-            var departmentCount = await departmentUserRepository.GetDepartmentIdsAsync(userId);
-            if (departmentCount.Count() > 0)
+            var departmentIds = await departmentUserRepository.GetDepartmentIdsAsync(userId);
+            if (departmentIds.Count() > 0)
             {
                 var userLinkModelAttributeResult = new List<UserLinkModelAttributeResult>();
-                foreach (var item in departmentCount)
+
+                var departments = await _departmentRepository.GetUserInDepartmentAsync(departmentIds.ToList());
+                foreach (var department in departments)
                 {
-                    var departments = await _departmentRepository.GetAllAsync(Q.Where(nameof(Entity.Department.Id), item));
-                    foreach (var department in departments)
+                    var userInfoResults = await GetUserInfoByDepartentResult(department.Id);
+                    userLinkModelAttributeResult.Add(new UserLinkModelAttributeResult
                     {
-                        var userInfoResults = await GetUserInfoByDepartentResult(department.Id);
-                        userLinkModelAttributeResult.Add(new UserLinkModelAttributeResult
-                        {
-                            Id = department.Id,
-                            Name = department.Name,
-                            UserCount = userInfoResults.Count(),
-                            TypeId = UserlinkConst.Work.GetHashCode(),
-                            TypeValue = 0,
-                        });
-                    }
+                        Id = department.Id,
+                        Name = department.Name,
+                        UserCount = userInfoResults.Count(),
+                        TypeId = UserlinkConst.Work.GetHashCode(),
+                        TypeValue = 0,
+                    });
                 }
+
                 //单位通讯录
                 userLinkResult.Add(new UserLinkModelResult
                 {

+ 2 - 0
gx_api/GxPress/Service/GxPress.Service.Interface/Department/IDepartmentService.cs

@@ -1,3 +1,4 @@
+using System.Collections.Generic;
 using System.Threading.Tasks;
 using GxPress.Request.Department;
 using GxPress.Result.Department;
@@ -18,5 +19,6 @@ namespace GxPress.Service.Interface.Department
         /// <param name="request"></param>
         /// <returns></returns>
         Task<DepartmentUserResult> GetDepartmentUserResultAsync(DepartmentUserRequest request);
+        Task<IEnumerable<Entity.Department>> GetUserInDepartmentAsync(int userId);
     }
 }