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 CommunityToolkit.Mvvm.Messaging;
|
2025-06-14 19:34:12 +08:00
|
|
|
|
using HandyControl.Controls;
|
|
|
|
|
|
using HandyControl.Data;
|
2025-06-23 13:42:02 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
using PMSWPF.Data.Entities;
|
|
|
|
|
|
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.Extensions;
|
|
|
|
|
|
using PMSWPF.Helper;
|
2025-06-23 13:42:02 +08:00
|
|
|
|
using PMSWPF.Message;
|
2025-06-12 13:15:55 +08:00
|
|
|
|
using PMSWPF.Models;
|
|
|
|
|
|
using PMSWPF.Services;
|
|
|
|
|
|
using PMSWPF.ViewModels.Dialogs;
|
|
|
|
|
|
using PMSWPF.Views.Dialogs;
|
2025-06-14 19:34:12 +08:00
|
|
|
|
using MessageBox = System.Windows.MessageBox;
|
|
|
|
|
|
using Notification = PMSWPF.Models.Notification;
|
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-12 13:15:55 +08:00
|
|
|
|
private readonly IDeviceDialogService _deviceDialogService;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
private readonly DevicesRepositories _devicesRepositories;
|
2025-06-23 13:42:02 +08:00
|
|
|
|
private readonly ILogger<DevicesViewModel> _logger;
|
|
|
|
|
|
[ObservableProperty]
|
2025-06-23 15:15:10 +08:00
|
|
|
|
private ObservableCollection<Device> _devices ;
|
2025-06-14 19:34:12 +08:00
|
|
|
|
|
2025-06-23 13:42:02 +08:00
|
|
|
|
public DevicesViewModel(IDeviceDialogService deviceDialogService, DevicesRepositories devicesRepositories,ILogger<DevicesViewModel> logger
|
|
|
|
|
|
)
|
2025-06-12 13:15:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
_deviceDialogService = deviceDialogService;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
_devicesRepositories = devicesRepositories;
|
2025-06-23 13:42:02 +08:00
|
|
|
|
_logger = logger;
|
2025-06-12 13:15:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-13 18:54:17 +08:00
|
|
|
|
public async Task OnLoadedAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var ds = await _devicesRepositories.GetAll();
|
2025-06-23 15:15:10 +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-20 18:53:29 +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-20 18:53:29 +08:00
|
|
|
|
device= await _deviceDialogService.ShowAddDeviceDialog();
|
2025-06-14 19:34:12 +08:00
|
|
|
|
if (device != null)
|
|
|
|
|
|
{
|
2025-06-20 18:53:29 +08:00
|
|
|
|
var isOk = await _devicesRepositories.Add(device);
|
|
|
|
|
|
if (isOk)
|
2025-06-14 19:34:12 +08:00
|
|
|
|
{
|
|
|
|
|
|
// MessageBox.Show("Device added successfully");
|
|
|
|
|
|
await OnLoadedAsync();
|
2025-06-23 13:42:02 +08:00
|
|
|
|
var msg = $"设备添加成功:{device.Name}";
|
|
|
|
|
|
_logger.LogInformation(msg);
|
|
|
|
|
|
NotificationHelper.ShowMessage(msg, NotificationType.Success);
|
2025-06-14 19:34:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-12 18:41:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (DbExistException e)
|
|
|
|
|
|
{
|
2025-06-23 13:42:02 +08:00
|
|
|
|
var msg = $"设备添加失败:名称为{device?.Name}的设备已经存在。请更换是被名称";
|
|
|
|
|
|
_logger.LogError(msg);
|
|
|
|
|
|
NotificationHelper.ShowMessage(msg, NotificationType.Error);
|
2025-06-12 18:41:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-06-23 13:42:02 +08:00
|
|
|
|
var msg = $"添加设备的过程中发生错误:{e.Message}";
|
|
|
|
|
|
_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 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
|
|
|
|
// OnLoadedAsync().Await((e) => { _deviceDialogService.ShowMessageDialog("", e.Message); }, () => { });
|
|
|
|
|
|
await OnLoadedAsync();
|
2025-06-13 18:54:17 +08:00
|
|
|
|
}
|
2025-06-10 20:55:39 +08:00
|
|
|
|
}
|