UserMiddleService.cs 11 KB

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