完成编辑设备和删除设备

This commit is contained in:
2025-07-01 21:34:20 +08:00
parent 0110ba9492
commit 663e4fda0c
23 changed files with 504 additions and 235 deletions

View File

@@ -1,14 +1,10 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using iNKORE.UI.WPF.Modern.Common.IconKeys;
using Microsoft.Extensions.Logging;
using PMSWPF.Data.Repositories;
using PMSWPF.Enums;
using PMSWPF.Excptions;
using PMSWPF.Helper;
using PMSWPF.Message;
using PMSWPF.Models;
using PMSWPF.Services;
@@ -21,7 +17,9 @@ public partial class DevicesViewModel : ViewModelBase
private readonly IDialogService _dialogService;
private readonly DataServices _dataServices;
[ObservableProperty] private ObservableCollection<Device> _devices;
[ObservableProperty] private Device _selectedDevice;
private readonly MenuRepository _menuRepository;
public DevicesViewModel(
@@ -33,15 +31,14 @@ public partial class DevicesViewModel : ViewModelBase
_logger = logger;
_dialogService = dialogService;
_dataServices = dataServices;
MessageHelper.SendLoadMessage(LoadTypes.Devices);
_dataServices.OnDeviceListChanged += (devices) => { Devices = new ObservableCollection<Device>(devices); };
}
public async Task OnLoadedAsync()
{
}
/// <summary>
/// 添加设备
/// </summary>
[RelayCommand]
public async void AddDevice()
{
@@ -51,12 +48,12 @@ public partial class DevicesViewModel : ViewModelBase
device = await _dialogService.ShowAddDeviceDialog();
if (device != null)
{
device= await _deviceRepository.Add(device);
if (device!=null)
device = await _deviceRepository.Add(device);
if (device != null)
{
var msg = $"添加设备成功:{device.Name}";
_logger.LogInformation(msg);
bool addMenuRes = await _menuRepository.AddDeviceMenu(device);
if (addMenuRes)
{
@@ -88,6 +85,67 @@ public partial class DevicesViewModel : ViewModelBase
}
}
/// <summary>
/// 编辑设备
/// </summary>
[RelayCommand]
public async void EditDevice()
{
try
{
if (SelectedDevice == null)
{
NotificationHelper.ShowMessage("你没有选择任何设备,请选择设备后再点击编辑设备", NotificationType.Error);
return;
}
var editDievce = await _dialogService.ShowEditDeviceDialog(SelectedDevice);
if (editDievce != null)
{
var res = await _deviceRepository.Edit(editDievce);
await _dataServices.UpdateMenuForDevice(editDievce);
MessageHelper.SendLoadMessage(LoadTypes.Menu);
MessageHelper.SendLoadMessage(LoadTypes.Devices);
}
}
catch (Exception e)
{
NotificationHelper.ShowMessage($"编辑设备的过程中发生错误:{e.Message}", NotificationType.Error);
_logger.LogError($"编辑设备的过程中发生错误:{e}");
}
}
[RelayCommand]
public async void DeleteDevice()
{
try
{
if (SelectedDevice == null)
{
NotificationHelper.ShowMessage("你没有选择任何设备,请选择设备后再点击删除设备", NotificationType.Error);
return;
}
string msg = $"确认要删除设备名为:{SelectedDevice.Name}";
var isDel = await _dialogService.ShowConfrimeDialog("删除设备", msg, "删除设备");
if (isDel)
{
var defDeviceRes = await _deviceRepository.DeleteById(SelectedDevice.Id);
var defMenuRes = await _dataServices.DeleteMenuForDevice(SelectedDevice);
MessageHelper.SendLoadMessage(LoadTypes.Menu);
MessageHelper.SendLoadMessage(LoadTypes.Devices);
NotificationHelper.ShowMessage($"删除设备成功,设备名:{SelectedDevice.Name}", NotificationType.Success);
}
}
catch (Exception e)
{
NotificationHelper.ShowMessage($"编辑设备的过程中发生错误:{e.Message}", NotificationType.Error);
_logger.LogError($"编辑设备的过程中发生错误:{e}");
}
}
[RelayCommand]
public void NavigateVt()
{
@@ -96,6 +154,5 @@ public partial class DevicesViewModel : ViewModelBase
public override async void OnLoaded()
{
await OnLoadedAsync();
}
}

View File

@@ -0,0 +1,11 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace PMSWPF.ViewModels.Dialogs;
public partial class ConfrimDialogViewModel : ObservableObject
{
[ObservableProperty] private string _title;
[ObservableProperty] private string primaryButtonText;
[ObservableProperty] private string message;
}

View File

@@ -1,6 +1,5 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using PMSWPF.Extensions;
using PMSWPF.Models;
namespace PMSWPF.ViewModels.Dialogs;
@@ -9,9 +8,9 @@ public partial class DeviceDialogViewModel : ObservableObject
{
[ObservableProperty]
private Device _device;
[ObservableProperty] private string title = "添加设备";
[ObservableProperty] private string title ;
[ObservableProperty] private string primaryButContent ;
public DeviceDialogViewModel(Device device)
{

View File

@@ -1,12 +1,8 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using PMSWPF.Data.Entities;
using PMSWPF.Data.Repositories;
using PMSWPF.Enums;
using PMSWPF.Helper;
using PMSWPF.Message;
using PMSWPF.Models;
using PMSWPF.Services;
@@ -35,7 +31,7 @@ public partial class MainViewModel : ViewModelBase
// 发送消息加载数据
MessageHelper.SendLoadMessage(LoadTypes.All);
// 当菜单加载成功后,在前台显示菜单
dataServices.OnMenuListChanged += (menus) =>
dataServices.OnMenuTreeListChanged += (menus) =>
{
Menus = new ObservableCollection<MenuBean>(menus);
};