GroupService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. item.TypeId = UserlinkConst.System.GetHashCode();
  109. if (item.AttributeValue == 2)
  110. item.TypeValue = UserlinkConst.MyGroupFolder.GetHashCode();
  111. else
  112. item.TypeValue = UserlinkConst.MyGroup.GetHashCode();
  113. // //共享数量
  114. // item.TopicSharingCount = await _groupRepository.SharingCountAsync(item.GroupId);
  115. // //总话题数量
  116. // item.TopicAllCount = await _topicRepository.GetGroupTopicCountAsync(item.GroupId);
  117. //未读话题数量
  118. //item.UReadTopicCount = await _groupRepository.GetUReadCountAsync(request.UserId, item.GroupId);
  119. //获取目录名称
  120. // var middle =
  121. // await _middleRepository.GetMiddleAsync(Q.Where(nameof(Entity.Middle.Middle.Id), item.ParentId));
  122. if (string.IsNullOrWhiteSpace(item.CategoryName))
  123. {
  124. item.CategoryName = "根目录";
  125. }
  126. }
  127. return result;
  128. }
  129. /// <summary>
  130. /// 添加成员
  131. /// </summary>
  132. /// <param name="request"></param>
  133. /// <returns></returns>
  134. public async Task<bool> InsertsAsync(GroupUserInRequest request)
  135. {
  136. request.UserIds = request.UserIds.Distinct().ToList();
  137. //获取当前小组
  138. var group = await _groupRepository.GetAsync(request.GroupId);
  139. //不是管理员添加
  140. if (request.SourceType == 2 || group.IsAdmin && request.SourceType == 1)
  141. {
  142. var adminVerifyInsertRequest = new AdminVerifyInsertRequest
  143. {
  144. AdminId = group.UserId,
  145. UserIds = request.UserIds,
  146. VerifyType = GroupTypeConst.Group,
  147. SourceId = group.Id,
  148. UserId = request.UserId,
  149. SourceType = request.SourceType
  150. };
  151. await _adminVerifyService.InsertAsync(adminVerifyInsertRequest);
  152. // if (request.SourceType == 2)
  153. // throw new BusinessException("等待用户同意同意");
  154. // if (request.SourceType == 1)
  155. // throw new BusinessException("等待管理员同意");
  156. }
  157. if (!group.IsAdmin && request.SourceType == 1)
  158. {
  159. //管理员插入
  160. await AddUsersAsync(request);
  161. return true;
  162. }
  163. return true;
  164. }
  165. /// <summary>
  166. /// 小组添加用户
  167. /// </summary>
  168. /// <param name="request"></param>
  169. /// <returns></returns>
  170. public async Task<bool> AddUsersAsync(GroupUserInRequest request)
  171. {
  172. var groupUsers = await _groupUserRepository.GetAllAsync(Q.Where(nameof(GroupUser.GroupId), request.GroupId)
  173. .WhereIn(nameof(GroupUser.UserId), request.UserIds));
  174. var middles = new List<Entity.Middle.Middle>();
  175. var groupUserList = new List<GroupUser>();
  176. foreach (var item in request.UserIds)
  177. {
  178. if (groupUsers.Any(n => n.UserId == item))
  179. continue;
  180. var groupUser = new GroupUser
  181. {
  182. GroupId = request.GroupId,
  183. UserId = item,
  184. GroupUserRoleId = GroupUserRoleTyeConst.General,
  185. IsUDisturb = false
  186. };
  187. groupUserList.Add(groupUser);
  188. //await _groupUserRepository.InsertAsync(groupUser);
  189. //新建中间件
  190. var result = new MiddleInsertTypeRequest
  191. {
  192. MiddleSonId = 0,
  193. FolderId = 0,
  194. FolderType = 7,
  195. MiddleId = request.GroupId,
  196. UserId = item,
  197. IsRead = false
  198. };
  199. var middle = new Entity.Middle.Middle
  200. {
  201. MiddleId = result.MiddleId,
  202. FolderType = result.FolderType,
  203. IsTop = false,
  204. AttributeValue = result.AttributeValue,
  205. ParentId = result.FolderId,
  206. UserId = result.UserId,
  207. MiddleSonId = result.MiddleSonId,
  208. IsAdmin = result.IsAdmin,
  209. NoticeAddresseeType = result.NoticeAddresseeType,
  210. IsRead = false,
  211. IsUpload = false
  212. };
  213. if (result.AttributeValue == 0)
  214. middle.AttributeValue = 1;
  215. middles.Add(middle);
  216. }
  217. await _middleRepository.InsertAsync(middles);
  218. await _groupUserRepository.InsertAsync(groupUserList);
  219. return true;
  220. }
  221. /// <summary>
  222. /// 获取小组详情
  223. /// </summary>
  224. /// <param name="request"></param>
  225. /// <returns></returns>
  226. public async Task<GroupDetailResult> GroupDetailAsync(GroupDetailRequest request)
  227. {
  228. var result = await _groupRepository.GroupDetailAsync(request);
  229. var user = await _userRepository.GetAsync(result.UserId);
  230. if (result.IsUser == false)
  231. {
  232. var adminVerify = await adminVerifyRepository.GetAsync(Q.Where(nameof(Entity.AdminVerify.VerifyType), 3).Where(nameof(Entity.AdminVerify.SourceId), result.Id));
  233. result.IsApply = adminVerify != null && adminVerify.DisposeType == 0;
  234. }
  235. result.UserName = user.Name;
  236. result.GroupUserNumber = result.GroupUserListResult.Count();
  237. //获取小组话题数量
  238. result.GroupTopicNumber = await _topicRepository.GetGroupTopicCountAsync(result.Id);
  239. return result;
  240. }
  241. /// <summary>
  242. /// 获取小组详情
  243. /// </summary>
  244. /// <param name="guId"></param>
  245. /// <param name="userId"></param>
  246. /// <returns></returns>
  247. public async Task<GroupDetailResult> GroupDetailByGuIdAsync(string guId, int userId)
  248. {
  249. var result = await _groupRepository.GroupDetailByGuIdAsync(guId, userId);
  250. var user = await _userRepository.GetAsync(result.UserId);
  251. result.UserName = user.Name;
  252. result.GroupUserNumber = result.GroupUserListResult.Count();
  253. result.IsUser = result.GroupUserListResult.Any(n => n.UserId == userId);
  254. //获取小组话题数量
  255. result.GroupTopicNumber = await _topicRepository.GetGroupTopicCountAsync(result.Id);
  256. return result;
  257. }
  258. /// <summary>
  259. /// 小组转移
  260. /// </summary>
  261. /// <param name="request"></param>
  262. /// <returns></returns>
  263. public async Task<bool> SetGroupTransferAsync(GroupTransferReqeust request)
  264. {
  265. var group = await _groupRepository.GetAsync(request.GroupId);
  266. if (group.UserId == request.UserId)
  267. {
  268. using (var transactionScope = new TransactionScope())
  269. {
  270. var query = Q.NewQuery();
  271. query.Where(nameof(Entity.GroupUser.UserId), request.TransferUserId);
  272. query.Where(nameof(Entity.GroupUser.GroupId), request.GroupId);
  273. var groupUser = await _groupUserRepository.GetAsync(query);
  274. if (groupUser == null)
  275. throw new BusinessException("转移人不是该小组成员");
  276. groupUser.GroupUserRoleId = GroupUserRoleTyeConst.SuperAdmin;
  277. await _groupUserRepository.UpdateAsync(groupUser);
  278. //获取
  279. query = Q.NewQuery();
  280. query.Where(nameof(Entity.GroupUser.UserId), request.UserId);
  281. query.Where(nameof(Entity.GroupUser.GroupId), request.GroupId);
  282. groupUser = await _groupUserRepository.GetAsync(query);
  283. if (groupUser == null)
  284. throw new BusinessException("转移人不是该小组成员");
  285. groupUser.GroupUserRoleId = GroupUserRoleTyeConst.General;
  286. await _groupUserRepository.UpdateAsync(groupUser);
  287. transactionScope.Complete();
  288. return true;
  289. }
  290. }
  291. throw new BusinessException("小组不是该用户创建者");
  292. }
  293. }
  294. }