TopicGroupUserRepository.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Transactions;
  7. using AutoMapper;
  8. using Dapper;
  9. using Datory;
  10. using GxPress.Common.AppOptions;
  11. using GxPress.Common.Page;
  12. using GxPress.Common.Tools;
  13. using GxPress.Entity;
  14. using GxPress.Entity.Topic;
  15. using GxPress.Repository.Interface.Topic;
  16. using GxPress.Request.App.Topic;
  17. using GxPress.Result.App.Topic;
  18. using Microsoft.Extensions.Options;
  19. using SqlKata;
  20. namespace GxPress.Repository.Implement.Topic
  21. {
  22. public class TopicGroupUserRepository:ITopicGroupUserRepository
  23. {
  24. private readonly Repository<TopicGroupUser> _repository;
  25. private readonly IMapper _mapper;
  26. private readonly string _connectionString;
  27. private readonly string _databaseTypestr;
  28. public TopicGroupUserRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  29. {
  30. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  31. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  32. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  33. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  34. _repository = new Repository<TopicGroupUser>(database);
  35. _mapper = mapper;
  36. }
  37. public IDatabase Database => _repository.Database;
  38. public string TableName => _repository.TableName;
  39. public List<TableColumn> TableColumns => _repository.TableColumns;
  40. public async Task<TopicGroupUser> GetAsync(int id)
  41. {
  42. return await _repository.GetAsync(id);
  43. }
  44. public async Task<bool> DeleteAsync(Query query)
  45. {
  46. return await _repository.DeleteAsync(query)>0;
  47. }
  48. /// <summary>
  49. /// 根据ID删除
  50. /// </summary>
  51. /// <param name="ids"></param>
  52. /// <returns></returns>
  53. public async Task<bool> DeleteAsync(List<int> ids)
  54. {
  55. return await _repository.DeleteAsync(Q.WhereIn(nameof(TopicGroupUser.Id), ids))>0;
  56. }
  57. public async Task<bool> InsertAsync(TopicGroupUser topicGroupUser)
  58. {
  59. return await _repository.InsertAsync(topicGroupUser)>0;
  60. }
  61. /// <summary>
  62. /// 根据GroupId删除
  63. /// </summary>
  64. /// <param name="ids"></param>
  65. /// <returns></returns>
  66. public async Task<bool> DeleteGroupIdAsync(List<int> ids)
  67. {
  68. return await _repository.DeleteAsync(Q.WhereIn(nameof(TopicGroupUser.TopicGroupId), ids)) > 0;
  69. }
  70. /// <summary>
  71. /// 添加成员
  72. /// </summary>
  73. /// <param name="userIds"></param>
  74. /// <param name="topicGroupId"></param>
  75. /// <returns></returns>
  76. public async Task<bool> InsertAsync(List<int> userIds, int topicGroupId)
  77. {
  78. try
  79. {
  80. using (TransactionScope transactionScope=new TransactionScope())
  81. {
  82. //获取话题小组成员
  83. var groupUsers =
  84. await _repository.GetAllAsync(Q.Where(nameof(TopicGroupUser.TopicGroupId), topicGroupId));
  85. foreach (var userId in userIds)
  86. {
  87. if (groupUsers.Any(n => n.UserId == userId))
  88. continue;
  89. var topicGroupUser = new TopicGroupUser {TopicGroupId = topicGroupId, UserId = userId};
  90. await _repository.InsertAsync(topicGroupUser);
  91. }
  92. transactionScope.Complete();
  93. }
  94. return true;
  95. }
  96. catch (Exception e)
  97. {
  98. Console.WriteLine(e);
  99. throw;
  100. }
  101. }
  102. /// <summary>
  103. /// 根据分组ID查询用户
  104. /// </summary>
  105. /// <param name="topicGroupIds"></param>
  106. /// <returns></returns>
  107. public async Task<PagedList<TopicGroupUserResult>> GetTopicGroupUserById( int topicGroupId)
  108. {
  109. var result = new PagedList<TopicGroupUserResult>();
  110. string sql =
  111. $"SELECT a.*,b.Name,b.AvatarUrl FROM ccpph.tede_topic_group_user a inner join ccpph.tede_user b on a.UserId=b.Id where 1=1 and a.TopicGroupId={topicGroupId}";
  112. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  113. var database = new Database(databaseType, _connectionString);
  114. var connection = database.GetConnection();
  115. result.Items =
  116. await connection
  117. .QueryAsync<TopicGroupUserResult, User, TopicGroupUserResult>(sql,
  118. (topicGroupUserResult, user) =>
  119. {
  120. topicGroupUserResult.Name = user.Name;
  121. topicGroupUserResult.AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl);
  122. return topicGroupUserResult;
  123. }, splitOn: "Name");
  124. result.Total = result.Items.Count();
  125. return result;
  126. }
  127. /// <summary>
  128. /// 根据分组ID和姓名查询用户
  129. /// </summary>
  130. /// <param name="request"></param>
  131. /// <returns></returns>
  132. public async Task<PagedList<TopicGroupUserResult>> SearchTopicGroupUsersAsync(SearchTopicGroupUsersRequest request)
  133. {
  134. var result = new PagedList<TopicGroupUserResult>();
  135. string sql =
  136. $"SELECT a.*,b.Name,b.AvatarUrl FROM ccpph.tede_topic_group_user a inner join ccpph.tede_user b on a.UserId=b.Id where a.TopicGroupId={request.TopicGroupId} and b.Name like '%{request.Key}%'";
  137. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypestr, DatabaseType.MySql);
  138. var database = new Database(databaseType, _connectionString);
  139. var connection = database.GetConnection();
  140. result.Items =
  141. await connection
  142. .QueryAsync<TopicGroupUserResult, User, TopicGroupUserResult>(sql,
  143. (topicGroupUserResult, user) =>
  144. {
  145. topicGroupUserResult.Name = user.Name;
  146. topicGroupUserResult.AvatarUrl = StringUtils.AddDomain(user.AvatarUrl);
  147. return topicGroupUserResult;
  148. }, splitOn: "Name");
  149. result.Total = result.Items.Count();
  150. return result;
  151. }
  152. public async Task<IEnumerable<TopicGroupUser>> GetAllAsync(Query query)
  153. {
  154. return await _repository.GetAllAsync(query);
  155. }
  156. public async Task<int> CountAsync(Query query)
  157. {
  158. return await _repository.CountAsync(query);
  159. }
  160. public async Task<bool> UpdateAsync(Query query)
  161. {
  162. return await _repository.UpdateAsync(query)>0;
  163. }
  164. }
  165. }