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