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 _repository; private readonly IMapper _mapper; private readonly string _connectionString; private readonly string _databaseTypestr; private readonly Repository _middleLableRepository; private readonly Repository _systemLabelRepository; private readonly Repository _mediaRepository; public NavigationRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType; _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString; var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _middleLableRepository = new Repository(database); _systemLabelRepository = new Repository(database); _mediaRepository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task GetAsync(int id) { return await _repository.GetAsync(id); } public async Task DeleteAsync(int id) { return await _repository.DeleteAsync(id); } public async Task InsertAsync(Entity.Navigations.Navigation note) { return await _repository.InsertAsync(note); } public async Task 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 UpdateAsync(SqlKata.Query query) { return await _repository.UpdateAsync(query) > 0; } /// /// 获取 pc 或者移动端 /// /// /// public async Task> 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(n)); foreach (var item in navigationResult) { item.IsChildren = await IsExistsAsync(item.Id); item.Children = await GetTreeAsync(item.Id, navigationResult.ToList()); } return navigationResult; } /// /// 递归获取子集 /// /// /// /// /// public async Task> GetTreeAsync(int id, List 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>(navigations); foreach (var childrenItem in item.Children) { await GetTreeAsync(childrenItem.Id, item.Children); } } } return items; } public async Task IsExistsAsync(int parentId) { return await _repository.ExistsAsync(Q.Where(nameof(Entity.Navigations.Navigation.ParentId), parentId)); } /// /// 获取导航栏数据 /// /// public async Task> GetNavigationResults(int navigationId) { var result = new List(); 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; } } }