GroupService.cs 11 KB

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