完成删除设备

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<NavgatorServices>();
// //services.AddSingleton<IDialogService, DialogService>();
// services.AddSingleton<GrowlNotificationService>();
services.AddSingleton<GrowlNotificationService>();
// services.AddHostedService<S7BackgroundService>();
// services.AddHostedService<OpcUaBackgroundService>();
// services.AddHostedService<DMS.Infrastructure.Services.MqttBackgroundService>();

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;
}
}
}
}

View File

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

View File

@@ -1,11 +1,27 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace DMS.WPF.ViewModels.Dialogs;
public partial class ConfrimDialogViewModel : ObservableObject
public partial class ConfrimDialogViewModel : DialogViewModelBase<ConfrimDialogViewModel>
{
[ObservableProperty] private string _title;
[ObservableProperty] private string primaryButtonText;
public bool IsPrimaryButton { get; set; }
[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>
public partial class DeviceItemViewModel : ObservableObject
{
public int Id { get; }
public int Id { get; set; }
[ObservableProperty]
private string _name;

View File

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

View File

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