diff --git a/DMS.Application/DTOs/CreateMqttWithMenuDto.cs b/DMS.Application/DTOs/CreateMqttWithMenuDto.cs
new file mode 100644
index 0000000..8cfd07b
--- /dev/null
+++ b/DMS.Application/DTOs/CreateMqttWithMenuDto.cs
@@ -0,0 +1,20 @@
+using DMS.Application.DTOs;
+
+namespace DMS.Application.DTOs
+{
+ ///
+ /// 创建MQTT服务器及其关联菜单的数据传输对象
+ ///
+ public class CreateMqttWithMenuDto
+ {
+ ///
+ /// MQTT服务器信息
+ ///
+ public MqttServerDto MqttServer { get; set; }
+
+ ///
+ /// 菜单项信息
+ ///
+ public MenuBeanDto MqttServerMenu { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/DMS.Application/Events/MenuChangedEventArgs.cs b/DMS.Application/Events/MenuChangedEventArgs.cs
index c78c7f5..d83ba64 100644
--- a/DMS.Application/Events/MenuChangedEventArgs.cs
+++ b/DMS.Application/Events/MenuChangedEventArgs.cs
@@ -18,28 +18,16 @@ namespace DMS.Application.Events
///
public MenuBeanDto Menu { get; }
- ///
- /// 父级菜单DTO
- ///
- public MenuBeanDto ParentMenu { get; }
-
- ///
- /// 变更时间
- ///
- public DateTime ChangeTime { get; }
-
///
/// 构造函数
///
/// 变更类型
/// 菜单DTO
/// 父级菜单DTO
- public MenuChangedEventArgs(DataChangeType changeType, MenuBeanDto menu, MenuBeanDto parentMenu)
+ public MenuChangedEventArgs(DataChangeType changeType, MenuBeanDto menu)
{
ChangeType = changeType;
Menu = menu;
- ParentMenu = parentMenu;
- ChangeTime = DateTime.Now;
}
}
}
\ No newline at end of file
diff --git a/DMS.Application/Interfaces/IMenuService.cs b/DMS.Application/Interfaces/Database/IMenuAppService.cs
similarity index 84%
rename from DMS.Application/Interfaces/IMenuService.cs
rename to DMS.Application/Interfaces/Database/IMenuAppService.cs
index 01d1889..0406a5b 100644
--- a/DMS.Application/Interfaces/IMenuService.cs
+++ b/DMS.Application/Interfaces/Database/IMenuAppService.cs
@@ -5,7 +5,7 @@ namespace DMS.Application.Interfaces;
///
/// 定义菜单管理相关的应用服务操作。
///
-public interface IMenuService
+public interface IMenuAppService
{
///
/// 异步根据ID获取菜单DTO。
@@ -25,11 +25,11 @@ public interface IMenuService
///
/// 异步更新一个已存在的菜单。
///
- Task UpdateMenuAsync(MenuBeanDto menuDto);
+ Task UpdateMenuAsync(MenuBeanDto menuDto);
///
/// 异步删除一个菜单。
///
- Task DeleteMenuAsync(int id);
+ Task DeleteMenuAsync(int id);
}
\ No newline at end of file
diff --git a/DMS.Application/Interfaces/IEventService.cs b/DMS.Application/Interfaces/IEventService.cs
index 63d5a2a..9102600 100644
--- a/DMS.Application/Interfaces/IEventService.cs
+++ b/DMS.Application/Interfaces/IEventService.cs
@@ -120,4 +120,20 @@ public interface IEventService
/// 事件发送者
/// 数据加载完成事件参数
void RaiseLoadDataCompleted(object sender, DataLoadCompletedEventArgs e);
+
+ #region 菜单事件
+
+ ///
+ /// 菜单改变事件
+ ///
+ event EventHandler OnMenuChanged;
+
+ ///
+ /// 触发菜单改变事件
+ ///
+ /// 事件发送者
+ /// 菜单改变事件参数
+ void RaiseMenuChanged(object sender, MenuChangedEventArgs e);
+
+ #endregion
}
\ No newline at end of file
diff --git a/DMS.Application/Interfaces/Management/IMenuManagementService.cs b/DMS.Application/Interfaces/Management/IMenuManagementService.cs
index ae1a5e2..fff4e3e 100644
--- a/DMS.Application/Interfaces/Management/IMenuManagementService.cs
+++ b/DMS.Application/Interfaces/Management/IMenuManagementService.cs
@@ -22,27 +22,12 @@ public interface IMenuManagementService
///
/// 异步更新一个已存在的菜单。
///
- Task UpdateMenuAsync(MenuBeanDto menuDto);
+ Task UpdateMenuAsync(MenuBeanDto menuDto);
///
/// 异步删除一个菜单。
///
- Task DeleteMenuAsync(int id);
-
- ///
- /// 在内存中添加菜单
- ///
- void AddMenuToMemory(MenuBeanDto menuDto);
-
- ///
- /// 在内存中更新菜单
- ///
- void UpdateMenuInMemory(MenuBeanDto menuDto);
-
- ///
- /// 在内存中删除菜单
- ///
- void RemoveMenuFromMemory(int menuId);
+ Task DeleteMenuAsync(int id);
///
/// 获取根菜单列表
@@ -60,4 +45,9 @@ public interface IMenuManagementService
/// 构建菜单树结构
///
void BuildMenuTree();
+
+ ///
+ /// 当菜单数据发生变化时触发
+ ///
+ event EventHandler MenuChanged;
}
\ No newline at end of file
diff --git a/DMS.Application/Interfaces/Management/IMqttManagementService.cs b/DMS.Application/Interfaces/Management/IMqttManagementService.cs
index 54142c5..74b6783 100644
--- a/DMS.Application/Interfaces/Management/IMqttManagementService.cs
+++ b/DMS.Application/Interfaces/Management/IMqttManagementService.cs
@@ -38,4 +38,5 @@ public interface IMqttManagementService
/// 异步批量删除MQTT服务器。
///
Task DeleteMqttServersAsync(List ids);
+
}
\ No newline at end of file
diff --git a/DMS.Application/Services/DataLoaderService.cs b/DMS.Application/Services/DataLoaderService.cs
index ddc7cd2..c4c9396 100644
--- a/DMS.Application/Services/DataLoaderService.cs
+++ b/DMS.Application/Services/DataLoaderService.cs
@@ -24,7 +24,7 @@ public class DataLoaderService : IDataLoaderService
private readonly IDeviceAppService _deviceAppService;
private readonly IVariableTableAppService _variableTableAppService;
private readonly IVariableAppService _variableAppService;
- private readonly IMenuService _menuService;
+ private readonly IMenuAppService _menuService;
private readonly IMqttAppService _mqttAppService;
private readonly INlogAppService _nlogAppService;
private readonly ITriggerManagementService _triggerManagementService; // 添加触发器管理服务
@@ -40,7 +40,7 @@ public class DataLoaderService : IDataLoaderService
IDeviceAppService deviceAppService,
IVariableTableAppService variableTableAppService,
IVariableAppService variableAppService,
- IMenuService menuService,
+ IMenuAppService menuService,
IMqttAppService mqttAppService,
INlogAppService nlogAppService,
ITriggerManagementService triggerManagementService, // 添加触发器管理服务参数
diff --git a/DMS.Application/Services/MenuService.cs b/DMS.Application/Services/Database/MenuAppService.cs
similarity index 78%
rename from DMS.Application/Services/MenuService.cs
rename to DMS.Application/Services/Database/MenuAppService.cs
index 4c0efa7..598b0dd 100644
--- a/DMS.Application/Services/MenuService.cs
+++ b/DMS.Application/Services/Database/MenuAppService.cs
@@ -2,15 +2,16 @@ using AutoMapper;
using DMS.Core.Interfaces;
using DMS.Core.Models;
using DMS.Application.DTOs;
+using DMS.Application.Interfaces.Database;
using DMS.Application.Interfaces;
-namespace DMS.Application.Services;
+namespace DMS.Application.Services.Database;
///
/// 菜单应用服务,负责处理菜单相关的业务逻辑。
-/// 实现 接口。
+/// 实现 接口。
///
-public class MenuService : IMenuService
+public class MenuAppService : IMenuAppService
{
private readonly IRepositoryManager _repoManager;
private readonly IMapper _mapper;
@@ -20,7 +21,7 @@ public class MenuService : IMenuService
///
/// 仓储管理器实例。
/// AutoMapper 实例。
- public MenuService(IRepositoryManager repoManager, IMapper mapper)
+ public MenuAppService(IRepositoryManager repoManager, IMapper mapper)
{
_repoManager = repoManager;
_mapper = mapper;
@@ -74,9 +75,9 @@ public class MenuService : IMenuService
/// 异步更新一个已存在的菜单(事务性操作)。
///
/// 要更新的菜单数据传输对象。
- /// 表示异步操作的任务。
+ /// 受影响的行数。
/// 如果找不到菜单或更新菜单时发生错误。
- public async Task UpdateMenuAsync(MenuBeanDto menuDto)
+ public async Task UpdateMenuAsync(MenuBeanDto menuDto)
{
try
{
@@ -87,8 +88,9 @@ public class MenuService : IMenuService
throw new ApplicationException($"Menu with ID {menuDto.Id} not found.");
}
_mapper.Map(menuDto, menu);
- await _repoManager.Menus.UpdateAsync(menu);
+ int res = await _repoManager.Menus.UpdateAsync(menu);
await _repoManager.CommitAsync();
+ return res;
}
catch (Exception ex)
{
@@ -101,15 +103,21 @@ public class MenuService : IMenuService
/// 异步删除一个菜单(事务性操作)。
///
/// 要删除菜单的ID。
- /// 表示异步操作的任务。
- /// 如果删除菜单时发生错误。
- public async Task DeleteMenuAsync(int id)
+ /// 如果删除成功则为 true,否则为 false。
+ /// 如果删除菜单失败。
+ /// 如果删除菜单时发生其他错误。
+ public async Task DeleteMenuAsync(int id)
{
try
{
await _repoManager.BeginTranAsync();
- await _repoManager.Menus.DeleteByIdAsync(id);
+ var delRes = await _repoManager.Menus.DeleteByIdAsync(id);
+ if (delRes == 0)
+ {
+ throw new InvalidOperationException($"删除菜单失败:菜单ID:{id},请检查菜单Id是否存在");
+ }
await _repoManager.CommitAsync();
+ return true;
}
catch (Exception ex)
{
diff --git a/DMS.Application/Services/EventService.cs b/DMS.Application/Services/EventService.cs
index 1a19561..08609a4 100644
--- a/DMS.Application/Services/EventService.cs
+++ b/DMS.Application/Services/EventService.cs
@@ -154,4 +154,23 @@ public class EventService : IEventService
}
#endregion
+
+ #region 菜单事件
+
+ ///
+ /// 菜单改变事件
+ ///
+ public event EventHandler OnMenuChanged;
+
+ ///
+ /// 触发菜单改变事件
+ ///
+ /// 事件发送者
+ /// 菜单改变事件参数
+ public void RaiseMenuChanged(object sender, MenuChangedEventArgs e)
+ {
+ OnMenuChanged?.Invoke(sender, e);
+ }
+
+ #endregion
}
\ No newline at end of file
diff --git a/DMS.Application/Services/Management/MenuManagementService.cs b/DMS.Application/Services/Management/MenuManagementService.cs
index f1efeb0..41b7d5b 100644
--- a/DMS.Application/Services/Management/MenuManagementService.cs
+++ b/DMS.Application/Services/Management/MenuManagementService.cs
@@ -11,18 +11,20 @@ namespace DMS.Application.Services.Management;
///
public class MenuManagementService : IMenuManagementService
{
- private readonly IMenuService _menuService;
+ private readonly IMenuAppService _menuService;
private readonly IAppDataStorageService _appDataStorageService;
+ private readonly IEventService _eventService;
///
/// 当菜单数据发生变化时触发
///
public event EventHandler MenuChanged;
- public MenuManagementService(IMenuService menuService,IAppDataStorageService appDataStorageService)
+ public MenuManagementService(IMenuAppService menuService, IAppDataStorageService appDataStorageService, IEventService eventService)
{
_menuService = menuService;
_appDataStorageService = appDataStorageService;
+ _eventService = eventService;
}
///
@@ -46,74 +48,71 @@ public class MenuManagementService : IMenuManagementService
///
public async Task CreateMenuAsync(MenuBeanDto menuDto)
{
- return await _menuService.CreateMenuAsync(menuDto);
+ var result = await _menuService.CreateMenuAsync(menuDto);
+
+ // 创建成功后,将菜单添加到内存中
+ if (result > 0)
+ {
+ menuDto.Id = result; // 假设返回的ID是新创建的
+ if (_appDataStorageService.Menus.TryAdd(menuDto.Id, menuDto))
+ {
+ MenuBeanDto parentMenu = null;
+ if (menuDto.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menuDto.ParentId, out var parent))
+ {
+ parentMenu = parent;
+ parent.Children.Add(menuDto);
+ }
+
+ _eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Added, menuDto));
+ }
+ }
+
+ return result;
}
///
/// 异步更新一个已存在的菜单。
///
- public async Task UpdateMenuAsync(MenuBeanDto menuDto)
+ public async Task UpdateMenuAsync(MenuBeanDto menuDto)
{
- await _menuService.UpdateMenuAsync(menuDto);
+ var result = await _menuService.UpdateMenuAsync(menuDto);
+
+ // 更新成功后,更新内存中的菜单
+ if (result > 0 && menuDto != null)
+ {
+ _appDataStorageService.Menus.AddOrUpdate(menuDto.Id, menuDto, (key, oldValue) => menuDto);
+
+
+ _eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Updated, menuDto));
+ }
+
+ return result;
}
///
/// 异步删除一个菜单。
///
- public async Task DeleteMenuAsync(int id)
+ public async Task DeleteMenuAsync(int id)
{
- await _menuService.DeleteMenuAsync(id);
- }
-
- ///
- /// 在内存中添加菜单
- ///
- public void AddMenuToMemory(MenuBeanDto menuDto)
- {
- if (_appDataStorageService.Menus.TryAdd(menuDto.Id, menuDto))
+ var menu = await _menuService.GetMenuByIdAsync(id); // 获取菜单信息用于内存删除
+ var result = await _menuService.DeleteMenuAsync(id);
+
+ // 删除成功后,从内存中移除菜单
+ if (result && menu != null)
{
- MenuBeanDto parentMenu = null;
- if (menuDto.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menuDto.ParentId, out var parent))
+ if (_appDataStorageService.Menus.TryRemove(id, out var menuDto))
{
- parentMenu = parent;
- parent.Children.Add(menuDto);
+ // 从父菜单中移除子菜单
+ if (menuDto.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menuDto.ParentId, out var parentMenu))
+ {
+ parentMenu.Children.Remove(menuDto);
+ }
+
+ _eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Deleted, menuDto));
}
-
- OnMenuChanged(new MenuChangedEventArgs(DataChangeType.Added, menuDto, parentMenu));
- }
- }
-
- ///
- /// 在内存中更新菜单
- ///
- public void UpdateMenuInMemory(MenuBeanDto menuDto)
- {
- _appDataStorageService.Menus.AddOrUpdate(menuDto.Id, menuDto, (key, oldValue) => menuDto);
-
- MenuBeanDto parentMenu = null;
- if (menuDto.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menuDto.ParentId, out var parent))
- {
- parentMenu = parent;
}
- OnMenuChanged(new MenuChangedEventArgs(DataChangeType.Updated, menuDto, parentMenu));
- }
-
- ///
- /// 在内存中删除菜单
- ///
- public void RemoveMenuFromMemory(int menuId)
- {
- if (_appDataStorageService.Menus.TryRemove(menuId, out var menuDto))
- {
- MenuBeanDto parentMenu = null;
- if (menuDto.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menuDto.ParentId, out var parent))
- {
- parentMenu = parent;
- }
-
- OnMenuChanged(new MenuChangedEventArgs(DataChangeType.Deleted, menuDto, parentMenu));
- }
+ return result;
}
///
@@ -159,6 +158,6 @@ public class MenuManagementService : IMenuManagementService
///
protected virtual void OnMenuChanged(MenuChangedEventArgs e)
{
- MenuChanged?.Invoke(this, e);
+ _eventService.RaiseMenuChanged(this, e);
}
}
\ No newline at end of file
diff --git a/DMS.Application/Services/Management/MqttManagementService.cs b/DMS.Application/Services/Management/MqttManagementService.cs
index 680e618..199095e 100644
--- a/DMS.Application/Services/Management/MqttManagementService.cs
+++ b/DMS.Application/Services/Management/MqttManagementService.cs
@@ -19,18 +19,21 @@ public class MqttManagementService : IMqttManagementService
private readonly IEventService _eventService;
private readonly IMapper _mapper;
private readonly IDataProcessingService _dataProcessingService;
+ private readonly IMenuManagementService _menuManagementService;
public MqttManagementService(IMqttAppService mqttAppService,
IAppDataStorageService appDataStorageService,
IEventService eventService,
IMapper mapper,
- IDataProcessingService dataProcessingService)
+ IDataProcessingService dataProcessingService,
+ IMenuManagementService menuManagementService)
{
_mqttAppService = mqttAppService;
_appDataStorageService = appDataStorageService;
_eventService = eventService;
_mapper = mapper;
_dataProcessingService = dataProcessingService;
+ _menuManagementService = menuManagementService;
}
///
@@ -49,27 +52,6 @@ public class MqttManagementService : IMqttManagementService
return await _mqttAppService.GetAllMqttServersAsync();
}
- ///
- /// 异步创建一个新的MQTT服务器。
- ///
- public async Task CreateMqttServerAsync(MqttServerDto mqttServerDto)
- {
- var result = await _mqttAppService.CreateMqttServerAsync(mqttServerDto);
-
- // 创建成功后,将MQTT服务器添加到内存中
- if (result > 0)
- {
- mqttServerDto.Id = result; // 假设返回的ID是新创建的
- if (_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto))
- {
- _eventService.RaiseMqttServerChanged(
- this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServerDto));
- }
- }
-
- return mqttServerDto;
- }
-
///
/// 异步更新一个已存在的MQTT服务器。
///
@@ -163,6 +145,32 @@ public class MqttManagementService : IMqttManagementService
return result;
}
+ ///
+ /// 异步创建MQTT服务器及其菜单项。
+ ///
+ public async Task CreateMqttServerAsync(MqttServerDto mqttServerDto)
+ {
+ // 首先创建MQTT服务器
+ var mqttServerId = await _mqttAppService.CreateMqttServerAsync(mqttServerDto);
+
+ if (mqttServerId > 0)
+ {
+ mqttServerDto.Id = mqttServerId;
+
+
+ // 将MQTT服务器添加到内存中
+ if (_appDataStorageService.MqttServers.TryAdd(mqttServerDto.Id, mqttServerDto))
+ {
+ _eventService.RaiseMqttServerChanged(
+ this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServerDto));
+ }
+
+
+ }
+
+ return mqttServerDto; // 返回null表示创建失败
+ }
+
///
/// 获取发生变化的属性列表
///
diff --git a/DMS.Core/Enums/MenuType.cs b/DMS.Core/Enums/MenuType.cs
index 6881d00..04d22e6 100644
--- a/DMS.Core/Enums/MenuType.cs
+++ b/DMS.Core/Enums/MenuType.cs
@@ -6,5 +6,6 @@ public enum MenuType
DeviceMenu,
VariableTableMenu,
VariableMenu,
- MqttMenu
+ MqttMenu,
+ MqttServerMenu
}
\ No newline at end of file
diff --git a/DMS.WPF.UnitTests/ViewModelTest/BaseServiceTest.cs b/DMS.WPF.UnitTests/ViewModelTest/BaseServiceTest.cs
index 3b5a4bd..6cd16bb 100644
--- a/DMS.WPF.UnitTests/ViewModelTest/BaseServiceTest.cs
+++ b/DMS.WPF.UnitTests/ViewModelTest/BaseServiceTest.cs
@@ -3,9 +3,7 @@
using AutoMapper;
using AutoMapper.Internal;
using DMS.Application.Configurations;
-using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Database;
-using DMS.Application.Services;
using DMS.Application.Services.Database;
using DMS.Core.Interfaces;
using DMS.Core.Interfaces.Repositories;
@@ -53,7 +51,7 @@ public class BaseServiceTest
services.AddTransient();
services.AddTransient();
services.AddTransient();
- services.AddTransient();
+ services.AddTransient();
services.AddTransient();
services.AddTransient();
// services.AddTransient();
diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs
index f6ef457..523c241 100644
--- a/DMS.WPF/App.xaml.cs
+++ b/DMS.WPF/App.xaml.cs
@@ -254,7 +254,7 @@ public partial class App : System.Windows.Application
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
- services.AddSingleton();
+ services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
diff --git a/DMS.WPF/Interfaces/IMenuDataService.cs b/DMS.WPF/Interfaces/IMenuDataService.cs
index ef92c94..f79428a 100644
--- a/DMS.WPF/Interfaces/IMenuDataService.cs
+++ b/DMS.WPF/Interfaces/IMenuDataService.cs
@@ -9,20 +9,16 @@ namespace DMS.WPF.Interfaces;
public interface IMenuDataService
{
- ///
- /// 构建菜单树。
- ///
- void BuildMenuTrees();
///
/// 添加菜单项。
///
- void AddMenuItem(MenuItemViewModel menuItemViewModel);
+ Task AddMenuItem(MenuItemViewModel menuItemViewModel);
///
/// 删除菜单项。
///
- void DeleteMenuItem(MenuItemViewModel? menuItemViewModel);
+ Task DeleteMenuItem(MenuItemViewModel? menuItemViewModel);
void LoadAllMenus();
diff --git a/DMS.WPF/Profiles/MappingProfile.cs b/DMS.WPF/Profiles/MappingProfile.cs
index 907c2f5..3019967 100644
--- a/DMS.WPF/Profiles/MappingProfile.cs
+++ b/DMS.WPF/Profiles/MappingProfile.cs
@@ -25,6 +25,8 @@ namespace DMS.WPF.Profiles
.ReverseMap();
CreateMap().ReverseMap();
+ CreateMap().ReverseMap();
+
CreateMap().ReverseMap();
CreateMap().ReverseMap();
CreateMap().ReverseMap();
diff --git a/DMS.WPF/Services/DeviceDataService.cs b/DMS.WPF/Services/DeviceDataService.cs
index abb5771..5459da4 100644
--- a/DMS.WPF/Services/DeviceDataService.cs
+++ b/DMS.WPF/Services/DeviceDataService.cs
@@ -112,7 +112,6 @@ public class DeviceDataService : IDeviceDataService
if (addDto.DeviceMenu != null)
{
_menuDataService.AddMenuItem(_mapper.Map(addDto.DeviceMenu));
- _appDataCenterService.MenuManagementService.AddMenuToMemory(addDto.DeviceMenu);
}
@@ -126,15 +125,11 @@ public class DeviceDataService : IDeviceDataService
if (addDto.VariableTable != null && addDto.VariableTableMenu != null)
{
_menuDataService.AddMenuItem(_mapper.Map(addDto.VariableTableMenu));
- _appDataCenterService.MenuManagementService.AddMenuToMemory(addDto.VariableTableMenu);
}
}
-
- // 添加null检查
- _menuDataService.BuildMenuTrees();
return addDto;
diff --git a/DMS.WPF/Services/MenuDataService.cs b/DMS.WPF/Services/MenuDataService.cs
index e72dfb1..2e56858 100644
--- a/DMS.WPF/Services/MenuDataService.cs
+++ b/DMS.WPF/Services/MenuDataService.cs
@@ -1,8 +1,12 @@
-using System.Collections.ObjectModel;
using AutoMapper;
+using DMS.Application.DTOs;
using DMS.Application.Interfaces;
+using DMS.Application.Interfaces.Management;
+using DMS.Application.Services.Management;
using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels.Items;
+using System.Collections.ObjectModel;
+using System.Threading.Tasks;
namespace DMS.WPF.Services;
@@ -14,6 +18,7 @@ public class MenuDataService : IMenuDataService
private readonly IMapper _mapper;
private readonly IDataStorageService _dataStorageService;
private readonly IAppDataStorageService _appDataStorageService;
+ private readonly IMenuManagementService _menuManagementService;
@@ -22,11 +27,12 @@ public class MenuDataService : IMenuDataService
///
/// AutoMapper 实例。
/// 数据服务中心实例。
- public MenuDataService(IMapper mapper,IDataStorageService dataStorageService, IAppDataStorageService appDataStorageService)
+ public MenuDataService(IMapper mapper,IDataStorageService dataStorageService, IAppDataStorageService appDataStorageService,IMenuManagementService menuManagementService)
{
_mapper = mapper;
_dataStorageService = dataStorageService;
_appDataStorageService = appDataStorageService;
+ _menuManagementService = menuManagementService;
}
public void LoadAllMenus()
@@ -65,18 +71,23 @@ public class MenuDataService : IMenuDataService
///
/// 添加菜单项。
///
- public void AddMenuItem(MenuItemViewModel menuItemViewModel)
+ public async Task AddMenuItem(MenuItemViewModel menuItemViewModel)
{
- if (menuItemViewModel == null)
- {
- return;
- }
+ if (menuItemViewModel is null) return;
var deviceMenu = _dataStorageService.Menus.FirstOrDefault(m => m.Id == menuItemViewModel.ParentId);
- if (deviceMenu != null)
+ if (deviceMenu is not null)
{
- deviceMenu.Children.Add(menuItemViewModel);
- _dataStorageService.Menus.Add(menuItemViewModel);
+
+ var menuId= await _menuManagementService.CreateMenuAsync(_mapper.Map(menuItemViewModel));
+ if (menuId>0)
+ {
+ menuItemViewModel.Id = menuId;
+ deviceMenu.Children.Add(menuItemViewModel);
+ _dataStorageService.Menus.Add(menuItemViewModel);
+ BuildMenuTrees();
+ }
+
}
}
@@ -84,17 +95,16 @@ public class MenuDataService : IMenuDataService
///
/// 删除菜单项。
///
- public void DeleteMenuItem(MenuItemViewModel? menuItemViewModel)
+ public async Task DeleteMenuItem(MenuItemViewModel? menuItemViewModel)
{
- if (menuItemViewModel == null)
- {
- return;
- }
+ if (menuItemViewModel is null) return;
+ await _menuManagementService.DeleteMenuAsync(menuItemViewModel.Id);
+
// 从扁平菜单列表中移除
_dataStorageService.Menus.Remove(menuItemViewModel);
- // 从树形结构中移除
+ //// 从树形结构中移除
if (menuItemViewModel.ParentId.HasValue && menuItemViewModel.ParentId.Value != 0)
{
// 如果有父菜单,从父菜单的Children中移除
@@ -106,5 +116,7 @@ public class MenuDataService : IMenuDataService
// 如果是根菜单,从MenuTrees中移除
_dataStorageService.MenuTrees.Remove(menuItemViewModel);
}
+
+ //BuildMenuTrees();
}
}
\ No newline at end of file
diff --git a/DMS.WPF/Services/MqttDataService.cs b/DMS.WPF/Services/MqttDataService.cs
index 62352be..e54f776 100644
--- a/DMS.WPF/Services/MqttDataService.cs
+++ b/DMS.WPF/Services/MqttDataService.cs
@@ -1,11 +1,10 @@
-using System.Collections.ObjectModel;
using AutoMapper;
-using CommunityToolkit.Mvvm.ComponentModel;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
-using DMS.Application.Interfaces.Database;
using DMS.Application.Interfaces.Management;
+using DMS.Core.Enums;
using DMS.WPF.Interfaces;
+using DMS.WPF.ViewModels;
using DMS.WPF.ViewModels.Items;
namespace DMS.WPF.Services;
@@ -18,6 +17,8 @@ public class MqttDataService : IMqttDataService
private readonly IMapper _mapper;
private readonly IAppDataStorageService _appDataStorageService;
private readonly IMqttManagementService _mqttManagementService;
+ private readonly IMenuDataService _menuDataService;
+ private readonly IMenuManagementService _menuManagementServiceImpl;
private readonly IDataStorageService _dataStorageService;
@@ -26,11 +27,13 @@ public class MqttDataService : IMqttDataService
///
/// AutoMapper 实例。
/// MQTT应用服务实例。
- public MqttDataService(IMapper mapper, IAppDataStorageService appDataStorageService,IMqttManagementService mqttManagementService, IDataStorageService dataStorageService)
+ public MqttDataService(IMapper mapper, IAppDataStorageService appDataStorageService, IMqttManagementService mqttManagementService, IMenuDataService menuDataService, IMenuManagementService menuManagementServiceImpl, IDataStorageService dataStorageService)
{
_mapper = mapper;
_appDataStorageService = appDataStorageService;
_mqttManagementService = mqttManagementService;
+ _menuDataService = menuDataService;
+ _menuManagementServiceImpl = menuManagementServiceImpl;
_dataStorageService = dataStorageService;
}
@@ -44,7 +47,7 @@ public class MqttDataService : IMqttDataService
// 加载MQTT服务器数据
foreach (var mqttServerDto in _appDataStorageService.MqttServers.Values)
{
- _dataStorageService.MqttServers.TryAdd(mqttServerDto.Id,_mapper.Map(mqttServerDto));
+ _dataStorageService.MqttServers.TryAdd(mqttServerDto.Id, _mapper.Map(mqttServerDto));
}
}
@@ -61,10 +64,30 @@ public class MqttDataService : IMqttDataService
///
public async Task AddMqttServer(MqttServerItemViewModel mqttServer)
{
- var mqttServerDto = await _mqttManagementService.CreateMqttServerAsync(_mapper.Map(mqttServer));
- var mqttServerItem = _mapper.Map(mqttServerDto);
- _dataStorageService.MqttServers.Add(mqttServerItem.Id,mqttServerItem);
-
+
+ var addMqttServerDto = await _mqttManagementService.CreateMqttServerAsync(_mapper.Map(mqttServer));
+
+ MqttServerItemViewModel mqttServerItem = _mapper.Map(addMqttServerDto);
+
+ _dataStorageService.MqttServers.Add(mqttServerItem.Id, mqttServerItem);
+
+
+ var mqttRootMenu = _dataStorageService.Menus.FirstOrDefault(m => m.Header == "Mqtt服务器");
+
+ if (mqttRootMenu is not null)
+ {
+ var mqttServerMenu = new MenuBeanDto()
+ {
+ Header = mqttServerItem.ServerName,
+ TargetId = mqttServerItem.Id,
+ ParentId = mqttRootMenu.Id,
+ Icon = "\uE753", // 使用设备图标
+ MenuType = MenuType.MqttServerMenu,
+ TargetViewKey = nameof(MqttServerDetailViewModel),
+ };
+ await _menuDataService.AddMenuItem(_mapper.Map(mqttServerMenu));
+ }
+
return mqttServerItem;
}
@@ -74,8 +97,24 @@ public class MqttDataService : IMqttDataService
public async Task UpdateMqttServer(MqttServerItemViewModel mqttServer)
{
var dto = _mapper.Map(mqttServer);
- await _mqttManagementService.UpdateMqttServerAsync(dto);
- return true;
+ var result = await _mqttManagementService.UpdateMqttServerAsync(dto);
+
+ if (result > 0)
+ {
+ // 更新菜单项
+ var menu = _dataStorageService.Menus.FirstOrDefault(m => m.MenuType == MenuType.MqttServerMenu && m.TargetId == mqttServer.Id);
+ if (menu != null)
+ {
+ // 更新菜单标题
+ menu.Header = mqttServer.ServerName;
+
+ // 使用菜单管理服务更新菜单
+ var menuDto = _mapper.Map(menu);
+ await _menuManagementServiceImpl.UpdateMenuAsync(menuDto);
+ }
+ }
+
+ return result > 0;
}
///
@@ -83,8 +122,23 @@ public class MqttDataService : IMqttDataService
///
public async Task DeleteMqttServer(MqttServerItemViewModel mqttServer)
{
- await _mqttManagementService.DeleteMqttServerAsync(mqttServer.Id);
- _dataStorageService.MqttServers.Remove(mqttServer.Id);
- return true;
+ // 从数据库和内存中删除MQTT服务器
+ var result = await _mqttManagementService.DeleteMqttServerAsync(mqttServer.Id);
+
+ if (result)
+ {
+ // 从界面删除MQTT服务器菜单
+ var mqttServerMenu = _dataStorageService.Menus.FirstOrDefault(m => m.MenuType == MenuType.MqttServerMenu && m.TargetId == mqttServer.Id);
+
+ if (mqttServerMenu != null)
+ {
+ await _menuDataService.DeleteMenuItem(mqttServerMenu);
+ }
+
+ // 从界面删除MQTT服务器
+ _dataStorageService.MqttServers.Remove(mqttServer.Id);
+ }
+
+ return result;
}
}
\ No newline at end of file
diff --git a/DMS.WPF/ViewModels/MqttsViewModel.cs b/DMS.WPF/ViewModels/MqttsViewModel.cs
index 3b31c84..4e9fe54 100644
--- a/DMS.WPF/ViewModels/MqttsViewModel.cs
+++ b/DMS.WPF/ViewModels/MqttsViewModel.cs
@@ -206,12 +206,6 @@ public partial class MqttsViewModel : ViewModelBase
return;
}
- // 导航到MQTT服务器详情页
- // var menu = new MenuItemViewModel
- // {
- // TargetViewKey = "MqttServerDetailView",
- // TargetId = SelectedMqtt.Id
- // };
await _navigationService.NavigateToAsync(
this, new NavigationParameter(nameof(MqttServerDetailViewModel), SelectedMqtt.Id, NavigationType.Mqtt));
diff --git a/DMS.WPF/Views/MainView.xaml b/DMS.WPF/Views/MainView.xaml
index 77378f0..5b1c5a4 100644
--- a/DMS.WPF/Views/MainView.xaml
+++ b/DMS.WPF/Views/MainView.xaml
@@ -127,10 +127,10 @@
-
-
-
-
+
+
+
+
diff --git a/DMS.WPF/Views/MqttServerDetailView.xaml b/DMS.WPF/Views/MqttServerDetailView.xaml
index 70d16e6..055ff87 100644
--- a/DMS.WPF/Views/MqttServerDetailView.xaml
+++ b/DMS.WPF/Views/MqttServerDetailView.xaml
@@ -3,11 +3,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:viewmodels="clr-namespace:DMS.WPF.ViewModels"
- xmlns:models="clr-namespace:DMS.Core.Models;assembly=DMS.Core"
- xmlns:iNKORE="clr-namespace:iNKORE.UI.WPF.Modern.Controls;assembly=iNKORE.UI.WPF.Modern"
- xmlns:extensions="clr-namespace:DMS.Extensions"
- xmlns:enums="clr-namespace:DMS.Core.Enums"
xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
diff --git a/DMS.WPF/Views/MqttsView.xaml b/DMS.WPF/Views/MqttsView.xaml
index 1053221..1d22213 100644
--- a/DMS.WPF/Views/MqttsView.xaml
+++ b/DMS.WPF/Views/MqttsView.xaml
@@ -3,6 +3,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+ xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
@@ -213,7 +214,13 @@
ItemTemplate="{StaticResource MqttItemTemplate}"
ItemsSource="{Binding MqttServeise}"
SelectedItem="{Binding SelectedMqtt}"
- SelectionMode="Single" />
+ SelectionMode="Single">
+
+
+
+
+
+
\ No newline at end of file