完成删除设备

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

@@ -114,7 +114,7 @@ public partial class App : System.Windows.Application
// services.AddSingleton<IDeviceDataService, DeviceDataService>(); // services.AddSingleton<IDeviceDataService, DeviceDataService>();
// services.AddSingleton<NavgatorServices>(); // services.AddSingleton<NavgatorServices>();
// //services.AddSingleton<IDialogService, DialogService>(); // //services.AddSingleton<IDialogService, DialogService>();
// services.AddSingleton<GrowlNotificationService>(); services.AddSingleton<GrowlNotificationService>();
// services.AddHostedService<S7BackgroundService>(); // services.AddHostedService<S7BackgroundService>();
// services.AddHostedService<OpcUaBackgroundService>(); // services.AddHostedService<OpcUaBackgroundService>();
// services.AddHostedService<DMS.Infrastructure.Services.MqttBackgroundService>(); // services.AddHostedService<DMS.Infrastructure.Services.MqttBackgroundService>();

View File

@@ -497,4 +497,70 @@ public partial class DataServices : ObservableRecipient, IRecipient<LoadMessage>
device.VariableTables.Add(variableTableItemViewModel); 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> private static readonly Dictionary<Type, Type> _viewModelViewMap = new Dictionary<Type, Type>
{ {
{ typeof(DeviceDialogViewModel), typeof(DeviceDialog) }, { typeof(DeviceDialogViewModel), typeof(DeviceDialog) },
{ typeof(ConfrimDialogViewModel), typeof(ConfirmDialog) },
// { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here // { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here
// ... other dialogs // ... other dialogs
}; };
@@ -48,7 +49,7 @@ namespace DMS.WPF.Services
viewModel.CloseRequested += closeHandler; viewModel.CloseRequested += closeHandler;
_ = dialog.ShowAsync(); _ = await dialog.ShowAsync();
return await tcs.Task; return await tcs.Task;
} }

View File

@@ -4,7 +4,7 @@ using DMS.Core.Enums;
using DMS.Message; using DMS.Message;
using HandyControl.Controls; using HandyControl.Controls;
namespace DMS.Services; namespace DMS.WPF.Services;
public class GrowlNotificationService : ObservableRecipient, IRecipient<NotificationMessage> public class GrowlNotificationService : ObservableRecipient, IRecipient<NotificationMessage>
{ {
@@ -13,73 +13,64 @@ public class GrowlNotificationService : ObservableRecipient, IRecipient<Notifica
IsActive = true; 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) 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;
}
}
} }

View File

@@ -170,31 +170,36 @@ public partial class DevicesViewModel : ViewModelBase, INavigatable
[RelayCommand] [RelayCommand]
public async void DeleteDevice() public async void DeleteDevice()
{ {
// try try
// { {
// if (SelectedDevice == null) if (SelectedDevice == null)
// { {
// NotificationHelper.ShowError("你没有选择任何设备,请选择设备后再点击删除设备"); NotificationHelper.ShowError("你没有选择任何设备,请选择设备后再点击删除设备");
// return; return;
// } }
//
// string msg = $"确认要删除设备名为:{SelectedDevice.Name}"; ConfrimDialogViewModel viewModel = new ConfrimDialogViewModel();
// var isDel = await _dialogService.ShowConfrimeDialog("删除设备", msg, "删除设备"); viewModel.Message = $"确认要删除设备名为:{SelectedDevice.Name}";
// if (isDel) viewModel.Title = "删除设备";
// { viewModel.PrimaryButContent = "删除";
//
// // 删除设备 var resViewModel = await _dialogService.ShowDialogAsync(viewModel);
// await _deviceRepository.DeleteAsync(SelectedDevice ,_dataServices.MenuTrees); if (resViewModel.IsPrimaryButton)
// {
// MessageHelper.SendLoadMessage(LoadTypes.Menu); var isDel = await _deviceAppService.DeleteDeviceByIdAsync(SelectedDevice.Id);
// MessageHelper.SendLoadMessage(LoadTypes.Devices); if (isDel)
// NotificationHelper.ShowSuccess($"删除设备成功,设备名:{SelectedDevice.Name}"); {
// } // 更新界面
// } DataServices.DeleteDeviceById(SelectedDevice.Id);
// catch (Exception e)
// { NotificationHelper.ShowSuccess($"删除设备成功,设备名:{SelectedDevice.Name}");
// NotificationHelper.ShowError($"删除设备的过程中发生错误:{e.Message}", e); }
// } }
}
catch (Exception e)
{
NotificationHelper.ShowError($"删除设备的过程中发生错误:{e.Message}", e);
}
} }
/// <summary> /// <summary>

