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-09-09 13:35:16 +08:00
|
|
|
|
private readonly IDataStorageService _dataStorageService;
|
2025-07-30 12:09:00 +08:00
|
|
|
|
private readonly INavigationService _navigationService;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
private readonly IWPFDataService _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
|
|
|
|
|
|
|
|
|
|
public DeviceDetailViewModel(IMapper mapper, IDialogService dialogService, IDataStorageService dataStorageService,
|
|
|
|
|
|
INavigationService navigationService,
|
2025-09-09 13:35:16 +08:00
|
|
|
|
IWPFDataService wpfDataService, 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-09-09 13:35:16 +08:00
|
|
|
|
_dataStorageService = dataStorageService;
|
2025-07-30 12:09:00 +08:00
|
|
|
|
_navigationService = navigationService;
|
2025-09-04 17:29:24 +08:00
|
|
|
|
_notificationService = notificationService;
|
2025-09-09 13:35:16 +08:00
|
|
|
|
_wpfDataService = wpfDataService;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private async Task AddVariableTable()
|
|
|
|
|
|
{
|
2025-10-02 13:19:55 +08:00
|
|
|
|
if (IsBusy) return;
|
|
|
|
|
|
IsBusy = true;
|
2025-07-30 12:54:14 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
VariableTableDialogViewModel variableTableDialogViewModel = new VariableTableDialogViewModel()
|
|
|
|
|
|
{
|
2025-08-23 16:01:30 +08:00
|
|
|
|
PrimaryButText = "添加变量表"
|
2025-07-30 12:54:14 +08:00
|
|
|
|
};
|
|
|
|
|
|
// 1. 显示添加设备对话框
|
2025-10-06 18:17:56 +08:00
|
|
|
|
var VariableTableItem = await _dialogService.ShowDialogAsync(variableTableDialogViewModel);
|
2025-07-30 12:54:14 +08:00
|
|
|
|
// 如果用户取消或对话框未返回设备,则直接返回
|
2025-10-06 18:17:56 +08:00
|
|
|
|
if (VariableTableItem == null)
|
2025-07-30 12:54:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-06 18:17:56 +08:00
|
|
|
|
VariableTableItem.DeviceId = CurrentDevice.Id;
|
2025-10-02 13:19:55 +08:00
|
|
|
|
var tableMenu = new MenuBeanDto()
|
|
|
|
|
|
{
|
2025-10-06 18:17:56 +08:00
|
|
|
|
Header = VariableTableItem.Name,
|
2025-10-02 13:19:55 +08:00
|
|
|
|
Icon = SegoeFluentIcons.DataSense.Glyph,
|
2025-10-03 22:28:58 +08:00
|
|
|
|
TargetViewKey = nameof(VariableTableViewModel)
|
2025-10-02 13:19:55 +08:00
|
|
|
|
};
|
|
|
|
|
|
int addVarTableId = await _wpfDataService.VariableTableDataService.AddVariableTable(
|
2025-10-07 17:51:24 +08:00
|
|
|
|
_mapper.Map<VariableTable>(VariableTableItem),
|
2025-10-02 13:19:55 +08:00
|
|
|
|
tableMenu, true);
|
|
|
|
|
|
|
|
|
|
|
|
if (addVarTableId > 0)
|
|
|
|
|
|
{
|
2025-10-06 18:17:56 +08:00
|
|
|
|
VariableTableItem.Id = addVarTableId;
|
|
|
|
|
|
VariableTableItem.Device = CurrentDevice;
|
|
|
|
|
|
CurrentDevice.VariableTables.Add(VariableTableItem);
|
|
|
|
|
|
_dataStorageService.VariableTables.Add(VariableTableItem.Id, VariableTableItem);
|
|
|
|
|
|
_notificationService.ShowSuccess($"添加变量表成功:{VariableTableItem.Name}");
|
2025-10-02 13:19:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-10-06 18:17:56 +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)
|
|
|
|
|
|
{
|
|
|
|
|
|
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
|
|
|
|
// 如果用户取消或对话框未返回变量表,则直接返回
|
|
|
|
|
|
if (variableTable == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-09 13:35:16 +08:00
|
|
|
|
if (await _wpfDataService.VariableDataService.UpdateVariableTable(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]
|
|
|
|
|
|
private async Task DeleteVariableTable()
|
|
|
|
|
|
{
|
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-07-30 13:21:03 +08:00
|
|
|
|
if (SelectedVariableTable == null)
|
|
|
|
|
|
{
|
2025-09-04 17:29:24 +08:00
|
|
|
|
_notificationService.ShowError("你没有选择任何变量表,请选择变量表后再点击删除变量表");
|
2025-07-30 13:21:03 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-23 09:09:07 +08:00
|
|
|
|
string message = $"确认要删除变量表名为:{SelectedVariableTable.Name} \n\n此操作将同时删除该变量表下的所有变量数据,且无法恢复!";
|
2025-10-02 13:19:55 +08:00
|
|
|
|
ConfirmDialogViewModel viewModel = new ConfirmDialogViewModel("删除变量表", message, "删除");
|
2025-09-03 18:08:42 +08:00
|
|
|
|
if (await _dialogService.ShowDialogAsync(viewModel))
|
2025-07-30 13:21:03 +08:00
|
|
|
|
{
|
2025-09-03 18:08:42 +08:00
|
|
|
|
var tableName = SelectedVariableTable.Name;
|
2025-10-02 13:19:55 +08:00
|
|
|
|
if (await _wpfDataService.VariableDataService.DeleteVariableTable(SelectedVariableTable, true))
|
2025-07-30 13:21:03 +08:00
|
|
|
|
{
|
2025-10-03 23:31:04 +08:00
|
|
|
|
if (SelectedVariableTable.Device != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_dataStorageService.Devices.TryGetValue(SelectedVariableTable.DeviceId ,out var device))
|
|
|
|
|
|
{
|
|
|
|
|
|
device.VariableTables.Remove(SelectedVariableTable);
|
|
|
|
|
|
}
|
|
|
|
|
|
_dataStorageService.VariableTables.Remove(SelectedVariableTable.Id);
|
|
|
|
|
|
SelectedVariableTable.Device.VariableTables.Remove(SelectedVariableTable);
|
|
|
|
|
|
}
|
2025-09-04 17:29:24 +08:00
|
|
|
|
_notificationService.ShowSuccess($"变量表:{tableName},删除成功。");
|
2025-09-03 18:08:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-09-04 17:29:24 +08:00
|
|
|
|
_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-03 22:28:58 +08:00
|
|
|
|
if (_dataStorageService.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
|
|
|
|
// var menu = _dataStorageService.Menus.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu &&
|
|
|
|
|
|
// m.TargetId == SelectedVariableTable.Id);
|
|
|
|
|
|
// if (menu == null) return;
|
|
|
|
|
|
_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
|
|
|
|
}
|