GroupService.cs 14 KB

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