GroupService.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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<object>> InsertsAsync(GroupUserInRequest request)
  139. {
  140. var result = new AppResult<object>()
  141. {
  142. Code = 200,
  143. Success = true,
  144. Msg = "",
  145. Data = null
  146. };
  147. request.UserIds = request.UserIds.Distinct().ToList();
  148. //获取当前小组
  149. var group = await _groupRepository.GetAsync(request.GroupId);
  150. //不是管理员添加
  151. if (request.SourceType == 2 || group.IsAdmin && request.SourceType == 1)
  152. {
  153. // if (request.SourceType == 1 && string.IsNullOrWhiteSpace(request.Remark))
  154. // throw new BusinessException("请填写备注!");
  155. var adminVerifyInsertRequest = new AdminVerifyInsertRequest
  156. {
  157. AdminId = group.UserId,
  158. UserIds = request.UserIds,
  159. VerifyType = GroupTypeConst.Group,
  160. SourceId = group.Id,
  161. UserId = request.UserId,
  162. SourceType = request.SourceType,
  163. Remark = request.Remark
  164. };
  165. await _adminVerifyService.InsertAsync(adminVerifyInsertRequest);
  166. if (request.SourceType == 2)
  167. result.Msg = "等待用户同意同意";
  168. if (request.SourceType == 1)
  169. result.Msg = "等待管理员同意";
  170. result.Code = 201;
  171. result.Success = true;
  172. }
  173. if (!group.IsAdmin && request.SourceType == 1)
  174. {
  175. //管理员插入
  176. await AddUsersAsync(request);
  177. return result;
  178. }
  179. return result;
  180. }
  181. /// <summary>
  182. /// 小组添加用户
  183. /// </summary>
  184. /// <param name="request"></param>
  185. /// <returns></returns>
  186. public async Task<bool> AddUsersAsync(GroupUserInRequest request)
  187. {
  188. var groupUsers = await _groupUserRepository.GetAllAsync(Q.Where(nameof(GroupUser.GroupId), request.GroupId)
  189. .WhereIn(nameof(GroupUser.UserId), request.UserIds));
  190. var middles = new List<Entity.Middle.Middle>();
  191. var groupUserList = new List<GroupUser>();
  192. foreach (var item in request.UserIds)
  193. {
  194. if (groupUsers.Any(n => n.UserId == item))
  195. continue;
  196. var groupUser = new GroupUser
  197. {
  198. GroupId = request.GroupId,
  199. UserId = item,
  200. GroupUserRoleId = GroupUserRoleTyeConst.General,
  201. IsUDisturb = false
  202. };
  203. groupUserList.Add(groupUser);
  204. //await _groupUserRepository.InsertAsync(groupUser);
  205. //新建中间件
  206. var result = new MiddleInsertTypeRequest
  207. {
  208. MiddleSonId = 0,
  209. FolderId = 0,
  210. FolderType = 7,
  211. MiddleId = request.GroupId,
  212. UserId = item,
  213. IsRead = false
  214. };
  215. var middle = new Entity.Middle.Middle
  216. {
  217. MiddleId = result.MiddleId,
  218. FolderType = result.FolderType,
  219. IsTop = false,
  220. AttributeValue = result.AttributeValue,
  221. ParentId = result.FolderId,
  222. UserId = result.UserId,
  223. MiddleSonId = result.MiddleSonId,
  224. IsAdmin = result.IsAdmin,
  225. NoticeAddresseeType = result.NoticeAddresseeType,
  226. IsRead = false,
  227. IsUpload = false
  228. };
  229. if (result.AttributeValue == 0)
  230. middle.AttributeValue = 1;
  231. middles.Add(middle);
  232. }
  233. await _middleRepository.InsertAsync(middles);
  234. await _groupUserRepository.InsertAsync(groupUserList);
  235. return true;
  236. }
  237. /// <summary>
  238. /// 获取小组详情
  239. /// </summary>
  240. /// <param name="request"></param>
  241. /// <returns></returns>
  242. public async Task<GroupDetailResult> GroupDetailAsync(GroupDetailRequest request)
  243. {
  244. var result = await _groupRepository.GroupDetailAsync(request);
  245. var user = await _userRepository.GetAsync(result.UserId);
  246. if (result.IsUser == false)
  247. {
  248. var adminVerify = await adminVerifyRepository.GetAsync(Q.Where(nameof(Entity.AdminVerify.VerifyType), 3).Where(nameof(Entity.AdminVerify.SourceId), result.Id));
  249. result.IsApply = adminVerify != null && adminVerify.DisposeType == 0;
  250. }
  251. if (result.GroupCategroyId > 0)
  252. {
  253. var groupCategory = await groupCategoryRepository.GetAsync(result.GroupCategroyId);
  254. if (groupCategory != null)
  255. result.CategroyName = groupCategory.PiazzaName;
  256. }
  257. result.UserName = user.Name;
  258. result.GroupUserNumber = result.GroupUserListResult.Count();
  259. //获取小组话题数量
  260. result.GroupTopicNumber = await _topicRepository.GetGroupTopicCountAsync(result.Id);
  261. return result;
  262. }
  263. /// <summary>
  264. /// 获取小组详情
  265. /// </summary>
  266. /// <param name="guId"></param>
  267. /// <param name="userId"></param>
  268. /// <returns></returns>
  269. public async Task<GroupDetailResult> GroupDetailByGuIdAsync(string guId, int userId)
  270. {
  271. var result = await _groupRepository.GroupDetailByGuIdAsync(guId, userId);
  272. var user = await _userRepository.GetAsync(result.UserId);
  273. result.UserName = user.Name;
  274. result.GroupUserNumber = result.GroupUserListResult.Count();
  275. result.IsUser = result.GroupUserListResult.Any(n => n.UserId == userId);
  276. //获取小组话题数量
  277. result.GroupTopicNumber = await _topicRepository.GetGroupTopicCountAsync(result.Id);
  278. return result;
  279. }
  280. /// <summary>
  281. /// 小组转移
  282. /// </summary>
  283. /// <param name="request"></param>
  284. /// <returns></returns>
  285. public async Task<bool> SetGroupTransferAsync(GroupTransferReqeust request)
  286. {
  287. var group = await _groupRepository.GetAsync(request.GroupId);
  288. if (group.UserId == request.UserId)
  289. {
  290. using (var transactionScope = new TransactionScope())
  291. {
  292. var query = Q.NewQuery();
  293. query.Where(nameof(Entity.GroupUser.UserId), request.TransferUserId);
  294. query.Where(nameof(Entity.GroupUser.GroupId), request.GroupId);
  295. var groupUser = await _groupUserRepository.GetAsync(query);
  296. if (groupUser == null)
  297. throw new BusinessException("转移人不是该小组成员");
  298. groupUser.GroupUserRoleId = GroupUserRoleTyeConst.SuperAdmin;
  299. await _groupUserRepository.UpdateAsync(groupUser);
  300. //获取
  301. query = Q.NewQuery();
  302. query.Where(nameof(Entity.GroupUser.UserId), request.UserId);
  303. query.Where(nameof(Entity.GroupUser.GroupId), request.GroupId);
  304. groupUser = await _groupUserRepository.GetAsync(query);
  305. if (groupUser == null)
  306. throw new BusinessException("转移人不是该小组成员");
  307. groupUser.GroupUserRoleId = GroupUserRoleTyeConst.General;
  308. await _groupUserRepository.UpdateAsync(groupUser);
  309. //
  310. group.UserId = request.TransferUserId;
  311. await _groupRepository.UpdateAsync(group);
  312. transactionScope.Complete();
  313. return true;
  314. }
  315. }
  316. throw new BusinessException("小组不是该用户创建者");
  317. }
  318. }
  319. }