refactor:完成变量表的重构添加,删除,更新

This commit is contained in:
2025-10-23 16:25:22 +08:00
parent 54d040b45f
commit 125348486a
15 changed files with 329 additions and 419 deletions

View File

@@ -11,6 +11,8 @@ using DMS.Core.Models;
using DMS.Message;
using DMS.WPF.Interfaces;
using DMS.WPF.ItemViewModel;
using DMS.WPF.Services.ViewService;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace DMS.WPF.Services;
@@ -32,8 +34,9 @@ public class DataEventService : IDataEventService
/// DataEventService类的构造函数。
/// </summary>
public DataEventService(IMapper mapper,
IViewDataService dataStorageService,
IViewDataService dataStorageService,
IEventService eventService,
INotificationService notificationService,
IAppCenterService appCenterService,
IViewCenterService wpfDataService,
@@ -46,16 +49,16 @@ public class DataEventService : IDataEventService
_appCenterService = appCenterService;
_wpfDataService = wpfDataService;
_logger = logger;
_logger?.LogInformation("正在初始化 DataEventService");
// 监听变量值变更事件
_eventService.OnVariableValueChanged += OnVariableValueChanged;
_eventService.OnMqttServerChanged += OnMqttServerChanged;
_eventService.OnLoadDataCompleted += OnLoadDataCompleted;
// 监听日志变更事件
// _appCenterService.OnLogChanged += _logDataService.OnNlogChanged;
_logger?.LogInformation("DataEventService 初始化完成");
}
@@ -81,7 +84,7 @@ public class DataEventService : IDataEventService
case MqttServerPropertyType.Port:
break;
case MqttServerPropertyType.IsConnect:
mqttServerItem.IsConnect=e.MqttServer.IsConnect;
mqttServerItem.IsConnect = e.MqttServer.IsConnect;
break;
case MqttServerPropertyType.Username:
break;
@@ -119,37 +122,41 @@ public class DataEventService : IDataEventService
}
private void OnLoadDataCompleted(object? sender, DataLoadCompletedEventArgs e)
private void OnLoadDataCompleted(object? sender, DataLoadCompletedEventArgs e)
{
_logger?.LogDebug("接收到数据加载完成事件,成功: {IsSuccess}", e.IsSuccess);
if (e.IsSuccess)
{
_logger?.LogInformation("开始加载所有数据项");
_wpfDataService.DeviceDataService.LoadAllDevices();
_logger?.LogDebug("设备数据加载完成");
_wpfDataService.VariableTableDataService.LoadAllVariableTables();
var variableTableViewService = App.Current.Services.GetRequiredService<IVariableTableViewService>();
if (variableTableViewService is not null)
{
variableTableViewService.LoadAllVariableTables();
}
_logger?.LogDebug("变量表数据加载完成");
_wpfDataService.VariableDataService.LoadAllVariables();
_logger?.LogDebug("变量数据加载完成");
_wpfDataService.MenuDataService.LoadAllMenus();
_logger?.LogDebug("菜单数据加载完成");
_wpfDataService.MqttDataService.LoadMqttServers();
_logger?.LogDebug("MQTT服务器数据加载完成");
_wpfDataService.MqttAliasDataService.LoadMqttAliases();
_logger?.LogDebug("MQTT别名加载完成");
_wpfDataService.TriggerDataService.LoadAllTriggers();
_logger?.LogDebug("触发器加载完成");
_wpfDataService.LogDataService.LoadAllLog();
_logger?.LogDebug("日志数据加载完成");
_logger?.LogInformation("所有数据项加载完成");
}
else
@@ -163,14 +170,14 @@ public class DataEventService : IDataEventService
/// </summary>
private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e)
{
_logger?.LogDebug("接收到变量值变更事件变量ID: {VariableId}, 新值: {NewValue}, 更新时间: {UpdateTime}",
_logger?.LogDebug("接收到变量值变更事件变量ID: {VariableId}, 新值: {NewValue}, 更新时间: {UpdateTime}",
e.Variable.Id, e.Variable.DataValue, e.Variable.UpdatedAt);
// 在UI线程上更新变量值
App.Current.Dispatcher.BeginInvoke(new Action(() =>
{
// 查找并更新对应的变量
if (_viewDataService.Variables.TryGetValue(e.Variable.Id,out var variableToUpdate))
if (_viewDataService.Variables.TryGetValue(e.Variable.Id, out var variableToUpdate))
{
variableToUpdate.DataValue = e.Variable.DataValue;
variableToUpdate.DisplayValue = e.Variable.DisplayValue;
@@ -183,7 +190,7 @@ public class DataEventService : IDataEventService
}));
}
/// <summary>
/// 处理LoadMessage消息。
@@ -191,10 +198,10 @@ public class DataEventService : IDataEventService
public async Task Receive(LoadMessage message)
{
_logger?.LogDebug("接收到LoadMessage消息消息类型: {MessageType}", message.GetType().Name);
// 这里可以添加加载消息处理的逻辑
// 目前是空实现,但已记录接收到消息的信息
_logger?.LogDebug("LoadMessage消息处理完成");
}
}

View File

@@ -1,11 +1,15 @@
using System.Collections.ObjectModel;
using AutoMapper;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Management;
using DMS.Application.Services;
using DMS.Core.Enums;
using DMS.Core.Models;
using DMS.WPF.Interfaces;
using DMS.WPF.ItemViewModel;
using DMS.WPF.ViewModels;
using iNKORE.UI.WPF.Modern.Common.IconKeys;
using System.Collections.ObjectModel;
namespace DMS.WPF.Services.ViewService;
@@ -13,72 +17,100 @@ public class VariableTableViewService : IVariableTableViewService
{
private readonly IMapper _mapper;
private readonly IViewDataService _viewDataService;
private readonly IAppCenterService _appCenterService;
private readonly IAppDataService _appDataService;
private readonly IVariableTableManagementService _variableTableManagementService;
private readonly IMenuViewService _menuDataService;
public VariableTableViewService(IMapper mapper, IViewDataService dataStorageService, IAppCenterService appCenterService,
public VariableTableViewService(IMapper mapper, IViewDataService viewDataService, IAppDataService appDataService, IVariableTableManagementService variableTableManagementService,
IMenuViewService menuDataService)
{
_mapper = mapper;
_viewDataService = dataStorageService;
_appCenterService = appCenterService;
_viewDataService = viewDataService;
_appDataService = appDataService;
_variableTableManagementService = variableTableManagementService;
_menuDataService = menuDataService;
}
public void LoadAllVariableTables()
{
foreach (var device in _viewDataService.Devices)
{
foreach (var variableTable in device.Value.VariableTables)
{
_viewDataService.VariableTables.Add(variableTable.Id,variableTable);
_viewDataService.VariableTables.Add(variableTable.Id, variableTable);
}
}
}
public async Task<int> AddVariableTable(VariableTable variableTable,
MenuBean menuDto = null, bool isAddDb = false)
/// <summary>
/// 异步添加变量表
/// 此方法创建一个新的变量表项,包括创建对应的菜单项,并将其添加到数据库和视图数据服务中
/// </summary>
/// <param name="variableTableItem">要添加的变量表项</param>
/// <returns>如果添加成功返回true否则返回false</returns>
public async Task<bool> AddAsync(VariableTableItem variableTableItem)
{
if (variableTable == null)
return 0;
// 检查传入的参数是否为空如果为空则直接返回false
if (variableTableItem is null) return false;
if (isAddDb && menuDto != null)
// 创建包含变量表和菜单的DTO对象
CreateVariableTableWithMenuDto createDto = new CreateVariableTableWithMenuDto();
createDto.VariableTable = _mapper.Map<VariableTable>(variableTableItem); // 将VariableTableItem映射为VariableTable对象
// 创建与变量表关联的菜单项
var deviceMenu = _viewDataService.Menus.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu && m.TargetId == variableTableItem.DeviceId);
if (deviceMenu is not null)
{
CreateVariableTableWithMenuDto createDto = new CreateVariableTableWithMenuDto();
createDto.VariableTable = variableTable;
createDto.DeviceId = variableTable.DeviceId;
createDto.Menu = menuDto;
var resDto = await _appCenterService.VariableTableManagementService.CreateVariableTableAsync(createDto);
_menuDataService.AddMenuToView(_mapper.Map<MenuItem>(resDto.Menu));
return resDto.VariableTable.Id;
createDto.Menu = new MenuBean()
{
Header = variableTableItem.Name, // 菜单标题为变量表名称
ParentId = deviceMenu.Id,
MenuType = MenuType.VariableTableMenu,
Icon = SegoeFluentIcons.DataSense.Glyph, // 设置菜单图标
TargetViewKey = nameof(VariableTableViewModel) // 设置目标视图
};
}
// 调用应用中心服务异步添加变量表
var resDto = await _variableTableManagementService.AddAsync(createDto);
// 检查返回结果是否为空如果为空则表示添加失败返回false
if (resDto is null) return false;
// 设置变量表项的ID为服务器返回的ID
variableTableItem.Id = resDto.VariableTable.Id;
// 如果变量表项有关联的设备,则将该变量表添加到设备的变量表集合中
variableTableItem.Device?.VariableTables.Add(variableTableItem);
return 0;
_viewDataService.VariableTables.TryAdd(variableTableItem.Id, variableTableItem);
// 将新创建的菜单项添加到菜单视图服务中
_menuDataService.AddMenuToView(_mapper.Map<MenuItem>(resDto.Menu));
// 成功添加返回true
return true;
}
public async Task<bool> UpdateVariableTable(VariableTableItem variableTable)
public async Task<bool> UpdateAsync(VariableTableItem variableTable)
{
if (variableTable == null)
// 检查变量表是否存在于应用数据存储中
if (!_appDataService.VariableTables.TryGetValue(variableTable.Id, out var variableTableInApp))
{
return false;
}
var variableTable_mapped = _mapper.Map<VariableTable>(variableTable);
if (await _appCenterService.VariableTableManagementService.UpdateVariableTableAsync(variableTable_mapped) > 0)
_mapper.Map(variableTable, variableTableInApp);
if (await _variableTableManagementService.UpdateAsync(variableTableInApp) > 0)
{
// 更新数据库后会自动更新内存,无需额外操作
var menu = _viewDataService.Menus.FirstOrDefault(m =>
m.MenuType == MenuType.VariableTableMenu &&
m.TargetId == variableTable.Id);
if (menu != null)
if (menu is not null && menu.Header != variableTable.Name)
{
menu.Header = variableTable.Name;
}
@@ -89,35 +121,47 @@ public class VariableTableViewService : IVariableTableViewService
return false;
}
public async Task<bool> DeleteVariableTable(VariableTableItem variableTable, bool isDeleteDb = false)
/// <summary>
/// 异步删除变量表
/// 此方法用于删除指定的变量表项,包括从数据库中删除、移除关联的变量和菜单项
/// </summary>
/// <param name="variableTable">要删除的变量表项</param>
/// <returns>如果删除成功返回true否则返回false</returns>
public async Task<bool> DeleteAsync(VariableTableItem variableTable)
{
if (variableTable == null)
// 检查变量表是否存在于应用数据存储中
if (!_appDataService.VariableTables.TryGetValue(variableTable.Id, out var variableTableInApp))
{
return false;
}
if (isDeleteDb)
// 从数据库和内存中删除变量表相关数据
if (!await _variableTableManagementService.DeleteAsync(variableTableInApp))
{
if (!await _appCenterService.VariableTableManagementService.DeleteVariableTableAsync(variableTable.Id))
{
return false;
}
return false;
}
// 删除与当前变量表关联的所有变量
// 从界面删除变量表相关数据集
foreach (var variable in variableTable.Variables)
{
_viewDataService.Variables.Remove(variable.Id);
}
// 删除变量表界面相关的菜单
// 删除变量表相关的菜单
var variableTableMenu
=_viewDataService.Menus.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu && m.TargetId == variableTable.Id);
_menuDataService.DeleteMenuToView(variableTableMenu);
// 删除变量表
= _viewDataService.Menus.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu &&
m.TargetId == variableTable.Id);
if (variableTableMenu != null)
{
_menuDataService.DeleteMenuToView(variableTableMenu);
}
// 从视图数据服务中删除变量表
_viewDataService.VariableTables.Remove(variableTable.Id);
variableTable.Device.VariableTables.Remove(variableTable);
// 从设备的变量表集合中移除该变量表
variableTable.Device?.VariableTables.Remove(variableTable);
return true;
}
}

View File

@@ -84,7 +84,7 @@ public class VariableViewService : IVariableViewService
}
var variableTable = _mapper.Map<VariableTable>(variableTableItem);
if (await _appCenterService.VariableTableManagementService.UpdateVariableTableAsync(variableTable) > 0)
if (await _appCenterService.VariableTableManagementService.UpdateAsync(variableTable) > 0)
{
// 更新数据库后会自动更新内存,无需额外操作
return true;
@@ -93,35 +93,6 @@ public class VariableViewService : IVariableViewService
return false;
}
/// <summary>
/// 删除变量表。
/// </summary>
public async Task<bool> DeleteVariableTable(VariableTableItem variableTable, bool isDeleteDb = false)
{
if (variableTable == null)
{
return false;
}
if (isDeleteDb)
{
if (!await _appCenterService.VariableTableManagementService.DeleteVariableTableAsync(variableTable.Id))
{
return false;
}
}
// 删除与当前变量表关联的所有变量
foreach (var variable in variableTable.Variables)
{
_viewDataService.Variables.Remove(variable.Id);
}
// 删除变量表
_viewDataService.VariableTables.Remove(variableTable.Id);
variableTable.Device.VariableTables.Remove(variableTable);
return true;
}
/// <summary>
/// 添加变量。