Files
DMS/ViewModels/DevicesViewModel.cs

166 lines
5.7 KiB
C#
Raw Normal View History

using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
using PMSWPF.Data.Repositories;
using PMSWPF.Enums;
using PMSWPF.Helper;
using PMSWPF.Models;
using PMSWPF.Services;
2025-06-10 20:55:39 +08:00
namespace PMSWPF.ViewModels;
public partial class DevicesViewModel : ViewModelBase
2025-06-10 20:55:39 +08:00
{
private readonly DeviceRepository _deviceRepository;
private readonly ILogger<DevicesViewModel> _logger;
private readonly IDialogService _dialogService;
private readonly DataServices _dataServices;
2025-07-01 21:34:20 +08:00
[ObservableProperty] private ObservableCollection<Device> _devices;
2025-07-01 21:34:20 +08:00
[ObservableProperty] private Device _selectedDevice;
private readonly MenuRepository _menuRepository;
public DevicesViewModel(
ILogger<DevicesViewModel> logger, IDialogService dialogService, DataServices dataServices
)
{
_deviceRepository = new DeviceRepository();
_menuRepository = new MenuRepository();
_logger = logger;
_dialogService = dialogService;
_dataServices = dataServices;
2025-07-01 21:34:20 +08:00
2025-06-30 13:06:51 +08:00
MessageHelper.SendLoadMessage(LoadTypes.Devices);
_dataServices.OnDeviceListChanged += (devices) => { Devices = new ObservableCollection<Device>(devices); };
}
2025-07-01 21:34:20 +08:00
/// <summary>
/// 添加设备
/// </summary>
[RelayCommand]
public async void AddDevice()
2025-06-10 22:13:06 +08:00
{
Device device = null;
try
{
device = await _dialogService.ShowAddDeviceDialog();
if (device != null)
{
2025-07-01 21:34:20 +08:00
device = await _deviceRepository.Add(device);
if (device != null)
{
var msg = $"添加设备成功:{device.Name}";
_logger.LogInformation(msg);
2025-07-01 21:34:20 +08:00
2025-06-29 12:37:35 +08:00
bool addMenuRes = await _menuRepository.AddDeviceMenu(device);
if (addMenuRes)
{
// 通知更新菜单
2025-06-30 13:06:51 +08:00
MessageHelper.SendLoadMessage(LoadTypes.Menu);
MessageHelper.SendLoadMessage(LoadTypes.Devices);
NotificationHelper.ShowMessage(msg, NotificationType.Success);
}
else
{
var msgerr = $"给设备添加菜单失败:{device.Name}";
_logger.LogInformation(msgerr);
NotificationHelper.ShowMessage(msgerr, NotificationType.Error);
}
}
else
{
var msg = $"添加设备失败:{device.Name}";
_logger.LogInformation(msg);
NotificationHelper.ShowMessage(msg, NotificationType.Error);
}
}
}
catch (Exception e)
{
var msg = $"添加设备失败:{e.Message}";
_logger.LogError(msg);
2025-06-30 13:06:51 +08:00
NotificationHelper.ShowMessage(msg, NotificationType.Error);
}
2025-06-10 22:13:06 +08:00
}
2025-07-01 21:34:20 +08:00
/// <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)
{
// 更新菜单
2025-07-01 21:34:20 +08:00
var res = await _deviceRepository.Edit(editDievce);
var menu = DataServicesHelper.FindMenusForDevice(editDievce, _dataServices.MenuTrees);
if (menu != null)
await _menuRepository.Edit(menu);
2025-07-01 21:34:20 +08:00
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)
{
// 删除设备
await _deviceRepository.DeleteById(SelectedDevice.Id);
// 删除菜单
var menu = DataServicesHelper.FindMenusForDevice(SelectedDevice, _dataServices.MenuTrees);
if (menu != null)
await _menuRepository.DeleteMenu(menu);
2025-07-01 21:34:20 +08:00
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}");
2025-07-01 21:34:20 +08:00
}
}
[RelayCommand]
public void NavigateVt()
{
}
public override async void OnLoaded()
{
}
2025-06-10 20:55:39 +08:00
}