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-25 22:33:57 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
|
|
|
|
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
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.Excptions;
|
|
|
|
|
|
using PMSWPF.Helper;
|
2025-06-25 22:33:57 +08:00
|
|
|
|
using PMSWPF.Message;
|
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-14 19:34:12 +08:00
|
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
|
[ObservableProperty] private ObservableCollection<Device> _devices;
|
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(
|
|
|
|
|
|
ILogger<DevicesViewModel> logger, IDialogService dialogService
|
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-12 13:15:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-13 18:54:17 +08:00
|
|
|
|
public async Task OnLoadedAsync()
|
|
|
|
|
|
{
|
2025-06-26 19:36:27 +08:00
|
|
|
|
var ds = await _deviceRepository.GetAll();
|
2025-06-23 17:01:06 +08:00
|
|
|
|
Devices = new ObservableCollection<Device>(ds);
|
2025-06-13 18:54:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-14 19:34:12 +08:00
|
|
|
|
[RelayCommand]
|
2025-06-23 17:01:06 +08:00
|
|
|
|
public async void AddDevice()
|
2025-06-10 22:13:06 +08:00
|
|
|
|
{
|
2025-06-20 18:53:29 +08:00
|
|
|
|
Device device = null;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-06-26 19:36:27 +08:00
|
|
|
|
device = await _dialogService.ShowAddDeviceDialog();
|
|
|
|
|
|
if (device != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (await _deviceRepository.Add(device))
|
|
|
|
|
|
{
|
|
|
|
|
|
var msg = $"添加设备成功:{device.Name}";
|
|
|
|
|
|
_logger.LogInformation(msg);
|
|
|
|
|
|
// 添加菜单项
|
|
|
|
|
|
MenuBean deviceMenu = new MenuBean()
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = device.Name,
|
|
|
|
|
|
Icon = SegoeFluentIcons.Devices4.Glyph,
|
|
|
|
|
|
};
|
2025-06-26 22:40:20 +08:00
|
|
|
|
bool addMenuRes = await _menuRepository.AddDeviceMenu(deviceMenu);
|
|
|
|
|
|
if (addMenuRes)
|
2025-06-26 19:36:27 +08:00
|
|
|
|
{
|
2025-06-26 22:40:20 +08:00
|
|
|
|
// 通知更新菜单
|
|
|
|
|
|
WeakReferenceMessenger.Default.Send<UpdateMenuMessage>(new UpdateMenuMessage(0));
|
|
|
|
|
|
NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var msgerr = $"给设备添加菜单失败:{device.Name}";
|
|
|
|
|
|
_logger.LogInformation(msgerr);
|
|
|
|
|
|
NotificationHelper.ShowMessage(msgerr, NotificationType.Error);
|
2025-06-26 19:36:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var msg = $"添加设备失败:{device.Name}";
|
|
|
|
|
|
_logger.LogInformation(msg);
|
2025-06-26 22:40:20 +08:00
|
|
|
|
NotificationHelper.ShowMessage(msg, NotificationType.Error);
|
2025-06-26 19:36:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-12 18:41:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-06-26 22:40:20 +08:00
|
|
|
|
var msg = $"添加设备失败:{e.Message}";
|
2025-06-23 13:42:02 +08:00
|
|
|
|
_logger.LogError(msg);
|
|
|
|
|
|
NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
2025-06-12 18:41:46 +08:00
|
|
|
|
}
|
2025-06-10 22:13:06 +08:00
|
|
|
|
}
|
2025-06-13 18:54:17 +08:00
|
|
|
|
|
2025-06-23 17:01:06 +08:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public void NavigateVt()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-23 13:42:02 +08:00
|
|
|
|
public override async void OnLoaded()
|
2025-06-13 18:54:17 +08:00
|
|
|
|
{
|
2025-06-23 13:42:02 +08:00
|
|
|
|
await OnLoadedAsync();
|
2025-06-13 18:54:17 +08:00
|
|
|
|
}
|
2025-06-10 20:55:39 +08:00
|
|
|
|
}
|