2025-09-16 14:42:23 +08:00
|
|
|
using DMS.Application.Events;
|
2025-09-07 21:16:56 +08:00
|
|
|
using DMS.Application.Interfaces;
|
2025-09-16 12:29:09 +08:00
|
|
|
using DMS.Application.Interfaces.Management;
|
2025-09-07 21:16:56 +08:00
|
|
|
using DMS.Core.Enums;
|
2025-10-13 20:20:09 +08:00
|
|
|
using DMS.Core.Models;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
2025-09-16 12:29:09 +08:00
|
|
|
namespace DMS.Application.Services.Management;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 菜单管理服务,负责菜单相关的业务逻辑。
|
|
|
|
|
/// </summary>
|
2025-09-09 15:28:07 +08:00
|
|
|
public class MenuManagementService : IMenuManagementService
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-05 17:07:17 +08:00
|
|
|
private readonly IMenuAppService _menuService;
|
2025-10-18 17:18:09 +08:00
|
|
|
private readonly IAppStorageService _appStorageService;
|
2025-10-05 17:07:17 +08:00
|
|
|
private readonly IEventService _eventService;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 当菜单数据发生变化时触发
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<MenuChangedEventArgs> MenuChanged;
|
|
|
|
|
|
2025-10-18 17:18:09 +08:00
|
|
|
public MenuManagementService(IMenuAppService menuService, IAppStorageService appStorageService, IEventService eventService)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
|
|
|
|
_menuService = menuService;
|
2025-10-18 17:18:09 +08:00
|
|
|
_appStorageService = appStorageService;
|
2025-10-05 17:07:17 +08:00
|
|
|
_eventService = eventService;
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-10-13 20:20:09 +08:00
|
|
|
/// 异步获取所有菜单列表。
|
2025-09-07 21:16:56 +08:00
|
|
|
/// </summary>
|
2025-10-13 20:20:09 +08:00
|
|
|
public async Task<List<MenuBean>> GetAllMenusAsync()
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
|
|
|
|
return await _menuService.GetAllMenusAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-10-13 20:20:09 +08:00
|
|
|
/// 异步根据ID获取菜单。
|
2025-09-07 21:16:56 +08:00
|
|
|
/// </summary>
|
2025-10-13 20:20:09 +08:00
|
|
|
public async Task<MenuBean> GetMenuByIdAsync(int id)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
|
|
|
|
return await _menuService.GetMenuByIdAsync(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步创建一个新菜单。
|
|
|
|
|
/// </summary>
|
2025-10-13 20:20:09 +08:00
|
|
|
public async Task<int> CreateMenuAsync(MenuBean menu)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-13 20:20:09 +08:00
|
|
|
var result = await _menuService.CreateMenuAsync(menu);
|
2025-10-05 17:07:17 +08:00
|
|
|
|
|
|
|
|
// 创建成功后,将菜单添加到内存中
|
|
|
|
|
if (result > 0)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-13 20:20:09 +08:00
|
|
|
menu.Id = result; // 假设返回的ID是新创建的
|
2025-10-18 17:18:09 +08:00
|
|
|
if (_appStorageService.Menus.TryAdd(menu.Id, menu))
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-13 20:20:09 +08:00
|
|
|
MenuBean parentMenu = null;
|
2025-10-18 17:18:09 +08:00
|
|
|
if (menu.ParentId > 0 && _appStorageService.Menus.TryGetValue(menu.ParentId.Value, out var parent))
|
2025-10-05 17:07:17 +08:00
|
|
|
{
|
|
|
|
|
parentMenu = parent;
|
2025-10-13 20:20:09 +08:00
|
|
|
parent.Children.Add(menu);
|
2025-10-05 17:07:17 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-13 20:20:09 +08:00
|
|
|
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Added, menu));
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-10-05 17:07:17 +08:00
|
|
|
|
|
|
|
|
return result;
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-10-05 17:07:17 +08:00
|
|
|
/// 异步更新一个已存在的菜单。
|
2025-09-07 21:16:56 +08:00
|
|
|
/// </summary>
|
2025-10-13 20:20:09 +08:00
|
|
|
public async Task<int> UpdateMenuAsync(MenuBean menu)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-13 20:20:09 +08:00
|
|
|
var result = await _menuService.UpdateMenuAsync(menu);
|
2025-10-05 17:07:17 +08:00
|
|
|
|
|
|
|
|
// 更新成功后,更新内存中的菜单
|
2025-10-19 20:34:20 +08:00
|
|
|
if (result > 0)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-19 20:34:20 +08:00
|
|
|
if (_appStorageService.Menus.TryGetValue(menu.Id,out var mMenu))
|
|
|
|
|
{
|
|
|
|
|
mMenu.Header = menu.Header;
|
|
|
|
|
}
|
2025-10-05 17:07:17 +08:00
|
|
|
|
2025-10-13 20:20:09 +08:00
|
|
|
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Updated, menu));
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-05 17:07:17 +08:00
|
|
|
return result;
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-10-05 17:07:17 +08:00
|
|
|
/// 异步删除一个菜单。
|
2025-09-07 21:16:56 +08:00
|
|
|
/// </summary>
|
2025-10-05 17:07:17 +08:00
|
|
|
public async Task<bool> DeleteMenuAsync(int id)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-05 17:07:17 +08:00
|
|
|
var menu = await _menuService.GetMenuByIdAsync(id); // 获取菜单信息用于内存删除
|
|
|
|
|
var result = await _menuService.DeleteMenuAsync(id);
|
|
|
|
|
|
|
|
|
|
// 删除成功后,从内存中移除菜单
|
|
|
|
|
if (result && menu != null)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-18 17:18:09 +08:00
|
|
|
if (_appStorageService.Menus.TryRemove(id, out var menuData))
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-05 17:07:17 +08:00
|
|
|
// 从父菜单中移除子菜单
|
2025-10-18 17:18:09 +08:00
|
|
|
if (menuData.ParentId > 0 && _appStorageService.Menus.TryGetValue(menuData.ParentId.Value, out var parentMenu))
|
2025-10-05 17:07:17 +08:00
|
|
|
{
|
2025-10-13 20:20:09 +08:00
|
|
|
parentMenu.Children.Remove(menuData);
|
2025-10-05 17:07:17 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-13 20:20:09 +08:00
|
|
|
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Deleted, menuData));
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-10-05 17:07:17 +08:00
|
|
|
|
|
|
|
|
return result;
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取根菜单列表
|
|
|
|
|
/// </summary>
|
2025-10-13 20:20:09 +08:00
|
|
|
public List<MenuBean> GetRootMenus()
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-18 17:18:09 +08:00
|
|
|
return _appStorageService.Menus.Values.Where(m => m.ParentId == 0)
|
2025-09-07 21:16:56 +08:00
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据父级ID获取子菜单列表
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="parentId">父级菜单ID</param>
|
|
|
|
|
/// <returns>子菜单列表</returns>
|
2025-10-13 20:20:09 +08:00
|
|
|
public List<MenuBean> GetChildMenus(int parentId)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-18 17:18:09 +08:00
|
|
|
return _appStorageService.Menus.Values.Where(m => m.ParentId == parentId)
|
2025-09-07 21:16:56 +08:00
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 构建菜单树结构
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void BuildMenuTree()
|
|
|
|
|
{
|
|
|
|
|
// 清空现有菜单树
|
2025-10-18 17:18:09 +08:00
|
|
|
_appStorageService.MenuTrees.Clear();
|
2025-09-07 21:16:56 +08:00
|
|
|
|
|
|
|
|
// 获取所有根菜单
|
|
|
|
|
var rootMenus = GetRootMenus();
|
|
|
|
|
|
|
|
|
|
// 将根菜单添加到菜单树中
|
|
|
|
|
foreach (var rootMenu in rootMenus)
|
|
|
|
|
{
|
2025-10-18 17:18:09 +08:00
|
|
|
_appStorageService.MenuTrees.TryAdd(rootMenu.Id, rootMenu);
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 触发菜单变更事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void OnMenuChanged(MenuChangedEventArgs e)
|
|
|
|
|
{
|
2025-10-05 17:07:17 +08:00
|
|
|
_eventService.RaiseMenuChanged(this, e);
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
2025-10-18 17:59:21 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步加载所有菜单数据到内存中。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task LoadAllMenusAsync()
|
|
|
|
|
{
|
|
|
|
|
_appStorageService.Menus.Clear();
|
|
|
|
|
_appStorageService.MenuTrees.Clear();
|
|
|
|
|
var menus = await _menuService.GetAllMenusAsync();
|
|
|
|
|
// 将菜单添加到安全字典
|
|
|
|
|
foreach (var menuBean in menus)
|
|
|
|
|
{
|
|
|
|
|
_appStorageService.Menus.TryAdd(menuBean.Id, menuBean);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|