UserMiddleService.cs 11 KB

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