lg_backend/langguanApi/Service/MenuService.cs

170 lines
6.7 KiB
C#
Raw Normal View History

2024-05-29 15:53:04 +00:00
using langguanApi.Extensions;
using langguanApi.Extensions.AutoDI;
2024-05-28 16:00:38 +00:00
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>
{
private ILogger<MenuService> _logger;
public MenuService(IConfiguration config, ILogger<MenuService> logger) : base(config, nameof(Menu))
2024-05-28 16:00:38 +00:00
{
_logger = logger;
2024-05-28 16:00:38 +00:00
}
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();
}
/// <summary>
/// 新增菜单
/// </summary>
/// <param name="menu">菜单实体</param>
/// <returns></returns>
2024-05-28 16:00:38 +00:00
public async Task<ApiResult> AddMenu(AddMenuDto menu)
{
try
{
var entity = menu.Adapt<Menu>();
await base.CreateAsync(entity);
}
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 };
2024-05-28 16:00:38 +00:00
}
2024-05-30 10:06:01 +00:00
/// <summary>
/// 更改菜单
/// </summary>
/// <param name="menu">菜单实体类</param>
/// <returns></returns>
2024-05-28 16:00:38 +00:00
public async Task<ApiResult> UpdateMenu(UpdateMenuDto menu)
{
2024-05-30 10:06:01 +00:00
try
{
if (string.IsNullOrEmpty(menu.Id))
{
return new ApiResult() { code = 0, data = false, msg = "更新菜单失败Id不能为空" };
}
var entity = menu.Adapt<Menu>();
await base.UpdateAsync(entity.Id, entity);
}
catch (Exception ex)
{
_logger.LogError($"修改菜单出现异常,请求参数:{Newtonsoft.Json.JsonConvert.SerializeObject(menu)}," +
$"请求接口:'api/Menu/UpdateMenu'" +
$"异常信息:{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, data = true, msg = "更新菜单成功" };
2024-05-28 16:00:38 +00:00
}
2024-05-29 15:53:04 +00:00
/// <summary>
/// 获取菜单树
/// </summary>
/// <returns></returns>
2024-05-30 14:04:41 +00:00
public async Task<ApiResult> GetMenuTree()
2024-05-29 15:53:04 +00:00
{
2024-05-30 14:04:41 +00:00
//Expression<Func<Menu, bool>> exp = filter => filter.IsDelete == false;
//if (!string.IsNullOrEmpty(input.key))
//{
// exp = exp.And(filter => filter.Name.Contains(input.key));
//}
2024-05-29 15:53:04 +00:00
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;
Expression<Func<Menu, bool>> expDataSource = filter => filter.IsDelete;
2024-05-29 15:53:04 +00:00
var list = (await GetListWithExp(exp))
.OrderBy(x => x.Sort)
2024-05-29 15:53:04 +00:00
.ToList().Adapt<List<MenuTreeDto>>();
var DataSourceList = (await GetAsync())
.OrderBy(x => x.Sort)
.ToList();//拿到所有数据源,筛选结果
2024-05-29 15:53:04 +00:00
foreach (var item in list)
{
item.Children = await GetChildList(item.Id);
item.ParentName = DataSourceList.FirstOrDefault(p => p.Id == item.ParentId)?.Name;
item.ParentId = DataSourceList.FirstOrDefault(p => p.Id == item.ParentId)?.Id;
2024-05-29 15:53:04 +00:00
}
return list;
}
2024-05-30 10:06:01 +00:00
/// <summary>
/// 作废菜单
/// </summary>
/// <param name="id">当前菜单id</param>
/// <returns></returns>
public async Task<ApiResult> DeleteMenu(string id)
{
try
{
var entity = await base.GetAsync(id);
if (entity == null)
{
return new ApiResult() { code = 0, data = false, msg = "删除菜单失败Id不存在" };
}
entity.IsDelete = true;
await base.UpdateAsync(id, entity);
}
catch (Exception ex)
{
_logger.LogError($"删除菜单出现异常,请求参数:{Newtonsoft.Json.JsonConvert.SerializeObject(id)}," +
$"请求接口:'api/Menu/DeleteMenu'" +
$"异常信息:{ex.Message}," +
$"异常位置:{ex.StackTrace}"
);
return new ApiResult { code = 1, msg = "删除菜单失败", data = false };
}
finally
{
_logger.LogInformation($"删除菜单参数menuId:{id}");
}
return new ApiResult() { code = 0, data = true, msg = "删除菜单成功" };
}
2024-05-28 16:00:38 +00:00
}
}