From 16bd8f79ee266ea6f8a8c4eda941ddb6a37323e7 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Fri, 5 Sep 2025 13:24:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Interfaces/IVariableAppService.cs | 10 ++++ .../Services/VariableAppService.cs | 22 +++++++++ .../Repositories/IVariableRepository.cs | 14 +++++- .../Repositories/VariableRepository.cs | 26 ++++++++++ DMS.WPF/ViewModels/VariableTableViewModel.cs | 48 ++++++++++++------- 5 files changed, 103 insertions(+), 17 deletions(-) diff --git a/DMS.Application/Interfaces/IVariableAppService.cs b/DMS.Application/Interfaces/IVariableAppService.cs index df1952c..2411d61 100644 --- a/DMS.Application/Interfaces/IVariableAppService.cs +++ b/DMS.Application/Interfaces/IVariableAppService.cs @@ -12,6 +12,16 @@ public interface IVariableAppService /// Task GetVariableByIdAsync(int id); + /// + /// 异步根据OPC UA NodeId获取变量DTO。 + /// + Task GetVariableByOpcUaNodeIdAsync(string opcUaNodeId); + + /// + /// 异步根据OPC UA NodeId列表获取变量DTO列表。 + /// + Task> GetVariableByOpcUaNodeIdsAsync(List opcUaNodeIds); + /// /// 异步获取所有变量DTO列表。 /// diff --git a/DMS.Application/Services/VariableAppService.cs b/DMS.Application/Services/VariableAppService.cs index 20b93c2..7cc15f0 100644 --- a/DMS.Application/Services/VariableAppService.cs +++ b/DMS.Application/Services/VariableAppService.cs @@ -39,6 +39,28 @@ public class VariableAppService : IVariableAppService return _mapper.Map(variable); } + /// + /// 异步根据OPC UA NodeId获取变量数据传输对象。 + /// + /// OPC UA NodeId。 + /// 变量数据传输对象。 + public async Task GetVariableByOpcUaNodeIdAsync(string opcUaNodeId) + { + var variable = await _repoManager.Variables.GetByOpcUaNodeIdAsync(opcUaNodeId); + return variable == null ? null : _mapper.Map(variable); + } + + /// + /// 异步根据OPC UA NodeId列表获取变量数据传输对象列表。 + /// + /// OPC UA NodeId列表。 + /// 变量数据传输对象列表。 + public async Task> GetVariableByOpcUaNodeIdsAsync(List opcUaNodeIds) + { + var variables = await _repoManager.Variables.GetByOpcUaNodeIdsAsync(opcUaNodeIds); + return _mapper.Map>(variables); + } + /// /// 异步获取所有变量数据传输对象列表。 /// diff --git a/DMS.Core/Interfaces/Repositories/IVariableRepository.cs b/DMS.Core/Interfaces/Repositories/IVariableRepository.cs index c5da0c3..9eea0fe 100644 --- a/DMS.Core/Interfaces/Repositories/IVariableRepository.cs +++ b/DMS.Core/Interfaces/Repositories/IVariableRepository.cs @@ -5,6 +5,18 @@ namespace DMS.Core.Interfaces.Repositories { public interface IVariableRepository:IBaseRepository { - + /// + /// 异步根据OPC UA NodeId获取单个变量实体。 + /// + /// OPC UA NodeId。 + /// 找到的变量实体,如果不存在则返回null。 + Task GetByOpcUaNodeIdAsync(string opcUaNodeId); + + /// + /// 异步根据OPC UA NodeId列表获取变量实体列表。 + /// + /// OPC UA NodeId列表。 + /// 找到的变量实体列表。 + Task> GetByOpcUaNodeIdsAsync(List opcUaNodeIds); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/VariableRepository.cs b/DMS.Infrastructure/Repositories/VariableRepository.cs index fe782ad..5af3def 100644 --- a/DMS.Infrastructure/Repositories/VariableRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableRepository.cs @@ -208,4 +208,30 @@ public class VariableRepository : BaseRepository, IVariableRepositor var dbEntities = _mapper.Map>(entities); return base.AddBatchAsync(dbEntities); } + + /// + /// 异步根据OPC UA NodeId获取单个变量实体。 + /// + /// OPC UA NodeId。 + /// 找到的变量实体,如果不存在则返回null。 + public async Task GetByOpcUaNodeIdAsync(string opcUaNodeId) + { + var dbVariable = await Db.Queryable() + .Where(v => v.OpcUaNodeId == opcUaNodeId) + .FirstAsync(); + return dbVariable == null ? null : _mapper.Map(dbVariable); + } + + /// + /// 异步根据OPC UA NodeId列表获取变量实体列表。 + /// + /// OPC UA NodeId列表。 + /// 找到的变量实体列表。 + public async Task> GetByOpcUaNodeIdsAsync(List opcUaNodeIds) + { + var dbVariables = await Db.Queryable() + .Where(v => opcUaNodeIds.Contains(v.OpcUaNodeId)) + .ToListAsync(); + return _mapper.Map>(dbVariables); + } } \ No newline at end of file diff --git a/DMS.WPF/ViewModels/VariableTableViewModel.cs b/DMS.WPF/ViewModels/VariableTableViewModel.cs index 9728828..74c1027 100644 --- a/DMS.WPF/ViewModels/VariableTableViewModel.cs +++ b/DMS.WPF/ViewModels/VariableTableViewModel.cs @@ -158,7 +158,6 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable } - /// /// 编辑选定的变量数据。 /// 此命令通常绑定到UI中的“编辑”按钮或双击事件。 @@ -177,7 +176,8 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable } // 显示编辑变量数据的对话框,并传入当前选中的变量数据 - VariableDialogViewModel variableDialogViewModel = App.Current.Services.GetRequiredService(); + VariableDialogViewModel variableDialogViewModel + = App.Current.Services.GetRequiredService(); variableDialogViewModel.Title = "编辑变量"; variableDialogViewModel.PrimaryButText = "保存修改"; variableDialogViewModel.IsAddModel = false; @@ -288,6 +288,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable _notificationService.ShowError("当前变量表的Device对象为空,请检查。"); return; } + // 检查OPC UA Endpoint URL是否已设置 string opcUaEndpointUrl = CurrentVariableTable.Device.OpcUaServerUrl; if (string.IsNullOrEmpty(opcUaEndpointUrl)) @@ -297,7 +298,8 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable } // 显示OPC UA导入对话框,让用户选择要导入的变量 - ImportOpcUaDialogViewModel importOpcUaDialogViewModel = App.Current.Services.GetRequiredService(); + ImportOpcUaDialogViewModel importOpcUaDialogViewModel + = App.Current.Services.GetRequiredService(); importOpcUaDialogViewModel.EndpointUrl = opcUaEndpointUrl; // 设置Endpoint URL var importedVariables = await _dialogService.ShowDialogAsync(importOpcUaDialogViewModel); if (importedVariables == null || !importedVariables.Any()) @@ -320,7 +322,8 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable { // 拼接要删除的变量名称,用于确认提示 var existNames = string.Join("、", existList.Select(v => v.Name)); - var confirmDialogViewModel = new ConfirmDialogViewModel("存在已经添加的变量", $"变量名称:{existNames},已经存在,是否跳过继续添加其他的变量。取消则不添加任何变量", "继续"); + var confirmDialogViewModel + = new ConfirmDialogViewModel("存在已经添加的变量", $"变量名称:{existNames},已经存在,是否跳过继续添加其他的变量。取消则不添加任何变量", "继续"); var res = await _dialogService.ShowDialogAsync(confirmDialogViewModel); if (!res) return; // 从导入列表中删除已经存在的变量 @@ -333,7 +336,10 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable var isSuccess = await _variableAppService.BatchImportVariablesAsync(importedVariableDtos); if (isSuccess) { - _variableItemList.AddRange(_mapper.Map>(importedVariableDtos)); + var addVariableDtos = await _variableAppService.GetVariableByOpcUaNodeIdsAsync( + importedVariableDtos.Select(v => v.OpcUaNodeId) + .ToList()); + _variableItemList.AddRange(_mapper.Map>(addVariableDtos)); _notificationService.ShowSuccess($"从OPC UA服务器导入变量成功,共导入变量:{importedVariableDtos.Count}个"); } else @@ -365,7 +371,8 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable try { // 显示添加变量数据的对话框 - VariableDialogViewModel variableDialogViewModel = App.Current.Services.GetRequiredService(); + VariableDialogViewModel variableDialogViewModel + = App.Current.Services.GetRequiredService(); VariableItemViewModel variableItem = new VariableItemViewModel(); variableItem.Protocol = CurrentVariableTable.Protocol; variableDialogViewModel.Title = "添加变量"; @@ -385,7 +392,8 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // // 添加变量数据到数据库 - var addVariable = await _variableAppService.CreateVariableAsync(_mapper.Map(variableItemViewModel)); + var addVariable + = await _variableAppService.CreateVariableAsync(_mapper.Map(variableItemViewModel)); _mapper.Map(addVariable, variableItemViewModel); // // 更新当前页面显示的数据:将新变量添加到集合中 _variableItemList.Add(variableItemViewModel); @@ -411,7 +419,8 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable { try { - List variablesToDelete = SelectedVariables.Cast().ToList(); + List variablesToDelete = SelectedVariables.Cast() + .ToList(); // 检查是否有变量被选中 if (variablesToDelete == null || !variablesToDelete.Any()) { @@ -423,14 +432,16 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable var names = string.Join("、", variablesToDelete.Select(v => v.Name)); // 显示确认删除对话框 - ConfirmDialogViewModel confirmDialogViewModel = new ConfirmDialogViewModel("删除变量", $"确认要删除变量:{names},删除后不可恢复,确认要删除吗?", "删除变量"); + ConfirmDialogViewModel confirmDialogViewModel + = new ConfirmDialogViewModel("删除变量", $"确认要删除变量:{names},删除后不可恢复,确认要删除吗?", "删除变量"); var isDel = await _dialogService.ShowDialogAsync(confirmDialogViewModel); if (!isDel) return; // 如果用户取消删除,则返回 // 从数据库中删除变量数据 - var result = await _variableAppService.DeleteVariablesAsync(variablesToDelete.Select(v => v.Id).ToList()); + var result = await _variableAppService.DeleteVariablesAsync(variablesToDelete.Select(v => v.Id) + .ToList()); if (result) { foreach (var variable in variablesToDelete) @@ -438,6 +449,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable _variableItemList.Remove(variable); _dataServices.DeleteVariable(variable.Id); } + // 显示成功通知 _notificationService.ShowSuccess($"成功删除 {variablesToDelete.Count} 个变量"); } @@ -470,10 +482,12 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable } // 获取选中的变量列表 - var validVariables = SelectedVariables.Cast().ToList(); + var validVariables = SelectedVariables.Cast() + .ToList(); // 显示轮询频率选择对话框,并传入第一个变量的当前轮询频率作为默认值 - PollLevelDialogViewModel viewModel = new PollLevelDialogViewModel(validVariables.First().PollLevel); + PollLevelDialogViewModel viewModel = new PollLevelDialogViewModel(validVariables.First() + .PollLevel); var newPollLevelType = await _dialogService.ShowDialogAsync(viewModel); if (newPollLevelType.HasValue) { @@ -487,7 +501,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 批量更新数据库中的变量数据 var variableDtos = _mapper.Map>(validVariables); var result = await _variableAppService.UpdateVariablesAsync(variableDtos); - + if (result > 0) { // 显示成功通知 @@ -654,10 +668,12 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable } // 获取选中的变量列表 - var validVariables = SelectedVariables.Cast().ToList(); + var validVariables = SelectedVariables.Cast() + .ToList(); // 显示启用状态选择对话框,并传入第一个变量的当前启用状态作为默认值 - IsActiveDialogViewModel viewModel = new IsActiveDialogViewModel(validVariables.First().IsActive); + IsActiveDialogViewModel viewModel = new IsActiveDialogViewModel(validVariables.First() + .IsActive); var newIsActive = await _dialogService.ShowDialogAsync(viewModel); if (newIsActive.HasValue) { @@ -671,7 +687,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 批量更新数据库中的变量数据 var variableDtos = _mapper.Map>(validVariables); var result = await _variableAppService.UpdateVariablesAsync(variableDtos); - + if (result > 0) { // 显示成功通知