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.Media; 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; private readonly Repository _mediaLableRepository; private readonly Repository slideRepository; 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); _mediaLableRepository = new Repository(database); slideRepository = 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; if (!string.IsNullOrEmpty(request.ActionUrl)) model.ActionUrl = request.ActionUrl; if (!string.IsNullOrEmpty(request.ControllerUrl)) model.ControllerUrl = request.ControllerUrl; 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.OrderByDesc(nameof(Entity.Navigations.Navigation.Sort)); var queryResult = await _repository.GetAllAsync(query); var navigationResult = queryResult.Select(n => _mapper.Map(n)).ToList(); var result = navigationResult.Where(n => n.ParentId == 0); foreach (var item in result) { item.IsChildren = navigationResult.Any(n => n.ParentId == item.Id); if (item.IsChildren) { var navigationResultChildren = navigationResult.Where(n => n.ParentId == item.Id).ToList(); item.Children = await GetTreeAsync(item.Id, navigationResult.ToList(), navigationResultChildren); } } return result; } /// /// 递归获取子集 /// /// /// /// /// public async Task> GetTreeAsync(int id, List navigationResults, List items) { foreach (var item in items) { item.IsChildren = navigationResults.Any(n => n.ParentId == item.Id); if (item.IsChildren) { var navigationResultsParent = navigationResults.Where(n => n.ParentId == item.Id); item.Children = _mapper.Map>(navigationResultsParent); foreach (var childrenItem in item.Children) { await GetTreeAsync(childrenItem.Id, navigationResults, item.Children); } } } return items; } } }