TopicGroupRepository.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using AutoMapper;
  7. using Dapper;
  8. using Datory;
  9. using GxPress.Common.AppOptions;
  10. using GxPress.Common.Tools;
  11. using GxPress.Entity;
  12. using GxPress.Entity.Topic;
  13. using GxPress.Repository.Interface.Topic;
  14. using GxPress.Request.App.Topic;
  15. using Microsoft.Extensions.Options;
  16. using SqlKata;
  17. namespace GxPress.Repository.Implement.Topic
  18. {
  19. public class TopicGroupRepository : ITopicGroupRepository
  20. {
  21. private readonly Repository<TopicGroup> _repository;
  22. private readonly IMapper _mapper;
  23. private readonly string _connectionString;
  24. private readonly string _databaseTypestr;
  25. public TopicGroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  26. {
  27. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  28. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  29. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  30. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  31. _repository = new Repository<TopicGroup>(database);
  32. _mapper = mapper;
  33. }
  34. public IDatabase Database => _repository.Database;
  35. public string TableName => _repository.TableName;
  36. public List<TableColumn> TableColumns => _repository.TableColumns;
  37. public async Task<TopicGroup> GetAsync(int id)
  38. {
  39. return await _repository.GetAsync(id);
  40. }
  41. /// <summary>
  42. /// 根据ID删除分组
  43. /// </summary>
  44. /// <param name="ids"></param>
  45. /// <returns></returns>
  46. public async Task<bool> DeleteAsync(List<int> ids)
  47. {
  48. return await _repository.DeleteAsync(Q.WhereIn(nameof(TopicGroup.Id), ids)) > 0;
  49. }
  50. /// <summary>
  51. /// 插入
  52. /// </summary>
  53. /// <param name="topicGroup"></param>
  54. /// <returns></returns>
  55. public async Task<bool> InsertAsync(TopicGroup topicGroup)
  56. {
  57. var id = await _repository.InsertAsync(topicGroup) > 0;
  58. return await _repository.UpdateAsync(Q.Set(nameof(TopicGroup.Sort), id).Where(nameof(TopicGroup.Id), id)) >
  59. 0;
  60. }
  61. /// <summary>
  62. /// 根据用户ID获取分组
  63. /// </summary>
  64. /// <param name="userId"></param>
  65. /// <returns></returns>
  66. public async Task<IEnumerable<TopicGroup>> GetTopicGroupsAsync(int userId)
  67. {
  68. var result = await _repository.GetAllAsync(Q.Where(nameof(TopicGroup.UserId), userId)
  69. .OrderByDesc(nameof(TopicGroup.Sort)));
  70. return result;
  71. }
  72. /// <summary>
  73. /// 修改分组名称
  74. /// </summary>
  75. /// <param name="request"></param>
  76. /// <returns></returns>
  77. public async Task<bool> UpdateAsync(TopicGroupInRequest request)
  78. {
  79. return await _repository.UpdateAsync(Q.Set(nameof(TopicGroup.Name), request.Name)
  80. .Where(nameof(TopicGroup.Id), request.Id)) > 0;
  81. }
  82. /// <summary>
  83. /// 排序
  84. /// </summary>
  85. /// <param name="firstId"></param>
  86. /// <param name="secondId"></param>
  87. /// <param name="userId"></param>
  88. /// <returns></returns>
  89. public async Task<bool> MoveSortAsync(int firstId, int secondId, int userId)
  90. {
  91. //获取第一个
  92. var topicGroupFirst = await _repository.GetAsync(firstId);
  93. //获取第二个数据
  94. var topicGroupSecond = await _repository.GetAsync(secondId);
  95. var sort = 0;
  96. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  97. var database = new Database(databaseType, _connectionString);
  98. var connection = database.GetConnection();
  99. if (topicGroupFirst.Sort < topicGroupSecond.Sort)
  100. {
  101. //修改第一个排序
  102. await _repository.UpdateAsync(Q.Set(nameof(TopicGroupUser.Sort), topicGroupSecond.Sort)
  103. .Where(nameof(TopicGroup.Id), firstId));
  104. sort = topicGroupSecond.Sort;
  105. var sql =
  106. $"update tede_topic_group set Sort=Sort-1 where UserId={userId} and Sort<={sort} and Id not in({firstId}) and id>0";
  107. return await connection.ExecuteAsync(sql) > 0;
  108. }
  109. else
  110. {
  111. //修改第一个排序
  112. await _repository.UpdateAsync(Q.Set(nameof(TopicGroupUser.Sort), topicGroupSecond.Sort)
  113. .Where(nameof(TopicGroup.Id), firstId));
  114. sort = topicGroupSecond.Sort;
  115. var sql =
  116. $"update tede_topic_group set Sort=Sort+1 where UserId={userId} and Sort>={sort} and Id not in({firstId}) and id>0";
  117. return await connection.ExecuteAsync(sql) > 0;
  118. }
  119. }
  120. public async Task<bool> UpdateAsync(Query query)
  121. {
  122. return await _repository.UpdateAsync(query) > 0;
  123. }
  124. }
  125. }