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-07-30 12:54:14 +08:00
|
|
|
|
using DMS.Helper;
|
2025-07-19 09:25:01 +08:00
|
|
|
|
using DMS.WPF.Services;
|
2025-07-19 11:11:01 +08:00
|
|
|
|
using DMS.Services;
|
2025-07-30 12:54:14 +08:00
|
|
|
|
using DMS.WPF.ViewModels.Dialogs;
|
2025-07-26 10:05:43 +08:00
|
|
|
|
using DMS.WPF.ViewModels.Items;
|
2025-07-30 12:54:14 +08:00
|
|
|
|
using iNKORE.UI.WPF.Modern.Common.IconKeys;
|
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-07-30 12:54:14 +08:00
|
|
|
|
public partial class DeviceDetailViewModel : ViewModelBase, INavigatable
|
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-07-30 12:09:00 +08:00
|
|
|
|
private readonly INavigationService _navigationService;
|
2025-09-03 18:08:42 +08:00
|
|
|
|
public DataServices DataServices { get; }
|
2025-07-16 18:39:00 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-07-26 10:05:43 +08:00
|
|
|
|
private DeviceItemViewModel _currentDevice;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
|
|
|
|
|
|
[ObservableProperty]
|
2025-07-30 13:21:03 +08:00
|
|
|
|
private VariableTableItemViewModel _selectedVariableTable;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
|
2025-07-30 12:54:14 +08:00
|
|
|
|
public DeviceDetailViewModel(IMapper mapper, IDialogService dialogService, INavigationService navigationService,
|
2025-09-03 18:08:42 +08:00
|
|
|
|
DataServices dataServices)
|
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-07-30 12:09:00 +08:00
|
|
|
|
_navigationService = navigationService;
|
|
|
|
|
|
DataServices = dataServices;
|
2025-07-16 18:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private async Task AddVariableTable()
|
|
|
|
|
|
{
|
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. 显示添加设备对话框
|
|
|
|
|
|
var variableTableItemViewModel = await _dialogService.ShowDialogAsync(variableTableDialogViewModel);
|
|
|
|
|
|
// 如果用户取消或对话框未返回设备,则直接返回
|
|
|
|
|
|
if (variableTableItemViewModel == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-03 18:08:42 +08:00
|
|
|
|
variableTableItemViewModel.DeviceId = CurrentDevice.Id;
|
|
|
|
|
|
var tableMenu = new MenuBeanDto()
|
2025-07-30 12:54:14 +08:00
|
|
|
|
{
|
|
|
|
|
|
Header = variableTableItemViewModel.Name,
|
|
|
|
|
|
Icon = SegoeFluentIcons.DataSense.Glyph
|
|
|
|
|
|
};
|
2025-09-03 18:08:42 +08:00
|
|
|
|
|
|
|
|
|
|
if (await DataServices.AddVariableTable(_mapper.Map<VariableTableDto>(variableTableItemViewModel),
|
|
|
|
|
|
tableMenu, true))
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowSuccess($"添加变量表成功:{variableTableItemViewModel.Name}");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError($"添加变量表失败:{variableTableItemViewModel.Name}!!");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-30 12:54:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError($"添加变量表时发生错误: {ex.Message}", ex);
|
|
|
|
|
|
}
|
2025-07-16 18:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private async Task EditVariableTable()
|
|
|
|
|
|
{
|
2025-07-30 13:21:03 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectedVariableTable == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError("你没有选择任何变量表,请选择变量表后再点击编辑变量表");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VariableTableDialogViewModel variableTableDialogViewModel = new VariableTableDialogViewModel(SelectedVariableTable)
|
|
|
|
|
|
{
|
2025-08-23 16:01:30 +08:00
|
|
|
|
PrimaryButText = "编辑变量表"
|
2025-07-30 13:21:03 +08:00
|
|
|
|
};
|
|
|
|
|
|
// 1. 显示变量表对话框
|
|
|
|
|
|
VariableTableItemViewModel variableTable = await _dialogService.ShowDialogAsync(variableTableDialogViewModel);
|
|
|
|
|
|
// 如果用户取消或对话框未返回变量表,则直接返回
|
|
|
|
|
|
if (variableTable == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-03 18:08:42 +08:00
|
|
|
|
if (await DataServices.UpdateVariableTable(variableTable))
|
2025-07-30 13:21:03 +08:00
|
|
|
|
{
|
2025-09-03 18:08:42 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"编辑变量表成功:{variableTable.Name}");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError($"编辑变量表失败:{variableTable.Name}");
|
2025-07-30 13:21:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
2025-07-30 12:09:00 +08:00
|
|
|
|
{
|
2025-07-30 13:21:03 +08:00
|
|
|
|
NotificationHelper.ShowError($"编辑变量表的过程中发生错误:{e.Message}", e);
|
2025-07-30 12:09:00 +08:00
|
|
|
|
}
|
2025-07-16 18:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[RelayCommand]
|
|
|
|
|
|
private async Task DeleteVariableTable()
|
|
|
|
|
|
{
|
2025-07-30 13:21:03 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (SelectedVariableTable == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError("你没有选择任何变量表,请选择变量表后再点击删除变量表");
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-23 09:09:07 +08:00
|
|
|
|
string message = $"确认要删除变量表名为:{SelectedVariableTable.Name} \n\n此操作将同时删除该变量表下的所有变量数据,且无法恢复!";
|
2025-08-24 11:31:07 +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;
|
|
|
|
|
|
if (await DataServices.DeleteVariableTable(SelectedVariableTable,true))
|
2025-07-30 13:21:03 +08:00
|
|
|
|
{
|
2025-09-03 18:08:42 +08:00
|
|
|
|
NotificationHelper.ShowSuccess($"变量表:{tableName},删除成功。");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError($"变量表:{tableName},删除失败!!!");
|
2025-07-30 13:21:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
NotificationHelper.ShowError($"删除变量表的过程中发生错误:{e.Message}", e);
|
|
|
|
|
|
}
|
2025-07-16 18:39:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-16 19:37:13 +08:00
|
|
|
|
|
2025-07-30 12:09:00 +08:00
|
|
|
|
|
|
|
|
|
|
public async Task OnNavigatedToAsync(MenuItemViewModel menu)
|
|
|
|
|
|
{
|
2025-07-30 12:54:14 +08:00
|
|
|
|
var device = DataServices.Devices.FirstOrDefault(d => d.Id == menu.TargetId);
|
|
|
|
|
|
if (device != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
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-07-30 12:54:14 +08:00
|
|
|
|
var menu = DataServices.Menus.FirstOrDefault(m => m.MenuType == MenuType.VariableTableMenu &&
|
|
|
|
|
|
m.TargetId == SelectedVariableTable.Id);
|
|
|
|
|
|
if (menu == null) return;
|
2025-07-30 12:09:00 +08:00
|
|
|
|
_navigationService.NavigateToAsync(menu);
|
2025-07-16 19:37:13 +08:00
|
|
|
|
}
|
2025-06-30 14:19:22 +08:00
|
|
|
|
}
|