TopicGroupRepository.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 = new List<TopicGroup>() {
  69. new TopicGroup {
  70. Id=-1,Name="我的"
  71. }, new TopicGroup { Id=-2,Name="同单位"},
  72. new TopicGroup {Id=-3,Name="推荐" } ,
  73. new TopicGroup {Id=0,Name="全部" }};
  74. result.AddRange(await _repository.GetAllAsync(Q.Where(nameof(TopicGroup.UserId), userId)
  75. .OrderByDesc(nameof(TopicGroup.Sort))));
  76. return result;
  77. }
  78. /// <summary>
  79. /// 修改分组名称
  80. /// </summary>
  81. /// <param name="request"></param>
  82. /// <returns></returns>
  83. public async Task<bool> UpdateAsync(TopicGroupInRequest request)
  84. {
  85. return await _repository.UpdateAsync(Q.Set(nameof(TopicGroup.Name), request.Name)
  86. .Where(nameof(TopicGroup.Id), request.Id)) > 0;
  87. }
  88. /// <summary>
  89. /// 排序
  90. /// </summary>
  91. /// <param name="firstId"></param>
  92. /// <param name="secondId"></param>
  93. /// <param name="userId"></param>
  94. /// <returns></returns>
  95. public async Task<bool> MoveSortAsync(int firstId, int secondId, int userId)
  96. {
  97. //获取第一个
  98. var topicGroupFirst = await _repository.GetAsync(firstId);
  99. //获取第二个数据
  100. var topicGroupSecond = await _repository.GetAsync(secondId);
  101. var sort = 0;
  102. var databaseType = _databaseTypestr.ToEnum<DatabaseType>(DatabaseType.MySql);
  103. var database = new Database(databaseType, _connectionString);
  104. var connection = database.GetConnection();
  105. if (topicGroupFirst.Sort < topicGroupSecond.Sort)
  106. {
  107. //修改第一个排序
  108. await _repository.UpdateAsync(Q.Set(nameof(TopicGroupUser.Sort), topicGroupSecond.Sort)
  109. .Where(nameof(TopicGroup.Id), firstId));
  110. sort = topicGroupSecond.Sort;
  111. var sql =
  112. $"update tede_topic_group set Sort=Sort-1 where UserId={userId} and Sort<={sort} and Id not in({firstId}) and id>0";
  113. return await connection.ExecuteAsync(sql) > 0;
  114. }
  115. else
  116. {
  117. //修改第一个排序
  118. await _repository.UpdateAsync(Q.Set(nameof(TopicGroupUser.Sort), topicGroupSecond.Sort)
  119. .Where(nameof(TopicGroup.Id), firstId));
  120. sort = topicGroupSecond.Sort;
  121. var sql =
  122. $"update tede_topic_group set Sort=Sort+1 where UserId={userId} and Sort>={sort} and Id not in({firstId}) and id>0";
  123. return await connection.ExecuteAsync(sql) > 0;
  124. }
  125. }
  126. public async Task<bool> UpdateAsync(Query query)
  127. {
  128. return await _repository.UpdateAsync(query) > 0;
  129. }
  130. }
  131. }