2025-06-13 18:54:17 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2025-06-23 13:42:02 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
using PMSWPF.Data.Repositories;
|
2025-06-14 19:34:12 +08:00
|
|
|
|
using PMSWPF.Enums;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
using PMSWPF.Helper;
|
2025-06-12 13:15:55 +08:00
|
|
|
|
using PMSWPF.Models;
|
|
|
|
|
|
using PMSWPF.Services;
|
2025-06-10 20:55:39 +08:00
|
|
|
|
|
2025-06-12 13:15:55 +08:00
|
|
|
|
namespace PMSWPF.ViewModels;
|
|
|
|
|
|
|
|
|
|
|
|
public partial class DevicesViewModel : ViewModelBase
|
2025-06-10 20:55:39 +08:00
|
|
|
|
{
|
2025-06-26 19:36:27 +08:00
|
|
|
|
private readonly DeviceRepository _deviceRepository;
|
2025-06-23 13:42:02 +08:00
|
|
|
|
private readonly ILogger<DevicesViewModel> _logger;
|
2025-06-26 19:36:27 +08:00
|
|
|
|
private readonly IDialogService _dialogService;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
private readonly DataServices _dataServices;
|
2025-06-14 19:34:12 +08:00
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
|
[ObservableProperty] private ObservableCollection<Device> _devices;
|
2025-07-01 21:34:20 +08:00
|
|
|
|
[ObservableProperty] private Device _selectedDevice;
|
2025-06-26 19:36:27 +08:00
|
|
|
|
private readonly MenuRepository _menuRepository;
|
2025-06-23 17:01:06 +08:00
|
|
|
|
|
2025-06-26 19:36:27 +08:00
|
|
|
|
public DevicesViewModel(
|
2025-06-28 19:32:51 +08:00
|
|
|
|
ILogger<DevicesViewModel> logger, IDialogService dialogService, DataServices dataServices
|
2025-06-23 17:01:06 +08:00
|
|
|
|
)
|
2025-06-12 13:15:55 +08:00
|
|
|
|
{
|
2025-06-26 19:36:27 +08:00
|
|
|
|
_deviceRepository = new DeviceRepository();
|
|
|
|
|
|
_menuRepository = new MenuRepository();
|
2025-06-23 13:42:02 +08:00
|
|
|
|
_logger = logger;
|
2025-06-26 19:36:27 +08:00
|
|
|
|
_dialogService = dialogService;
|
2025-06-28 19:32:51 +08:00
|
|
|
|
_dataServices = dataServices;
|
2025-07-01 21:34:20 +08:00
|
|
|
|
|
2025-06-30 13:06:51 +08:00
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Devices);
|
2025-06-28 19:32:51 +08:00
|
|
|
|
_dataServices.OnDeviceListChanged += (devices) => { Devices = new ObservableCollection<Device>(devices); };
|
2025-06-12 13:15:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-02 18:33:08 +08:00
|
|
|
|
// /// <summary>
|
|
|
|
|
|
// /// 添加设备
|
|
|
|
|
|
// /// </summary>
|
|
|
|
|
|
// [RelayCommand]
|
|
|
|
|
|
// public async void AddDevice()
|
|
|
|
|
|
// {
|
|
|
|
|
|
// Device device = null;
|
|
|
|
|
|
// try
|
|
|
|
|
|
// {
|
|
|
|
|
|
// device = await _dialogService.ShowAddDeviceDialog();
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// // 通知更新菜单
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
// NotificationHelper.ShowMessage(msg, NotificationType.Error);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
2025-07-01 21:34:20 +08:00
|
|
|
|
/// <summary>
|
2025-07-02 18:33:08 +08:00
|
|
|
|
/// 辅助方法:处理成功通知和日志
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void HandleOperationSuccess(string entityType, string operation, string entityName)
|
|
|
|
|
|
{
|
|
|
|
|
|
var message = $"{entityType}{operation}成功:{entityName}";
|
|
|
|
|
|
_logger.LogInformation(message);
|
|
|
|
|
|
NotificationHelper.ShowMessage(message, NotificationType.Success);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 辅助方法:处理失败通知和日志
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private void HandleOperationFailure(string entityType, string operation, string entityName, Exception ex = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var message = $"{entityType}{operation}失败:{entityName}";
|
|
|
|
|
|
if (ex != null)
|
2025-06-10 22:13:06 +08:00
|
|
|
|
{
|
2025-07-02 18:33:08 +08:00
|
|
|
|
_logger.LogError(ex, message);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.LogError(message);
|
|
|
|
|
|
}
|
|
|
|
|
|
NotificationHelper.ShowMessage(message, NotificationType.Error);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 添加设备
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public async void AddDevice()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 1. 显示添加设备对话框
|
|
|
|
|
|
var device = await _dialogService.ShowAddDeviceDialog();
|
|
|
|
|
|
// 如果用户取消或对话框未返回设备,则直接返回
|
|
|
|
|
|
if (device == null)
|
2025-06-12 18:41:46 +08:00
|
|
|
|
{
|
2025-07-02 18:33:08 +08:00
|
|
|
|
_logger.LogInformation("用户取消了添加设备操作。");
|
|
|
|
|
|
return;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
}
|
2025-07-02 18:33:08 +08:00
|
|
|
|
|
|
|
|
|
|
// 2. 将设备添加到数据库
|
|
|
|
|
|
var addedDevice = await _deviceRepository.Add(device);
|
|
|
|
|
|
// 如果数据库添加失败
|
|
|
|
|
|
if (addedDevice == null)
|
2025-06-12 18:41:46 +08:00
|
|
|
|
{
|
2025-07-02 18:33:08 +08:00
|
|
|
|
HandleOperationFailure("设备", "添加", device.Name);
|
|
|
|
|
|
return; // 提前返回
|
2025-06-12 18:41:46 +08:00
|
|
|
|
}
|
2025-07-02 18:33:08 +08:00
|
|
|
|
|
|
|
|
|
|
// 3. 设备成功添加到数据库,进行菜单添加
|
|
|
|
|
|
// 这里立即发出成功的通知和日志
|
|
|
|
|
|
HandleOperationSuccess("设备", "添加", addedDevice.Name);
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 为新设备添加菜单
|
|
|
|
|
|
bool menuAddedSuccessfully = await _menuRepository.AddDeviceMenu(addedDevice);
|
|
|
|
|
|
if (menuAddedSuccessfully)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 菜单也添加成功,通知 UI 更新
|
|
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Menu);
|
|
|
|
|
|
MessageHelper.SendLoadMessage(LoadTypes.Devices);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 菜单添加失败,通知用户并记录日志
|
|
|
|
|
|
HandleOperationFailure("设备", "添加菜单", addedDevice.Name);
|
|
|
|
|
|
// 考虑:如果菜单添加失败,是否需要回滚设备添加?
|
|
|
|
|
|
// 例如:await _deviceRepository.Delete(addedDevice.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 捕获并记录所有未预期的异常
|
|
|
|
|
|
_logger.LogError(e, "在添加设备过程中发生未预期错误。");
|
|
|
|
|
|
NotificationHelper.ShowMessage($"添加设备失败:{e.Message}", NotificationType.Error);
|
2025-06-10 22:13:06 +08:00
|
|
|
|
}
|
2025-07-02 18:33:08 +08:00
|
|
|
|
}
|
2025-06-13 18:54:17 +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-02 12:01:20 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 更新菜单
|
2025-07-01 21:34:20 +08:00
|
|
|
|
var res = await _deviceRepository.Edit(editDievce);
|
2025-07-02 12:01:20 +08:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2025-07-02 12:01:20 +08:00
|
|
|
|
// 删除设备
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2025-07-02 12:01:20 +08:00
|
|
|
|
NotificationHelper.ShowMessage($"删除设备的过程中发生错误:{e.Message}", NotificationType.Error);
|
|
|
|
|
|
_logger.LogError($"删除设备的过程中发生错误:{e}");
|
2025-07-01 21:34:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-23 17:01:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-02 18:33:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-10 20:55:39 +08:00
|
|
|
|
}
|