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)
{