Files
DMS/DMS.Application/Services/Management/MenuManagementService.cs
David P.G 816827e5e9 refactor: 移除MenuBeanDto,直接使用MenuBean模型
- 删除了 DMS.Application/DTOs/MenuBeanDto.cs 文件
- 在DTOs中将MenuBeanDto类型替换为MenuBean类型
- 更新了IMenuAppService和IMenuManagementService接口中相关方法的参数和返回值类型
- 修改了MenuAppService、MenuManagementService等服务类的实现
- 在DMS.Core/Models/MenuBean.cs中添加了Children属性以支持菜单树结构
- 更新了WPF层相关的菜单处理逻辑
- 修改了映射配置和视图模型中的菜单对象创建方式
- 这一更改简化了数据模型,消除了DTO与模型之间的重复定义,直接在各层之间使用MenuBean实体。
2025-10-13 20:20:09 +08:00

163 lines
4.9 KiB
C#

using DMS.Application.Events;
using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Management;
using DMS.Core.Enums;
using DMS.Core.Models;
namespace DMS.Application.Services.Management;
/// <summary>
/// 菜单管理服务,负责菜单相关的业务逻辑。
/// </summary>
public class MenuManagementService : IMenuManagementService
{
private readonly IMenuAppService _menuService;
private readonly IAppDataStorageService _appDataStorageService;
private readonly IEventService _eventService;
/// <summary>
/// 当菜单数据发生变化时触发
/// </summary>
public event EventHandler<MenuChangedEventArgs> MenuChanged;
public MenuManagementService(IMenuAppService menuService, IAppDataStorageService appDataStorageService, IEventService eventService)
{
_menuService = menuService;
_appDataStorageService = appDataStorageService;
_eventService = eventService;
}
/// <summary>
/// 异步获取所有菜单列表。
/// </summary>
public async Task<List<MenuBean>> GetAllMenusAsync()
{
return await _menuService.GetAllMenusAsync();
}
/// <summary>
/// 异步根据ID获取菜单。
/// </summary>
public async Task<MenuBean> GetMenuByIdAsync(int id)
{
return await _menuService.GetMenuByIdAsync(id);
}
/// <summary>
/// 异步创建一个新菜单。
/// </summary>
public async Task<int> CreateMenuAsync(MenuBean menu)
{
var result = await _menuService.CreateMenuAsync(menu);
// 创建成功后,将菜单添加到内存中
if (result > 0)
{
menu.Id = result; // 假设返回的ID是新创建的
if (_appDataStorageService.Menus.TryAdd(menu.Id, menu))
{
MenuBean parentMenu = null;
if (menu.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menu.ParentId.Value, out var parent))
{
parentMenu = parent;
parent.Children.Add(menu);
}
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Added, menu));
}
}
return result;
}
/// <summary>
/// 异步更新一个已存在的菜单。
/// </summary>
public async Task<int> UpdateMenuAsync(MenuBean menu)
{
var result = await _menuService.UpdateMenuAsync(menu);
// 更新成功后,更新内存中的菜单
if (result > 0 && menu != null)
{
_appDataStorageService.Menus.AddOrUpdate(menu.Id, menu, (key, oldValue) => menu);
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Updated, menu));
}
return result;
}
/// <summary>
/// 异步删除一个菜单。
/// </summary>
public async Task<bool> DeleteMenuAsync(int id)
{
var menu = await _menuService.GetMenuByIdAsync(id); // 获取菜单信息用于内存删除
var result = await _menuService.DeleteMenuAsync(id);
// 删除成功后,从内存中移除菜单
if (result && menu != null)
{
if (_appDataStorageService.Menus.TryRemove(id, out var menuData))
{
// 从父菜单中移除子菜单
if (menuData.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menuData.ParentId.Value, out var parentMenu))
{
parentMenu.Children.Remove(menuData);
}
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Deleted, menuData));
}
}
return result;
}
/// <summary>
/// 获取根菜单列表
/// </summary>
public List<MenuBean> GetRootMenus()
{
return _appDataStorageService.Menus.Values.Where(m => m.ParentId == 0)
.ToList();
}
/// <summary>
/// 根据父级ID获取子菜单列表
/// </summary>
/// <param name="parentId">父级菜单ID</param>
/// <returns>子菜单列表</returns>
public List<MenuBean> GetChildMenus(int parentId)
{
return _appDataStorageService.Menus.Values.Where(m => m.ParentId == parentId)
.ToList();
}
/// <summary>
/// 构建菜单树结构
/// </summary>
public void BuildMenuTree()
{
// 清空现有菜单树
_appDataStorageService.MenuTrees.Clear();
// 获取所有根菜单
var rootMenus = GetRootMenus();
// 将根菜单添加到菜单树中
foreach (var rootMenu in rootMenus)
{
_appDataStorageService.MenuTrees.TryAdd(rootMenu.Id, rootMenu);
}
}
/// <summary>
/// 触发菜单变更事件
/// </summary>
protected virtual void OnMenuChanged(MenuChangedEventArgs e)
{
_eventService.RaiseMenuChanged(this, e);
}
}