feat: 菜单管理重构及MQTT服务器功能增强

This commit is contained in:
2025-10-05 17:07:17 +08:00
parent 80ea47e627
commit 6b55211dbf
23 changed files with 287 additions and 184 deletions

View File

@@ -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, // 添加触发器管理服务参数

View File

@@ -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;
/// <summary>
/// 菜单应用服务,负责处理菜单相关的业务逻辑。
/// 实现 <see cref="IMenuService"/> 接口。
/// 实现 <see cref="IMenuAppService"/> 接口。
/// </summary>
public class MenuService : IMenuService
public class MenuAppService : IMenuAppService
{
private readonly IRepositoryManager _repoManager;
private readonly IMapper _mapper;
@@ -20,7 +21,7 @@ public class MenuService : IMenuService
/// </summary>
/// <param name="repoManager">仓储管理器实例。</param>
/// <param name="mapper">AutoMapper 实例。</param>
public MenuService(IRepositoryManager repoManager, IMapper mapper)
public MenuAppService(IRepositoryManager repoManager, IMapper mapper)
{
_repoManager = repoManager;
_mapper = mapper;
@@ -74,9 +75,9 @@ public class MenuService : IMenuService
/// 异步更新一个已存在的菜单(事务性操作)。
/// </summary>
/// <param name="menuDto">要更新的菜单数据传输对象。</param>
/// <returns>表示异步操作的任务。</returns>
/// <returns>受影响的行数。</returns>
/// <exception cref="ApplicationException">如果找不到菜单或更新菜单时发生错误。</exception>
public async Task UpdateMenuAsync(MenuBeanDto menuDto)
public async Task<int> 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
/// 异步删除一个菜单(事务性操作)。
/// </summary>
/// <param name="id">要删除菜单的ID。</param>
/// <returns>表示异步操作的任务。</returns>
/// <exception cref="ApplicationException">如果删除菜单时发生错误。</exception>
public async Task DeleteMenuAsync(int id)
/// <returns>如果删除成功则为 true否则为 false。</returns>
/// <exception cref="InvalidOperationException">如果删除菜单失败。</exception>
/// <exception cref="ApplicationException">如果删除菜单时发生其他错误。</exception>
public async Task<bool> 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)
{

View File

@@ -154,4 +154,23 @@ public class EventService : IEventService
}
#endregion
#region
/// <summary>
/// 菜单改变事件
/// </summary>
public event EventHandler<MenuChangedEventArgs> OnMenuChanged;
/// <summary>
/// 触发菜单改变事件
/// </summary>
/// <param name="sender">事件发送者</param>
/// <param name="e">菜单改变事件参数</param>
public void RaiseMenuChanged(object sender, MenuChangedEventArgs e)
{
OnMenuChanged?.Invoke(sender, e);
}
#endregion
}

View File

@@ -11,18 +11,20 @@ namespace DMS.Application.Services.Management;
/// </summary>
public class MenuManagementService : IMenuManagementService
{
private readonly IMenuService _menuService;
private readonly IMenuAppService _menuService;
private readonly IAppDataStorageService _appDataStorageService;
private readonly IEventService _eventService;
/// <summary>
/// 当菜单数据发生变化时触发
/// </summary>
public event EventHandler<MenuChangedEventArgs> MenuChanged;
public MenuManagementService(IMenuService menuService,IAppDataStorageService appDataStorageService)
public MenuManagementService(IMenuAppService menuService, IAppDataStorageService appDataStorageService, IEventService eventService)
{
_menuService = menuService;
_appDataStorageService = appDataStorageService;
_eventService = eventService;
}
/// <summary>
@@ -46,74 +48,71 @@ public class MenuManagementService : IMenuManagementService
/// </summary>
public async Task<int> 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;
}
/// <summary>
/// 异步更新一个已存在的菜单。
/// </summary>
public async Task UpdateMenuAsync(MenuBeanDto menuDto)
public async Task<int> 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;
}
/// <summary>
/// 异步删除一个菜单。
/// </summary>
public async Task DeleteMenuAsync(int id)
public async Task<bool> DeleteMenuAsync(int id)
{
await _menuService.DeleteMenuAsync(id);
}
/// <summary>
/// 在内存中添加菜单
/// </summary>
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));
}
}
/// <summary>
/// 在内存中更新菜单
/// </summary>
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));
}
/// <summary>
/// 在内存中删除菜单
/// </summary>
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;
}
/// <summary>
@@ -159,6 +158,6 @@ public class MenuManagementService : IMenuManagementService
/// </summary>
protected virtual void OnMenuChanged(MenuChangedEventArgs e)
{
MenuChanged?.Invoke(this, e);
_eventService.RaiseMenuChanged(this, e);
}
}

View File

@@ -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;
}
/// <summary>
@@ -49,27 +52,6 @@ public class MqttManagementService : IMqttManagementService
return await _mqttAppService.GetAllMqttServersAsync();
}
/// <summary>
/// 异步创建一个新的MQTT服务器。
/// </summary>
public async Task<MqttServerDto> 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;
}
/// <summary>
/// 异步更新一个已存在的MQTT服务器。
/// </summary>
@@ -163,6 +145,32 @@ public class MqttManagementService : IMqttManagementService
return result;
}
/// <summary>
/// 异步创建MQTT服务器及其菜单项。
/// </summary>
public async Task<MqttServerDto> 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表示创建失败
}
/// <summary>
/// 获取发生变化的属性列表
/// </summary>