ArticleGroupRepository.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using GxPress.Common.AppOptions;
  4. using GxPress.Common.Exceptions;
  5. using GxPress.Common.Tools;
  6. using GxPress.Entity;
  7. using GxPress.Repository.Interface;
  8. using GxPress.Request.ArticleGroup;
  9. using Microsoft.Extensions.Options;
  10. using Datory;
  11. using Dapper;
  12. using GxPress.Result.ArticleGroupUser;
  13. namespace GxPress.Repository.Implement
  14. {
  15. public class ArticleGroupRepository : IArticleGroupRepository
  16. {
  17. private readonly Repository<ArticleGroup> _repository;
  18. private readonly string _connectionString;
  19. private readonly string _databaseTypeStr;
  20. public ArticleGroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor)
  21. {
  22. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  23. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  24. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  25. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  26. _repository = new Repository<ArticleGroup>(database);
  27. }
  28. public IDatabase Database => _repository.Database;
  29. public string TableName => _repository.TableName;
  30. public List<TableColumn> TableColumns => _repository.TableColumns;
  31. public async Task<bool> ExistsAsync(int id)
  32. {
  33. return await _repository.ExistsAsync(id);
  34. }
  35. public async Task<bool> IsNameExistsAsync(string name)
  36. {
  37. return await _repository.ExistsAsync(Q.Where(nameof(ArticleGroup.Name), name));
  38. }
  39. public async Task<int> InsertAsync(ArticleGroup processGroup)
  40. {
  41. return await _repository.InsertAsync(processGroup);
  42. }
  43. public async Task<ArticleGroup> GetAsync(int id)
  44. {
  45. return await _repository.GetAsync(id);
  46. }
  47. public async Task<bool> UpdateAsync(ArticleGroup processGroup)
  48. {
  49. return await _repository.UpdateAsync(processGroup);
  50. }
  51. public async Task<IEnumerable<ArticleGroupUserListResult>> GetListAsync(int userId)
  52. {
  53. string sql = $@"select * from tede_article_group where Id not in(SELECT ArticleGroupId FROM tede_article_group_user where UserId={userId}) and IsSystemDefault=0";
  54. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  55. var database = new Database(databaseType, _connectionString);
  56. var connection = database.GetConnection();
  57. var result = await connection.QueryAsync<ArticleGroup>(sql);
  58. var list = new List<ArticleGroupUserListResult>();
  59. foreach (var item in result)
  60. {
  61. list.Add(new ArticleGroupUserListResult() { Id = item.Id, UserId = userId, ArticleGroupId = item.Id, ArticleGroupName = item.Name, IsSystemDefault = item.IsSystemDefault,Sort=item.Sort });
  62. }
  63. return list;
  64. }
  65. /// <summary>
  66. /// 添加
  67. /// </summary>
  68. /// <param name="request"></param>
  69. /// <returns></returns>
  70. public async Task<ArticleGroup> AddAsync(ArticleGroupAddRequest request)
  71. {
  72. var exist = await IsNameExistsAsync(request.Name);
  73. if (exist) throw new BusinessException("分组名不能重复");
  74. var group = new ArticleGroup
  75. {
  76. Name = request.Name,
  77. Sort = request.Sort,
  78. IsSystemDefault = request.IsSystemDefault
  79. };
  80. group.Id = await InsertAsync(group);
  81. return group;
  82. }
  83. /// <summary>
  84. /// 修改
  85. /// </summary>
  86. /// <param name="id"></param>
  87. /// <param name="request"></param>
  88. /// <returns></returns>
  89. public async Task<bool> UpdateAsync(int id, ArticleGroupAddRequest request)
  90. {
  91. var group = await GetAsync(id);
  92. if (group == null) throw new BusinessException("该分组不存在");
  93. if (group.Name != request.Name)
  94. {
  95. var exist = await IsNameExistsAsync(request.Name);
  96. if (exist) throw new BusinessException("分组名不能重复");
  97. }
  98. group.IsSystemDefault = request.IsSystemDefault;
  99. group.Name = request.Name;
  100. group.Sort = request.Sort;
  101. return await UpdateAsync(group);
  102. }
  103. /// <summary>
  104. /// 删除
  105. /// </summary>
  106. /// <param name="id"></param>
  107. /// <returns></returns>
  108. public async Task<bool> DeleteAsync(int id)
  109. {
  110. var group = await GetAsync(id);
  111. if (group == null) throw new BusinessException("该文章组不存在");
  112. return await _repository.DeleteAsync(id);
  113. }
  114. }
  115. }