NavigationRepository.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.Media;
  11. using GxPress.Result.Navigation;
  12. using Microsoft.Extensions.Options;
  13. namespace GxPress.Repository.Implement.Navigation
  14. {
  15. public class NavigationRepository : INavigationRepository
  16. {
  17. private readonly Repository<Entity.Navigations.Navigation> _repository;
  18. private readonly IMapper _mapper;
  19. private readonly string _connectionString;
  20. private readonly string _databaseTypestr;
  21. private readonly Repository<Entity.Navigations.MiddleLable> _middleLableRepository;
  22. private readonly Repository<Entity.SystemLabel.SystemLabel> _systemLabelRepository;
  23. private readonly Repository<Entity.tede2.Media.Media> _mediaRepository;
  24. private readonly Repository<Entity.tede2.Media.MediaLable> _mediaLableRepository;
  25. public NavigationRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  26. {
  27. _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
  28. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  29. var databaseType =
  30. StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  31. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  32. _repository = new Repository<Entity.Navigations.Navigation>(database);
  33. _middleLableRepository = new Repository<Entity.Navigations.MiddleLable>(database);
  34. _systemLabelRepository = new Repository<Entity.SystemLabel.SystemLabel>(database);
  35. _mediaRepository = new Repository<Entity.tede2.Media.Media>(database);
  36. _mediaLableRepository = new Repository<Entity.tede2.Media.MediaLable>(database);
  37. _mapper = mapper;
  38. }
  39. public IDatabase Database => _repository.Database;
  40. public string TableName => _repository.TableName;
  41. public List<TableColumn> TableColumns => _repository.TableColumns;
  42. public async Task<Entity.Navigations.Navigation> GetAsync(int id)
  43. {
  44. return await _repository.GetAsync(id);
  45. }
  46. public async Task<bool> DeleteAsync(int id)
  47. {
  48. return await _repository.DeleteAsync(id);
  49. }
  50. public async Task<int> InsertAsync(Entity.Navigations.Navigation note)
  51. {
  52. return await _repository.InsertAsync(note);
  53. }
  54. /// <summary>
  55. /// 修改
  56. /// </summary>
  57. /// <param name="request"></param>
  58. /// <returns></returns>
  59. public async Task<bool> UpdateAsync(NavigationUpRequest request)
  60. {
  61. if (request.Id > 0)
  62. {
  63. var model = await GetAsync(request.Id);
  64. if (request.IsDisable > 0)
  65. model.IsDisable = request.IsDisable == 1;
  66. if (request.Isoperate > 0)
  67. model.Isoperate = request.Isoperate == 1;
  68. if (request.MiddleLableId > 0)
  69. model.MiddleLableId = request.MiddleLableId;
  70. if (!string.IsNullOrEmpty(request.Name))
  71. model.Name = request.Name;
  72. if (request.ParentId > 0)
  73. model.ParentId = request.ParentId;
  74. if (request.Sort > 0)
  75. model.Sort = request.Sort;
  76. if (request.Terminal > 0)
  77. model.Terminal = request.Terminal;
  78. if (!string.IsNullOrEmpty(request.ActionUrl))
  79. model.ActionUrl = request.ActionUrl;
  80. if (!string.IsNullOrEmpty(request.ControllerUrl))
  81. model.ControllerUrl = request.ControllerUrl;
  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. /// 获取 pc 或者移动端
  92. /// </summary>
  93. /// <param name="terminal"></param>
  94. /// <returns></returns>
  95. public async Task<IEnumerable<NavigationResult>> GetAllAsync(int terminal)
  96. {
  97. var query = Q.NewQuery();
  98. if (terminal > 0)
  99. query.Where(nameof(Entity.Navigations.Navigation.Terminal), terminal);
  100. query.Where(nameof(Entity.Navigations.Navigation.IsDisable), false);
  101. query.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort));
  102. var queryResult = await _repository.GetAllAsync(query);
  103. var navigationResult = queryResult.Select(n => _mapper.Map<NavigationResult>(n)).ToList();
  104. var result = navigationResult.Where(n => n.ParentId == 0);
  105. foreach (var item in result)
  106. {
  107. item.IsChildren = navigationResult.Any(n => n.ParentId == item.Id);
  108. if (item.IsChildren)
  109. {
  110. var navigationResultChildren = navigationResult.Where(n => n.ParentId == item.Id).ToList();
  111. item.Children = await GetTreeAsync(item.Id, navigationResult.ToList(), navigationResultChildren);
  112. }
  113. }
  114. return result;
  115. }
  116. /// <summary>
  117. /// 递归获取子集
  118. /// </summary>
  119. /// <param name="navigationResult"></param>
  120. /// <param name="id"></param>
  121. /// <param name="items"></param>
  122. /// <returns></returns>
  123. public async Task<List<NavigationResult>> GetTreeAsync(int id, List<NavigationResult> navigationResults, List<NavigationResult> items)
  124. {
  125. foreach (var item in items)
  126. {
  127. item.IsChildren = navigationResults.Any(n => n.ParentId == item.Id);
  128. if (item.IsChildren)
  129. {
  130. var navigationResultsParent = navigationResults.Where(n => n.ParentId == item.Id);
  131. item.Children = _mapper.Map<List<NavigationResult>>(navigationResultsParent);
  132. foreach (var childrenItem in item.Children)
  133. {
  134. await GetTreeAsync(childrenItem.Id, navigationResults, item.Children);
  135. }
  136. }
  137. }
  138. return items;
  139. }
  140. /// <summary>
  141. /// 获取导航栏数据
  142. /// </summary>
  143. /// <returns></returns>
  144. public async Task<List<NavigationMediaResult>> GetNavigationResults(int navigationId)
  145. {
  146. var result = new List<NavigationMediaResult>();
  147. var navigation = await GetAsync(navigationId);
  148. if (navigation.MiddleLableId > 0)
  149. {
  150. //获取中间页面
  151. var middleLable = await _middleLableRepository.GetAsync(navigation.MiddleLableId);
  152. if (string.IsNullOrEmpty(middleLable.LabelId))
  153. return result;
  154. //获取轮播
  155. if (middleLable.IsSlide)
  156. {
  157. //result
  158. }
  159. //获取标签
  160. var systemLabels = await _systemLabelRepository.GetAllAsync(Q.WhereIn(nameof(Entity.SystemLabel.SystemLabel.Id), StringUtils.StringCollectionToIntList(middleLable.LabelId)));
  161. foreach (var item in systemLabels)
  162. {
  163. var navigationMediaResult = new NavigationMediaResult()
  164. {
  165. Id = item.Id,
  166. Sort = item.Sort,
  167. StyleType = item.StyleType,
  168. ActionUrl = item.ActionUrl,
  169. ControllerUrl = item.ControllerUrl,
  170. IsPage = item.IsPage,
  171. IsSkip = item.IsSkip,
  172. LabelName = item.LabelName,
  173. ResourceType = item.ResourceType,
  174. LabelNameDescribe = item.LabelNameDescribe,
  175. Remark = item.Remark
  176. };
  177. //获取媒体标签
  178. var mediaLables = await _mediaLableRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLable.LableId), item.Id));
  179. //获取媒体
  180. var query = Q.NewQuery();
  181. query.WhereIn(nameof(Entity.tede2.Media.Media.Id), mediaLables.Select(n => n.MediaId));
  182. query.Where(nameof(Entity.tede2.Media.Media.IsChecked), true);
  183. query.Where(nameof(Entity.tede2.Media.Media.IsDelete), false);
  184. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsTop));
  185. query.OrderByDesc(nameof(Entity.tede2.Media.Media.IsRecommend));
  186. query.ForPage(1, 10);
  187. var medias = await _mediaRepository.GetAllAsync(query);
  188. navigationMediaResult.MediaResults = medias.Select(n => _mapper.Map<MediaResult>(n)).ToList();
  189. foreach (var itemMedia in navigationMediaResult.MediaResults)
  190. {
  191. itemMedia.ImageUrls = StringUtils.AddDomain(itemMedia.ImageUrls);
  192. }
  193. result.Add(navigationMediaResult);
  194. }
  195. }
  196. return result;
  197. }
  198. }
  199. }