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-07-02 22:07:16 +08:00
|
|
|
|
using PMSWPF.Data;
|
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;
|
|
|
|
|
|
|
2025-07-03 13:17:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设备管理视图模型,负责设备的增删改查操作。
|
|
|
|
|
|
/// </summary>
|
2025-06-12 13:15:55 +08:00
|
|
|
|
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;
|
2025-06-26 19:36:27 +08:00
|
|
|
|
private readonly DeviceRepository _deviceRepository;
|
2025-07-06 19:51:53 +08:00
|
|
|
|
|
2025-06-26 19:36:27 +08:00
|
|
|
|
private readonly MenuRepository _menuRepository;
|
2025-07-02 22:07:16 +08:00
|
|
|
|
private readonly VarTableRepository _varTableRepository;
|
2025-06-23 17:01:06 +08:00
|
|
|
|
|
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>
|
2025-06-26 19:36:27 +08:00
|
|
|
|
public DevicesViewModel(
|
2025-07-06 19:51:53 +08:00
|
|
|
|
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();
|
2025-07-02 22:07:16 +08:00
|
|
|
|
_varTableRepository = new VarTableRepository();
|
2025-06-26 19:36:27 +08:00
|
|
|
|
_menuRepository = new MenuRepository();
|
2025-07-06 19:51:53 +08:00
|
|
|
|
|
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-07-10 17:16:15 +08:00
|
|
|
|
_dataServices.OnDeviceListChanged += (sender, devices) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
Devices = new ObservableCollection<Device>(devices);
|
|
|
|
|
|
};
|
2025-06-12 13:15:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
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-02 18:33:08 +08:00
|
|
|
|
{
|
2025-07-04 18:33:48 +08:00
|
|
|
|
try
|
2025-06-12 18:41:46 +08:00
|
|
|
|
{
|
2025-07-04 18:33:48 +08:00
|
|
|
|
// 1. 显示添加设备对话框
|
|
|
|
|
|
var device = await _dialogService.ShowAddDeviceDialog();
|
|
|
|
|
|
// 如果用户取消或对话框未返回设备,则直接返回
|
|
|
|
|
|
if (device == null)
|
|
|
|
|
|
{
|
2025-07-06 19:51:53 +08:00
|
|
|
|
NlogHelper.Info("用户取消了添加设备操作。");
|
2025-07-04 18:33:48 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-07-03 13:17:25 +08:00
|
|
|
|
|
2025-07-04 18:33:48 +08:00
|
|
|
|
await _deviceRepository.Add(device);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"添加设备的过程中发生错误:{e.Message}", e);
|
2025-07-04 18:33:48 +08:00
|
|
|
|
}
|
2025-06-10 22:13:06 +08:00
|
|
|
|
}
|
2025-06-13 18:54:17 +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-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowError("你没有选择任何设备,请选择设备后再点击删除设备");
|
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-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"删除设备成功,设备名:{SelectedDevice.Name}");
|
2025-07-01 21:34:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-07-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"删除设备的过程中发生错误:{e.Message}", 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-06 15:36:53 +08:00
|
|
|
|
NotificationHelper.ShowError("你没有选择任何设备,请选择设备后再点击编辑设备");
|
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);
|
2025-07-02 12:01:20 +08:00
|
|
|
|
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-06 15:15:38 +08:00
|
|
|
|
NotificationHelper.ShowError($"编辑设备的过程中发生错误:{e.Message}", e);
|
2025-07-01 21:34:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-06-10 20:55:39 +08:00
|
|
|
|
}
|