View File

@@ -1,11 +1,27 @@
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace DMS.WPF.ViewModels.Dialogs; namespace DMS.WPF.ViewModels.Dialogs;
public partial class ConfrimDialogViewModel : ObservableObject public partial class ConfrimDialogViewModel : DialogViewModelBase<ConfrimDialogViewModel>
{ {
[ObservableProperty] private string _title; public bool IsPrimaryButton { get; set; }
[ObservableProperty] private string primaryButtonText;
[ObservableProperty] private string message; [ObservableProperty]
private string message;
[RelayCommand]
public void ParimaryButton()
{
IsPrimaryButton=true;
Close(this);
}
[RelayCommand]
public void CancleButton()
{
IsPrimaryButton=false;
Close(this);
}
} }

View File

@@ -12,7 +12,7 @@ namespace DMS.WPF.ViewModels.Items;
/// </summary> /// </summary>
public partial class DeviceItemViewModel : ObservableObject public partial class DeviceItemViewModel : ObservableObject
{ {
public int Id { get; } public int Id { get; set; }
[ObservableProperty] [ObservableProperty]
private string _name; private string _name;

View File

@@ -6,18 +6,24 @@
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf" xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:vmd="clr-namespace:DMS.WPF.ViewModels.Dialogs" xmlns:vmd="clr-namespace:DMS.WPF.ViewModels.Dialogs"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:ex="clr-namespace:DMS.Extensions" xmlns:ex="clr-namespace:DMS.Extensions"
xmlns:en="clr-namespace:DMS.Core.Enums" xmlns:en="clr-namespace:DMS.Core.Enums"
Title="{Binding Title}" Title="{Binding Title}"
CloseButtonText="取消" CloseButtonText="取消"
DefaultButton="Primary" DefaultButton="Primary"
PrimaryButtonText="{Binding PrimaryButtonText}" PrimaryButtonText="{Binding PrimaryButContent}"
Background="#fff" Background="#fff"
d:DataContext="{d:DesignInstance vmd:ConfrimDialogViewModel}" d:DataContext="{d:DesignInstance vmd:ConfrimDialogViewModel}"
mc:Ignorable="d"> mc:Ignorable="d">
<ui:ContentDialog.Resources> <i:Interaction.Triggers>
<i:EventTrigger EventName="PrimaryButtonClick">
</ui:ContentDialog.Resources> <i:InvokeCommandAction Command="{Binding ParimaryButtonCommand}" />
</i:EventTrigger>
<i:EventTrigger EventName="CloseButtonClick">
<i:InvokeCommandAction Command="{Binding CancleButtonCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid Width="360" <Grid Width="360"
Margin="10"> Margin="10">

View File

@@ -6,9 +6,8 @@ namespace DMS.WPF.Views.Dialogs;
public partial class ConfirmDialog : ContentDialog public partial class ConfirmDialog : ContentDialog
{ {
public ConfirmDialog(ConfrimDialogViewModel viewModel) public ConfirmDialog()
{ {
InitializeComponent(); InitializeComponent();
this.DataContext = viewModel;
} }
} }