ArticleGroupRepository.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. using AutoMapper;
  16. namespace GxPress.Repository.Implement
  17. {
  18. public class ArticleGroupRepository : IArticleGroupRepository
  19. {
  20. private readonly Repository<ArticleGroup> _repository;
  21. private readonly string _connectionString;
  22. private readonly string _databaseTypeStr;
  23. private readonly IMapper _mapper;
  24. public ArticleGroupRepository(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<ArticleGroup>(database);
  31. this._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<bool> ExistsAsync(int id)
  37. {
  38. return await _repository.ExistsAsync(id);
  39. }
  40. public async Task<bool> IsNameExistsAsync(string name)
  41. {
  42. return await _repository.ExistsAsync(Q.Where(nameof(ArticleGroup.Name), name));
  43. }
  44. public async Task<int> InsertAsync(ArticleGroup processGroup)
  45. {
  46. return await _repository.InsertAsync(processGroup);
  47. }
  48. public async Task<ArticleGroupResult> GetAsync(int id)
  49. {
  50. var result = new ArticleGroupResult();
  51. if (id > 0)
  52. {
  53. var articleGroup = await _repository.GetAsync(id);
  54. result = _mapper.Map<ArticleGroupResult>(articleGroup);
  55. }
  56. if (result == null)
  57. result = new ArticleGroupResult();
  58. result.StyleCssDictionary = new Dictionary<string, string>();
  59. Type t = typeof(AppPageStyleCssConst);
  60. Array arrays = Enum.GetValues(t);
  61. for (int i = 0; i < arrays.LongLength; i++)
  62. {
  63. var appPageStyleCssConst = (AppPageStyleCssConst)arrays.GetValue(i);
  64. var descriptionattrib = appPageStyleCssConst.GetDescriptionOriginal();
  65. result.StyleCssDictionary.Add(descriptionattrib, appPageStyleCssConst.ToString());
  66. }
  67. return result;
  68. }
  69. public async Task<ArticleGroup> GetArticleGroupAsync(int id)
  70. {
  71. return await _repository.GetAsync(id);
  72. }
  73. public async Task<bool> UpdateAsync(ArticleGroup processGroup)
  74. {
  75. return await _repository.UpdateAsync(processGroup);
  76. }
  77. public async Task<IEnumerable<ArticleGroup>> GetListAsync(int userId)
  78. {
  79. string sql = $@"select * from tede_article_group where Id not in(SELECT ArticleGroupId FROM tede_article_group_user where UserId={userId})";
  80. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  81. var database = new Database(databaseType, _connectionString);
  82. var connection = database.GetConnection();
  83. var result = await connection.QueryAsync<ArticleGroup>(sql);
  84. return result;
  85. }
  86. /// <summary>
  87. /// 添加
  88. /// </summary>
  89. /// <param name="request"></param>
  90. /// <returns></returns>
  91. public async Task<ArticleGroup> AddAsync(ArticleGroupAddRequest request)
  92. {
  93. var exist = await IsNameExistsAsync(request.Name);
  94. if (exist) throw new BusinessException("分组名不能重复");
  95. var group = new ArticleGroup
  96. {
  97. Name = request.Name,
  98. Sort = request.Sort,
  99. IsMiddleLable = request.IsMiddleLable,
  100. IsSystemDefault = request.IsSystemDefault,
  101. MiddleLableId = request.MiddleLableId,
  102. StyleCss = request.StyleCss
  103. };
  104. group.Id = await InsertAsync(group);
  105. return group;
  106. }
  107. /// <summary>
  108. /// 修改
  109. /// </summary>
  110. /// <param name="id"></param>
  111. /// <param name="request"></param>
  112. /// <returns></returns>
  113. public async Task<bool> UpdateAsync(int id, ArticleGroupAddRequest request)
  114. {
  115. var group = await GetArticleGroupAsync(id);
  116. if (group == null) throw new BusinessException("该分组不存在");
  117. if (group.Name != request.Name)
  118. {
  119. var exist = await IsNameExistsAsync(request.Name);
  120. if (exist) throw new BusinessException("分组名不能重复");
  121. }
  122. group.Name = request.Name;
  123. group.Sort = request.Sort;
  124. group.IsMiddleLable = request.IsMiddleLable;
  125. group.IsSystemDefault = request.IsSystemDefault;
  126. group.MiddleLableId = request.MiddleLableId;
  127. group.StyleCss = request.StyleCss;
  128. return await UpdateAsync(group);
  129. }
  130. /// <summary>
  131. /// 删除
  132. /// </summary>
  133. /// <param name="id"></param>
  134. /// <returns></returns>
  135. public async Task<bool> DeleteAsync(int id)
  136. {
  137. var group = await GetAsync(id);
  138. if (group == null) throw new BusinessException("该文章组不存在");
  139. return await _repository.DeleteAsync(id);
  140. }
  141. /// <summary>
  142. /// 根据样式获取导航
  143. /// </summary>
  144. /// <param name="styleCss"></param>
  145. /// <returns></returns>
  146. public async Task<ArticleGroup> GetArticleGroupAsync(string styleCss)
  147. {
  148. return await _repository.GetAsync(Q.Where(nameof(ArticleGroup.StyleCss), styleCss));
  149. }
  150. }
  151. }