ArticleGroupRepository.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.Article;
  13. using System;
  14. using GxPress.EnumConst;
  15. namespace GxPress.Repository.Implement
  16. {
  17. public class ArticleGroupRepository : IArticleGroupRepository
  18. {
  19. private readonly Repository<ArticleGroup> _repository;
  20. private readonly string _connectionString;
  21. private readonly string _databaseTypeStr;
  22. public ArticleGroupRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor)
  23. {
  24. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  25. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  26. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  27. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  28. _repository = new Repository<ArticleGroup>(database);
  29. }
  30. public IDatabase Database => _repository.Database;
  31. public string TableName => _repository.TableName;
  32. public List<TableColumn> TableColumns => _repository.TableColumns;
  33. public async Task<bool> ExistsAsync(int id)
  34. {
  35. return await _repository.ExistsAsync(id);
  36. }
  37. public async Task<bool> IsNameExistsAsync(string name)
  38. {
  39. return await _repository.ExistsAsync(Q.Where(nameof(ArticleGroup.Name), name));
  40. }
  41. public async Task<int> InsertAsync(ArticleGroup processGroup)
  42. {
  43. return await _repository.InsertAsync(processGroup);
  44. }
  45. public async Task<ArticleGroupResult> GetAsync(int id)
  46. {
  47. var result = await _repository.GetAsync<ArticleGroupResult>(Q.Where(nameof(Entity.ArticleGroup.Id), id));
  48. result.StyleCssDictionary = new Dictionary<string, string>();
  49. Type t = typeof(AppPageStyleCssConst);
  50. Array arrays = Enum.GetValues(t);
  51. for (int i = 0; i < arrays.LongLength; i++)
  52. {
  53. var appPageStyleCssConst = (AppPageStyleCssConst)arrays.GetValue(i);
  54. var descriptionattrib = appPageStyleCssConst.GetDescriptionOriginal();
  55. result.StyleCssDictionary.Add(descriptionattrib, appPageStyleCssConst.ToString());
  56. }
  57. return result;
  58. }
  59. public async Task<ArticleGroup> GetArticleGroupAsync(int id)
  60. {
  61. return await _repository.GetAsync(id);
  62. }
  63. public async Task<bool> UpdateAsync(ArticleGroup processGroup)
  64. {
  65. return await _repository.UpdateAsync(processGroup);
  66. }
  67. public async Task<IEnumerable<ArticleGroup>> GetListAsync(int userId)
  68. {
  69. string sql = $@"select * from ccpph.tede_article_group where Id not in(SELECT ArticleGroupId FROM ccpph.tede_article_group_user where UserId={userId})";
  70. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  71. var database = new Database(databaseType, _connectionString);
  72. var connection = database.GetConnection();
  73. var result = await connection.QueryAsync<ArticleGroup>(sql);
  74. return result;
  75. }
  76. /// <summary>
  77. /// 添加
  78. /// </summary>
  79. /// <param name="request"></param>
  80. /// <returns></returns>
  81. public async Task<ArticleGroup> AddAsync(ArticleGroupAddRequest request)
  82. {
  83. var exist = await IsNameExistsAsync(request.Name);
  84. if (exist) throw new BusinessException("分组名不能重复");
  85. var group = new ArticleGroup
  86. {
  87. Name = request.Name,
  88. Sort = request.Sort,
  89. IsMiddleLable = request.IsMiddleLable,
  90. IsSystemDefault = request.IsSystemDefault,
  91. MiddleLableId = request.MiddleLableId,
  92. StyleCss = request.StyleCss
  93. };
  94. group.Id = await InsertAsync(group);
  95. return group;
  96. }
  97. /// <summary>
  98. /// 修改
  99. /// </summary>
  100. /// <param name="id"></param>
  101. /// <param name="request"></param>
  102. /// <returns></returns>
  103. public async Task<bool> UpdateAsync(int id, ArticleGroupAddRequest request)
  104. {
  105. var group = await GetArticleGroupAsync(id);
  106. if (group == null) throw new BusinessException("该分组不存在");
  107. if (group.Name != request.Name)
  108. {
  109. var exist = await IsNameExistsAsync(request.Name);
  110. if (exist) throw new BusinessException("分组名不能重复");
  111. }
  112. group.Name = request.Name;
  113. group.Sort = request.Sort;
  114. group.IsMiddleLable = request.IsMiddleLable;
  115. group.IsSystemDefault = request.IsSystemDefault;
  116. group.MiddleLableId = request.MiddleLableId;
  117. group.StyleCss = request.StyleCss;
  118. return await UpdateAsync(group);
  119. }
  120. /// <summary>
  121. /// 删除
  122. /// </summary>
  123. /// <param name="id"></param>
  124. /// <returns></returns>
  125. public async Task<bool> DeleteAsync(int id)
  126. {
  127. var group = await GetAsync(id);
  128. if (group == null) throw new BusinessException("该文章组不存在");
  129. return await _repository.DeleteAsync(id);
  130. }
  131. }
  132. }