diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index 5905faa..f4fbbba 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -114,7 +114,7 @@ public partial class App : System.Windows.Application // services.AddSingleton(); // services.AddSingleton(); // //services.AddSingleton(); - // services.AddSingleton(); + services.AddSingleton(); // services.AddHostedService(); // services.AddHostedService(); // services.AddHostedService(); diff --git a/DMS.WPF/Services/DataServices.cs b/DMS.WPF/Services/DataServices.cs index 8190fcf..4782c24 100644 --- a/DMS.WPF/Services/DataServices.cs +++ b/DMS.WPF/Services/DataServices.cs @@ -497,4 +497,70 @@ public partial class DataServices : ObservableRecipient, IRecipient 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(); + } + } } \ No newline at end of file diff --git a/DMS.WPF/Services/DialogService.cs b/DMS.WPF/Services/DialogService.cs index 2367712..7a9c773 100644 --- a/DMS.WPF/Services/DialogService.cs +++ b/DMS.WPF/Services/DialogService.cs @@ -14,6 +14,7 @@ namespace DMS.WPF.Services private static readonly Dictionary _viewModelViewMap = new Dictionary { { 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; } diff --git a/DMS.WPF/Services/GrowlNotificationService.cs b/DMS.WPF/Services/GrowlNotificationService.cs index fcd9cd3..56ae342 100644 --- a/DMS.WPF/Services/GrowlNotificationService.cs +++ b/DMS.WPF/Services/GrowlNotificationService.cs @@ -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 { @@ -13,73 +13,64 @@ public class GrowlNotificationService : ObservableRecipient, IRecipient(addDto.DeviceMenu)); DataServices.AddVariableTable(_mapper.Map(addDto.VariableTable)); DataServices.AddMenuItem(_mapper.Map(addDto.VariableTableMenu)); - + NotificationHelper.ShowSuccess($"设备添加成功:{addDto.Device.Name}"); } catch (Exception e) @@ -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); + } } /// diff --git a/DMS.WPF/ViewModels/Dialogs/ConfrimDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/ConfrimDialogViewModel.cs index 41e3a74..3791c4e 100644 --- a/DMS.WPF/ViewModels/Dialogs/ConfrimDialogViewModel.cs +++ b/DMS.WPF/ViewModels/Dialogs/ConfrimDialogViewModel.cs @@ -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 { - [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); + } } \ No newline at end of file diff --git a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs index 40dadb6..f732e8c 100644 --- a/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs +++ b/DMS.WPF/ViewModels/Items/DeviceItemViewModel.cs @@ -12,7 +12,7 @@ namespace DMS.WPF.ViewModels.Items; /// public partial class DeviceItemViewModel : ObservableObject { - public int Id { get; } + public int Id { get; set; } [ObservableProperty] private string _name; diff --git a/DMS.WPF/Views/Dialogs/ConfirmDialog.xaml b/DMS.WPF/Views/Dialogs/ConfirmDialog.xaml index 56344cb..4243ee0 100644 --- a/DMS.WPF/Views/Dialogs/ConfirmDialog.xaml +++ b/DMS.WPF/Views/Dialogs/ConfirmDialog.xaml @@ -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"> - - - + + + + + + + + diff --git a/DMS.WPF/Views/Dialogs/ConfirmDialog.xaml.cs b/DMS.WPF/Views/Dialogs/ConfirmDialog.xaml.cs index 4aeb3b7..df84eb2 100644 --- a/DMS.WPF/Views/Dialogs/ConfirmDialog.xaml.cs +++ b/DMS.WPF/Views/Dialogs/ConfirmDialog.xaml.cs @@ -6,9 +6,8 @@ namespace DMS.WPF.Views.Dialogs; public partial class ConfirmDialog : ContentDialog { - public ConfirmDialog(ConfrimDialogViewModel viewModel) + public ConfirmDialog() { InitializeComponent(); - this.DataContext = viewModel; } } \ No newline at end of file