Files
DMS/ViewModels/DevicesViewModel.cs

153 lines
5.2 KiB
C#
Raw Normal View History

using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Extensions.Logging;
2025-07-02 22:07:16 +08:00
using PMSWPF.Data;
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;
2025-07-03 13:17:25 +08:00
/// <summary>
/// 设备管理视图模型,负责设备的增删改查操作。
/// </summary>
public partial class DevicesViewModel : ViewModelBase
2025-06-10 20:55:39 +08:00
{
2025-07-03 13:17:25 +08:00
private readonly DataServices _dataServices;
private readonly IDialogService _dialogService;
private readonly DeviceRepository _deviceRepository;
private readonly ILogger<DevicesViewModel> _logger;
private readonly MenuRepository _menuRepository;
2025-07-02 22:07:16 +08:00
private readonly VarTableRepository _varTableRepository;
2025-07-03 13:17:25 +08:00
/// <summary>
/// 设备列表。
/// </summary>
[ObservableProperty]
private ObservableCollection<Device> _devices;
/// <summary>
/// 当前选中的设备。
/// </summary>
[ObservableProperty]
private Device _selectedDevice;
/// <summary>
/// 初始化 <see cref="DevicesViewModel"/> 类的新实例。
/// </summary>
/// <param name="logger">日志记录器。</param>
/// <param name="dialogService">对话框服务。</param>
/// <param name="dataServices">数据服务。</param>
public DevicesViewModel(
ILogger<DevicesViewModel> logger, IDialogService dialogService, DataServices dataServices
)
{
_deviceRepository = new DeviceRepository();
2025-07-02 22:07:16 +08:00
_varTableRepository = new VarTableRepository();
_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 += (sender, devices) => { Devices = new ObservableCollection<Device>(devices); };
}
2025-07-01 21:34:20 +08:00
/// <summary>
2025-07-03 13:17:25 +08:00
/// 添加设备命令。
2025-07-02 22:07:16 +08:00
/// </summary>
[RelayCommand]
public async void AddDevice()
{
2025-07-04 18:33:48 +08:00
try
{
2025-07-04 18:33:48 +08:00
// 1. 显示添加设备对话框
var device = await _dialogService.ShowAddDeviceDialog();
// 如果用户取消或对话框未返回设备,则直接返回
if (device == null)
{
_logger.LogInformation("用户取消了添加设备操作。");
return;
}
2025-07-03 13:17:25 +08:00
2025-07-04 18:33:48 +08:00
await _deviceRepository.Add(device);
}
catch (Exception e)
{
NotificationHelper.ShowMessage($"添加设备的过程中发生错误:{e.Message}", NotificationType.Error);
_logger.LogError($"添加设备的过程中发生错误:{e}");
}
2025-06-10 22:13:06 +08:00
}
2025-07-01 21:34:20 +08:00
/// <summary>
2025-07-03 13:17:25 +08:00
/// 删除设备命令。
2025-07-01 21:34:20 +08:00
/// </summary>
[RelayCommand]
2025-07-03 13:17:25 +08:00
public async void DeleteDevice()
2025-07-01 21:34:20 +08:00
{
2025-07-04 18:33:48 +08:00
2025-07-01 21:34:20 +08:00
try
{
if (SelectedDevice == null)
{
2025-07-03 13:17:25 +08:00
NotificationHelper.ShowMessage("你没有选择任何设备,请选择设备后再点击删除设备", NotificationType.Error);
2025-07-01 21:34:20 +08:00
return;
}
2025-07-03 13:17:25 +08:00
string msg = $"确认要删除设备名为:{SelectedDevice.Name}";
var isDel = await _dialogService.ShowConfrimeDialog("删除设备", msg, "删除设备");
if (isDel)
2025-07-02 22:07:16 +08:00
{
2025-07-04 18:33:48 +08:00
2025-07-03 13:17:25 +08:00
// 删除设备
2025-07-04 18:33:48 +08:00
await _deviceRepository.Delete(SelectedDevice ,_dataServices.MenuTrees);
2025-07-01 21:34:20 +08:00
MessageHelper.SendLoadMessage(LoadTypes.Menu);
MessageHelper.SendLoadMessage(LoadTypes.Devices);
2025-07-03 13:17:25 +08:00
NotificationHelper.ShowMessage($"删除设备成功,设备名:{SelectedDevice.Name}", NotificationType.Success);
2025-07-01 21:34:20 +08:00
}
}
catch (Exception e)
{
2025-07-03 13:17:25 +08:00
NotificationHelper.ShowMessage($"删除设备的过程中发生错误:{e.Message}", NotificationType.Error);
_logger.LogError($"删除设备的过程中发生错误:{e}");
2025-07-01 21:34:20 +08:00
}
}
2025-07-03 13:17:25 +08:00
/// <summary>
/// 编辑设备命令。
/// </summary>
2025-07-01 21:34:20 +08:00
[RelayCommand]
2025-07-03 13:17:25 +08:00
public async void EditDevice()
2025-07-01 21:34:20 +08:00
{
try
{
if (SelectedDevice == null)
{
2025-07-03 13:17:25 +08:00
NotificationHelper.ShowMessage("你没有选择任何设备,请选择设备后再点击编辑设备", NotificationType.Error);
2025-07-01 21:34:20 +08:00
return;
}
2025-07-03 13:17:25 +08:00
var editDievce = await _dialogService.ShowEditDeviceDialog(SelectedDevice);
if (editDievce != null)
2025-07-01 21:34:20 +08:00
{
2025-07-03 13:17:25 +08:00
// 更新菜单
var res = await _deviceRepository.Edit(editDievce);
var menu = DataServicesHelper.FindMenusForDevice(editDievce, _dataServices.MenuTrees);
if (menu != null)
2025-07-03 13:17:25 +08:00
await _menuRepository.Edit(menu);
2025-07-02 22:07:16 +08:00
2025-07-01 21:34:20 +08:00
MessageHelper.SendLoadMessage(LoadTypes.Menu);
MessageHelper.SendLoadMessage(LoadTypes.Devices);
}
}
catch (Exception e)
{
2025-07-03 13:17:25 +08:00
NotificationHelper.ShowMessage($"编辑设备的过程中发生错误:{e.Message}", NotificationType.Error);
_logger.LogError($"编辑设备的过程中发生错误:{e}");
2025-07-01 21:34:20 +08:00
}
}
2025-06-10 20:55:39 +08:00
}