GroupUserRepository.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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> UpdateAsync(Entity.GroupUser groupUser)
  56. {
  57. return await _repository.UpdateAsync(groupUser);
  58. }
  59. public async Task<bool> DeleteAsync(Query query)
  60. {
  61. return await _repository.DeleteAsync(query) > 0;
  62. }
  63. /// <summary>
  64. /// 根据小组ID获取成员
  65. /// </summary>
  66. /// <param name="request"></param>
  67. /// <returns></returns>
  68. public async Task<IEnumerable<GroupUser>> FindGroupUserAsync(GroupUserFindRequest request)
  69. {
  70. return await _repository.GetAllAsync(Q.Where(nameof(GroupUser.GroupId), request.GroupId));
  71. }
  72. /// <summary>
  73. /// 插入用户
  74. /// </summary>
  75. /// <param name="userId"></param>
  76. /// <param name="groupId"></param>
  77. /// <param name="groupUserRoleId"></param>
  78. /// <returns></returns>
  79. public async Task<int> InsertAsync(int userId, int groupId, GroupUserRoleTyeConst groupUserRoleId)
  80. {
  81. var groupUser = new GroupUser { GroupId = groupId, UserId = userId, GroupUserRoleId = groupUserRoleId, IsUDisturb = false, ReadDate = DateTime.Now };
  82. return await _repository.InsertAsync(groupUser);
  83. }
  84. public async Task<GroupUser> GetAsync(Query q)
  85. {
  86. return await _repository.GetAsync(q);
  87. }
  88. public async Task<int> CountAsync(int groupId)
  89. {
  90. string sql = $@"select count(1) from tede_group_user a inner join tede_user b on a.UserId=b.Id where a.GroupId={groupId}";
  91. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  92. var database = new Database(databaseType, _connectionString);
  93. var connection = database.GetConnection();
  94. return await connection.ExecuteScalarAsync<int>(sql);
  95. }
  96. public async Task<IEnumerable<GroupUser>> GetAllAsync(Query q)
  97. {
  98. return await _repository.GetAllAsync(q);
  99. }
  100. public async Task<int> InsertAsync(GroupUser groupUser)
  101. {
  102. return await _repository.InsertAsync(groupUser);
  103. }
  104. public async Task<bool> InsertAsync(List<GroupUser> groupUsers)
  105. {
  106. string sql = @"INSERT INTO `tede_group_user`
  107. (`Guid`,
  108. `CreatedDate`,
  109. `LastModifiedDate`,
  110. `GroupId`,
  111. `UserId`,
  112. `GroupUserRoleId`,
  113. `IsUDisturb`,`ReadDate`)
  114. VALUES";
  115. if (groupUsers.Count == 0)
  116. return false;
  117. foreach (var item in groupUsers)
  118. {
  119. var guId = Guid.NewGuid().ToString();
  120. var createdDate = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  121. var IsUDisturb = item.IsUDisturb ? 1 : 0;
  122. sql += $@"('{guId}','{createdDate}','{createdDate}',{item.GroupId},{item.UserId},{item.GroupUserRoleId.GetHashCode()},{IsUDisturb},'{createdDate}'),";
  123. }
  124. sql = sql.Remove(sql.Length - 1, 1);
  125. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  126. var database = new Database(databaseType, _connectionString);
  127. var connection = database.GetConnection();
  128. var result = await connection.ExecuteAsync(sql);
  129. return result > 0;
  130. }
  131. /// <summary>
  132. /// 设置用户是否免打扰
  133. /// </summary>
  134. /// <param name="groupId"></param>
  135. /// <param name="userId"></param>
  136. /// <returns></returns>
  137. public async Task<bool> SetIsUDisturbAsync(int groupId, int userId)
  138. {
  139. var groupUser = await _repository.GetAsync(Q.Where(nameof(GroupUser.GroupId), groupId)
  140. .Where(nameof(GroupUser.UserId), userId));
  141. return await _repository.UpdateAsync(Q.Set(nameof(GroupUser.IsUDisturb), !groupUser.IsUDisturb)
  142. .Where(nameof(GroupUser.GroupId), groupId)
  143. .Where(nameof(GroupUser.UserId), userId)) > 0;
  144. }
  145. /// <summary>
  146. /// 根据用户ID获取所加入的小组用户
  147. /// </summary>
  148. /// <param name="userId"></param>
  149. /// <returns></returns>
  150. public async Task<IEnumerable<int>> GetGroupUserByUserIdAsync(int userId)
  151. {
  152. string sql = $"SELECT UserId FROM tede_group_user where GroupId in(SELECT GroupId FROM tede_group_user where UserId={userId})";
  153. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  154. var database = new Database(databaseType, _connectionString);
  155. var connection = database.GetConnection();
  156. var result = await connection.QueryAsync<int>(sql);
  157. return result;
  158. }
  159. /// <summary>
  160. /// 获取用户加入小组的人数
  161. /// </summary>
  162. /// <param name="userId"></param>
  163. /// <returns></returns>
  164. public async Task<int> GetUserIntoGroupUserCountAsync(int userId)
  165. {
  166. string sql = $@"SELECT
  167. COUNT(1)
  168. FROM
  169. tede_user
  170. WHERE
  171. Id IN (SELECT
  172. UserId
  173. FROM
  174. tede_group_user
  175. WHERE
  176. GroupId IN (SELECT
  177. groupId
  178. FROM
  179. tede_group_user
  180. WHERE
  181. userId ={userId}))";
  182. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  183. var database = new Database(databaseType, _connectionString);
  184. var connection = database.GetConnection();
  185. var result = await connection.ExecuteScalarAsync<int>(sql);
  186. return result;
  187. }
  188. /// <summary>
  189. /// 查询小组用户是否存在
  190. /// </summary>
  191. /// <param name="userId"></param>
  192. /// <param name="groupId"></param>
  193. /// <returns></returns>
  194. public async Task<bool> ExistsAsync(int userId, int groupId)
  195. {
  196. var query = Q.NewQuery();
  197. query.Where(nameof(Entity.GroupUser.GroupId), groupId);
  198. query.Where(nameof(Entity.GroupUser.UserId), userId);
  199. return await _repository.ExistsAsync(query);
  200. }
  201. /// <summary>
  202. /// 修改用户阅读时间
  203. /// </summary>
  204. /// <param name="userId"></param>
  205. /// <param name="groupId"></param>
  206. /// <returns></returns>
  207. public async Task<bool> UpdateGroupUserReadDateAsync(int userId, int groupId)
  208. {
  209. var query = Q.NewQuery();
  210. query.Where(nameof(Entity.GroupUser.GroupId), groupId);
  211. query.Where(nameof(Entity.GroupUser.UserId), userId);
  212. query.Set(nameof(Entity.GroupUser.ReadDate), DateTime.Now.ToString());
  213. return await _repository.UpdateAsync(query) > 0;
  214. }
  215. }
  216. }