完成删除设备

This commit is contained in:
2025-07-28 13:06:36 +08:00
parent 442ee3f9c1
commit 61807bfc65
9 changed files with 190 additions and 106 deletions

View File

@@ -497,4 +497,70 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
device.VariableTables.Add(variableTableItemViewModel);
}
}
public void DeleteMenuItem(MenuBeanItemViewModel menuBeanItemViewModel)
{
if (menuBeanItemViewModel == null)
{
return;
}
// 从扁平菜单列表中移除
Menus.Remove(menuBeanItemViewModel);
// 从树形结构中移除
if (menuBeanItemViewModel.ParentId.HasValue && menuBeanItemViewModel.ParentId.Value != 0)
{
// 如果有父菜单从父菜单的Children中移除
var parentMenu = Menus.FirstOrDefault(m => m.Id == menuBeanItemViewModel.ParentId.Value);
parentMenu?.Children.Remove(menuBeanItemViewModel);
}
else
{
// 如果是根菜单从MenuTrees中移除
MenuTrees.Remove(menuBeanItemViewModel);
}
}
public async Task DeleteDeviceById(int selectedDeviceId)
{
var device = Devices.FirstOrDefault(d => d.Id == selectedDeviceId);
if (device != null)
{
// 1. 删除与设备关联的所有变量表及其变量
var variableTablesToDelete = VariableTables.Where(vt => vt.DeviceId == device.Id).ToList();
foreach (var vt in variableTablesToDelete)
{
// 删除与当前变量表关联的所有变量
var variablesToDelete = Variables.Where(v => v.VariableTableId == vt.Id).ToList();
foreach (var variable in variablesToDelete)
{
Variables.Remove(variable);
}
// 删除变量表
VariableTables.Remove(vt);
// 删除与变量表关联的菜单项
var variableTableMenu = Menus.FirstOrDefault(m => m.TargetViewKey == "VariableTableView" && m.Header == vt.Name);
if (variableTableMenu != null)
{
DeleteMenuItem(variableTableMenu);
}
}
// 2. 删除设备
Devices.Remove(device);
// 3. 删除与设备关联的菜单项
var deviceMenu = Menus.FirstOrDefault(m => m.TargetViewKey == "DevicesView" && m.Header == device.Name);
if (deviceMenu != null)
{
DeleteMenuItem(deviceMenu);
}
// 4. 重新构建菜单树以反映变更
// BuildMenuTree();
}
}
}

View File

@@ -14,6 +14,7 @@ namespace DMS.WPF.Services
private static readonly Dictionary<Type, Type> _viewModelViewMap = new Dictionary<Type, Type>
{
{ typeof(DeviceDialogViewModel), typeof(DeviceDialog) },
{ typeof(ConfrimDialogViewModel), typeof(ConfirmDialog) },
// { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here
// ... other dialogs
};
@@ -48,7 +49,7 @@ namespace DMS.WPF.Services
viewModel.CloseRequested += closeHandler;
_ = dialog.ShowAsync();
_ = await dialog.ShowAsync();
return await tcs.Task;
}

View File

@@ -4,7 +4,7 @@ using DMS.Core.Enums;
using DMS.Message;
using HandyControl.Controls;
namespace DMS.Services;
namespace DMS.WPF.Services;
public class GrowlNotificationService : ObservableRecipient, IRecipient<NotificationMessage>
{
@@ -13,73 +13,64 @@ public class GrowlNotificationService : ObservableRecipient, IRecipient<Notifica
IsActive = true;
}
// public void Receive(NotificationMessage message)
// {
// Show(message.Value, message.Type, message.IsGlobal);
// }
//
// public void Show(Models_Notification notification)
// {
// if (notification == null) return;
//
// if (notification.IsGlobal)
// switch (notification.Type)
// {
// case NotificationType.Info:
// Growl.InfoGlobal(notification.Message);
// break;
// case NotificationType.Error:
// Growl.ErrorGlobal(notification.Message);
// break;
// case NotificationType.Warning:
// Growl.WarningGlobal(notification.Message);
// break;
// case NotificationType.Success:
// Growl.SuccessGlobal(notification.Message);
// break;
// case NotificationType.Fatal:
// Growl.FatalGlobal(notification.Message);
// break;
// case NotificationType.Clear:
// Growl.ClearGlobal();
// break;
// default:
// Growl.InfoGlobal(notification.Message);
// break;
// }
// else
// switch (notification.Type)
// {
// case NotificationType.Info:
// Growl.Info(notification.Message);
// break;
// case NotificationType.Error:
// Growl.Error(notification.Message);
// break;
// case NotificationType.Warning:
// Growl.Warning(notification.Message);
// break;
// case NotificationType.Success:
// Growl.Success(notification.Message);
// break;
// case NotificationType.Fatal:
// Growl.Fatal(notification.Message);
// break;
// case NotificationType.Clear:
// Growl.Clear();
// break;
// default:
// Growl.Info(notification.Message);
// break;
// }
// }
//
// public void Show(string message, NotificationType type = NotificationType.Info, bool IsGlobal = true)
// {
// Show(new Models_Notification { Message = message, Type = type, IsGlobal = IsGlobal });
// }
public void Receive(NotificationMessage message)
{
Show(message.Value, message.Type, message.IsGlobal);
}
public void Show(string message, NotificationType type = NotificationType.Info, bool IsGlobal = true)
{
if (IsGlobal)
switch (type)
{
case NotificationType.Info:
Growl.InfoGlobal(message);
break;
case NotificationType.Error:
Growl.ErrorGlobal(message);
break;
case NotificationType.Warning:
Growl.WarningGlobal(message);
break;
case NotificationType.Success:
Growl.SuccessGlobal(message);
break;
case NotificationType.Fatal:
Growl.FatalGlobal(message);
break;
case NotificationType.Clear:
Growl.ClearGlobal();
break;
default:
Growl.InfoGlobal(message);
break;
}
else
switch (type)
{
case NotificationType.Info:
Growl.Info(message);
break;
case NotificationType.Error:
Growl.Error(message);
break;
case NotificationType.Warning:
Growl.Warning(message);
break;
case NotificationType.Success:
Growl.Success(message);
break;
case NotificationType.Fatal:
Growl.Fatal(message);
break;
case NotificationType.Clear:
Growl.Clear();
break;
default:
Growl.Info(message);
break;
}
}
}