Files
DMS/DMS.WPF/ViewModels/DevicesViewModel.cs

225 lines
8.0 KiB
C#
Raw Normal View History

using System.Collections.ObjectModel;
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
2025-09-03 18:22:01 +08:00
using DMS.WPF.Interfaces;
2025-07-19 11:11:01 +08:00
using DMS.WPF.Services;
using DMS.WPF.ViewModels.Dialogs;
2025-07-26 10:05:43 +08:00
using DMS.WPF.ViewModels.Items;
using iNKORE.UI.WPF.Modern.Common.IconKeys;
2025-06-10 20:55:39 +08:00
2025-07-19 11:11:01 +08:00
namespace DMS.WPF.ViewModels;
2025-07-03 13:17:25 +08:00
/// <summary>
/// 设备管理视图模型,负责设备的增删改查操作。
/// </summary>
public partial class DevicesViewModel : ViewModelBase, INavigatable
2025-06-10 20:55:39 +08:00
{
private readonly IWPFDataService _wpfDataService;
private readonly IDeviceAppService _deviceAppService;
private readonly IMapper _mapper;
private readonly IDataStorageService _dataStorageService;
2025-07-03 13:17:25 +08:00
private readonly IDialogService _dialogService;
2025-07-30 12:09:00 +08:00
private readonly INavigationService _navigationService;
2025-07-26 10:05:43 +08:00
2025-07-03 13:17:25 +08:00
/// <summary>
/// 设备列表。
/// </summary>
[ObservableProperty]
2025-07-26 10:05:43 +08:00
private ObservableCollection<DeviceItemViewModel> _devices;
2025-07-03 13:17:25 +08:00
2025-07-03 13:17:25 +08:00
/// <summary>
/// 当前选中的设备。
/// </summary>
[ObservableProperty]
2025-07-26 10:05:43 +08:00
private DeviceItemViewModel _selectedDevice;
2025-07-03 13:17:25 +08:00
private readonly INotificationService _notificationService;
2025-07-03 13:17:25 +08:00
/// <summary>
/// 初始化 <see cref="DevicesViewModel"/> 类的新实例。
/// </summary>
2025-09-12 13:25:39 +08:00
/// <param name="mapper">对象映射器。</param>
/// <param name="dataStorageService">数据存储服务。</param>
2025-07-03 13:17:25 +08:00
/// <param name="dialogService">对话框服务。</param>
2025-09-12 13:25:39 +08:00
/// <param name="navigationService">导航服务。</param>
/// <param name="wpfDataService">主数据服务。</param>
2025-09-12 13:25:39 +08:00
/// <param name="deviceAppService">设备应用服务。</param>
/// <param name="notificationService">通知服务。</param>
public DevicesViewModel(IMapper mapper,IDataStorageService dataStorageService,
2025-07-30 12:09:00 +08:00
IDialogService dialogService, INavigationService navigationService,
IWPFDataService wpfDataService, IDeviceAppService deviceAppService,
INotificationService notificationService)
{
_mapper = mapper;
_dataStorageService = dataStorageService;
_dialogService = dialogService;
2025-07-30 12:09:00 +08:00
_navigationService = navigationService;
_wpfDataService = wpfDataService;
_deviceAppService = deviceAppService;
_notificationService = notificationService;
Devices = _dataStorageService.Devices;
2025-09-12 13:25:39 +08:00
// 设置DeviceItemViewModel的静态服务引用
DeviceItemViewModel.EventService = wpfDataService.EventService;
}
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 Task AddDevice()
{
try
{
// 1. 显示添加设备对话框
DeviceItemViewModel device = await _dialogService.ShowDialogAsync(new DeviceDialogViewModel()
2025-09-03 15:16:07 +08:00
{
Title = "添加设备",
PrimaryButText = "添加设备"
});
2025-07-27 21:58:50 +08:00
// 如果用户取消或对话框未返回设备,则直接返回
if (device == null)
{
return;
}
CreateDeviceWithDetailsDto dto = new CreateDeviceWithDetailsDto();
dto.Device = _mapper.Map<DeviceDto>(device);
dto.DeviceMenu = new MenuBeanDto()
{
Header = device.Name,
Icon = SegoeFluentIcons.Devices2.Glyph,
2025-08-25 21:26:18 +08:00
TargetViewKey = "DeviceDetailView"
2025-09-03 15:16:07 +08:00
};
2025-07-28 11:08:56 +08:00
if (device.IsAddDefVarTable)
{
dto.VariableTable = new VariableTableDto()
{
2025-07-28 11:08:56 +08:00
Name = "默认变量表",
Description = "默认变量表",
IsActive = true
};
2025-07-28 11:08:56 +08:00
dto.VariableTableMenu = new MenuBeanDto()
{
Header = dto.VariableTable.Name,
Icon = SegoeFluentIcons.DataSense.Glyph,
TargetViewKey = "VariableTableView"
};
}
2025-09-03 15:16:07 +08:00
// 添加设备
var addDto = await _wpfDataService.DeviceDataService.AddDevice(dto);
2025-07-28 13:06:36 +08:00
_notificationService.ShowSuccess($"设备添加成功:{addDto.Device.Name}");
}
catch (Exception e)
{
Console.WriteLine(e);
_notificationService.ShowError($"添加设备的过程中发生错误:{e.Message}", 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-09-03 15:16:07 +08:00
private async Task DeleteDevice()
2025-07-01 21:34:20 +08:00
{
2025-07-28 13:06:36 +08:00
try
{
if (SelectedDevice == null)
{
_notificationService.ShowError("你没有选择任何设备,请选择设备后再点击删除设备");
2025-07-28 13:06:36 +08:00
return;
}
2025-09-03 15:16:07 +08:00
var viewModel = new ConfirmDialogViewModel("删除设备", $"确认要删除设备名为:{SelectedDevice.Name}", "删除设备");
if (await _dialogService.ShowDialogAsync(viewModel))
2025-07-28 13:06:36 +08:00
{
2025-09-03 15:16:07 +08:00
var deviceName = SelectedDevice.Name;
if (await _wpfDataService.DeviceDataService.DeleteDevice(SelectedDevice))
2025-07-28 13:06:36 +08:00
{
_notificationService.ShowSuccess($"删除设备成功,设备名:{deviceName}");
2025-07-28 13:06:36 +08:00
}
}
}
catch (Exception e)
{
_notificationService.ShowError($"删除设备的过程中发生错误:{e.Message}", e);
2025-07-28 13:06:36 +08:00
}
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-09-03 15:16:07 +08:00
private async Task EditDevice()
2025-07-01 21:34:20 +08:00
{
2025-07-29 20:02:09 +08:00
try
{
if (SelectedDevice == null)
{
_notificationService.ShowError("你没有选择任何设备,请选择设备后再点击编辑设备");
2025-07-29 20:02:09 +08:00
return;
}
DeviceDialogViewModel deviceDialogViewModel = new DeviceDialogViewModel(SelectedDevice)
{
2025-09-03 15:16:07 +08:00
PrimaryButText = "编辑设备"
2025-07-29 20:02:09 +08:00
};
// 1. 显示设备对话框
DeviceItemViewModel device = await _dialogService.ShowDialogAsync(deviceDialogViewModel);
// 如果用户取消或对话框未返回设备,则直接返回
if (device == null)
{
return;
}
if (await _wpfDataService.DeviceDataService.UpdateDevice(device))
2025-07-29 20:02:09 +08:00
{
_notificationService.ShowSuccess($"编辑设备成功:{device.Name}");
2025-07-29 20:02:09 +08:00
}
}
catch (Exception e)
{
_notificationService.ShowError($"编辑设备的过程中发生错误:{e.Message}", e);
2025-07-29 20:02:09 +08:00
}
2025-07-01 21:34:20 +08:00
}
[RelayCommand]
public void NavigateToDetail()
{
if (SelectedDevice == null) return;
2025-07-30 12:09:00 +08:00
var menu = _dataStorageService.Menus.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu &&
m.TargetId == SelectedDevice.Id);
2025-09-03 15:16:07 +08:00
if (menu == null) return;
2025-07-30 12:09:00 +08:00
_navigationService.NavigateToAsync(menu);
}
2025-07-26 18:58:52 +08:00
2025-07-30 12:09:00 +08:00
public async Task OnNavigatedToAsync(MenuItemViewModel menu)
2025-07-26 18:58:52 +08:00
{
2025-09-12 13:25:39 +08:00
}
private void OnDeviceIsActiveChanged(object? sender, bool isActive)
{
if (sender is DeviceItemViewModel deviceItemViewModel)
{
}
2025-07-26 18:58:52 +08:00
}
2025-06-10 20:55:39 +08:00
}