DepartmentService.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using GxPress.Repository.Interface;
  5. using GxPress.Request.Department;
  6. using GxPress.Result.Department;
  7. using GxPress.Service.Interface;
  8. using GxPress.Service.Interface.Department;
  9. using GxPress.Repository.Interface.DepartmentUser;
  10. using System.Linq;
  11. using Datory;
  12. using GxPress.EnumConst;
  13. namespace GxPress.Service.Implement.Department
  14. {
  15. /// <summary>
  16. /// 部门
  17. /// </summary>
  18. public partial class DepartmentService : IDepartmentService
  19. {
  20. private readonly IMapper _mapper;
  21. private readonly IDepartmentRepository _departmentRepository;
  22. private readonly IUserRepository _userRepository;
  23. private readonly IUserService _userService;
  24. private readonly IDepartmentUserRepository departmentUserRepository;
  25. public DepartmentService(IMapper mapper, IDepartmentRepository departmentRepository,
  26. IUserRepository userRepository, IUserService userService, IDepartmentUserRepository departmentUserRepository)
  27. {
  28. _departmentRepository = departmentRepository;
  29. _userRepository = userRepository;
  30. _userService = userService;
  31. _mapper = mapper;
  32. this.departmentUserRepository = departmentUserRepository;
  33. }
  34. /// <summary>
  35. /// 根据部门ID获取成员以及下级部门
  36. /// </summary>
  37. /// <param name="request"></param>
  38. /// <returns></returns>
  39. public async Task<DepartmentUserResult> GetDepartmentUserResultAsync(DepartmentUserRequest request)
  40. {
  41. var result = await _departmentRepository.GetDepartmentUserResultAsync(request);
  42. if (result.Departments != null)
  43. {
  44. foreach (var item in result.Departments)
  45. {
  46. item.TypeId = UserlinkConst.Work.GetHashCode();
  47. //获取部门
  48. var departments = new List<DepartmentResult>();
  49. await _departmentRepository.GetDepartmentById(item.Id, departments);
  50. var userIds = await departmentUserRepository.GetUserIdsByDepartmentIdsAsync(departments.Select(n => n.Id));
  51. item.UserCount = await _userRepository.CountAsync(Q.WhereIn(nameof(Entity.User.Id), userIds));
  52. }
  53. }
  54. return result;
  55. }
  56. public async Task<IEnumerable<Entity.Department>> GetUserInDepartmentAsync(int userId)
  57. {
  58. //获取用户所在部门
  59. var departmentIds = await departmentUserRepository.GetDepartmentIdsAsync(userId);
  60. departmentIds = departmentIds.Distinct();
  61. //
  62. var departmentIdsList = new List<int>();
  63. var result = new DepartmentUserInsResult();
  64. result.Result = new List<DepartmentUserInResult>();
  65. var allDepartment = await _departmentRepository.GetAllAsync(Q.Where(nameof(Entity.Department.Id), ">", 0));
  66. foreach (var departmentId in departmentIds)
  67. {
  68. result.Result.Add(new DepartmentUserInResult
  69. {
  70. DepartmentId = departmentId,
  71. Item = await _departmentRepository.GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
  72. });
  73. }
  74. if (departmentIds.Count() == 1)
  75. departmentIdsList.AddRange(departmentIds);
  76. for (int i = 0; i < result.Result.Count; i++)
  77. {
  78. for (int k = i + 1; k < result.Result.Count; k++)
  79. {
  80. if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
  81. {
  82. departmentIdsList.Add(result.Result[i].DepartmentId);
  83. }
  84. else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
  85. {
  86. departmentIdsList.Add(result.Result[k].DepartmentId);
  87. result.Result[i].DepartmentId = result.Result[k].DepartmentId;
  88. result.Result[i].Item = result.Result[k].Item;
  89. }
  90. else
  91. {
  92. //departmentIdsList.Add(result.Result[i].DepartmentId);
  93. departmentIdsList.Add(result.Result[k].DepartmentId);
  94. }
  95. }
  96. }
  97. departmentIdsList = await GetDepartmentDistinct(allDepartment.ToList(), departmentIdsList.Distinct().ToList());
  98. return await _departmentRepository.GetAllAsync(Q.WhereIn(nameof(Entity.Department.Id), departmentIdsList));
  99. //
  100. }
  101. public async Task<List<int>> GetDepartmentDistinct(List<Entity.Department> allDepartment, List<int> departmentIds)
  102. {
  103. //
  104. var departmentIdsList = new List<int>();
  105. var result = new DepartmentUserInsResult();
  106. result.Result = new List<DepartmentUserInResult>();
  107. foreach (var departmentId in departmentIds)
  108. {
  109. result.Result.Add(new DepartmentUserInResult
  110. {
  111. DepartmentId = departmentId,
  112. Item = await _departmentRepository.GetDepartmentByPid(departmentId, new List<Entity.Department>(), allDepartment.ToList())
  113. });
  114. }
  115. if (departmentIds.Count() == 1)
  116. departmentIdsList.AddRange(departmentIds);
  117. for (int i = 0; i < result.Result.Count; i++)
  118. {
  119. for (int k = i + 1; k < result.Result.Count; k++)
  120. {
  121. if (result.Result[i].Item.Any(n => n.Id == result.Result[k].DepartmentId))
  122. {
  123. departmentIdsList.Add(result.Result[i].DepartmentId);
  124. }
  125. else if (result.Result[k].Item.Any(n => n.Id == result.Result[i].DepartmentId))
  126. {
  127. departmentIdsList.Add(result.Result[k].DepartmentId);
  128. result.Result[i].DepartmentId = result.Result[k].DepartmentId;
  129. result.Result[i].Item = result.Result[k].Item;
  130. }
  131. else
  132. {
  133. //departmentIdsList.Add(result.Result[i].DepartmentId);
  134. departmentIdsList.Add(result.Result[i].DepartmentId);
  135. }
  136. }
  137. }
  138. return departmentIdsList;
  139. }
  140. }
  141. }