2025-09-09 13:35:16 +08:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using DMS.Application.Interfaces;
|
2025-10-05 17:07:17 +08:00
|
|
|
|
using DMS.Application.Interfaces.Management;
|
|
|
|
|
|
using DMS.Application.Services.Management;
|
2025-10-13 20:20:09 +08:00
|
|
|
|
using DMS.Core.Models;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
using DMS.WPF.Interfaces;
|
2025-10-06 18:17:56 +08:00
|
|
|
|
using DMS.WPF.ItemViewModel;
|
2025-10-05 17:07:17 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
|
|
|
|
|
namespace DMS.WPF.Services;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 菜单数据服务类,负责管理菜单相关的数据和操作。
|
|
|
|
|
|
/// </summary>
|
2025-10-20 19:39:17 +08:00
|
|
|
|
public class MenuWpfService : IMenuWpfService
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IMapper _mapper;
|
2025-10-20 19:39:17 +08:00
|
|
|
|
private readonly IWpfDataService _wpfDataService;
|
2025-10-18 17:18:09 +08:00
|
|
|
|
private readonly IAppStorageService _appStorageService;
|
2025-10-05 17:07:17 +08:00
|
|
|
|
private readonly IMenuManagementService _menuManagementService;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// MenuDataService类的构造函数。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="mapper">AutoMapper 实例。</param>
|
2025-10-18 17:18:09 +08:00
|
|
|
|
/// <param name="appStorageService">数据服务中心实例。</param>
|
2025-10-20 19:39:17 +08:00
|
|
|
|
public MenuWpfService(IMapper mapper, IWpfDataService dataStorageService, IAppStorageService appStorageService, IMenuManagementService menuManagementService)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
_mapper = mapper;
|
2025-10-20 19:39:17 +08:00
|
|
|
|
_wpfDataService = dataStorageService;
|
2025-10-18 17:18:09 +08:00
|
|
|
|
_appStorageService = appStorageService;
|
2025-10-05 17:07:17 +08:00
|
|
|
|
_menuManagementService = menuManagementService;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void LoadAllMenus()
|
|
|
|
|
|
{
|
2025-10-20 19:39:17 +08:00
|
|
|
|
_wpfDataService.Menus = _mapper.Map<ObservableCollection<MenuItem>>(_appStorageService.Menus.Values);
|
2025-09-09 13:35:16 +08:00
|
|
|
|
BuildMenuTrees();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构建菜单树。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void BuildMenuTrees()
|
|
|
|
|
|
{
|
2025-10-20 19:39:17 +08:00
|
|
|
|
_wpfDataService.MenuTrees.Clear();
|
2025-09-09 13:35:16 +08:00
|
|
|
|
// 遍历所有菜单项,构建树形结构
|
2025-10-20 19:39:17 +08:00
|
|
|
|
foreach (var menu in _wpfDataService.Menus)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-10-20 19:39:17 +08:00
|
|
|
|
var parentMenu = _wpfDataService.Menus.FirstOrDefault(m => m.Id == menu.ParentId);
|
2025-09-09 13:35:16 +08:00
|
|
|
|
// 检查是否有父ID,并且父ID不为0(通常0或null表示根节点)
|
|
|
|
|
|
if (parentMenu != null && menu.ParentId != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 将当前菜单添加到父菜单的Children列表中
|
|
|
|
|
|
if (!parentMenu.Children.Contains(menu))
|
|
|
|
|
|
{
|
|
|
|
|
|
parentMenu.Children.Add(menu);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果没有父ID,则这是一个根菜单
|
2025-10-20 19:39:17 +08:00
|
|
|
|
_wpfDataService.MenuTrees.Add(menu);
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加菜单项。
|
|
|
|
|
|
/// </summary>
|
2025-10-20 19:39:17 +08:00
|
|
|
|
public void AddMenuToView(MenuItem MenuItem)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-10-06 18:17:56 +08:00
|
|
|
|
if (MenuItem is null) return;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
2025-10-20 19:39:17 +08:00
|
|
|
|
var deviceMenu = _wpfDataService.Menus.FirstOrDefault(m => m.Id == MenuItem.ParentId);
|
2025-10-05 17:07:17 +08:00
|
|
|
|
if (deviceMenu is not null)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-10-05 17:07:17 +08:00
|
|
|
|
|
2025-10-20 19:39:17 +08:00
|
|
|
|
deviceMenu.Children.Add(MenuItem);
|
|
|
|
|
|
_wpfDataService.Menus.Add(MenuItem);
|
|
|
|
|
|
BuildMenuTrees();
|
2025-10-19 20:34:20 +08:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新菜单项。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task UpdateMenuItem(MenuItem MenuItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (MenuItem is null) return;
|
|
|
|
|
|
|
2025-10-20 19:39:17 +08:00
|
|
|
|
var menu = _wpfDataService.Menus.FirstOrDefault(m => m.Id == MenuItem.Id);
|
2025-10-19 20:34:20 +08:00
|
|
|
|
if (menu is not null)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
var res = await _menuManagementService.UpdateMenuAsync(_mapper.Map<MenuBean>(MenuItem));
|
|
|
|
|
|
if (res > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
menu.Header = MenuItem.Header;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 删除菜单项。
|
|
|
|
|
|
/// </summary>
|
2025-10-06 18:17:56 +08:00
|
|
|
|
public async Task DeleteMenuItem(MenuItem? MenuItem)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
2025-10-06 18:17:56 +08:00
|
|
|
|
if (MenuItem is null) return;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
2025-10-19 20:34:20 +08:00
|
|
|
|
await _menuManagementService.DeleteMenuAsync(MenuItem.Id);
|
|
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
// 从扁平菜单列表中移除
|
2025-10-20 19:39:17 +08:00
|
|
|
|
_wpfDataService.Menus.Remove(MenuItem);
|
2025-09-09 13:35:16 +08:00
|
|
|
|
|
2025-10-05 17:07:17 +08:00
|
|
|
|
//// 从树形结构中移除
|
2025-10-06 18:17:56 +08:00
|
|
|
|
if (MenuItem.ParentId.HasValue && MenuItem.ParentId.Value != 0)
|
2025-09-09 13:35:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 如果有父菜单,从父菜单的Children中移除
|
2025-10-20 19:39:17 +08:00
|
|
|
|
var parentMenu = _wpfDataService.Menus.FirstOrDefault(m => m.Id == MenuItem.ParentId.Value);
|
2025-10-06 18:17:56 +08:00
|
|
|
|
parentMenu?.Children.Remove(MenuItem);
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果是根菜单,从MenuTrees中移除
|
2025-10-20 19:39:17 +08:00
|
|
|
|
_wpfDataService.MenuTrees.Remove(MenuItem);
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
2025-10-05 17:07:17 +08:00
|
|
|
|
|
|
|
|
|
|
//BuildMenuTrees();
|
2025-09-09 13:35:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|