using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using Datory; using GxPress.Common.AppOptions; using GxPress.Common.Tools; using GxPress.Repository.Interface.Menus; using Microsoft.Extensions.Options; using System.Linq; namespace GxPress.Repository.Implement.Menus { public class MenusRepository : IMenusRepository { private readonly Repository _repository; private readonly IMapper _mapper; public MenusRepository(IOptionsMonitor dbOptionsAccessor, IMapper mapper) { var databaseType = StringUtils.ToEnum(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql); var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString); _repository = new Repository(database); _mapper = mapper; } public IDatabase Database => _repository.Database; public string TableName => _repository.TableName; public List TableColumns => _repository.TableColumns; public async Task InsertAsync(Entity.Menus.Menus model) { return await _repository.InsertAsync(model); } public async Task UpdateAsync(Entity.Menus.Menus model) { return await _repository.UpdateAsync(model); } public async Task DeleteAsync(int id) { return await _repository.DeleteAsync(id); } public async Task> GetAllAsync() { var all = await _repository.GetAllAsync(); var list = new List(); list = all.Where(n => n.ParentId == 0).ToList(); foreach (var item in list) { item.Children = all.Where(n => n.ParentId == item.Id).ToList(); } return list; } public async Task> GetAllAsync(List menusIds) { var all = await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.Menus.Menus.Id), menusIds)); var list = new List(); list = all.Where(n => n.ParentId == 0).ToList(); foreach (var item in list) { item.Children = all.Where(n => n.ParentId == item.Id).ToList(); } return list; } public async Task> GetAllMenusAsync(List menusIds) { var all = await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.Menus.Menus.Id), menusIds)); return all; } } }