GroupService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Transactions;
  6. using AutoMapper;
  7. using Datory;
  8. using GxPress.Common.Exceptions;
  9. using GxPress.Common.Page;
  10. using GxPress.Common.Tools;
  11. using GxPress.Entity;
  12. using GxPress.EnumConst;
  13. using GxPress.Repository.Interface;
  14. using GxPress.Request.App.AdminVerify;
  15. using GxPress.Request.App.Group;
  16. using GxPress.Request.App.GroupUser;
  17. using GxPress.Request.App.Middle;
  18. using GxPress.Result.App.Group;
  19. using GxPress.Result.App.GroupUser;
  20. using GxPress.Service.Interface.AdminVerify;
  21. using GxPress.Service.Interface.Group;
  22. using GxPress.Service.Interface.Middle;
  23. namespace GxPress.Service.Implement.Group
  24. {
  25. public partial class GroupService : IGroupService
  26. {
  27. private readonly IGroupRepository _groupRepository;
  28. private readonly IGroupUserRepository _groupUserRepository;
  29. private readonly ITopicRepository _topicRepository;
  30. private readonly IMiddleService _middleService;
  31. private readonly IAdminVerifyInsertService _adminVerifyService;
  32. private readonly IMiddleRepository _middleRepository;
  33. private readonly IMapper _mapper;
  34. private readonly IUserRepository _userRepository;
  35. public GroupService(IGroupRepository groupRepository, IGroupUserRepository groupUserRepository,
  36. ITopicRepository topicRepository, IMiddleService middleService,
  37. IAdminVerifyInsertService adminVerifyService, IMiddleRepository middleRepository, IMapper mapper,
  38. IUserRepository userRepository)
  39. {
  40. _groupRepository = groupRepository;
  41. _groupUserRepository = groupUserRepository;
  42. _topicRepository = topicRepository;
  43. _middleService = middleService;
  44. _adminVerifyService = adminVerifyService;
  45. _middleRepository = middleRepository;
  46. _mapper = mapper;
  47. _userRepository = userRepository;
  48. }
  49. /// <summary>
  50. /// 添加小组
  51. /// </summary>
  52. /// <param name="request"></param>
  53. /// <returns></returns>
  54. public async Task<GroupDetailResult> InsertGroup(GroupInRequest request)
  55. {
  56. if (string.IsNullOrEmpty(request.Name))
  57. throw new BusinessException("小组名称不能为空");
  58. if (string.IsNullOrEmpty(request.Introduce))
  59. throw new BusinessException("小组简介不能为空");
  60. if (string.IsNullOrEmpty(request.AvatarUrl))
  61. throw new BusinessException("小组头像不能为空");
  62. try
  63. {
  64. using (TransactionScope transactionScope = new TransactionScope())
  65. {
  66. var groupId = await _groupRepository.InsertAsync(request);
  67. await _groupUserRepository.InsertAsync(request.UserId, groupId, GroupUserRoleTyeConst.SuperAdmin);
  68. //插入用户
  69. //await AddUsersAsync(new GroupUserInRequest { GroupId = groupId, UserIds = request.UserIds });
  70. //新建中间件
  71. var result = new MiddleInsertTypeRequest
  72. {
  73. MiddleSonId = 0,
  74. FolderId = request.GroupFolderId,
  75. FolderType = 7,
  76. MiddleId = groupId,
  77. UserId = request.UserId
  78. };
  79. await _middleService.InsertAsync(result);
  80. var group = await _groupRepository.GetAsync(groupId);
  81. var groupDetail = _mapper.Map<GroupDetailResult>(group);
  82. var user = await _userRepository.GetAsync(group.UserId);
  83. groupDetail.UserName = user.Name;
  84. groupDetail.AvatarUrl = StringUtils.AddDomain(groupDetail.AvatarUrl);
  85. groupDetail.QRCoder = StringUtils.AddDomain(groupDetail.QRCoder);
  86. transactionScope.Complete();
  87. return groupDetail;
  88. }
  89. }
  90. catch (Exception e)
  91. {
  92. throw new BusinessException(e.Message);
  93. }
  94. }
  95. /// <summary>
  96. /// 根据用户ID文件夹ID查询小组
  97. /// </summary>
  98. /// <param name="request"></param>
  99. /// <returns></returns>
  100. public async Task<PagedList<GroupUserFindResult>> FindGroupByGroupFolderIdAsync(GroupSearchRequest request)
  101. {
  102. var result = await _groupRepository.FindGroupByGroupFolderId(request);
  103. foreach (var item in result.Items)
  104. {
  105. // //共享数量
  106. // item.TopicSharingCount = await _groupRepository.SharingCountAsync(item.GroupId);
  107. // //总话题数量
  108. // item.TopicAllCount = await _topicRepository.GetGroupTopicCountAsync(item.GroupId);
  109. //未读话题数量
  110. //item.UReadTopicCount = await _groupRepository.GetUReadCountAsync(request.UserId, item.GroupId);
  111. //获取目录名称
  112. // var middle =
  113. // await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), item.ParentId));
  114. if (string.IsNullOrWhiteSpace(item.CategoryName))
  115. {
  116. item.CategoryName = "根目录";
  117. }
  118. }
  119. return result;
  120. }
  121. /// <summary>
  122. /// 添加成员
  123. /// </summary>
  124. /// <param name="request"></param>
  125. /// <returns></returns>
  126. public async Task<bool> InsertsAsync(GroupUserInRequest request)
  127. {
  128. //获取当前小组
  129. var group = await _groupRepository.GetAsync(request.GroupId);
  130. //不是管理员添加
  131. if (request.SourceType == 2 || group.IsAdmin && request.SourceType == 1)
  132. {
  133. var adminVerifyInsertRequest = new AdminVerifyInsertRequest
  134. {
  135. AdminId = group.UserId,
  136. UserIds = request.UserIds,
  137. VerifyType = GroupTypeConst.Group,
  138. SourceId = group.Id,
  139. UserId = request.UserId,
  140. SourceType = request.SourceType
  141. };
  142. await _adminVerifyService.InsertAsync(adminVerifyInsertRequest);
  143. // if (request.SourceType == 2)
  144. // throw new BusinessException("等待用户同意同意");
  145. // if (request.SourceType == 1)
  146. // throw new BusinessException("等待管理员同意");
  147. }
  148. if (!group.IsAdmin && request.SourceType == 1)
  149. {
  150. //管理员插入
  151. await AddUsersAsync(request);
  152. return true;
  153. }
  154. return false;
  155. }
  156. /// <summary>
  157. /// 小组添加用户
  158. /// </summary>
  159. /// <param name="request"></param>
  160. /// <returns></returns>
  161. public async Task<bool> AddUsersAsync(GroupUserInRequest request)
  162. {
  163. var groupUsers = await _groupUserRepository.GetAllAsync(Q.Where(nameof(GroupUser.GroupId), request.GroupId)
  164. .WhereIn(nameof(GroupUser.UserId), request.UserIds));
  165. var middles = new List<Entity.Middle.Middle>();
  166. var groupUserList = new List<GroupUser>();
  167. foreach (var item in request.UserIds)
  168. {
  169. if (groupUsers.Any(n => n.UserId == item))
  170. continue;
  171. var groupUser = new GroupUser
  172. {
  173. GroupId = request.GroupId,
  174. UserId = item,
  175. GroupUserRoleId = GroupUserRoleTyeConst.General,
  176. IsUDisturb = false
  177. };
  178. groupUserList.Add(groupUser);
  179. //await _groupUserRepository.InsertAsync(groupUser);
  180. //新建中间件
  181. var result = new MiddleInsertTypeRequest
  182. {
  183. MiddleSonId = 0,
  184. FolderId = 0,
  185. FolderType = 7,
  186. MiddleId = request.GroupId,
  187. UserId = item,
  188. IsRead = false
  189. };
  190. var middle = new Entity.Middle.Middle
  191. {
  192. MiddleId = result.MiddleId,
  193. FolderType = result.FolderType,
  194. IsTop = false,
  195. AttributeValue = result.AttributeValue,
  196. ParentId = result.FolderId,
  197. UserId = result.UserId,
  198. MiddleSonId = result.MiddleSonId,
  199. IsAdmin = result.IsAdmin,
  200. NoticeAddresseeType = result.NoticeAddresseeType,
  201. IsRead = false,
  202. IsUpload = false
  203. };
  204. if (result.AttributeValue == 0)
  205. middle.AttributeValue = 1;
  206. middles.Add(middle);
  207. }
  208. await _middleRepository.InsertAsync(middles);
  209. await _groupUserRepository.InsertAsync(groupUserList);
  210. return true;
  211. }
  212. /// <summary>
  213. /// 获取小组详情
  214. /// </summary>
  215. /// <param name="request"></param>
  216. /// <returns></returns>
  217. public async Task<GroupDetailResult> GroupDetailAsync(GroupDetailRequest request)
  218. {
  219. var result = await _groupRepository.GroupDetailAsync(request);
  220. var user = await _userRepository.GetAsync(result.UserId);
  221. result.UserName = user.Name;
  222. result.GroupUserNumber = result.GroupUserListResult.Count();
  223. //获取小组话题数量
  224. result.GroupTopicNumber = await _topicRepository.GetGroupTopicCountAsync(result.Id);
  225. return result;
  226. }
  227. /// <summary>
  228. /// 获取小组详情
  229. /// </summary>
  230. /// <param name="guId"></param>
  231. /// <param name="userId"></param>
  232. /// <returns></returns>
  233. public async Task<GroupDetailResult> GroupDetailByGuIdAsync(string guId, int userId)
  234. {
  235. var result = await _groupRepository.GroupDetailByGuIdAsync(guId, userId);
  236. var user = await _userRepository.GetAsync(result.UserId);
  237. result.UserName = user.Name;
  238. result.GroupUserNumber = result.GroupUserListResult.Count();
  239. result.IsUser = result.GroupUserListResult.Any(n => n.UserId == userId);
  240. //获取小组话题数量
  241. result.GroupTopicNumber = await _topicRepository.GetGroupTopicCountAsync(result.Id);
  242. return result;
  243. }
  244. }
  245. }