ArticleGroupRepository.cs 6.7 KB

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