123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using AutoMapper;
- using Datory;
- using GxPress.Common.AppOptions;
- using GxPress.Common.Tools;
- using GxPress.Repository.Interface.Navigation;
- using GxPress.Request.Navigation;
- using GxPress.Result.Navigation;
- using Microsoft.Extensions.Options;
- namespace GxPress.Repository.Implement.Navigation
- {
- public class NavigationRepository : INavigationRepository
- {
- private readonly Repository<Entity.Navigations.Navigation> _repository;
- private readonly IMapper _mapper;
- private readonly string _connectionString;
- private readonly string _databaseTypestr;
- private readonly Repository<Entity.Navigations.MiddleLable> _middleLableRepository;
- private readonly Repository<Entity.SystemLabel.SystemLabel> _systemLabelRepository;
- private readonly Repository<Entity.tede2.Media.Media> _mediaRepository;
- public NavigationRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
- {
- _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
- _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
- var databaseType =
- StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
- var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
- _repository = new Repository<Entity.Navigations.Navigation>(database);
- _middleLableRepository = new Repository<Entity.Navigations.MiddleLable>(database);
- _systemLabelRepository = new Repository<Entity.SystemLabel.SystemLabel>(database);
- _mediaRepository = new Repository<Entity.tede2.Media.Media>(database);
- _mapper = mapper;
- }
- public IDatabase Database => _repository.Database;
- public string TableName => _repository.TableName;
- public List<TableColumn> TableColumns => _repository.TableColumns;
- public async Task<Entity.Navigations.Navigation> GetAsync(int id)
- {
- return await _repository.GetAsync(id);
- }
- public async Task<bool> DeleteAsync(int id)
- {
- return await _repository.DeleteAsync(id);
- }
- public async Task<int> InsertAsync(Entity.Navigations.Navigation note)
- {
- return await _repository.InsertAsync(note);
- }
- public async Task<bool> UpdateAsync(NavigationUpRequest request)
- {
- if (request.Id > 0)
- {
- var model = await GetAsync(request.Id);
- if (request.IsDisable > 0)
- model.IsDisable = request.IsDisable == 1;
- if (request.Isoperate > 0)
- model.Isoperate = request.Isoperate == 1;
- if (request.MiddleLableId > 0)
- model.MiddleLableId = request.MiddleLableId;
- if (!string.IsNullOrEmpty(request.Name))
- model.Name = request.Name;
- if (request.ParentId > 0)
- model.ParentId = request.ParentId;
- if (request.Sort > 0)
- model.Sort = request.Sort;
- if (request.Terminal > 0)
- model.Terminal = request.Terminal;
- return await _repository.UpdateAsync(model);
- }
- return false;
- }
- public async Task<bool> UpdateAsync(SqlKata.Query query)
- {
- return await _repository.UpdateAsync(query) > 0;
- }
- /// <summary>
- /// 获取 pc 或者移动端
- /// </summary>
- /// <param name="terminal"></param>
- /// <returns></returns>
- public async Task<IEnumerable<NavigationResult>> GetAllAsync(int terminal)
- {
- var query = Q.NewQuery();
- if (terminal > 0)
- query.Where(nameof(Entity.Navigations.Navigation.Terminal), terminal);
- query.Where(nameof(Entity.Navigations.Navigation.IsDisable), false);
- query.Where(nameof(Entity.Navigations.Navigation.ParentId), 0);
- query.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort));
- var result = await _repository.GetAllAsync(query);
- var navigationResult = result.Select(n => _mapper.Map<NavigationResult>(n));
- foreach (var item in navigationResult)
- {
- item.IsChildren = await IsExistsAsync(item.Id);
- item.Children = await GetTreeAsync(item.Id, navigationResult.ToList());
- }
- return navigationResult;
- }
- /// <summary>
- /// 递归获取子集
- /// </summary>
- /// <param name="navigationResult"></param>
- /// <param name="id"></param>
- /// <param name="items"></param>
- /// <returns></returns>
- public async Task<List<NavigationResult>> GetTreeAsync(int id, List<NavigationResult> items)
- {
- foreach (var item in items)
- {
- item.IsChildren = await IsExistsAsync(item.Id);
- if (item.IsChildren)
- {
- var navigations = await _repository.GetAllAsync(Q.Where(nameof(Entity.Navigations.Navigation.ParentId), item.Id));
- item.Children = _mapper.Map<List<NavigationResult>>(navigations);
- foreach (var childrenItem in item.Children)
- {
- await GetTreeAsync(childrenItem.Id, item.Children);
- }
- }
- }
- return items;
- }
- public async Task<bool> IsExistsAsync(int parentId)
- {
- return await _repository.ExistsAsync(Q.Where(nameof(Entity.Navigations.Navigation.ParentId), parentId));
- }
- /// <summary>
- /// 获取导航栏数据
- /// </summary>
- /// <returns></returns>
- public async Task<List<NavigationMediaResult>> GetNavigationResults(int navigationId)
- {
- var result = new List<NavigationMediaResult>();
- var navigation = await GetAsync(navigationId);
- if (navigation.Terminal > 0)
- {
- //获取中间页面
- var middleLable = await _middleLableRepository.GetAsync(navigation.MiddleLableId);
- if (string.IsNullOrEmpty(middleLable.LabelId))
- return result;
- //获取标签
- var systemLabels = await _systemLabelRepository.GetAllAsync(Q.WhereIn(nameof(Entity.SystemLabel.SystemLabel.Id), StringUtils.StringCollectionToIntList(middleLable.LabelId)));
- foreach (var item in systemLabels)
- {
- var navigationResult = new NavigationResult()
- {
- Sort = item.Sort,
- StyleType = item.StyleType,
- ActionUrl = item.ActionUrl,
- ControllerUrl = item.ControllerUrl,
- IsPage = item.IsPage,
- IsSkip = item.IsSkip,
- LabelName = item.LabelName,
- ResourceType = item.ResourceType
- };
- //获取媒体
- }
- }
- return result;
- }
- }
- }
|