From 078d7b390332ff5734dcf06837b324987c8f751e Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Wed, 1 Oct 2025 20:13:30 +0800 Subject: [PATCH] =?UTF-8?q?=201.=20=E5=88=9B=E5=BB=BA=E4=BA=86=20BatchImpo?= =?UTF-8?q?rtVariablesEventArgs=20=E4=BA=8B=E4=BB=B6=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=B1=BB=20=20=20=202.=20=E5=9C=A8=20IEventService=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=B8=AD=E6=B7=BB=E5=8A=A0=E4=BA=86=20OnBatchImportVa?= =?UTF-8?q?riables=20=E4=BA=8B=E4=BB=B6=E5=92=8C=20RaiseBatchImportVariabl?= =?UTF-8?q?es=20=E6=96=B9=E6=B3=95=20=20=20=203.=20=E5=9C=A8=20EventServic?= =?UTF-8?q?e=20=E5=AE=9E=E7=8E=B0=E4=B8=AD=E6=B7=BB=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E5=AF=B9=E6=96=B0=E4=BA=8B=E4=BB=B6=E7=9A=84=E6=94=AF=E6=8C=81?= =?UTF-8?q?=20=20=20=204.=20=E5=9C=A8=20IVariableManagementService=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=B8=AD=E6=B7=BB=E5=8A=A0=E4=BA=86=20BatchI?= =?UTF-8?q?mportVariablesAsync=20=E6=96=B9=E6=B3=95=20=20=20=205.=20?= =?UTF-8?q?=E5=9C=A8=20VariableManagementService=20=E4=B8=AD=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E4=BA=86=E8=AF=A5=E6=96=B9=E6=B3=95=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E5=9C=A8=E6=89=B9=E9=87=8F=E5=AF=BC=E5=85=A5=E5=90=8E=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E4=BA=8B=E4=BB=B6=20=20=20=206.=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=BA=86=20VariableTableViewModel=20=E4=BB=A5=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20IVariableManagementService=20=E8=80=8C=E4=B8=8D?= =?UTF-8?q?=E6=98=AF=20IVariableAppService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Events/BatchImportVariablesEventArgs.cs | 37 +++++++++++++++++++ DMS.Application/Interfaces/IEventService.cs | 12 ++++++ .../Management/IVariableManagementService.cs | 10 +++++ DMS.Application/Services/EventService.cs | 15 ++++++++ .../Management/VariableManagementService.cs | 21 +++++++++++ DMS.WPF/Services/VariableDataService.cs | 7 ++++ DMS.WPF/ViewModels/VariableTableViewModel.cs | 29 ++++++++------- 7 files changed, 117 insertions(+), 14 deletions(-) create mode 100644 DMS.Application/Events/BatchImportVariablesEventArgs.cs diff --git a/DMS.Application/Events/BatchImportVariablesEventArgs.cs b/DMS.Application/Events/BatchImportVariablesEventArgs.cs new file mode 100644 index 0000000..21792df --- /dev/null +++ b/DMS.Application/Events/BatchImportVariablesEventArgs.cs @@ -0,0 +1,37 @@ +using DMS.Application.DTOs; +using DMS.Core.Enums; + +namespace DMS.Application.Events +{ + /// + /// 批量导入变量事件参数 + /// + public class BatchImportVariablesEventArgs : System.EventArgs + { + /// + /// 导入的变量列表 + /// + public List Variables { get; } + + /// + /// 导入的变量数量 + /// + public int Count { get; } + + /// + /// 变更时间 + /// + public DateTime ChangeTime { get; } + + /// + /// 构造函数 + /// + /// 导入的变量列表 + public BatchImportVariablesEventArgs(List variables) + { + Variables = variables ?? new List(); + Count = Variables.Count; + ChangeTime = DateTime.Now; + } + } +} \ No newline at end of file diff --git a/DMS.Application/Interfaces/IEventService.cs b/DMS.Application/Interfaces/IEventService.cs index 8db6560..b376d45 100644 --- a/DMS.Application/Interfaces/IEventService.cs +++ b/DMS.Application/Interfaces/IEventService.cs @@ -97,6 +97,18 @@ public interface IEventService void RaiseVariableActiveChanged(object sender,VariablesActiveChangedEventArgs e); + /// + /// 批量导入变量事件 + /// + event EventHandler OnBatchImportVariables; + + /// + /// 触发批量导入变量事件 + /// + /// 事件发送者 + /// 批量导入变量事件参数 + void RaiseBatchImportVariables(object sender, BatchImportVariablesEventArgs e); + /// /// 变量启停改变事件 /// diff --git a/DMS.Application/Interfaces/Management/IVariableManagementService.cs b/DMS.Application/Interfaces/Management/IVariableManagementService.cs index a1b693d..353e4f1 100644 --- a/DMS.Application/Interfaces/Management/IVariableManagementService.cs +++ b/DMS.Application/Interfaces/Management/IVariableManagementService.cs @@ -39,4 +39,14 @@ public interface IVariableManagementService /// 异步批量删除变量。 /// Task DeleteVariablesAsync(List ids); + + /// + /// 异步批量导入变量。 + /// + Task> BatchImportVariablesAsync(List variables); + + /// + /// 查找已存在的变量。 + /// + Task> FindExistingVariablesAsync(IEnumerable variablesToCheck); } \ No newline at end of file diff --git a/DMS.Application/Services/EventService.cs b/DMS.Application/Services/EventService.cs index 1ba2b48..51fb2ff 100644 --- a/DMS.Application/Services/EventService.cs +++ b/DMS.Application/Services/EventService.cs @@ -124,6 +124,21 @@ public class EventService : IEventService OnVariableActiveChanged?.Invoke(sender, e); } + /// + /// 批量导入变量事件 + /// + public event EventHandler OnBatchImportVariables; + + /// + /// 触发批量导入变量事件 + /// + /// 事件发送者 + /// 批量导入变量事件参数 + public void RaiseBatchImportVariables(object sender, BatchImportVariablesEventArgs e) + { + OnBatchImportVariables?.Invoke(sender, e); + } + /// /// 变量值改变事件 diff --git a/DMS.Application/Services/Management/VariableManagementService.cs b/DMS.Application/Services/Management/VariableManagementService.cs index 56c7fea..6bc92c0 100644 --- a/DMS.Application/Services/Management/VariableManagementService.cs +++ b/DMS.Application/Services/Management/VariableManagementService.cs @@ -153,6 +153,27 @@ public class VariableManagementService : IVariableManagementService return result; } + /// + /// 异步批量导入变量。 + /// + public async Task> BatchImportVariablesAsync(List variables) + { + var result = await _variableAppService.BatchImportVariablesAsync(variables); + + // 批量导入成功后,触发批量导入事件 + if (result != null && result.Any()) + { + _eventService.RaiseBatchImportVariables(this, new BatchImportVariablesEventArgs(result)); + } + + return result; + } + + public async Task> FindExistingVariablesAsync(IEnumerable variablesToCheck) + { + return await _variableAppService.FindExistingVariablesAsync(variablesToCheck); + } + /// /// 异步批量删除变量。 /// diff --git a/DMS.WPF/Services/VariableDataService.cs b/DMS.WPF/Services/VariableDataService.cs index ca2688e..2a9deab 100644 --- a/DMS.WPF/Services/VariableDataService.cs +++ b/DMS.WPF/Services/VariableDataService.cs @@ -6,6 +6,7 @@ using DMS.Core.Models; using DMS.WPF.Interfaces; using DMS.WPF.ViewModels.Items; using System.Collections.ObjectModel; +using DMS.Application.Services.Management; namespace DMS.WPF.Services; @@ -30,6 +31,12 @@ public class VariableDataService : IVariableDataService _mapper = mapper; _dataStorageService = dataStorageService; _appDataCenterService = appDataCenterService; + + // 订阅批量导入变量事件 + if (_appDataCenterService.VariableManagementService is VariableManagementService variableManagementService) + { + // 如果需要直接订阅事件,这将需要EventService实例 + } } /// diff --git a/DMS.WPF/ViewModels/VariableTableViewModel.cs b/DMS.WPF/ViewModels/VariableTableViewModel.cs index 10d5d9e..8fda634 100644 --- a/DMS.WPF/ViewModels/VariableTableViewModel.cs +++ b/DMS.WPF/ViewModels/VariableTableViewModel.cs @@ -5,6 +5,7 @@ using CommunityToolkit.Mvvm.Input; using DMS.Application.DTOs; using DMS.Application.Interfaces; using DMS.Application.Interfaces.Database; +using DMS.Application.Interfaces.Management; using DMS.Core.Enums; using DMS.Core.Events; using DMS.Core.Models; @@ -25,7 +26,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable /// private readonly IDialogService _dialogService; - private readonly IVariableAppService _variableAppService; + private readonly IVariableManagementService _variableManagementService; private readonly IEventService _eventService; private readonly IMqttAliasAppService _mqttAliasAppService; private readonly IMqttAppService _mqttAppService; @@ -92,7 +93,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable private readonly INotificationService _notificationService; - public VariableTableViewModel(IMapper mapper, IDialogService dialogService, IVariableAppService variableAppService, + public VariableTableViewModel(IMapper mapper, IDialogService dialogService, IVariableManagementService variableManagementService, IEventService eventService, IMqttAliasAppService mqttAliasAppService, IMqttAppService mqttAppService, IWPFDataService wpfDataService, IDataStorageService dataStorageService, @@ -100,7 +101,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable { _mapper = mapper; _dialogService = dialogService; - _variableAppService = variableAppService; + _variableManagementService = variableManagementService; _eventService = eventService; _mqttAliasAppService = mqttAliasAppService; _mqttAppService = mqttAppService; @@ -207,7 +208,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable editedVariable.UpdatedAt = DateTime.Now; // 更新数据库中的变量数据 - var updateResult = await _variableAppService.UpdateVariableAsync(_mapper.Map(editedVariable)); + var updateResult = await _variableManagementService.UpdateVariableAsync(_mapper.Map(editedVariable)); if (updateResult > 0) @@ -253,7 +254,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable variableDto.VariableTableId = CurrentVariableTable.Id; } - var existList = await _variableAppService.FindExistingVariablesAsync(improtVariableDtos); + var existList = await _variableManagementService.FindExistingVariablesAsync(improtVariableDtos); if (existList.Count > 0) { // // 拼接要删除的变量名称,用于确认提示 @@ -268,7 +269,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable if (improtVariableDtos.Count != 0) { - var addVariableDtos = await _variableAppService.BatchImportVariablesAsync(improtVariableDtos); + var addVariableDtos = await _variableManagementService.BatchImportVariablesAsync(improtVariableDtos); if (addVariableDtos is { Count: > 0 }) { List variableItemViewModels = _mapper.Map>(addVariableDtos); @@ -337,7 +338,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable } // 检查是否存在同名变量 - var existList = await _variableAppService.FindExistingVariablesAsync(importedVariableDtos); + var existList = await _variableManagementService.FindExistingVariablesAsync(importedVariableDtos); if (existList.Count > 0) { // 拼接要删除的变量名称,用于确认提示 @@ -353,7 +354,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 如果还有变量需要导入,则执行导入操作 if (importedVariableDtos.Count != 0) { - var addVariableDtos = await _variableAppService.BatchImportVariablesAsync(importedVariableDtos); + var addVariableDtos = await _variableManagementService.BatchImportVariablesAsync(importedVariableDtos); if (addVariableDtos is { Count: > 0 }) { List variableItemViewModels = _mapper.Map>(addVariableDtos); @@ -415,7 +416,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // // 添加变量数据到数据库 var addVariable - = await _variableAppService.CreateVariableAsync(_mapper.Map(variableItemViewModel)); + = await _variableManagementService.CreateVariableAsync(_mapper.Map(variableItemViewModel)); _mapper.Map(addVariable, variableItemViewModel); // // 更新当前页面显示的数据:将新变量添加到集合中 _variableItemList.Add(variableItemViewModel); @@ -462,7 +463,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable return; // 如果用户取消删除,则返回 // 从数据库中删除变量数据 - var result = await _variableAppService.DeleteVariablesAsync(variablesToDelete.Select(v => v.Id) + var result = await _variableManagementService.DeleteVariablesAsync(variablesToDelete.Select(v => v.Id) .ToList()); if (result) { @@ -522,7 +523,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 批量更新数据库中的变量数据 var variableDtos = _mapper.Map>(validVariables); - var result = await _variableAppService.UpdateVariablesAsync(variableDtos); + var result = await _variableManagementService.UpdateVariablesAsync(variableDtos); if (result > 0) { @@ -673,7 +674,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 批量更新数据库中的变量数据 var variableDtos = _mapper.Map>(validVariables); - var result = await _variableAppService.UpdateVariablesAsync(variableDtos); + var result = await _variableManagementService.UpdateVariablesAsync(variableDtos); if (result > 0) { @@ -737,7 +738,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 批量更新数据库中的变量数据 var variableDtos = _mapper.Map>(validVariables); - var updateResult = await _variableAppService.UpdateVariablesAsync(variableDtos); + var updateResult = await _variableManagementService.UpdateVariablesAsync(variableDtos); if (updateResult > 0) @@ -792,7 +793,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 批量更新数据库中的变量数据 var variableDtos = _mapper.Map>(validVariables); - var updateResult = await _variableAppService.UpdateVariablesAsync(variableDtos); + var updateResult = await _variableManagementService.UpdateVariablesAsync(variableDtos); if (updateResult > 0) {