From 442ee3f9c147255a344d908794fcc9421448b364 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Mon, 28 Jul 2025 11:33:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=B7=BB=E5=8A=A0=E8=AE=BE?= =?UTF-8?q?=E5=A4=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DMS.WPF/Services/DataServices.cs | 62 +++++++++++++++++++------- DMS.WPF/ViewModels/DevicesViewModel.cs | 15 +++---- 2 files changed, 53 insertions(+), 24 deletions(-) diff --git a/DMS.WPF/Services/DataServices.cs b/DMS.WPF/Services/DataServices.cs index a987583..8190fcf 100644 --- a/DMS.WPF/Services/DataServices.cs +++ b/DMS.WPF/Services/DataServices.cs @@ -45,6 +45,7 @@ public partial class DataServices : ObservableRecipient, IRecipient // 菜单树列表。 [ObservableProperty] private ObservableCollection _menus; + // 菜单树列表。 [ObservableProperty] private ObservableCollection _menuTrees; @@ -72,7 +73,6 @@ public partial class DataServices : ObservableRecipient, IRecipient public event Action OnDeviceIsActiveChanged; - /// /// DataServices类的构造函数。 /// 注入ILogger,并初始化各个数据仓库。 @@ -80,7 +80,8 @@ public partial class DataServices : ObservableRecipient, IRecipient /// AutoMapper 实例。 /// public DataServices(IMapper mapper, IDeviceAppService deviceAppService, - IVariableTableAppService variableTableAppService, IVariableAppService variableAppService,IMenuService menuService,INavigationService navigationService) + IVariableTableAppService variableTableAppService, IVariableAppService variableAppService, + IMenuService menuService, INavigationService navigationService) { _mapper = mapper; _deviceAppService = deviceAppService; @@ -115,7 +116,7 @@ public partial class DataServices : ObservableRecipient, IRecipient { // 设备仍然存在,检查是否有更新 var dto = deviceDtos.First(d => d.Id == existingItem.Id); - + // 逐一比较属性,只有在发生变化时才更新 if (existingItem.Name != dto.Name) existingItem.Name = dto.Name; if (existingItem.Protocol != dto.Protocol) existingItem.Protocol = dto.Protocol; @@ -204,13 +205,13 @@ public partial class DataServices : ObservableRecipient, IRecipient { // 这是一个新菜单项,添加到集合中 // 注意:这里直接添加 IMenuService 返回的 MenuItemViewModel 实例 - Menus.Add(new MenuBeanItemViewModel(newDto,_navigationService)); + Menus.Add(new MenuBeanItemViewModel(newDto, _navigationService)); } } - + BuildMenuTree(); } - + /// /// 根据扁平菜单列表构建树形结构。 /// @@ -244,7 +245,6 @@ public partial class DataServices : ObservableRecipient, IRecipient MenuTrees.Add(menu); } } - } @@ -280,17 +280,21 @@ public partial class DataServices : ObservableRecipient, IRecipient if (existingItem.SignalType != dto.SignalType) existingItem.SignalType = dto.SignalType; if (existingItem.PollLevel != dto.PollLevel) existingItem.PollLevel = dto.PollLevel; if (existingItem.IsActive != dto.IsActive) existingItem.IsActive = dto.IsActive; - if (existingItem.VariableTableId != dto.VariableTableId) existingItem.VariableTableId = dto.VariableTableId; + if (existingItem.VariableTableId != dto.VariableTableId) + existingItem.VariableTableId = dto.VariableTableId; if (existingItem.OpcUaNodeId != dto.OpcUaNodeId) existingItem.OpcUaNodeId = dto.OpcUaNodeId; - if (existingItem.IsHistoryEnabled != dto.IsHistoryEnabled) existingItem.IsHistoryEnabled = dto.IsHistoryEnabled; - if (existingItem.HistoryDeadband != dto.HistoryDeadband) existingItem.HistoryDeadband = dto.HistoryDeadband; + if (existingItem.IsHistoryEnabled != dto.IsHistoryEnabled) + existingItem.IsHistoryEnabled = dto.IsHistoryEnabled; + if (existingItem.HistoryDeadband != dto.HistoryDeadband) + existingItem.HistoryDeadband = dto.HistoryDeadband; if (existingItem.IsAlarmEnabled != dto.IsAlarmEnabled) existingItem.IsAlarmEnabled = dto.IsAlarmEnabled; if (existingItem.AlarmMinValue != dto.AlarmMinValue) existingItem.AlarmMinValue = dto.AlarmMinValue; if (existingItem.AlarmMaxValue != dto.AlarmMaxValue) existingItem.AlarmMaxValue = dto.AlarmMaxValue; if (existingItem.AlarmDeadband != dto.AlarmDeadband) existingItem.AlarmDeadband = dto.AlarmDeadband; if (existingItem.Protocol != dto.Protocol) existingItem.Protocol = dto.Protocol; if (existingItem.CSharpDataType != dto.CSharpDataType) existingItem.CSharpDataType = dto.CSharpDataType; - if (existingItem.ConversionFormula != dto.ConversionFormula) existingItem.ConversionFormula = dto.ConversionFormula; + if (existingItem.ConversionFormula != dto.ConversionFormula) + existingItem.ConversionFormula = dto.ConversionFormula; if (existingItem.CreatedAt != dto.CreatedAt) existingItem.CreatedAt = dto.CreatedAt; if (existingItem.UpdatedAt != dto.UpdatedAt) existingItem.UpdatedAt = dto.UpdatedAt; if (existingItem.UpdatedBy != dto.UpdatedBy) existingItem.UpdatedBy = dto.UpdatedBy; @@ -381,8 +385,8 @@ public partial class DataServices : ObservableRecipient, IRecipient { // 1. 创建一个字典,按 DeviceId 分组所有变量表,以便高效查找 var variableTablesGroupedByDevice = _variableTables - .GroupBy(vt => vt.DeviceId) - .ToDictionary(g => g.Key, g => g.ToList()); + .GroupBy(vt => vt.DeviceId) + .ToDictionary(g => g.Key, g => g.ToList()); foreach (var device in _devices) { @@ -427,8 +431,8 @@ public partial class DataServices : ObservableRecipient, IRecipient { // 1. 创建一个字典,按 VariableTableId 分组所有变量,以便高效查找 var variablesGroupedByVariableTable = _variables - .GroupBy(v => v.VariableTableId) - .ToDictionary(g => g.Key, g => g.ToList()); + .GroupBy(v => v.VariableTableId) + .ToDictionary(g => g.Key, g => g.ToList()); foreach (var variableTable in _variableTables) { @@ -465,4 +469,32 @@ public partial class DataServices : ObservableRecipient, IRecipient } } } + + public void AddMenuItem(MenuBeanItemViewModel menuBeanItemViewModel) + { + if (menuBeanItemViewModel == null) + { + return; + } + + var deviceMenu = Menus.FirstOrDefault(m => m.Id == menuBeanItemViewModel.ParentId); + if (deviceMenu != null) + { + deviceMenu.Children.Add(menuBeanItemViewModel); + Menus.Add(menuBeanItemViewModel); + } + } + + public void AddVariableTable(VariableTableItemViewModel variableTableItemViewModel) + { + if (variableTableItemViewModel == null) + return; + + VariableTables.Add(variableTableItemViewModel); + var device = Devices.FirstOrDefault(d => d.Id == variableTableItemViewModel.DeviceId); + if (device != null) + { + device.VariableTables.Add(variableTableItemViewModel); + } + } } \ No newline at end of file diff --git a/DMS.WPF/ViewModels/DevicesViewModel.cs b/DMS.WPF/ViewModels/DevicesViewModel.cs index 328a0e2..0966df5 100644 --- a/DMS.WPF/ViewModels/DevicesViewModel.cs +++ b/DMS.WPF/ViewModels/DevicesViewModel.cs @@ -147,18 +147,15 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable }; } - var addDto = await _deviceAppService.CreateDeviceWithDetailsAsync(dto); + // 更新界面 DataServices.Devices.Add(_mapper.Map(addDto.Device)); - if (addDto.DeviceMenu != null) - { - var deviceMenu = DataServices.Menus.FirstOrDefault(m => m.Id == addDto.DeviceMenu.ParentId); - if (deviceMenu!=null) - { - deviceMenu.Children.Add(_mapper.Map(addDto.DeviceMenu)); - } - } + DataServices.AddMenuItem(_mapper.Map(addDto.DeviceMenu)); + DataServices.AddVariableTable(_mapper.Map(addDto.VariableTable)); + DataServices.AddMenuItem(_mapper.Map(addDto.VariableTableMenu)); + + NotificationHelper.ShowSuccess($"设备添加成功:{addDto.Device.Name}"); } catch (Exception e) {