diff --git a/DMS.Application/DTOs/CreateVariableTableWithMenuDto.cs b/DMS.Application/DTOs/CreateVariableTableWithMenuDto.cs index 99cfe2e..efb3c35 100644 --- a/DMS.Application/DTOs/CreateVariableTableWithMenuDto.cs +++ b/DMS.Application/DTOs/CreateVariableTableWithMenuDto.cs @@ -6,7 +6,6 @@ namespace DMS.Application.DTOs public class CreateVariableTableWithMenuDto { public VariableTable VariableTable { get; set; } - public int DeviceId { get; set; } public MenuBean Menu { get; set; } } } diff --git a/DMS.Application/Interfaces/Database/IVariableTableAppService.cs b/DMS.Application/Interfaces/Database/IVariableTableAppService.cs index b643d7d..4a2d8c6 100644 --- a/DMS.Application/Interfaces/Database/IVariableTableAppService.cs +++ b/DMS.Application/Interfaces/Database/IVariableTableAppService.cs @@ -8,8 +8,8 @@ namespace DMS.Application.Interfaces.Database { Task GetVariableTableByIdAsync(int id); Task> GetAllVariableTablesAsync(); - Task CreateVariableTableAsync(CreateVariableTableWithMenuDto createDto); - Task UpdateVariableTableAsync(VariableTable variableTableDto); - Task DeleteVariableTableAsync(int id); + Task AddAsync(CreateVariableTableWithMenuDto createDto); + Task UpdateAsync(VariableTable variableTableDto); + Task DeleteAsync(VariableTable variableTable); } } diff --git a/DMS.Application/Interfaces/Management/IVariableTableManagementService.cs b/DMS.Application/Interfaces/Management/IVariableTableManagementService.cs index 8960a42..88ac9df 100644 --- a/DMS.Application/Interfaces/Management/IVariableTableManagementService.cs +++ b/DMS.Application/Interfaces/Management/IVariableTableManagementService.cs @@ -19,17 +19,17 @@ public interface IVariableTableManagementService /// /// 异步创建一个新变量表及其关联菜单(事务性操作)。 /// - Task CreateVariableTableAsync(CreateVariableTableWithMenuDto dto); + Task AddAsync(CreateVariableTableWithMenuDto dto); /// /// 异步更新一个已存在的变量表。 /// - Task UpdateVariableTableAsync(VariableTable variableTableDto); + Task UpdateAsync(VariableTable variableTableDto); /// /// 异步删除一个变量表。 /// - Task DeleteVariableTableAsync(int id); + Task DeleteAsync(VariableTable variableTable); /// /// 异步加载所有变量表数据到内存中。 diff --git a/DMS.Application/Services/Database/VariableTableAppService.cs b/DMS.Application/Services/Database/VariableTableAppService.cs index da452fe..82efe4a 100644 --- a/DMS.Application/Services/Database/VariableTableAppService.cs +++ b/DMS.Application/Services/Database/VariableTableAppService.cs @@ -15,6 +15,7 @@ namespace DMS.Application.Services.Database public class VariableTableAppService : IVariableTableAppService { private readonly IRepositoryManager _repositoryManager; + private readonly IAppDataService _appDataService; private readonly IMapper _mapper; /// @@ -22,9 +23,10 @@ namespace DMS.Application.Services.Database /// /// 仓储管理器实例。 /// AutoMapper 实例。 - public VariableTableAppService(IRepositoryManager repositoryManager, IMapper mapper) + public VariableTableAppService(IRepositoryManager repositoryManager, IAppDataService appDataService, IMapper mapper) { _repositoryManager = repositoryManager; + _appDataService = appDataService; _mapper = mapper; } @@ -55,43 +57,24 @@ namespace DMS.Application.Services.Database /// 包含变量表和菜单信息的创建数据传输对象。 /// 创建后的变量表数据传输对象。 /// 如果添加变量表失败或找不到设备菜单。 - public async Task CreateVariableTableAsync(CreateVariableTableWithMenuDto createDto) + public async Task AddAsync(CreateVariableTableWithMenuDto createDto) { await _repositoryManager.BeginTranAsync(); try { - var variableTable = _mapper.Map(createDto.VariableTable); - variableTable.DeviceId = createDto.DeviceId; - - var createdVariableTable = await _repositoryManager.VariableTables.AddAsync(variableTable); - if (createdVariableTable.Id == 0) + + createDto.VariableTable = await _repositoryManager.VariableTables.AddAsync(createDto.VariableTable); + if (createDto.VariableTable.Id == 0) { - throw new ApplicationException($"添加变量表失败,设备ID:{createDto.DeviceId},请检查。"); + throw new ApplicationException($"添加变量表失败,设备ID:{createDto.VariableTable.DeviceId},请检查。"); } - - _mapper.Map(createdVariableTable, createDto.VariableTable); - if (createDto.Menu!=null) + if (createDto.Menu is not null) { - // 获取设备菜单,作为变量表菜单的父级 - //var deviceMenu - // = await _repositoryManager.Menus.GetMenuByTargetIdAsync( - // MenuType.DeviceMenu, createDto.DeviceId); - //if (deviceMenu == null) - //{ - // throw new ApplicationException($"添加变量表菜单时,找不到设备ID:{createDto.DeviceId},请检查。"); - //} - - // 映射菜单实体并设置关联信息 - // var menu = _mapper.Map(createDto.Menu); - // menu.ParentId = deviceMenu.Id; - // menu.TargetId = createdVariableTable.Id; - // menu.MenuType = MenuType.VariableTableMenu; - //var addMenu= await _repositoryManager.Menus.AddAsync(menu); - //_mapper.Map(addMenu, createDto.Menu); + createDto.Menu.TargetId = createDto.VariableTable.Id; + //映射菜单实体并设置关联信息 + createDto.Menu = await _repositoryManager.Menus.AddAsync(createDto.Menu); } - - await _repositoryManager.CommitAsync(); @@ -110,28 +93,21 @@ namespace DMS.Application.Services.Database /// 要更新的变量表数据传输对象。 /// 受影响的行数。 /// 如果找不到变量表。 - public async Task UpdateVariableTableAsync(VariableTable variableTableDto) + public async Task UpdateAsync(VariableTable variableTable) { - try - { - await _repositoryManager.BeginTranAsync(); - var variableTable = await _repositoryManager.VariableTables.GetByIdAsync(variableTableDto.Id); - if (variableTable == null) - { - throw new ApplicationException($"VariableTable with ID {variableTableDto.Id} not found."); - } - _mapper.Map(variableTableDto, variableTable); - int res = await _repositoryManager.VariableTables.UpdateAsync(variableTable); - await _repositoryManager.CommitAsync(); - return res; - } - catch (Exception ex) + int res = await _repositoryManager.VariableTables.UpdateAsync(variableTable); + + // 更新关联的菜单树 + var menu = _appDataService.Menus.Values.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu && m.TargetId == variableTable.Id); + if (menu is not null && variableTable.Name!= menu.Header) { - await _repositoryManager.RollbackAsync(); - // 可以在此记录日志 - throw new ApplicationException($"更新变量表时发生错误,操作已回滚,错误信息:{ex.Message}", ex); + menu.Header = variableTable.Name; + // 更新关联的菜单树 + await _repositoryManager.Menus.UpdateAsync(menu); } + + return res; } /// @@ -141,30 +117,20 @@ namespace DMS.Application.Services.Database /// 如果删除成功则为 true,否则为 false。 /// 如果删除变量表失败。 /// 如果删除变量表时发生其他错误。 - public async Task DeleteVariableTableAsync(int id) + public async Task DeleteAsync(VariableTable variableTable) { try { await _repositoryManager.BeginTranAsync(); - var variableTable = await _repositoryManager.VariableTables.GetByIdAsync(id); - if (variableTable == null) - { - throw new InvalidOperationException($"删除变量表失败:变量表ID:{id},请检查变量表Id是否存在"); - } var delRes = await _repositoryManager.VariableTables.DeleteAsync(variableTable); - if (delRes == 0) - { - throw new InvalidOperationException($"删除变量表失败:变量表ID:{id},请检查变量表Id是否存在"); - } - - // 删除关联的变量 - await _repositoryManager.Variables.DeleteByVariableTableIdAsync(id); - - // 删除关联的MQTT别名 - // await _repositoryManager.MqttAlias.DeleteByVariableTableIdAsync(id); // 删除关联的菜单树 - //await _repositoryManager.Menus.DeleteMenuTreeByTargetIdAsync(MenuType.VariableTableMenu, id); + var menu = _appDataService.Menus.Values.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu && m.TargetId == variableTable.Id); + if (menu is not null) + { + // 删除关联的菜单树 + await _repositoryManager.Menus.DeleteAsync(menu); + } await _repositoryManager.CommitAsync(); return true; diff --git a/DMS.Application/Services/Management/DeviceManagementService.cs b/DMS.Application/Services/Management/DeviceManagementService.cs index 5f6ac07..030bbc7 100644 --- a/DMS.Application/Services/Management/DeviceManagementService.cs +++ b/DMS.Application/Services/Management/DeviceManagementService.cs @@ -49,8 +49,6 @@ public class DeviceManagementService : IDeviceManagementService public async Task CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto) { - - var result = await _deviceAppService.CreateDeviceWithDetailsAsync(dto); // 创建成功后,将设备添加到内存中 @@ -71,29 +69,31 @@ public class DeviceManagementService : IDeviceManagementService } } + //关联菜单 + if (result.DeviceMenu is not null) + { + + var deviceRootMenu = _appDataService.Menus.Values.FirstOrDefault(m => m.TargetViewKey == "DevicesViewModel" && m.TargetId == 0); + if (deviceRootMenu is not null) + { + _appDataService.Menus.TryAdd(result.DeviceMenu.Id, result.DeviceMenu); + //将设备菜单添加到根菜单中 + deviceRootMenu.Children.Add(result.DeviceMenu); + + if (result.VariableTableMenu is not null) + { + _appDataService.Menus.TryAdd(result.VariableTableMenu.Id, result.VariableTableMenu); + //将变量表菜单添加到设备菜单中 + result.DeviceMenu.Children.Add(result.VariableTableMenu); + } + } + + } + _eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Added, result.Device)); } - //关联菜单 - if (result.DeviceMenu is not null) - { - - var deviceRootMenu = _appDataService.Menus.Values.FirstOrDefault(m => m.TargetViewKey == "DevicesViewModel" && m.TargetId == 0); - if (deviceRootMenu is not null) - { - _appDataService.Menus.TryAdd(result.DeviceMenu.Id, result.DeviceMenu); - //将设备菜单添加到根菜单中 - deviceRootMenu.Children.Add(result.DeviceMenu); - - if (result.VariableTableMenu is not null) - { - _appDataService.Menus.TryAdd(result.VariableTableMenu.Id, result.VariableTableMenu); - //将变量表菜单添加到设备菜单中 - result.DeviceMenu.Children.Add(result.VariableTableMenu); - } - } - - } + diff --git a/DMS.Application/Services/Management/MenuManagementService.cs b/DMS.Application/Services/Management/MenuManagementService.cs index f89fe84..8b72658 100644 --- a/DMS.Application/Services/Management/MenuManagementService.cs +++ b/DMS.Application/Services/Management/MenuManagementService.cs @@ -56,7 +56,7 @@ public class MenuManagementService : IMenuManagementService if (_appDataService.Menus.TryAdd(resMenu.Id, resMenu)) { MenuBean parentMenu = null; - if (resMenu.ParentId > 0 && _appDataService.Menus.TryGetValue(resMenu.ParentId.Value, out var parent)) + if (resMenu.ParentId > 0 && _appDataService.Menus.TryGetValue(resMenu.ParentId, out var parent)) { parentMenu = parent; parent.Children.Add(resMenu); @@ -103,7 +103,7 @@ public class MenuManagementService : IMenuManagementService if (_appDataService.Menus.TryRemove(menu.Id, out var menuData)) { // 从父菜单中移除子菜单 - if (menuData.ParentId > 0 && _appDataService.Menus.TryGetValue(menuData.ParentId.Value, out var parentMenu)) + if (menuData.ParentId > 0 && _appDataService.Menus.TryGetValue(menuData.ParentId, out var parentMenu)) { parentMenu.Children.Remove(menuData); } diff --git a/DMS.Application/Services/Management/VariableTableManagementService.cs b/DMS.Application/Services/Management/VariableTableManagementService.cs index e651279..81bb9c8 100644 --- a/DMS.Application/Services/Management/VariableTableManagementService.cs +++ b/DMS.Application/Services/Management/VariableTableManagementService.cs @@ -50,36 +50,49 @@ public class VariableTableManagementService : IVariableTableManagementService /// /// 异步创建一个新变量表及其关联菜单(事务性操作)。 /// - public async Task CreateVariableTableAsync(CreateVariableTableWithMenuDto dto) + public async Task AddAsync(CreateVariableTableWithMenuDto dto) { - var result = await _variableTableAppService.CreateVariableTableAsync(dto); - // 创建成功后,将变量表添加到内存中 - if (result?.VariableTable != null) + + var result = await _variableTableAppService.AddAsync(dto); + + // 检查结果是否为空 + if (result?.VariableTable is null) { - // 添加null检查 - if (_appDataService.Devices != null && - _appDataService.Devices.TryGetValue(result.VariableTable.DeviceId, out var device)) - { - // 确保VariableTables不为null - if (device.VariableTables == null) - device.VariableTables = new List(); + return null; + } - device.VariableTables.Add(result.VariableTable); + // 获取设备对象 + if (!_appDataService.Devices.TryGetValue(result.VariableTable.DeviceId, out var device)) + { + return null; + } - // 确保Device属性不为null - if (result.VariableTable != null) - result.VariableTable.Device = device; - } + // 将变量表添加到设备列表并设置设备引用 + device.VariableTables.Add(result.VariableTable); + result.VariableTable.Device = device; - // 确保_variableTables和result.VariableTable不为null - if (_appDataService.VariableTables.TryAdd(result.VariableTable.Id, result.VariableTable)) - { - _eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs( + // 尝试将变量表添加到内存中 + if (!_appDataService.VariableTables.TryAdd(result.VariableTable.Id, result.VariableTable)) + { + return null; + } + + // 如果有菜单对象,则处理菜单相关逻辑 + if (result.Menu is null) return null; + + var deviceMenu = _appDataService.Menus.Values.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu && m.TargetId == device.Id); + if (deviceMenu is not null) + { + _appDataService.Menus.TryAdd(result.Menu.Id, result.Menu); + //将设备菜单添加到根菜单中 + deviceMenu.Children.Add(result.Menu); + } + + // 触发变量表变化事件 + _eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs( DataChangeType.Added, result.VariableTable)); - } - } return result; } @@ -87,9 +100,9 @@ public class VariableTableManagementService : IVariableTableManagementService /// /// 异步更新一个已存在的变量表。 /// - public async Task UpdateVariableTableAsync(VariableTable variableTable) + public async Task UpdateAsync(VariableTable variableTable) { - var result = await _variableTableAppService.UpdateVariableTableAsync(variableTable); + var result = await _variableTableAppService.UpdateAsync(variableTable); // 更新成功后,更新内存中的变量表 if (result > 0 && variableTable != null) @@ -106,25 +119,37 @@ public class VariableTableManagementService : IVariableTableManagementService /// /// 异步删除一个变量表。 /// - public async Task DeleteVariableTableAsync(int id) + public async Task DeleteAsync(VariableTable variableTable) { - var result = await _variableTableAppService.DeleteVariableTableAsync(id); + var result = await _variableTableAppService.DeleteAsync(variableTable); // 删除成功后,从内存中移除变量表 - if (result ) + if (result) { - if (_appDataService.VariableTables.TryRemove(id, out var variableTable)) + if (_appDataService.Devices.TryGetValue(variableTable.DeviceId, out var device)) { - if (variableTable != null && _appDataService.Devices.TryGetValue(variableTable.DeviceId, out var device)) + device.VariableTables.Remove(variableTable); + + _appDataService.VariableTables.TryRemove(variableTable.Id, out _); + + //删除菜单 + var variableTableMenu = _appDataService.Menus.Values.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu && m.TargetId == variableTable.Id); + if (variableTableMenu is not null) { - if (device.VariableTables != null) - device.VariableTables.Remove(variableTable); + if (_appDataService.Menus.TryGetValue(variableTableMenu.ParentId, out var deviceMenu)) + { + deviceMenu.Children.Remove(variableTableMenu); + } + + + _appDataService.Menus.TryRemove(variableTableMenu.Id, out _); } - _eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs( - DataChangeType.Deleted, - variableTable)); } + + _eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs( + DataChangeType.Deleted, + variableTable)); } return result; diff --git a/DMS.Core/Models/MenuBean.cs b/DMS.Core/Models/MenuBean.cs index 4d720a8..f398496 100644 --- a/DMS.Core/Models/MenuBean.cs +++ b/DMS.Core/Models/MenuBean.cs @@ -18,7 +18,7 @@ public class MenuBean /// 父级菜单项的ID,用于构建层级菜单结构 /// 如果为null表示为顶级菜单项 /// - public int? ParentId { get; set; } + public int ParentId { get; set; } /// /// 菜单项显示的标题文本 diff --git a/DMS.WPF/Interfaces/IVariableTableViewService.cs b/DMS.WPF/Interfaces/IVariableTableViewService.cs index 49ba50c..db6aeee 100644 --- a/DMS.WPF/Interfaces/IVariableTableViewService.cs +++ b/DMS.WPF/Interfaces/IVariableTableViewService.cs @@ -10,9 +10,8 @@ public interface IVariableTableViewService void LoadAllVariableTables(); - Task AddVariableTable(VariableTable variableTable, - MenuBean menu = null, bool isAddDb = false); + Task AddAsync(VariableTableItem variableTable ); - Task UpdateVariableTable(VariableTableItem variableTable); - Task DeleteVariableTable(VariableTableItem variableTable, bool isDeleteDb = false); + Task UpdateAsync(VariableTableItem variableTable); + Task DeleteAsync(VariableTableItem variableTable); } \ No newline at end of file diff --git a/DMS.WPF/Interfaces/IVariableViewService.cs b/DMS.WPF/Interfaces/IVariableViewService.cs index 6738835..7cb904f 100644 --- a/DMS.WPF/Interfaces/IVariableViewService.cs +++ b/DMS.WPF/Interfaces/IVariableViewService.cs @@ -26,11 +26,6 @@ public interface IVariableViewService /// Task UpdateVariableTable(VariableTableItem variableTable); - /// - /// 删除变量表。 - /// - Task DeleteVariableTable(VariableTableItem variableTable, bool isDeleteDb = false); - /// /// 添加变量。 /// diff --git a/DMS.WPF/Services/DataEventService.cs b/DMS.WPF/Services/DataEventService.cs index 041bcaf..bb6914a 100644 --- a/DMS.WPF/Services/DataEventService.cs +++ b/DMS.WPF/Services/DataEventService.cs @@ -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类的构造函数。 /// 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(); + 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 /// 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 })); } - + /// /// 处理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消息处理完成"); } } \ No newline at end of file diff --git a/DMS.WPF/Services/ViewService/VariableTableViewService.cs b/DMS.WPF/Services/ViewService/VariableTableViewService.cs index 8e7bfad..b9b5aac 100644 --- a/DMS.WPF/Services/ViewService/VariableTableViewService.cs +++ b/DMS.WPF/Services/ViewService/VariableTableViewService.cs @@ -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 AddVariableTable(VariableTable variableTable, - MenuBean menuDto = null, bool isAddDb = false) + /// + /// 异步添加变量表 + /// 此方法创建一个新的变量表项,包括创建对应的菜单项,并将其添加到数据库和视图数据服务中 + /// + /// 要添加的变量表项 + /// 如果添加成功返回true,否则返回false + public async Task 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(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(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(resDto.Menu)); + // 成功添加,返回true + return true; } - public async Task UpdateVariableTable(VariableTableItem variableTable) + public async Task UpdateAsync(VariableTableItem variableTable) { - if (variableTable == null) + // 检查变量表是否存在于应用数据存储中 + if (!_appDataService.VariableTables.TryGetValue(variableTable.Id, out var variableTableInApp)) { return false; } - - var variableTable_mapped = _mapper.Map(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 DeleteVariableTable(VariableTableItem variableTable, bool isDeleteDb = false) + /// + /// 异步删除变量表 + /// 此方法用于删除指定的变量表项,包括从数据库中删除、移除关联的变量和菜单项 + /// + /// 要删除的变量表项 + /// 如果删除成功返回true,否则返回false + public async Task 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; } } \ No newline at end of file diff --git a/DMS.WPF/Services/ViewService/VariableViewService.cs b/DMS.WPF/Services/ViewService/VariableViewService.cs index 2883b35..2902267 100644 --- a/DMS.WPF/Services/ViewService/VariableViewService.cs +++ b/DMS.WPF/Services/ViewService/VariableViewService.cs @@ -84,7 +84,7 @@ public class VariableViewService : IVariableViewService } var variableTable = _mapper.Map(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; } - /// - /// 删除变量表。 - /// - public async Task 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; - } /// /// 添加变量。 diff --git a/DMS.WPF/ViewModels/DeviceDetailViewModel.cs b/DMS.WPF/ViewModels/DeviceDetailViewModel.cs index ae0040e..3d3648a 100644 --- a/DMS.WPF/ViewModels/DeviceDetailViewModel.cs +++ b/DMS.WPF/ViewModels/DeviceDetailViewModel.cs @@ -22,6 +22,7 @@ public partial class DeviceDetailViewModel : ViewModelBase private readonly IDialogService _dialogService; private readonly IViewDataService _viewDataService; private readonly INavigationService _navigationService; + private readonly IVariableTableViewService _variableTableViewService; private readonly IViewCenterService _wpfDataService; [ObservableProperty] @@ -38,57 +39,49 @@ public partial class DeviceDetailViewModel : ViewModelBase public DeviceDetailViewModel(IMapper mapper, IDialogService dialogService, IViewDataService dataStorageService, INavigationService navigationService, - IViewCenterService wpfDataService, INotificationService notificationService) + IVariableTableViewService variableTableViewService, INotificationService notificationService) { _mapper = mapper; _dialogService = dialogService; _viewDataService = dataStorageService; _navigationService = navigationService; + _variableTableViewService = variableTableViewService; _notificationService = notificationService; - _wpfDataService = wpfDataService; } [RelayCommand] - private async Task AddVariableTable() + private async Task AddVariableTable(DeviceItem deviceItem = null) { if (IsBusy) return; IsBusy = true; try { - VariableTableDialogViewModel variableTableDialogViewModel = new VariableTableDialogViewModel() - { - PrimaryButText = "添加变量表" - }; - // 1. 显示添加设备对话框 - var VariableTableItem = await _dialogService.ShowDialogAsync(variableTableDialogViewModel); - // 如果用户取消或对话框未返回设备,则直接返回 - if (VariableTableItem == null) + if (deviceItem is null) { - return; + deviceItem = CurrentDevice; } - VariableTableItem.DeviceId = CurrentDevice.Id; - var tableMenu = new MenuBean() - { - Header = VariableTableItem.Name, - Icon = SegoeFluentIcons.DataSense.Glyph, - TargetViewKey = nameof(VariableTableViewModel) - }; - int addVarTableId = await _wpfDataService.VariableTableDataService.AddVariableTable( - _mapper.Map(VariableTableItem), - tableMenu, true); - - if (addVarTableId > 0) + VariableTableDialogViewModel variableTableDialogViewModel = new VariableTableDialogViewModel() { - VariableTableItem.Id = addVarTableId; - VariableTableItem.Device = CurrentDevice; - CurrentDevice.VariableTables.Add(VariableTableItem); - _viewDataService.VariableTables.Add(VariableTableItem.Id, VariableTableItem); - _notificationService.ShowSuccess($"添加变量表成功:{VariableTableItem.Name}"); + PrimaryButText = "添加变量表" + }; + // 1. 显示添加设备对话框 + var variableTableItem = await _dialogService.ShowDialogAsync(variableTableDialogViewModel); + // 如果用户取消或对话框未返回设备,则直接返回 + if (variableTableItem is null) return; + + variableTableItem.DeviceId = deviceItem.Id; + variableTableItem.Device = deviceItem; + variableTableItem.Protocol = deviceItem.Protocol; + var res = await _variableTableViewService.AddAsync(variableTableItem); + + if (res) + { + _notificationService.ShowSuccess($"添加变量表成功:{variableTableItem.Name}"); } else { - _notificationService.ShowError($"添加变量表失败:{VariableTableItem.Name}!!"); + _notificationService.ShowError($"添加变量表失败:{variableTableItem.Name}!!"); } } catch (Exception ex) @@ -116,19 +109,16 @@ public partial class DeviceDetailViewModel : ViewModelBase VariableTableDialogViewModel variableTableDialogViewModel = new VariableTableDialogViewModel(SelectedVariableTable) - { - PrimaryButText = "编辑变量表" - }; + { + PrimaryButText = "编辑变量表" + }; // 1. 显示变量表对话框 VariableTableItem variableTable = await _dialogService.ShowDialogAsync(variableTableDialogViewModel); // 如果用户取消或对话框未返回变量表,则直接返回 - if (variableTable == null) - { - return; - } + if (variableTable is null) return; - if (await _wpfDataService.VariableDataService.UpdateVariableTable(variableTable)) + if (await _variableTableViewService.UpdateAsync(variableTable)) { _notificationService.ShowSuccess($"编辑变量表成功:{variableTable.Name}"); } @@ -148,40 +138,35 @@ public partial class DeviceDetailViewModel : ViewModelBase } [RelayCommand] - private async Task DeleteVariableTable() + private async Task DeleteVariableTable(VariableTableItem variableTableItem=null) { if (IsBusy) return; IsBusy = true; try { - if (SelectedVariableTable == null) + if (variableTableItem is null) { - _notificationService.ShowError("你没有选择任何变量表,请选择变量表后再点击删除变量表"); - return; + if (SelectedVariableTable is null) + { + _notificationService.ShowError("你没有选择任何变量表,请选择变量表后再点击删除变量表"); + return; + } + + variableTableItem = SelectedVariableTable; } - string message = $"确认要删除变量表名为:{SelectedVariableTable.Name} \n\n此操作将同时删除该变量表下的所有变量数据,且无法恢复!"; + string message = $"确认要删除变量表名为:{variableTableItem.Name} \n\n此操作将同时删除该变量表下的所有变量数据,且无法恢复!"; ConfirmDialogViewModel viewModel = new ConfirmDialogViewModel("删除变量表", message, "删除"); - if (await _dialogService.ShowDialogAsync(viewModel)) + bool confirmRes = await _dialogService.ShowDialogAsync(viewModel); + if (!confirmRes) return; + var tableName = variableTableItem.Name; + if (await _variableTableViewService.DeleteAsync(variableTableItem)) { - var tableName = SelectedVariableTable.Name; - if (await _wpfDataService.VariableDataService.DeleteVariableTable(SelectedVariableTable, true)) - { - if (SelectedVariableTable.Device != null) - { - if (_viewDataService.Devices.TryGetValue(SelectedVariableTable.DeviceId ,out var device)) - { - device.VariableTables.Remove(SelectedVariableTable); - } - _viewDataService.VariableTables.Remove(SelectedVariableTable.Id); - SelectedVariableTable.Device.VariableTables.Remove(SelectedVariableTable); - } - _notificationService.ShowSuccess($"变量表:{tableName},删除成功。"); - } - else - { - _notificationService.ShowError($"变量表:{tableName},删除失败!!!"); - } + _notificationService.ShowSuccess($"变量表:{tableName},删除成功。"); + } + else + { + _notificationService.ShowError($"变量表:{tableName},删除失败!!!"); } } catch (Exception e) @@ -207,9 +192,6 @@ public partial class DeviceDetailViewModel : ViewModelBase public void NavigateToVariableTable() { if (SelectedVariableTable == null) return; - // var menu = _viewCenterService.Menus.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu && - // m.TargetId == SelectedVariableTable.Id); - // if (menu == null) return; _navigationService.NavigateToAsync( this, new NavigationParameter(nameof(VariableTableViewModel), SelectedVariableTable.Id, diff --git a/DMS.WPF/ViewModels/DevicesViewModel.cs b/DMS.WPF/ViewModels/DevicesViewModel.cs index 82145f4..901e3ba 100644 --- a/DMS.WPF/ViewModels/DevicesViewModel.cs +++ b/DMS.WPF/ViewModels/DevicesViewModel.cs @@ -1,5 +1,4 @@ -using System.Collections.ObjectModel; -using AutoMapper; +using AutoMapper; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using DMS.Application.DTOs; @@ -7,12 +6,15 @@ using DMS.Application.Interfaces; using DMS.Application.Interfaces.Database; using DMS.Core.Enums; using DMS.Core.Models; +using DMS.Infrastructure.Entities; using DMS.WPF.Interfaces; +using DMS.WPF.ItemViewModel; using DMS.WPF.Services; using DMS.WPF.ViewModels.Dialogs; -using DMS.WPF.ItemViewModel; using iNKORE.UI.WPF.Modern.Common.IconKeys; +using Microsoft.Extensions.DependencyInjection; using ObservableCollections; +using System.Collections.ObjectModel; namespace DMS.WPF.ViewModels; @@ -80,10 +82,10 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable { // 1. 显示添加设备对话框 DeviceItem device = await _dialogService.ShowDialogAsync(new DeviceDialogViewModel() - { - Title = "添加设备", - PrimaryButText = "添加设备" - }); + { + Title = "添加设备", + PrimaryButText = "添加设备" + }); // 如果用户取消或对话框未返回设备,则直接返回 if (device == null) { @@ -105,26 +107,26 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable dto.DeviceMenu = new MenuBean() - { - Header = device.Name, - Icon = SegoeFluentIcons.Devices2.Glyph, - TargetViewKey = nameof(DeviceDetailViewModel), - }; + { + Header = device.Name, + Icon = SegoeFluentIcons.Devices2.Glyph, + TargetViewKey = nameof(DeviceDetailViewModel), + }; if (device.IsAddDefVarTable) { dto.VariableTable = new VariableTable() - { - Name = "默认变量表", - Description = "默认变量表", - IsActive = true - }; + { + Name = "默认变量表", + Description = "默认变量表", + IsActive = true + }; dto.VariableTableMenu = new MenuBean() - { - Header = dto.VariableTable.Name, - Icon = SegoeFluentIcons.DataSense.Glyph, - TargetViewKey = nameof(VariableTableViewModel) - }; + { + Header = dto.VariableTable.Name, + Icon = SegoeFluentIcons.DataSense.Glyph, + TargetViewKey = nameof(VariableTableViewModel) + }; } @@ -138,7 +140,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable if (addDto != null && addDto.Device != null && _notificationService != null) { _notificationService.ShowSuccess($"设备添加成功:{addDto.Device.Name}"); - + } else if (_notificationService != null) { @@ -203,7 +205,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable { try { - + if (SelectedDevice == null) { _notificationService.ShowError("你没有选择任何设备,请选择设备后再点击编辑设备"); @@ -211,9 +213,9 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable } DeviceDialogViewModel deviceDialogViewModel = new DeviceDialogViewModel(SelectedDevice) - { - PrimaryButText = "编辑设备" - }; + { + PrimaryButText = "编辑设备" + }; // 1. 显示设备对话框 DeviceItem device = await _dialogService.ShowDialogAsync(deviceDialogViewModel); // 如果用户取消或对话框未返回设备,则直接返回 @@ -238,60 +240,26 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable { if (SelectedDevice == null) return; - _navigationService.NavigateToAsync(this,new NavigationParameter(nameof(DeviceDetailViewModel),SelectedDevice.Id,NavigationType.Device)); + _navigationService.NavigateToAsync(this, new NavigationParameter(nameof(DeviceDetailViewModel), SelectedDevice.Id, NavigationType.Device)); } [RelayCommand] private async Task AddVariableTable(DeviceItem device) { - if (device == null) return; - try { - VariableTableDialogViewModel variableTableDialogViewModel = new VariableTableDialogViewModel() - { - PrimaryButText = "添加变量表" - }; - // 显示添加变量表对话框 - var VariableTableItem = await _dialogService.ShowDialogAsync(variableTableDialogViewModel); - // 如果用户取消或对话框未返回变量表,则直接返回 - if (VariableTableItem == null) - { - return; - } + if (device is null) return; + DeviceDetailViewModel deviceDetailViewModel = App.Current.Services.GetRequiredService(); + if (deviceDetailViewModel is null) return; + deviceDetailViewModel.AddVariableTableCommand.Execute(device); - VariableTableItem.DeviceId = device.Id; - var tableMenu = new MenuBean() - { - Header = VariableTableItem.Name, - Icon = SegoeFluentIcons.DataSense.Glyph, - TargetViewKey = nameof(VariableTableViewModel) - }; - int addVarTableId = await _wpfDataService.VariableTableDataService.AddVariableTable( - _mapper.Map(VariableTableItem), - tableMenu, true); - - if (addVarTableId > 0) - { - VariableTableItem.Id = addVarTableId; - if (_viewDataService.Devices.TryGetValue(VariableTableItem.DeviceId, out var deviceModel)) - { - VariableTableItem.Device = deviceModel; - deviceModel.VariableTables.Add(VariableTableItem); - _viewDataService.VariableTables.Add(VariableTableItem.Id, VariableTableItem); - } - _notificationService.ShowSuccess($"添加变量表成功:{VariableTableItem.Name}"); - } - else - { - _notificationService.ShowError($"添加变量表失败:{VariableTableItem.Name}!!"); - } } - catch (Exception ex) + catch (Exception e) { - _notificationService.ShowError($"添加变量表时发生错误: {ex.Message}", ex); + _notificationService.ShowError($"添加变量表的过程中发生错误:{e.Message}", e); } + } [RelayCommand] @@ -305,33 +273,9 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable try { - VariableTableDialogViewModel variableTableDialogViewModel - = new VariableTableDialogViewModel(variableTable) - { - PrimaryButText = "编辑变量表" - }; - // 显示变量表对话框 - VariableTableItem updatedVariableTable - = await _dialogService.ShowDialogAsync(variableTableDialogViewModel); - // 如果用户取消或对话框未返回变量表,则直接返回 - if (updatedVariableTable == null) - { - return; - } - - if (await _wpfDataService.VariableDataService.UpdateVariableTable(updatedVariableTable)) - { - _notificationService.ShowSuccess($"编辑变量表成功:{updatedVariableTable.Name}"); - - // Update the properties in the original variable table - variableTable.Name = updatedVariableTable.Name; - variableTable.Description = updatedVariableTable.Description; - variableTable.IsActive = updatedVariableTable.IsActive; - } - else - { - _notificationService.ShowError($"编辑变量表失败:{updatedVariableTable.Name}"); - } + DeviceDetailViewModel deviceDetailViewModel = App.Current.Services.GetRequiredService(); + if (deviceDetailViewModel is null) return; + deviceDetailViewModel.EditVariableTableCommand.Execute(variableTable); } catch (Exception e) { @@ -342,7 +286,7 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable [RelayCommand] private async Task DeleteVariableTable(VariableTableItem variableTable) { - if (variableTable == null) + if (variableTable is null) { _notificationService.ShowError("你没有选择任何变量表,请选择变量表后再点击删除变量表"); return; @@ -350,31 +294,9 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable try { - string message = $"确认要删除变量表名为:{variableTable.Name} \n\n此操作将同时删除该变量表下的所有变量数据,且无法恢复!"; - ConfirmDialogViewModel viewModel = new ConfirmDialogViewModel("删除变量表", message, "删除"); - if (await _dialogService.ShowDialogAsync(viewModel)) - { - var tableName = variableTable.Name; - if (await _wpfDataService.VariableDataService.DeleteVariableTable(variableTable, true)) - { - // Remove from parent device's collection - if (variableTable.Device != null) - { - if (_viewDataService.Devices.TryGetValue(variableTable.DeviceId ,out var device)) - { - device.VariableTables.Remove(variableTable); - } - _viewDataService.VariableTables.Remove(variableTable.Id); - - variableTable.Device.VariableTables.Remove(variableTable); - } - _notificationService.ShowSuccess($"变量表:{tableName},删除成功。"); - } - else - { - _notificationService.ShowError($"变量表:{tableName},删除失败!!!"); - } - } + DeviceDetailViewModel deviceDetailViewModel = App.Current.Services.GetRequiredService(); + if (deviceDetailViewModel is null) return; + deviceDetailViewModel.DeleteVariableTableCommand.Execute(variableTable); } catch (Exception e) {