GroupService.cs 14 KB

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