2025-06-13 18:54:17 +08:00
|
|
|
|
using System.Collections.ObjectModel;
|
2025-07-27 21:08:58 +08:00
|
|
|
|
using AutoMapper;
|
2025-06-13 18:54:17 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
2025-06-12 18:41:46 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2025-07-27 21:08:58 +08:00
|
|
|
|
using DMS.Application.DTOs;
|
|
|
|
|
|
using DMS.Application.Interfaces;
|
2025-07-18 22:21:16 +08:00
|
|
|
|
using DMS.Core.Enums;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
using DMS.Core.Helper;
|
|
|
|
|
|
using DMS.Core.Models;
|
2025-07-18 19:56:00 +08:00
|
|
|
|
using DMS.Helper;
|
|
|
|
|
|
using DMS.Services;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
using DMS.WPF.Helper;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-06-23 13:42:02 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
using DMS.WPF.Services;
|
2025-07-27 21:08:58 +08:00
|
|
|
|
using DMS.WPF.ViewModels.Dialogs;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
using DMS.WPF.ViewModels.Items;
|
2025-07-27 21:08:58 +08:00
|
|
|
|
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-06-12 13:15:55 +08:00
|
|
|
|
|
2025-07-03 13:17:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设备管理视图模型,负责设备的增删改查操作。
|
|
|
|
|
|
/// </summary>
|
2025-07-27 21:08:58 +08:00
|
|
|
|
public partial class DevicesViewModel : ViewModelBase, INavigatable
|
2025-06-10 20:55:39 +08:00
|
|
|
|
{
|
2025-07-28 11:08:56 +08:00
|
|
|
|
public DataServices DataServices { get; }
|
2025-07-27 21:08:58 +08:00
|
|
|
|
private readonly IDeviceAppService _deviceAppService;
|
|
|
|
|
|
private readonly IMapper _mapper;
|
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-06-23 17:01:06 +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-27 21:08:58 +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
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化 <see cref="DevicesViewModel"/> 类的新实例。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="logger">日志记录器。</param>
|
|
|
|
|
|
/// <param name="dialogService">对话框服务。</param>
|
|
|
|
|
|
/// <param name="dataServices">数据服务。</param>
|
2025-07-27 21:08:58 +08:00
|
|
|
|
public DevicesViewModel(IMapper mapper,
|
2025-07-30 12:09:00 +08:00
|
|
|
|
IDialogService dialogService, INavigationService navigationService,
|
|
|
|
|
|
DataServices dataServices, IDeviceAppService deviceAppService)
|
2025-06-12 13:15:55 +08:00
|
|
|
|
{
|
2025-07-27 21:08:58 +08:00
|
|
|
|
_mapper = mapper;
|
2025-06-26 19:36:27 +08:00
|
|
|
|
_dialogService = dialogService;
|
2025-07-30 12:09:00 +08:00
|
|
|
|
_navigationService = navigationService;
|
2025-07-27 21:08:58 +08:00
|
|
|
|
DataServices = dataServices;
|
|
|
|
|
|
_deviceAppService = deviceAppService;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
Devices = new ObservableCollection<DeviceItemViewModel>();
|
2025-07-27 21:08:58 +08:00
|
|
|
|
DataServices.OnDeviceListChanged += (devices) => { };
|
2025-06-12 13:15:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-16 19:37:13 +08:00
|
|
|
|
|
|
|
|
|
|
public override Task<bool> OnExitAsync()
|
|
|
|
|
|
{
|
2025-07-26 10:05:43 +08:00
|
|
|
|
// if (_dataServices.Devices!=null && _dataServices.Devices.Count>0)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// foreach (var device in Devices)
|
|
|
|
|
|
// {
|
|
|
|
|
|
// device.OnDeviceIsActiveChanged -= HandleDeviceIsActiveChanged;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
2025-07-16 19:37:13 +08:00
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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]
|
2025-07-27 21:08:58 +08:00
|
|
|
|
public async Task AddDevice()
|
2025-07-02 18:33:08 +08:00
|
|
|
|
{
|
2025-07-27 21:08:58 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-29 20:02:09 +08:00
|
|
|
|
DeviceDialogViewModel deviceDialogViewModel = new DeviceDialogViewModel()
|
2025-07-27 21:08:58 +08:00
|
|
|
|
{
|
2025-08-23 16:01:30 +08:00
|
|
|
|
PrimaryButText = "添加设备"
|
2025-07-27 21:08:58 +08:00
|
|
|
|
};
|
|
|
|
|
|
// 1. 显示添加设备对话框
|
2025-07-27 21:58:50 +08:00
|
|
|
|
DeviceItemViewModel device = await _dialogService.ShowDialogAsync(deviceDialogViewModel);
|
|
|
|
|
|
// 如果用户取消或对话框未返回设备,则直接返回
|
|
|
|
|
|
if (device == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-27 21:08:58 +08:00
|
|
|
|
|
|
|
|
|
|
CreateDeviceWithDetailsDto dto = new CreateDeviceWithDetailsDto();
|
|
|
|
|
|
dto.Device = _mapper.Map<DeviceDto>(device);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dto.DeviceMenu = new MenuBeanDto()
|
|
|
|
|
|
{
|
|
|
|
|
|
Header = device.Name,
|
|
|
|
|
|
Icon = SegoeFluentIcons.Devices2.Glyph,
|
|
|
|
|
|
TargetViewKey = "DevicesView"
|
|
|
|
|
|
};
|
2025-07-28 11:08:56 +08:00
|
|
|
|
if (device.IsAddDefVarTable)
|
|
|
|
|
|
{
|
|
|
|
|
|
dto.VariableTable = new VariableTableDto()
|
2025-07-27 21:08:58 +08:00
|
|
|
|
{
|
2025-07-28 11:08:56 +08:00
|
|
|
|
Name = "默认变量表",
|
|
|
|
|
|
Description = "默认变量表",
|
|
|
|
|
|
IsActive = true
|
2025-07-27 21:08:58 +08:00
|
|
|
|
};
|
2025-07-28 11:08:56 +08:00
|
|
|
|
dto.VariableTableMenu = new MenuBeanDto()
|
|
|
|
|
|
{
|
|
|
|
|
|
Header = dto.VariableTable.Name,
|
|
|
|
|
|
Icon = SegoeFluentIcons.DataSense.Glyph,
|
|
|
|
|
|
TargetViewKey = "VariableTableView"
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-27 21:08:58 +08:00
|
|
|
|
var addDto = await _deviceAppService.CreateDeviceWithDetailsAsync(dto);
|
2025-07-28 11:08:56 +08:00
|
|
|
|
|
2025-07-28 11:33:52 +08:00
|
|
|
|
// 更新界面
|
2025-07-28 11:08:56 +08:00
|
|
|
|
DataServices.Devices.Add(_mapper.Map<DeviceItemViewModel>(addDto.Device));
|
2025-07-30 12:09:00 +08:00
|
|
|
|
DataServices.AddMenuItem(_mapper.Map<MenuItemViewModel>(addDto.DeviceMenu));
|
2025-07-28 11:33:52 +08:00
|
|
|
|
DataServices.AddVariableTable(_mapper.Map<VariableTableItemViewModel>(addDto.VariableTable));
|
2025-07-30 12:09:00 +08:00
|
|
|
|
DataServices.AddMenuItem(_mapper.Map<MenuItemViewModel>(addDto.VariableTableMenu));
|
2025-07-28 13:06:36 +08:00
|
|
|
|
|
2025-07-28 11:33:52 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"设备添加成功:{addDto.Device.Name}");
|
2025-07-27 21:08:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(e);
|
|
|
|
|
|
NotificationHelper.ShowError($"添加设备的过程中发生错误:{e.Message}", e);
|
|
|
|
|
|
}
|
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-28 13:06:36 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectedDevice == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError("你没有选择任何设备,请选择设备后再点击删除设备");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-23 09:09:07 +08:00
|
|
|
|
if (await _dialogService.ShowDialogAsync(new ConfrimDialogViewModel("删除设备",$"确认要删除设备名为:{SelectedDevice.Name}","删除设备")))
|
2025-07-28 13:06:36 +08:00
|
|
|
|
{
|
|
|
|
|
|
var isDel = await _deviceAppService.DeleteDeviceByIdAsync(SelectedDevice.Id);
|
|
|
|
|
|
if (isDel)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 更新界面
|
|
|
|
|
|
DataServices.DeleteDeviceById(SelectedDevice.Id);
|
|
|
|
|
|
|
|
|
|
|
|
NotificationHelper.ShowSuccess($"删除设备成功,设备名:{SelectedDevice.Name}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
{
|
2025-07-29 20:02:09 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectedDevice == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError("你没有选择任何设备,请选择设备后再点击编辑设备");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DeviceDialogViewModel deviceDialogViewModel = new DeviceDialogViewModel(SelectedDevice)
|
|
|
|
|
|
{
|
2025-08-23 16:01:30 +08:00
|
|
|
|
PrimaryButText = "编辑设备"
|
2025-07-29 20:02:09 +08:00
|
|
|
|
};
|
|
|
|
|
|
// 1. 显示设备对话框
|
|
|
|
|
|
DeviceItemViewModel device = await _dialogService.ShowDialogAsync(deviceDialogViewModel);
|
|
|
|
|
|
// 如果用户取消或对话框未返回设备,则直接返回
|
|
|
|
|
|
if (device == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int res = await _deviceAppService.UpdateDeviceAsync(_mapper.Map<DeviceDto>(device));
|
|
|
|
|
|
if (res > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var menu = DataServices.Menus.FirstOrDefault(m =>
|
|
|
|
|
|
m.MenuType == MenuType.DeviceMenu &&
|
|
|
|
|
|
m.TargetId == device.Id);
|
2025-07-30 12:09:00 +08:00
|
|
|
|
if (menu != null)
|
2025-07-29 20:02:09 +08:00
|
|
|
|
{
|
2025-07-30 12:09:00 +08:00
|
|
|
|
menu.Header = device.Name;
|
2025-07-29 20:02:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError($"编辑设备的过程中发生错误:{e.Message}", e);
|
|
|
|
|
|
}
|
2025-07-01 21:34:20 +08:00
|
|
|
|
}
|
2025-07-27 21:08:58 +08:00
|
|
|
|
|
2025-07-16 18:39:00 +08:00
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
public void NavigateToDetail()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectedDevice == null) return;
|
2025-07-30 12:09:00 +08:00
|
|
|
|
|
|
|
|
|
|
var menu=DataServices.Menus.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu && m.TargetId == SelectedDevice.Id);
|
|
|
|
|
|
if (menu==null) return;
|
|
|
|
|
|
|
|
|
|
|
|
_navigationService.NavigateToAsync(menu);
|
2025-07-16 18:39:00 +08:00
|
|
|
|
}
|
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-06-10 20:55:39 +08:00
|
|
|
|
}
|