2025-07-30 12:54:14 +08:00
|
|
|
|
using AutoMapper;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel;
|
|
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
2025-09-03 18:08:42 +08:00
|
|
|
|
using Dm;
|
2025-07-30 12:54:14 +08:00
|
|
|
|
using DMS.Application.DTOs;
|
|
|
|
|
|
using DMS.Application.Interfaces;
|
2025-07-30 12:09:00 +08:00
|
|
|
|
using DMS.Core.Enums;
|
2025-10-03 22:28:58 +08:00
|
|
|
|
using DMS.Core.Models;
|
2025-07-19 09:25:01 +08:00
|
|
|
|
using DMS.WPF.Services;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
using DMS.Services;
|
2025-09-03 18:22:01 +08:00
|
|
|
|
using DMS.WPF.Interfaces;
|
2025-07-30 12:54:14 +08:00
|
|
|
|
using DMS.WPF.ViewModels.Dialogs;
|
2025-10-06 18:17:56 +08:00
|
|
|
|
using DMS.WPF.ItemViewModel;
|
2025-07-30 12:54:14 +08:00
|
|
|
|
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
2025-10-03 23:06:04 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-06-30 14:19:22 +08:00
|
|
|
|
|
2025-07-19 11:11:01 +08:00
|
|
|
|
namespace DMS.WPF.ViewModels;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
|
2025-10-03 22:28:58 +08:00
|
|
|
|
public partial class DeviceDetailViewModel : ViewModelBase
|
2025-06-30 14:19:22 +08:00
|
|
|
|
{
|
2025-07-30 12:54:14 +08:00
|
|
|
|
private readonly IMapper _mapper;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
private readonly IDialogService _dialogService;
|
2025-10-22 14:06:16 +08:00
|
|
|
|
private readonly IViewDataService _viewDataService;
|
2025-07-30 12:09:00 +08:00
|
|
|
|
private readonly INavigationService _navigationService;
|
2025-10-23 16:25:22 +08:00
|
|
|
|
private readonly IVariableTableViewService _variableTableViewService;
|
2025-10-22 14:06:16 +08:00
|
|
|
|
private readonly IViewCenterService _wpfDataService;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-10-06 18:17:56 +08:00
|
|
|
|
private DeviceItem _currentDevice;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-10-06 18:17:56 +08:00
|
|
|
|
private VariableTableItem _selectedVariableTable;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
|
2025-10-02 13:19:55 +08:00
|
|
|
|
[ObservableProperty]
|
|
|
|
|
|
private bool _isBusy;
|
|
|
|
|
|
|
2025-09-04 19:59:35 +08:00
|
|
|
|
private readonly INotificationService _notificationService;
|
2025-09-04 17:29:24 +08:00
|
|
|
|
|
2025-10-02 13:19:55 +08:00
|
|
|
|
|
2025-10-22 14:06:16 +08:00
|
|
|
|
public DeviceDetailViewModel(IMapper mapper, IDialogService dialogService, IViewDataService dataStorageService,
|
2025-10-02 13:19:55 +08:00
|
|
|
|
INavigationService navigationService,
|
2025-10-23 16:25:22 +08:00
|
|
|
|
IVariableTableViewService variableTableViewService, INotificationService notificationService)
|
2025-07-16 18:39:00 +08:00
|
|
|
|
{
|
2025-07-30 12:54:14 +08:00
|
|
|
|
_mapper = mapper;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
_dialogService = dialogService;
|
2025-10-22 14:06:16 +08:00
|
|
|
|
_viewDataService = dataStorageService;
|
2025-07-30 12:09:00 +08:00
|
|
|
|
_navigationService = navigationService;
|
2025-10-23 16:25:22 +08:00
|
|
|
|
_variableTableViewService = variableTableViewService;
|
2025-09-04 17:29:24 +08:00
|
|
|
|
_notificationService = notificationService;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
2025-10-23 16:25:22 +08:00
|
|
|
|
private async Task AddVariableTable(DeviceItem deviceItem = null)
|
2025-07-16 18:39:00 +08:00
|
|
|
|
{
|
2025-10-02 13:19:55 +08:00
|
|
|
|
if (IsBusy) return;
|
|
|
|
|
|
IsBusy = true;
|
2025-07-30 12:54:14 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
|
if (deviceItem is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
deviceItem = CurrentDevice;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-30 12:54:14 +08:00
|
|
|
|
VariableTableDialogViewModel variableTableDialogViewModel = new VariableTableDialogViewModel()
|
2025-10-23 16:25:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
PrimaryButText = "添加变量表"
|
|
|
|
|
|
};
|
2025-07-30 12:54:14 +08:00
|
|
|
|
// 1. 显示添加设备对话框
|
2025-10-23 16:25:22 +08:00
|
|
|
|
var variableTableItem = await _dialogService.ShowDialogAsync(variableTableDialogViewModel);
|
2025-07-30 12:54:14 +08:00
|
|
|
|
// 如果用户取消或对话框未返回设备,则直接返回
|
2025-10-23 16:25:22 +08:00
|
|
|
|
if (variableTableItem is null) return;
|
|
|
|
|
|
|
|
|
|
|
|
variableTableItem.DeviceId = deviceItem.Id;
|
|
|
|
|
|
variableTableItem.Device = deviceItem;
|
|
|
|
|
|
variableTableItem.Protocol = deviceItem.Protocol;
|
|
|
|
|
|
var res = await _variableTableViewService.AddAsync(variableTableItem);
|
2025-07-30 12:54:14 +08:00
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
if (res)
|
2025-10-02 13:19:55 +08:00
|
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
|
_notificationService.ShowSuccess($"添加变量表成功:{variableTableItem.Name}");
|
2025-10-02 13:19:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
|
_notificationService.ShowError($"添加变量表失败:{variableTableItem.Name}!!");
|
2025-10-02 13:19:55 +08:00
|
|
|
|
}
|
2025-07-30 12:54:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
2025-09-04 17:29:24 +08:00
|
|
|
|
_notificationService.ShowError($"添加变量表时发生错误: {ex.Message}", ex);
|
2025-07-30 12:54:14 +08:00
|
|
|
|
}
|
2025-10-02 13:19:55 +08:00
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
IsBusy = false;
|
|
|
|
|
|
}
|
2025-07-16 18:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private async Task EditVariableTable()
|
|
|
|
|
|
{
|
2025-10-02 13:19:55 +08:00
|
|
|
|
if (IsBusy) return;
|
|
|
|
|
|
IsBusy = true;
|
2025-07-30 13:21:03 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectedVariableTable == null)
|
|
|
|
|
|
{
|
2025-09-04 17:29:24 +08:00
|
|
|
|
_notificationService.ShowError("你没有选择任何变量表,请选择变量表后再点击编辑变量表");
|
2025-07-30 13:21:03 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-02 13:19:55 +08:00
|
|
|
|
VariableTableDialogViewModel variableTableDialogViewModel
|
|
|
|
|
|
= new VariableTableDialogViewModel(SelectedVariableTable)
|
2025-10-23 16:25:22 +08:00
|
|
|
|
{
|
|
|
|
|
|
PrimaryButText = "编辑变量表"
|
|
|
|
|
|
};
|
2025-07-30 13:21:03 +08:00
|
|
|
|
// 1. 显示变量表对话框
|
2025-10-06 18:17:56 +08:00
|
|
|
|
VariableTableItem variableTable
|
2025-10-02 13:19:55 +08:00
|
|
|
|
= await _dialogService.ShowDialogAsync(variableTableDialogViewModel);
|
2025-07-30 13:21:03 +08:00
|
|
|
|
// 如果用户取消或对话框未返回变量表,则直接返回
|
2025-10-23 16:25:22 +08:00
|
|
|
|
if (variableTable is null) return;
|
2025-07-30 13:21:03 +08:00
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
if (await _variableTableViewService.UpdateAsync(variableTable))
|
2025-07-30 13:21:03 +08:00
|
|
|
|
{
|
2025-09-04 17:29:24 +08:00
|
|
|
|
_notificationService.ShowSuccess($"编辑变量表成功:{variableTable.Name}");
|
2025-09-03 18:08:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-04 17:29:24 +08:00
|
|
|
|
_notificationService.ShowError($"编辑变量表失败:{variableTable.Name}");
|
2025-07-30 13:21:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
2025-07-30 12:09:00 +08:00
|
|
|
|
{
|
2025-09-04 17:29:24 +08:00
|
|
|
|
_notificationService.ShowError($"编辑变量表的过程中发生错误:{e.Message}", e);
|
2025-07-30 12:09:00 +08:00
|
|
|
|
}
|
2025-10-02 13:19:55 +08:00
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
IsBusy = false;
|
|
|
|
|
|
}
|
2025-07-16 18:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
2025-10-23 16:25:22 +08:00
|
|
|
|
private async Task DeleteVariableTable(VariableTableItem variableTableItem=null)
|
2025-07-16 18:39:00 +08:00
|
|
|
|
{
|
2025-10-02 13:19:55 +08:00
|
|
|
|
if (IsBusy) return;
|
|
|
|
|
|
IsBusy = true;
|
2025-07-30 13:21:03 +08:00
|
|
|
|
try
|
2025-10-02 13:19:55 +08:00
|
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
|
if (variableTableItem is null)
|
2025-07-30 13:21:03 +08:00
|
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
|
if (SelectedVariableTable is null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_notificationService.ShowError("你没有选择任何变量表,请选择变量表后再点击删除变量表");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
variableTableItem = SelectedVariableTable;
|
2025-07-30 13:21:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
string message = $"确认要删除变量表名为:{variableTableItem.Name} \n\n此操作将同时删除该变量表下的所有变量数据,且无法恢复!";
|
2025-10-02 13:19:55 +08:00
|
|
|
|
ConfirmDialogViewModel viewModel = new ConfirmDialogViewModel("删除变量表", message, "删除");
|
2025-10-23 16:25:22 +08:00
|
|
|
|
bool confirmRes = await _dialogService.ShowDialogAsync(viewModel);
|
|
|
|
|
|
if (!confirmRes) return;
|
|
|
|
|
|
var tableName = variableTableItem.Name;
|
|
|
|
|
|
if (await _variableTableViewService.DeleteAsync(variableTableItem))
|
2025-07-30 13:21:03 +08:00
|
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
|
_notificationService.ShowSuccess($"变量表:{tableName},删除成功。");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
_notificationService.ShowError($"变量表:{tableName},删除失败!!!");
|
2025-07-30 13:21:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2025-09-04 17:29:24 +08:00
|
|
|
|
_notificationService.ShowError($"删除变量表的过程中发生错误:{e.Message}", e);
|
2025-07-30 13:21:03 +08:00
|
|
|
|
}
|
2025-10-02 13:19:55 +08:00
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
IsBusy = false;
|
|
|
|
|
|
}
|
2025-07-16 18:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-16 19:37:13 +08:00
|
|
|
|
|
2025-10-03 22:28:58 +08:00
|
|
|
|
public override async Task OnNavigatedToAsync(NavigationParameter parameter)
|
2025-07-30 12:09:00 +08:00
|
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
|
if (_viewDataService.Devices.TryGetValue(parameter.TargetId, out var device))
|
2025-07-30 12:54:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
CurrentDevice = device;
|
|
|
|
|
|
}
|
2025-07-30 12:09:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-16 19:37:13 +08:00
|
|
|
|
[RelayCommand]
|
2025-07-30 12:09:00 +08:00
|
|
|
|
public void NavigateToVariableTable()
|
2025-07-16 19:37:13 +08:00
|
|
|
|
{
|
2025-07-30 12:09:00 +08:00
|
|
|
|
if (SelectedVariableTable == null) return;
|
2025-10-03 22:28:58 +08:00
|
|
|
|
_navigationService.NavigateToAsync(
|
|
|
|
|
|
this,
|
|
|
|
|
|
new NavigationParameter(nameof(VariableTableViewModel), SelectedVariableTable.Id,
|
|
|
|
|
|
NavigationType.VariableTable));
|
2025-07-16 19:37:13 +08:00
|
|
|
|
}
|
2025-10-03 23:06:04 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 返回到设备列表页命令
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private async Task NavigateToDevicesList()
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// 导航到设备列表页面
|
|
|
|
|
|
var navigationService = App.Current.Services.GetRequiredService<INavigationService>();
|
2025-10-03 23:31:04 +08:00
|
|
|
|
await navigationService.NavigateToAsync(
|
|
|
|
|
|
this, new NavigationParameter(nameof(DevicesViewModel), 0, NavigationType.Device));
|
2025-10-03 23:06:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_notificationService.ShowError($"导航到设备列表失败: {ex.Message}", ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-10-03 23:31:04 +08:00
|
|
|
|
}
|