GroupUserRepository.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Exceptions;
  7. using GxPress.Common.Tools;
  8. using GxPress.Entity;
  9. using GxPress.EnumConst;
  10. using GxPress.Repository.Interface;
  11. using GxPress.Request.App.GroupUser;
  12. using Microsoft.AspNetCore.Hosting;
  13. using Microsoft.Extensions.Options;
  14. using SqlKata;
  15. using Datory;
  16. using Dapper;
  17. namespace GxPress.Repository.Implement
  18. {
  19. public class GroupUserRepository : IGroupUserRepository
  20. {
  21. private readonly Repository<GroupUser> _repository;
  22. private readonly IMapper _mapper;
  23. private readonly IWebHostEnvironment _environment;
  24. private readonly Repository<Entity.Group> _groupRepository;
  25. private readonly string _connectionString;
  26. private readonly string _databaseTypeStr;
  27. public GroupUserRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper,
  28. IWebHostEnvironment environment)
  29. {
  30. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  31. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  32. var databaseType =
  33. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  34. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  35. _repository = new Repository<GroupUser>(database);
  36. _groupRepository = new Repository<Entity.Group>(database);
  37. _environment = environment;
  38. _mapper = mapper;
  39. }
  40. public IDatabase Database => _repository.Database;
  41. public string TableName => _repository.TableName;
  42. public List<TableColumn> TableColumns => _repository.TableColumns;
  43. /// <summary>
  44. /// 修改角色权限
  45. /// </summary>
  46. /// <param name="request"></param>
  47. /// <returns></returns>
  48. public async Task<bool> UpdateAsync(GroupUserUpRequest request)
  49. {
  50. if (request.RoleId == GroupUserRoleTyeConst.SuperAdmin)
  51. throw new BusinessException("已存在");
  52. return await _repository.UpdateAsync(Q.Set(nameof(GroupUser.GroupUserRoleId), request.RoleId)
  53. .Where(nameof(GroupUser.Id), request.Id)) > 0;
  54. }
  55. public async Task<bool> DeleteAsync(Query query)
  56. {
  57. return await _repository.DeleteAsync(query) > 0;
  58. }
  59. /// <summary>
  60. /// 根据小组ID获取成员
  61. /// </summary>
  62. /// <param name="request"></param>
  63. /// <returns></returns>
  64. public async Task<IEnumerable<GroupUser>> FindGroupUserAsync(GroupUserFindRequest request)
  65. {
  66. return await _repository.GetAllAsync(Q.Where(nameof(GroupUser.GroupId), request.GroupId));
  67. }
  68. /// <summary>
  69. /// 插入用户
  70. /// </summary>
  71. /// <param name="userId"></param>
  72. /// <param name="groupId"></param>
  73. /// <param name="groupUserRoleId"></param>
  74. /// <returns></returns>
  75. public async Task<int> InsertAsync(int userId, int groupId, GroupUserRoleTyeConst groupUserRoleId)
  76. {
  77. GroupUser groupUser = new GroupUser { GroupId = groupId, UserId = userId, GroupUserRoleId = groupUserRoleId, IsUDisturb = false };
  78. return await _repository.InsertAsync(groupUser);
  79. }
  80. public async Task<GroupUser> GetAsync(Query q)
  81. {
  82. return await _repository.GetAsync(q);
  83. }
  84. public async Task<IEnumerable<GroupUser>> GetAllAsync(Query q)
  85. {
  86. return await _repository.GetAllAsync(q);
  87. }
  88. public async Task<int> InsertAsync(GroupUser groupUser)
  89. {
  90. return await _repository.InsertAsync(groupUser);
  91. }
  92. public async Task<bool> InsertAsync(List<GroupUser> groupUsers)
  93. {
  94. string sql = @"INSERT INTO `tede_group_user`
  95. (`Guid`,
  96. `CreatedDate`,
  97. `LastModifiedDate`,
  98. `GroupId`,
  99. `UserId`,
  100. `GroupUserRoleId`,
  101. `IsUDisturb`)
  102. VALUES";
  103. if (groupUsers.Count == 0)
  104. return false;
  105. foreach (var item in groupUsers)
  106. {
  107. var guId = Guid.NewGuid().ToString();
  108. var createdDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  109. var IsUDisturb = item.IsUDisturb ? 1 : 0;
  110. sql += $@"('{guId}','{createdDate}','{createdDate}',{item.GroupId},{item.UserId},{item.GroupUserRoleId.GetHashCode()},{IsUDisturb}),";
  111. }
  112. sql = sql.Remove(sql.Length - 1, 1);
  113. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  114. var database = new Database(databaseType, _connectionString);
  115. var connection = database.GetConnection();
  116. var result = await connection.ExecuteAsync(sql);
  117. return result > 0;
  118. }
  119. /// <summary>
  120. /// 设置用户是否免打扰
  121. /// </summary>
  122. /// <param name="groupId"></param>
  123. /// <param name="userId"></param>
  124. /// <returns></returns>
  125. public async Task<bool> SetIsUDisturbAsync(int groupId, int userId)
  126. {
  127. var groupUser = await _repository.GetAsync(Q.Where(nameof(GroupUser.GroupId), groupId)
  128. .Where(nameof(GroupUser.UserId), userId));
  129. return await _repository.UpdateAsync(Q.Set(nameof(GroupUser.IsUDisturb), !groupUser.IsUDisturb)
  130. .Where(nameof(GroupUser.GroupId), groupId)
  131. .Where(nameof(GroupUser.UserId), userId)) > 0;
  132. }
  133. /// <summary>
  134. /// 根据用户ID获取所加入的小组用户
  135. /// </summary>
  136. /// <param name="userId"></param>
  137. /// <returns></returns>
  138. public async Task<IEnumerable<int>> GetGroupUserByUserIdAsync(int userId)
  139. {
  140. string sql = $"SELECT UserId FROM tede_group_user where GroupId in(SELECT GroupId FROM tede_group_user where UserId={userId})";
  141. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  142. var database = new Database(databaseType, _connectionString);
  143. var connection = database.GetConnection();
  144. var result = await connection.QueryAsync<int>(sql);
  145. return result;
  146. }
  147. /// <summary>
  148. /// 获取用户加入小组的人数
  149. /// </summary>
  150. /// <param name="userId"></param>
  151. /// <returns></returns>
  152. public async Task<int> GetUserIntoGroupUserCountAsync(int userId)
  153. {
  154. string sql = $"select count(1) from tede_group_user where GroupId in(select groupId from tede_group_user where userId={userId})";
  155. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  156. var database = new Database(databaseType, _connectionString);
  157. var connection = database.GetConnection();
  158. var result = await connection.ExecuteScalarAsync<int>(sql);
  159. return result;
  160. }
  161. }
  162. }