NavigationRepository.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Datory;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Tools;
  8. using GxPress.Repository.Interface.Navigation;
  9. using GxPress.Request.Navigation;
  10. using GxPress.Result.Navigation;
  11. using Microsoft.Extensions.Options;
  12. namespace GxPress.Repository.Implement.Navigation
  13. {
  14. public class NavigationRepository : INavigationRepository
  15. {
  16. private readonly Repository<Entity.Navigations.Navigation> _repository;
  17. private readonly IMapper _mapper;
  18. private readonly string _connectionString;
  19. private readonly string _databaseTypestr;
  20. private readonly Repository<Entity.Navigations.MiddleLable> _middleLableRepository;
  21. private readonly Repository<Entity.SystemLabel.SystemLabel> _systemLabelRepository;
  22. private readonly Repository<Entity.tede2.Media.Media> _mediaRepository;
  23. public NavigationRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  24. {
  25. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  26. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  27. var databaseType =
  28. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  29. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  30. _repository = new Repository<Entity.Navigations.Navigation>(database);
  31. _middleLableRepository = new Repository<Entity.Navigations.MiddleLable>(database);
  32. _systemLabelRepository = new Repository<Entity.SystemLabel.SystemLabel>(database);
  33. _mediaRepository = new Repository<Entity.tede2.Media.Media>(database);
  34. _mapper = mapper;
  35. }
  36. public IDatabase Database => _repository.Database;
  37. public string TableName => _repository.TableName;
  38. public List<TableColumn> TableColumns => _repository.TableColumns;
  39. public async Task<Entity.Navigations.Navigation> GetAsync(int id)
  40. {
  41. return await _repository.GetAsync(id);
  42. }
  43. public async Task<bool> DeleteAsync(int id)
  44. {
  45. return await _repository.DeleteAsync(id);
  46. }
  47. public async Task<int> InsertAsync(Entity.Navigations.Navigation note)
  48. {
  49. return await _repository.InsertAsync(note);
  50. }
  51. public async Task<bool> UpdateAsync(NavigationUpRequest 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.Isoperate > 0)
  59. model.Isoperate = request.Isoperate == 1;
  60. if (request.MiddleLableId > 0)
  61. model.MiddleLableId = request.MiddleLableId;
  62. if (!string.IsNullOrEmpty(request.Name))
  63. model.Name = request.Name;
  64. if (request.ParentId > 0)
  65. model.ParentId = request.ParentId;
  66. if (request.Sort > 0)
  67. model.Sort = request.Sort;
  68. if (request.Terminal > 0)
  69. model.Terminal = request.Terminal;
  70. return await _repository.UpdateAsync(model);
  71. }
  72. return false;
  73. }
  74. public async Task<bool> UpdateAsync(SqlKata.Query query)
  75. {
  76. return await _repository.UpdateAsync(query) > 0;
  77. }
  78. /// <summary>
  79. /// 获取 pc 或者移动端
  80. /// </summary>
  81. /// <param name="terminal"></param>
  82. /// <returns></returns>
  83. public async Task<IEnumerable<NavigationResult>> GetAllAsync(int terminal)
  84. {
  85. var query = Q.NewQuery();
  86. if (terminal > 0)
  87. query.Where(nameof(Entity.Navigations.Navigation.Terminal), terminal);
  88. query.Where(nameof(Entity.Navigations.Navigation.IsDisable), false);
  89. query.Where(nameof(Entity.Navigations.Navigation.ParentId), 0);
  90. query.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort));
  91. var result = await _repository.GetAllAsync(query);
  92. var navigationResult = result.Select(n => _mapper.Map<NavigationResult>(n));
  93. foreach (var item in navigationResult)
  94. {
  95. item.IsChildren = await IsExistsAsync(item.Id);
  96. item.Children = await GetTreeAsync(item.Id, navigationResult.ToList());
  97. }
  98. return navigationResult;
  99. }
  100. /// <summary>
  101. /// 递归获取子集
  102. /// </summary>
  103. /// <param name="navigationResult"></param>
  104. /// <param name="id"></param>
  105. /// <param name="items"></param>
  106. /// <returns></returns>
  107. public async Task<List<NavigationResult>> GetTreeAsync(int id, List<NavigationResult> items)
  108. {
  109. foreach (var item in items)
  110. {
  111. item.IsChildren = await IsExistsAsync(item.Id);
  112. if (item.IsChildren)
  113. {
  114. var navigations = await _repository.GetAllAsync(Q.Where(nameof(Entity.Navigations.Navigation.ParentId), item.Id));
  115. item.Children = _mapper.Map<List<NavigationResult>>(navigations);
  116. foreach (var childrenItem in item.Children)
  117. {
  118. await GetTreeAsync(childrenItem.Id, item.Children);
  119. }
  120. }
  121. }
  122. return items;
  123. }
  124. public async Task<bool> IsExistsAsync(int parentId)
  125. {
  126. return await _repository.ExistsAsync(Q.Where(nameof(Entity.Navigations.Navigation.ParentId), parentId));
  127. }
  128. /// <summary>
  129. /// 获取导航栏数据
  130. /// </summary>
  131. /// <returns></returns>
  132. public async Task<List<NavigationMediaResult>> GetNavigationResults(int navigationId)
  133. {
  134. var result = new List<NavigationMediaResult>();
  135. var navigation = await GetAsync(navigationId);
  136. if (navigation.Terminal > 0)
  137. {
  138. //获取中间页面
  139. var middleLable = await _middleLableRepository.GetAsync(navigation.MiddleLableId);
  140. if (string.IsNullOrEmpty(middleLable.LabelId))
  141. return result;
  142. //获取标签
  143. var systemLabels = await _systemLabelRepository.GetAllAsync(Q.WhereIn(nameof(Entity.SystemLabel.SystemLabel.Id), StringUtils.StringCollectionToIntList(middleLable.LabelId)));
  144. foreach (var item in systemLabels)
  145. {
  146. var navigationResult = new NavigationResult()
  147. {
  148. Sort = item.Sort,
  149. StyleType = item.StyleType,
  150. ActionUrl = item.ActionUrl,
  151. ControllerUrl = item.ControllerUrl,
  152. IsPage = item.IsPage,
  153. IsSkip = item.IsSkip,
  154. LabelName = item.LabelName,
  155. ResourceType = item.ResourceType
  156. };
  157. //获取媒体
  158. }
  159. }
  160. return result;
  161. }
  162. }
  163. }