TopicGroupRepository.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. var topicGroupId = 0;
  64. using (var transactionScope = new TransactionScope())
  65. {
  66. topicGroupId = await _repository.InsertAsync(topicGroup);
  67. await _repository.UpdateAsync(Q.Set(nameof(TopicGroup.Sort), topicGroupId).Where(nameof(TopicGroup.Id), topicGroupId));
  68. transactionScope.Complete();
  69. }
  70. var topicGroupDto = await _repository.GetAsync(topicGroupId);
  71. result.Data = topicGroupDto;
  72. }
  73. catch
  74. {
  75. result.Success = false;
  76. result.Code = StatusCodeConst.ErrorCode.GetHashCode();
  77. result.Msg = StatusCodeConst.ErrorMsg.GetDescriptionOriginal();
  78. }
  79. return result;
  80. }
  81. /// <summary>
  82. /// 根据用户ID获取分组
  83. /// </summary>
  84. /// <param name="userId"></param>
  85. /// <returns></returns>
  86. public async Task<IEnumerable<TopicGroup>> GetTopicGroupsAsync(int userId)
  87. {
  88. string sql = $@"SELECT
  89. Id,
  90. UserId,
  91. Name,
  92. Sort,
  93. (SELECT
  94. COUNT(1)
  95. FROM
  96. tede_user
  97. WHERE
  98. id IN (SELECT
  99. UserId
  100. FROM
  101. tede_topic_group_user
  102. WHERE
  103. TopicGroupId = a.Id)) AS IncludeCount
  104. FROM
  105. tede_topic_group a
  106. WHERE
  107. a.userId = {userId}
  108. ORDER BY a.Sort DESC";
  109. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  110. var database = new Database(databaseType, _connectionString);
  111. var connection = database.GetConnection();
  112. return await connection.QueryAsync<Entity.Topic.TopicGroup>(sql);
  113. }
  114. /// <summary>
  115. /// 修改分组名称
  116. /// </summary>
  117. /// <param name="request"></param>
  118. /// <returns></returns>
  119. public async Task<bool> UpdateAsync(TopicGroupInRequest request)
  120. {
  121. return await _repository.UpdateAsync(Q.Set(nameof(TopicGroup.Name), request.Name)
  122. .Where(nameof(TopicGroup.Id), request.Id)) > 0;
  123. }
  124. /// <summary>
  125. /// 排序
  126. /// </summary>
  127. /// <param name="firstId"></param>
  128. /// <param name="secondId"></param>
  129. /// <param name="userId"></param>
  130. /// <returns></returns>
  131. public async Task<bool> MoveSortAsync(int firstId, int secondId, int userId)
  132. {
  133. //获取第一个
  134. var topicGroupFirst = await _repository.GetAsync(firstId);
  135. //获取第二个数据
  136. var topicGroupSecond = await _repository.GetAsync(secondId);
  137. var sort = 0;
  138. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  139. var database = new Database(databaseType, _connectionString);
  140. var connection = database.GetConnection();
  141. if (topicGroupFirst.Sort < topicGroupSecond.Sort)
  142. {
  143. //修改第一个排序
  144. await _repository.UpdateAsync(Q.Set(nameof(TopicGroupUser.Sort), topicGroupSecond.Sort)
  145. .Where(nameof(TopicGroup.Id), firstId));
  146. sort = topicGroupSecond.Sort;
  147. var sql =
  148. $"update tede_topic_group set Sort=Sort-1 where UserId={userId} and Sort<={sort} and Id not in({firstId}) and id>0";
  149. return await connection.ExecuteAsync(sql) > 0;
  150. }
  151. else
  152. {
  153. //修改第一个排序
  154. await _repository.UpdateAsync(Q.Set(nameof(TopicGroupUser.Sort), topicGroupSecond.Sort)
  155. .Where(nameof(TopicGroup.Id), firstId));
  156. sort = topicGroupSecond.Sort;
  157. var sql =
  158. $"update tede_topic_group set Sort=Sort+1 where UserId={userId} and Sort>={sort} and Id not in({firstId}) and id>0";
  159. return await connection.ExecuteAsync(sql) > 0;
  160. }
  161. }
  162. public async Task<bool> UpdateAsync(Query query)
  163. {
  164. return await _repository.UpdateAsync(query) > 0;
  165. }
  166. }
  167. }