SystemLabelRepository.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using Datory;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Page;
  7. using GxPress.Common.Tools;
  8. using GxPress.Repository.Interface.SystemLabel;
  9. using GxPress.Request.SystemLabel;
  10. using GxPress.Result.SystemLabel;
  11. using Microsoft.Extensions.Options;
  12. namespace GxPress.Repository.Implement.SystemLabel
  13. {
  14. public class SystemLabelRepository : ISystemLabelRepository
  15. {
  16. private readonly Repository<Entity.SystemLabel.SystemLabel> _repository;
  17. private readonly IMapper _mapper;
  18. private readonly string _connectionString;
  19. private readonly string _databaseTypestr;
  20. private readonly Repository<Entity.tede2.Media.MediaLable> mediaLableRepository;
  21. public SystemLabelRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  22. {
  23. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  24. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  25. var databaseType =
  26. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  27. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  28. _repository = new Repository<Entity.SystemLabel.SystemLabel>(database);
  29. _mapper = mapper;
  30. mediaLableRepository = new Repository<Entity.tede2.Media.MediaLable>(database);
  31. }
  32. public IDatabase Database => _repository.Database;
  33. public string TableName => _repository.TableName;
  34. public List<TableColumn> TableColumns => _repository.TableColumns;
  35. public async Task<Entity.SystemLabel.SystemLabel> GetAsync(int id)
  36. {
  37. return await _repository.GetAsync(id);
  38. }
  39. public async Task<Entity.SystemLabel.SystemLabel> GetByNameAsync(string name)
  40. {
  41. return await _repository.GetAsync(Q.WhereLike(nameof(Entity.SystemLabel.SystemLabel.LabelName), $"%{name}%"));
  42. }
  43. public async Task<bool> DeleteAsync(int id)
  44. {
  45. return await _repository.DeleteAsync(id);
  46. }
  47. public async Task<int> InsertAsync(Entity.SystemLabel.SystemLabel note)
  48. {
  49. return await _repository.InsertAsync(note);
  50. }
  51. public async Task<bool> UpdateAsync(SystemLabelUpRequest request)
  52. {
  53. if (request.Id > 0)
  54. {
  55. var model = await GetAsync(request.Id);
  56. if (request.IsDisable > 0)
  57. model.IsDisable = request.IsDisable == 1;
  58. if (request.IsPage > 0)
  59. model.IsPage = request.IsPage == 1;
  60. if (request.IsSkip > 0)
  61. model.IsSkip = request.IsSkip == 1;
  62. if (!string.IsNullOrEmpty(request.LabelName))
  63. model.LabelName = request.LabelName;
  64. if (!string.IsNullOrEmpty(request.ActionUrl))
  65. model.ActionUrl = request.ActionUrl;
  66. if (!string.IsNullOrEmpty(request.LabelNameDescribe))
  67. model.LabelNameDescribe = request.LabelNameDescribe;
  68. if (!string.IsNullOrEmpty(request.Remark))
  69. model.Remark = request.Remark;
  70. if (request.ResourceType > 0)
  71. model.ResourceType = request.ResourceType;
  72. if (request.Sort > 0)
  73. model.Sort = request.Sort;
  74. if (!string.IsNullOrEmpty(request.StyleType))
  75. model.StyleType = request.StyleType;
  76. if (request.IsShowLabelName > 0)
  77. model.IsShowLabelName = request.IsShowLabelName == 1;
  78. if (request.PageSize > 0)
  79. model.PageSize = request.PageSize;
  80. if (request.IsStarLable > 0)
  81. model.IsStarLable = request.IsStarLable == 1;
  82. return await _repository.UpdateAsync(model);
  83. }
  84. return false;
  85. }
  86. public async Task<bool> UpdateAsync(SqlKata.Query query)
  87. {
  88. return await _repository.UpdateAsync(query) > 0;
  89. }
  90. /// <summary>
  91. /// 获取标签
  92. /// </summary>
  93. /// <param name="request"></param>
  94. /// <returns></returns>
  95. public async Task<PagedList<SystemLabelResult>> GetAllPageAsync(SystemLabelRequest request)
  96. {
  97. var result = new PagedList<SystemLabelResult>();
  98. var query = Q.NewQuery();
  99. query.Where(nameof(Entity.SystemLabel.SystemLabel.IsDisable), false);
  100. query.Where(nameof(Entity.SystemLabel.SystemLabel.IsStarLable),request.TypeValue);
  101. result.Total = await _repository.CountAsync(query);
  102. query.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort));
  103. query.ForPage(request.Page, request.PerPage);
  104. result.Items = await _repository.GetAllAsync<SystemLabelResult>(query);
  105. foreach (var item in result.Items)
  106. {
  107. query = Q.NewQuery();
  108. query.Where(nameof(Entity.tede2.Media.MediaLable.LableId), item.Id);
  109. //获取资源数量
  110. item.ResourceCount = await mediaLableRepository.CountAsync(query);
  111. }
  112. return result;
  113. }
  114. /// <summary>
  115. /// 获取明栏
  116. /// </summary>
  117. /// <param name="request"></param>
  118. /// <returns></returns>
  119. public async Task<PagedList<SystemLabelResult>> GetAllStarLablePageAsync(Common.Page.PageParameter request)
  120. {
  121. var result = new PagedList<SystemLabelResult>();
  122. var query = Q.NewQuery();
  123. query.Where(nameof(Entity.SystemLabel.SystemLabel.IsDisable), false);
  124. query.Where(nameof(Entity.SystemLabel.SystemLabel.IsStarLable), true);
  125. result.Total = await _repository.CountAsync(query);
  126. query.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort));
  127. query.ForPage(request.Page, request.PerPage);
  128. result.Items = await _repository.GetAllAsync<SystemLabelResult>(query);
  129. foreach (var item in result.Items)
  130. {
  131. query = Q.NewQuery();
  132. query.Where(nameof(Entity.tede2.Media.MediaLable.LableId), item.Id);
  133. //获取资源数量
  134. item.ResourceCount = await mediaLableRepository.CountAsync(query);
  135. }
  136. return result;
  137. }
  138. /// <summary>
  139. /// 获取明栏
  140. /// </summary>
  141. /// <returns></returns>
  142. public async Task<IEnumerable<SystemLabelResult>> GetStarLableAllAsync()
  143. {
  144. var query = Q.NewQuery();
  145. query.Where(nameof(Entity.SystemLabel.SystemLabel.IsDisable), false);
  146. query.Where(nameof(Entity.SystemLabel.SystemLabel.IsStarLable), true);
  147. query.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort));
  148. var result = await _repository.GetAllAsync<SystemLabelResult>(query);
  149. return result;
  150. }
  151. public async Task<IEnumerable<SystemLabelResult>> GetAllAsync()
  152. {
  153. var query = Q.NewQuery();
  154. query.Where(nameof(Entity.SystemLabel.SystemLabel.IsDisable), false);
  155. query.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort));
  156. query.Where(nameof(Entity.SystemLabel.SystemLabel.IsStarLable), false);
  157. var result = await _repository.GetAllAsync<SystemLabelResult>(query);
  158. return result;
  159. }
  160. public async Task<IEnumerable<Entity.SystemLabel.SystemLabel>> GetAllAsync(List<int> ids)
  161. {
  162. return await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.SystemLabel.SystemLabel.Id), ids));
  163. }
  164. }
  165. }