角色加日志和列表加筛选数据

This commit is contained in:
pangwenpeng 2024-05-30 17:51:36 +08:00
parent c584c03da2
commit 8721251475
1 changed files with 91 additions and 27 deletions

View File

@ -11,19 +11,43 @@ namespace langguanApi.Service
[ServiceInjection(InjectionType.Transient)] [ServiceInjection(InjectionType.Transient)]
public class MenuService : BaseService<Menu> public class MenuService : BaseService<Menu>
{ {
public MenuService(IConfiguration config) : base(config, nameof(Menu)) private ILogger<MenuService> _logger;
public MenuService(IConfiguration config, ILogger<MenuService> logger) : base(config, nameof(Menu))
{ {
_logger = logger;
} }
public async Task<List<Menu>> GetMenusByParentId(string parentId) public async Task<List<Menu>> GetMenusByParentId(string parentId)
{ {
Expression<Func<Menu, bool>> exp = filter => filter.IsDelete == false && filter.ParentId == parentId; Expression<Func<Menu, bool>> exp = filter => filter.IsDelete == false && filter.ParentId == parentId;
return (await base.GetListWithExp(exp)).OrderBy(x => x.Sort).ToList(); return (await base.GetListWithExp(exp)).OrderBy(x => x.Sort).ToList();
} }
/// <summary>
/// 新增菜单
/// </summary>
/// <param name="menu">菜单实体</param>
/// <returns></returns>
public async Task<ApiResult> AddMenu(AddMenuDto menu) public async Task<ApiResult> AddMenu(AddMenuDto menu)
{
try
{ {
var entity = menu.Adapt<Menu>(); var entity = menu.Adapt<Menu>();
await base.CreateAsync(entity); await base.CreateAsync(entity);
return new ApiResult(); }
catch (Exception ex)
{
_logger.LogError($"新增菜单出现异常,请求参数:{Newtonsoft.Json.JsonConvert.SerializeObject(menu)}," +
$"请求接口:'api/Menu/AddMenu'" +
$"异常信息:{ex.Message}," +
$"异常位置:{ex.StackTrace}"
);
return new ApiResult { code = 1, msg = "保存菜单失败", data = false };
}
finally
{
_logger.LogInformation($"新增菜单参数menu:{Newtonsoft.Json.JsonConvert.SerializeObject(menu)}");
}
return new ApiResult { code = 0, msg = "保存菜单信息成功", data = true };
} }
public async Task<ApiResult> UpdateMenu(UpdateMenuDto menu) public async Task<ApiResult> UpdateMenu(UpdateMenuDto menu)
{ {
@ -38,13 +62,12 @@ namespace langguanApi.Service
/// <returns></returns> /// <returns></returns>
public async Task<ApiResult> Pager(reqpage input) public async Task<ApiResult> Pager(reqpage input)
{ {
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>(); List<MenuTreeDto> dto = new List<MenuTreeDto>();
var MenuList = await GetChildList(null); try
{
var MenuList = await GetChildList("0");//获取跟节点
if (MenuList.Any())
{
foreach (var item in MenuList) foreach (var item in MenuList)
{ {
dto.Add(new MenuTreeDto() dto.Add(new MenuTreeDto()
@ -57,13 +80,49 @@ namespace langguanApi.Service
Children = await GetChildList(item.Id) Children = await GetChildList(item.Id)
}); });
} }
return new ApiResult() { data = dto }; //筛选数据
if (!string.IsNullOrEmpty(input.key))
{
if (dto.Exists(p => p.Name == input.key) ||
dto.Exists(p => p.Children.Exists(c => c.Name == input.key)) ||
dto.Exists(p => p.ParentName == input.key))
{
if (dto.Exists(p => p.Name == input.key))
{
dto = dto.Where(p => p.Name == input.key).ToList();
}
else if (dto.Exists(p => p.Children.Exists(c => c.Name == input.key)))
{
dto = dto.SelectMany(p => p.Children).Where(p => p.Name == input.key).ToList();
}
else
{
dto = dto.SelectMany(p => p.Children).Where(p => p.ParentName == input.key).ToList();
}
return new ApiResult() { code = 0, data = dto, msg = "获取菜单列表" };
}
else
{
return new ApiResult() { code = 0, data = null, msg = "获取菜单列表不存在" };
}
}
}
}
catch (Exception ex)
{
//return await base.GetPager(new ReqPaing() _logger.LogError($"获取菜单列表出现异常,请求参数:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}," +
//{ $"请求接口:'api/Menu/Pager'" +
// pageSize = input.pageSize, $"异常信息:{ex.Message}," +
// current = input.current $"异常位置:{ex.StackTrace}"
//}, exp); );
return new ApiResult { code = 1, msg = "获取菜单列表失败", data = false };
}
finally
{
_logger.LogInformation($"获取菜单列表参数menu:{Newtonsoft.Json.JsonConvert.SerializeObject(input)}");
}
return new ApiResult() { code = 0, data = dto, msg = "获取菜单列表" };
} }
/// <summary> /// <summary>
/// 递归获取子菜单列表 /// 递归获取子菜单列表
@ -73,13 +132,18 @@ namespace langguanApi.Service
public async Task<List<MenuTreeDto>> GetChildList(string parentId) public async Task<List<MenuTreeDto>> GetChildList(string parentId)
{ {
Expression<Func<Menu, bool>> exp = filter => filter.IsDelete == false && filter.ParentId == parentId; Expression<Func<Menu, bool>> exp = filter => filter.IsDelete == false && filter.ParentId == parentId;
Expression<Func<Menu, bool>> expDataSource = filter => filter.IsDelete;
var list = (await GetListWithExp(exp)) var list = (await GetListWithExp(exp))
.OrderByDescending(x => x.Sort) .OrderBy(x => x.Sort)
.ToList().Adapt<List<MenuTreeDto>>(); .ToList().Adapt<List<MenuTreeDto>>();
var DataSourceList = (await GetAsync())
.OrderBy(x => x.Sort)
.ToList();//拿到所有数据源,筛选结果
foreach (var item in list) foreach (var item in list)
{ {
item.Children = await GetChildList(item.Id); item.Children = await GetChildList(item.Id);
item.ParentName = (await base.GetAsync(item.ParentId))?.Name; item.ParentName = DataSourceList.FirstOrDefault(p => p.Id == item.ParentId)?.Name;
item.ParentId = DataSourceList.FirstOrDefault(p => p.Id == item.ParentId)?.Id;
} }
return list; return list;
} }