TopicGroupRepository.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using Dapper;
  5. using Datory;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Tools;
  8. using GxPress.Entity.Topic;
  9. using GxPress.EnumConst;
  10. using GxPress.Repository.Interface.Topic;
  11. using GxPress.Request.App.Topic;
  12. using GxPress.Result;
  13. using Microsoft.Extensions.Options;
  14. using SqlKata;
  15. using System.Transactions;
  16. namespace GxPress.Repository.Implement.Topic
  17. {
  18. public class TopicGroupRepository : ITopicGroupRepository
  19. {
  20. private readonly Repository<TopicGroup> _repository;
  21. private readonly IMapper _mapper;
  22. private readonly string _connectionString;
  23. private readonly string _databaseTypestr;
  24. public TopicGroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  25. {
  26. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  27. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  28. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  29. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  30. _repository = new Repository<TopicGroup>(database);
  31. _mapper = mapper;
  32. }
  33. public IDatabase Database => _repository.Database;
  34. public string TableName => _repository.TableName;
  35. public List<TableColumn> TableColumns => _repository.TableColumns;
  36. public async Task<TopicGroup> GetAsync(int id)
  37. {
  38. return await _repository.GetAsync(id);
  39. }
  40. /// <summary>
  41. /// 根据ID删除分组
  42. /// </summary>
  43. /// <param name="ids"></param>
  44. /// <returns></returns>
  45. public async Task<bool> DeleteAsync(List<int> ids)
  46. {
  47. return await _repository.DeleteAsync(Q.WhereIn(nameof(TopicGroup.Id), ids)) > 0;
  48. }
  49. /// <summary>
  50. /// 插入
  51. /// </summary>
  52. /// <param name="topicGroup"></param>
  53. /// <returns></returns>
  54. public async Task<AppResultJson<Entity.Topic.TopicGroup>> InsertAsync(TopicGroup topicGroup)
  55. {
  56. var result = new AppResultJson<Entity.Topic.TopicGroup>()
  57. {
  58. Code = StatusCodeConst.SucceedCode.GetHashCode(),
  59. Success = true
  60. };
  61. try
  62. {
  63. using (var transactionScope = new TransactionScope())
  64. {
  65. var id = await _repository.InsertAsync(topicGroup);
  66. await _repository.UpdateAsync(Q.Set(nameof(TopicGroup.Sort), id).Where(nameof(TopicGroup.Id), id));
  67. transactionScope.Complete();
  68. result.Data = await _repository.GetAsync(id);
  69. }
  70. }
  71. catch
  72. {
  73. result.Success = false;
  74. result.Code = StatusCodeConst.ErrorCode.GetHashCode();
  75. }
  76. return result;
  77. }
  78. /// <summary>
  79. /// 根据用户ID获取分组
  80. /// </summary>
  81. /// <param name="userId"></param>
  82. /// <returns></returns>
  83. public async Task<IEnumerable<TopicGroup>> GetTopicGroupsAsync(int userId)
  84. {
  85. string sql = $@"SELECT
  86. Id,
  87. UserId,
  88. Name,
  89. Sort,
  90. (SELECT
  91. COUNT(1)
  92. FROM
  93. tede_user
  94. WHERE
  95. id IN (SELECT
  96. UserId
  97. FROM
  98. tede_topic_group_user
  99. WHERE
  100. TopicGroupId = a.Id)) AS IncludeCount
  101. FROM
  102. tede_topic_group a
  103. WHERE
  104. a.userId = {userId}
  105. ORDER BY a.Sort DESC";
  106. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  107. var database = new Database(databaseType, _connectionString);
  108. var connection = database.GetConnection();
  109. return await connection.QueryAsync<Entity.Topic.TopicGroup>(sql);
  110. }
  111. /// <summary>
  112. /// 修改分组名称
  113. /// </summary>
  114. /// <param name="request"></param>
  115. /// <returns></returns>
  116. public async Task<bool> UpdateAsync(TopicGroupInRequest request)
  117. {
  118. return await _repository.UpdateAsync(Q.Set(nameof(TopicGroup.Name), request.Name)
  119. .Where(nameof(TopicGroup.Id), request.Id)) > 0;
  120. }
  121. /// <summary>
  122. /// 排序
  123. /// </summary>
  124. /// <param name="firstId"></param>
  125. /// <param name="secondId"></param>
  126. /// <param name="userId"></param>
  127. /// <returns></returns>
  128. public async Task<bool> MoveSortAsync(int firstId, int secondId, int userId)
  129. {
  130. //获取第一个
  131. var topicGroupFirst = await _repository.GetAsync(firstId);
  132. //获取第二个数据
  133. var topicGroupSecond = await _repository.GetAsync(secondId);
  134. var sort = 0;
  135. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  136. var database = new Database(databaseType, _connectionString);
  137. var connection = database.GetConnection();
  138. if (topicGroupFirst.Sort < topicGroupSecond.Sort)
  139. {
  140. //修改第一个排序
  141. await _repository.UpdateAsync(Q.Set(nameof(TopicGroupUser.Sort), topicGroupSecond.Sort)
  142. .Where(nameof(TopicGroup.Id), firstId));
  143. sort = topicGroupSecond.Sort;
  144. var sql =
  145. $"update tede_topic_group set Sort=Sort-1 where UserId={userId} and Sort<={sort} and Id not in({firstId}) and id>0";
  146. return await connection.ExecuteAsync(sql) > 0;
  147. }
  148. else
  149. {
  150. //修改第一个排序
  151. await _repository.UpdateAsync(Q.Set(nameof(TopicGroupUser.Sort), topicGroupSecond.Sort)
  152. .Where(nameof(TopicGroup.Id), firstId));
  153. sort = topicGroupSecond.Sort;
  154. var sql =
  155. $"update tede_topic_group set Sort=Sort+1 where UserId={userId} and Sort>={sort} and Id not in({firstId}) and id>0";
  156. return await connection.ExecuteAsync(sql) > 0;
  157. }
  158. }
  159. public async Task<bool> UpdateAsync(Query query)
  160. {
  161. return await _repository.UpdateAsync(query) > 0;
  162. }
  163. }
  164. }