81 lines
2.9 KiB
C#
81 lines
2.9 KiB
C#
using langguanApi.Extensions;
|
|
using langguanApi.Extensions.AutoDI;
|
|
using langguanApi.Model;
|
|
using langguanApi.Model.Entity;
|
|
using Mapster;
|
|
using Microsoft.AspNetCore.Mvc.ApiExplorer;
|
|
using System.Linq.Expressions;
|
|
|
|
namespace langguanApi.Service
|
|
{
|
|
[ServiceInjection(InjectionType.Transient)]
|
|
public class MenuService : BaseService<Menu>
|
|
{
|
|
public MenuService(IConfiguration config) : base(config, nameof(Menu))
|
|
{
|
|
}
|
|
public async Task<List<Menu>> GetMenusByParentId(string parentId)
|
|
{
|
|
Expression<Func<Menu, bool>> exp = filter => filter.IsDelete == false && filter.ParentId == parentId;
|
|
return (await base.GetListWithExp(exp)).OrderBy(x => x.Sort).ToList();
|
|
}
|
|
public async Task<ApiResult> AddMenu(AddMenuDto menu)
|
|
{
|
|
var entity = menu.Adapt<Menu>();
|
|
await base.CreateAsync(entity);
|
|
return new ApiResult();
|
|
}
|
|
public async Task<ApiResult> UpdateMenu(UpdateMenuDto menu)
|
|
{
|
|
var entity = menu.Adapt<Menu>();
|
|
await base.UpdateAsync(entity.Id, entity);
|
|
return new ApiResult();
|
|
}
|
|
/// <summary>
|
|
/// 获取菜单树
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<ApiResult> GetMenuTree()
|
|
{
|
|
//Expression<Func<Menu, bool>> exp = filter => filter.IsDelete == false;
|
|
//if (!string.IsNullOrEmpty(input.key))
|
|
//{
|
|
// exp = exp.And(filter => filter.Name.Contains(input.key));
|
|
//}
|
|
List<MenuTreeDto> dto = new List<MenuTreeDto>();
|
|
var MenuList = await GetChildList(null);
|
|
foreach (var item in MenuList)
|
|
{
|
|
dto.Add(new MenuTreeDto()
|
|
{
|
|
Id = item.Id,
|
|
Name = item.Name,
|
|
Sort = item.Sort,
|
|
ParentId = item.ParentId,
|
|
ParentName = item.ParentName,
|
|
Children = await GetChildList(item.Id)
|
|
});
|
|
}
|
|
return new ApiResult() { data = dto };
|
|
}
|
|
/// <summary>
|
|
/// 递归获取子菜单列表
|
|
/// </summary>
|
|
/// <param name="parentId"></param>
|
|
/// <returns></returns>
|
|
public async Task<List<MenuTreeDto>> GetChildList(string parentId)
|
|
{
|
|
Expression<Func<Menu, bool>> exp = filter => filter.IsDelete == false && filter.ParentId == parentId;
|
|
var list = (await GetListWithExp(exp))
|
|
.OrderByDescending(x => x.Sort)
|
|
.ToList().Adapt<List<MenuTreeDto>>();
|
|
foreach (var item in list)
|
|
{
|
|
item.Children = await GetChildList(item.Id);
|
|
item.ParentName = (await base.GetAsync(item.ParentId))?.Name;
|
|
}
|
|
return list;
|
|
}
|
|
}
|
|
}
|