NavigationRepository.cs 9.9 KB

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