UserMiddleService.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System.Collections.Generic;
  2. using GxPress.Repository.Interface.UserMiddle;
  3. using GxPress.Request.UserMiddle;
  4. using GxPress.Service.Interface.UserMiddle;
  5. using GxPress.Common.Exceptions;
  6. using System.Threading.Tasks;
  7. using GxPress.Repository.Interface;
  8. using Datory;
  9. using System.Linq;
  10. namespace GxPress.Service.Implement.UserMiddle
  11. {
  12. public class UserMiddleService : IUserMiddleService
  13. {
  14. private readonly IUserMiddleRepository _userMiddleRepoitory;
  15. private readonly IUserRepository _userRepoitory;
  16. private readonly IAddressBookGroupUserRepository _addressBookGroupUserRepository;
  17. private readonly IAddressBookGroupRepository _addressBookGroupRepository;
  18. private readonly IDepartmentRepository _departmentRepository;
  19. private readonly IGroupChatUserRepository groupChatUserRepository;
  20. private readonly IGroupUserRepository groupUserRepository;
  21. public UserMiddleService(IUserMiddleRepository userMiddleRepository, IUserRepository userRepository, IAddressBookGroupUserRepository addressBookGroupUserRepository, IDepartmentRepository departmentRepository, IAddressBookGroupRepository addressBookGroupRepository, IGroupChatUserRepository groupChatUserRepository, IGroupUserRepository groupUserRepository)
  22. {
  23. _userMiddleRepoitory = userMiddleRepository;
  24. _userRepoitory = userRepository;
  25. _addressBookGroupUserRepository = addressBookGroupUserRepository;
  26. _departmentRepository = departmentRepository;
  27. _addressBookGroupRepository = addressBookGroupRepository;
  28. this.groupChatUserRepository = groupChatUserRepository;
  29. this.groupUserRepository = groupUserRepository;
  30. }
  31. /// <summary>
  32. /// 添加用户中间表
  33. /// </summary>
  34. /// <param name="userMiddles"></param>
  35. /// <returns></returns>
  36. public async Task<bool> AddUserMiddleAsync(List<UserMiddleInRequest> request)
  37. {
  38. try
  39. {
  40. foreach (var userMiddle in request)
  41. {
  42. //添加用户中间件
  43. var userMiddleDto = new Entity.UserMiddle.UserMiddle();
  44. userMiddleDto.SourceId = userMiddle.SourceId;
  45. userMiddleDto.SourceType = userMiddle.SourceType;
  46. userMiddleDto.SourceTypeValue = userMiddle.SourceTypeValue;
  47. userMiddleDto.MiddleType = userMiddle.MiddleType;
  48. userMiddleDto.SourceName = userMiddle.SourceName;
  49. userMiddleDto.DataSourceId = userMiddle.DataSourceId;
  50. await _userMiddleRepoitory.InsertAsync(userMiddleDto);
  51. }
  52. }
  53. catch (System.Exception ex)
  54. {
  55. throw new BusinessException(ex.Message);
  56. }
  57. return true;
  58. }
  59. /// <summary>
  60. /// 删除
  61. /// </summary>
  62. /// <param name="query"></param>
  63. /// <returns></returns>
  64. public async Task<bool> DeleteAsync(SqlKata.Query query)
  65. {
  66. return await _userMiddleRepoitory.DeleteAsync(query);
  67. }
  68. /// <summary>
  69. /// 删除收件人
  70. /// </summary>
  71. /// <param name="dataSourceId"></param>
  72. /// <param name="middleType">类型类型 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7小组 10:通知收件人 20:通知抄送人 30:站内信收集人 40:站内信抄送人 400 会议纪要</param>
  73. /// <returns></returns>
  74. public async Task<bool> DeleteAsync(int dataSourceId, int middleType)
  75. {
  76. return await _userMiddleRepoitory.DeleteAsync(Q.Where(nameof(Entity.UserMiddle.UserMiddle.DataSourceId), dataSourceId).Where(nameof(Entity.UserMiddle.UserMiddle.MiddleType), middleType));
  77. }
  78. /// <summary>
  79. /// 是否存在
  80. /// </summary>
  81. /// <param name="query"></param>
  82. /// <returns></returns>
  83. public async Task<bool> ExistsAsync(SqlKata.Query query)
  84. {
  85. return await _userMiddleRepoitory.ExistsAsync(query);
  86. }
  87. /// <summary>
  88. /// 获取用户列表
  89. /// </summary>
  90. /// <param name="middleType">来源类型 1:通知收件人 2:通知抄送人 3:站内信收集人 4:站内信抄送人 5:话题 6:笔记共享文件夹 7:收藏共享文件夹 </param>
  91. /// <param name="sourceId"></param>
  92. /// <returns></returns>
  93. public async Task<List<Entity.User>> FindUsersAsync(List<Request.UserMiddle.UserMiddleInRequest> userMiddles, int userId)
  94. {
  95. List<Entity.User> users = new List<Entity.User>();
  96. var userIds = new List<int>();
  97. userIds = userMiddles.Where(n => n.SourceType == 0).Select(n => n.SourceId).ToList();
  98. foreach (var userMiddle in userMiddles)
  99. {
  100. if (userMiddle.SourceType != 0)
  101. {
  102. if (userMiddle.SourceType == 1)
  103. {
  104. if (userMiddle.SourceId == 0)
  105. //获取所有用户
  106. userIds.AddRange(await _userRepoitory.GetAllIntsAsync(Q.Select(nameof(Entity.User.Id)).Where(nameof(Entity.User.Id), ">", 0)));
  107. else
  108. {
  109. //递归获取部门
  110. var departments = new List<Entity.Department>();
  111. departments = await _departmentRepository.GetDepartmentById(userMiddle.SourceId, departments);
  112. userIds.AddRange(await _userRepoitory.GetAllIntsAsync(Q.WhereIn(nameof(Entity.User.DepartmentId), departments.Select(n => n.Id))));
  113. }
  114. }
  115. if (userMiddle.SourceType == 2)
  116. {
  117. /// <summary>
  118. /// 我的小组
  119. /// </summary>
  120. /// <value></value>
  121. if (userMiddle.SourceTypeValue == 1)
  122. {
  123. if (userMiddle.SourceId == 0)
  124. {
  125. userIds.AddRange(await groupUserRepository.GetGroupUserByUserIdAsync(userId));
  126. }
  127. else
  128. {
  129. var groupUsers = await groupUserRepository.GetAllAsync(Q.Where(nameof(Entity.GroupUser.GroupId), userMiddle.SourceId));
  130. userIds.AddRange(groupUsers.Select(n => n.UserId));
  131. }
  132. }
  133. /// <summary>
  134. /// 我的群聊
  135. /// </summary>
  136. /// <value></value>
  137. if (userMiddle.SourceTypeValue == 2)
  138. {
  139. if (userMiddle.SourceId == 0)
  140. {
  141. userIds.AddRange(await groupChatUserRepository.GetUserIdsAsync(userId));
  142. }
  143. else
  144. {
  145. var groupChatUsers = await groupChatUserRepository.GetAllAsync(Q.Where(nameof(Entity.GroupChatUser.GroupChatId), userMiddle.SourceId));
  146. userIds.AddRange(groupChatUsers.Select(n => n.UserId));
  147. }
  148. }
  149. }
  150. if (userMiddle.SourceType == 3)
  151. {
  152. if (userMiddle.SourceId == 0)
  153. {
  154. var addressBookGroups = await _addressBookGroupRepository.GetAllAsync(Q.Where(nameof(Entity.AddressBookGroup.UserId), userId));
  155. var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync(Q.WhereIn(nameof(Entity.AddressBookGroupUser.AddressBookGroupId), addressBookGroups.Select(n => n.Id)));
  156. userIds.AddRange(addressBookGroupUsers.Select(n => n.UserId).ToList());
  157. }
  158. else
  159. {
  160. //获取自建分组
  161. var addressBookGroupUsers = await _addressBookGroupUserRepository.GetAllAsync(Q.Where(nameof(Entity.AddressBookGroupUser.AddressBookGroupId), userMiddle.SourceId));
  162. userIds.AddRange(addressBookGroupUsers.Select(n => n.UserId).ToList());
  163. }
  164. }
  165. }
  166. }
  167. if (userIds.Count() > 0)
  168. //获取用户
  169. users.AddRange(await _userRepoitory.GetAllAsync(Q.WhereIn(nameof(Entity.User.Id), userIds)));
  170. users = users.Distinct().ToList();
  171. foreach (var item in users)
  172. {
  173. item.AvatarUrl = Common.Tools.StringUtils.AddDomainMin(item.AvatarUrl);
  174. }
  175. return users;
  176. }
  177. /// <summary>
  178. /// 查询列表
  179. /// </summary>
  180. /// <param name="middleType">类型类型 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7小组 10:通知收件人 20:通知抄送人 30:站内信收集人 40:站内信抄送人 400 会议纪要 </param>
  181. /// <param name="sourceId"></param>
  182. /// <returns></returns>
  183. public async Task<IEnumerable<Entity.UserMiddle.UserMiddle>> FindUserMiddlesAsync(int middleType, int dataSourceId)
  184. {
  185. var userMiddles = await _userMiddleRepoitory.GetAllAsync(Q.Where(nameof(Entity.UserMiddle.UserMiddle.MiddleType), middleType).Where(nameof(Entity.UserMiddle.UserMiddle.DataSourceId), dataSourceId));
  186. return userMiddles;
  187. }
  188. }
  189. }