diff --git a/DMS.Application/Profiles/MappingProfile.cs b/DMS.Application/Profiles/MappingProfile.cs index 4645918..f1d6750 100644 --- a/DMS.Application/Profiles/MappingProfile.cs +++ b/DMS.Application/Profiles/MappingProfile.cs @@ -24,6 +24,8 @@ public class MappingProfile : Profile CreateMap() .ReverseMap(); + CreateMap() + .ReverseMap(); // MqttServer 映射 CreateMap().ReverseMap(); diff --git a/DMS.Application/Services/Management/VariableManagementService.cs b/DMS.Application/Services/Management/VariableManagementService.cs index a698369..22c8053 100644 --- a/DMS.Application/Services/Management/VariableManagementService.cs +++ b/DMS.Application/Services/Management/VariableManagementService.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using AutoMapper; using DMS.Application.DTOs; using DMS.Application.Events; using DMS.Application.Interfaces; @@ -15,17 +16,20 @@ public class VariableManagementService : IVariableManagementService { private readonly IVariableAppService _variableAppService; private readonly IEventService _eventService; + private readonly IMapper _mapper; private readonly IAppDataStorageService _appDataStorageService; private readonly IDataProcessingService _dataProcessingService; public VariableManagementService(IVariableAppService variableAppService, IEventService eventService, + IMapper mapper, IAppDataStorageService appDataStorageService, IDataProcessingService dataProcessingService) { _variableAppService = variableAppService; _eventService = eventService; + _mapper = mapper; _appDataStorageService = appDataStorageService; _dataProcessingService = dataProcessingService; } @@ -116,7 +120,12 @@ public class VariableManagementService : IVariableManagementService variableTableDto = variableTable; } - _appDataStorageService.Variables.AddOrUpdate(variableDto.Id, variableDto, (key, oldValue) => variableDto); + if (_appDataStorageService.Variables.TryGetValue(variableDto.Id,out var mVariableDto)) + { + _mapper.Map(variableDto, mVariableDto); + } + + // _appDataStorageService.Variables.AddOrUpdate(variableDto.Id, variableDto, (key, oldValue) => variableDto); _eventService.RaiseVariableChanged( this, new VariableChangedEventArgs(DataChangeType.Updated, variableDto, variableTableDto)); } diff --git a/DMS.WPF/Services/DialogService.cs b/DMS.WPF/Services/DialogService.cs index 349c886..a16bc53 100644 --- a/DMS.WPF/Services/DialogService.cs +++ b/DMS.WPF/Services/DialogService.cs @@ -25,7 +25,8 @@ namespace DMS.WPF.Services { typeof(AlarmSettingsDialogViewModel), typeof(AlarmSettingsDialog) }, { typeof(EmailAccountDialogViewModel), typeof(EmailAccountDialog) }, { typeof(EmailTemplateDialogViewModel), typeof(EmailTemplateDialog) }, - { typeof(TriggerDialogViewModel), typeof(TriggerDialog) } + { typeof(TriggerDialogViewModel), typeof(TriggerDialog) }, + { typeof(InputDialogViewModel), typeof(InputDialog) } // Add other mappings here // ... other dialogs }; diff --git a/DMS.WPF/ViewModels/Dialogs/InputDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/InputDialogViewModel.cs new file mode 100644 index 0000000..1a5ef5a --- /dev/null +++ b/DMS.WPF/ViewModels/Dialogs/InputDialogViewModel.cs @@ -0,0 +1,64 @@ +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; + +namespace DMS.WPF.ViewModels.Dialogs; + +public partial class InputDialogViewModel : DialogViewModelBase +{ + [ObservableProperty] + private string _message = string.Empty; + + [ObservableProperty] + private string _inputLabel = "输入"; + + [ObservableProperty] + private string _inputPlaceholder = "请输入..."; + + [ObservableProperty] + private string? _inputText; + + [ObservableProperty] + private string _primaryButtonText = "确定"; + + [ObservableProperty] + private string _closeButtonText = "取消"; + + public InputDialogViewModel(string title, string message) + { + Title = title; + Message = message; + } + + public InputDialogViewModel(string title, string message, string inputLabel) : this(title, message) + { + InputLabel = inputLabel; + } + + public InputDialogViewModel(string title, string message, string inputLabel, string inputPlaceholder) : this(title, message, inputLabel) + { + InputPlaceholder = inputPlaceholder; + } + + public InputDialogViewModel(string title, string message, string inputLabel, string inputPlaceholder, string? defaultValue) : this(title, message, inputLabel, inputPlaceholder) + { + InputText = defaultValue; + } + + public InputDialogViewModel() + { + Title = "输入"; + Message = "请输入信息:"; + } + + [RelayCommand] + private void Confirm() + { + Close(InputText); + } + + [RelayCommand] + private void Cancel() + { + Close(null); + } +} \ No newline at end of file diff --git a/DMS.WPF/ViewModels/VariableTableViewModel.cs b/DMS.WPF/ViewModels/VariableTableViewModel.cs index efeedb2..f9c7d1d 100644 --- a/DMS.WPF/ViewModels/VariableTableViewModel.cs +++ b/DMS.WPF/ViewModels/VariableTableViewModel.cs @@ -11,6 +11,7 @@ using DMS.Core.Events; using DMS.Core.Models; using DMS.WPF.Interfaces; using DMS.WPF.ViewModels.Dialogs; +using DMS.WPF.ViewModels.Dialogs; using DMS.WPF.ViewModels.Items; using Microsoft.Extensions.DependencyInjection; using ObservableCollections; @@ -539,6 +540,48 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable } } + [RelayCommand] + public async Task ModifyConversionFormula() + { + // 检查是否有变量被选中 + if (SelectedVariables.Count == 0) + { + _notificationService.ShowInfo("请选择要修改转换公式的变量"); + return; + } + + // 获取选中的变量列表 + var validVariables = SelectedVariables.Cast().ToList(); + + // --- 对话框调用 --- + var viewModel = new InputDialogViewModel("修改数值转换公式", "请输入新的转换公式 (使用'x'代表变量值):", validVariables.First().ConversionFormula); + string newFormula = await _dialogService.ShowDialogAsync(viewModel); + + if (newFormula != null) // 当用户输入了新公式并确认后 + { + // 更新所有选定变量的转换公式 + foreach (var variable in validVariables) + { + variable.ConversionFormula = newFormula; + variable.UpdatedAt = DateTime.Now; + } + + // 批量更新数据库中的变量数据 + var variableDtos = _mapper.Map>(validVariables); + var result = await _variableManagementService.UpdateVariablesAsync(variableDtos); + + if (result > 0) + { + // 显示成功通知 + _notificationService.ShowSuccess($"已成功更新 {validVariables.Count} 个变量的转换公式"); + } + else + { + _notificationService.ShowError("更新转换公式失败"); + } + } + } + /// /// 为选定的变量添加MQTT服务器。 diff --git a/DMS.WPF/Views/Dialogs/InputDialog.xaml b/DMS.WPF/Views/Dialogs/InputDialog.xaml new file mode 100644 index 0000000..ffbd753 --- /dev/null +++ b/DMS.WPF/Views/Dialogs/InputDialog.xaml @@ -0,0 +1,25 @@ + + + + + + + + \ No newline at end of file diff --git a/DMS.WPF/Views/Dialogs/InputDialog.xaml.cs b/DMS.WPF/Views/Dialogs/InputDialog.xaml.cs new file mode 100644 index 0000000..ad616e5 --- /dev/null +++ b/DMS.WPF/Views/Dialogs/InputDialog.xaml.cs @@ -0,0 +1,11 @@ +using iNKORE.UI.WPF.Modern.Controls; + +namespace DMS.WPF.Views.Dialogs; + +public partial class InputDialog : ContentDialog +{ + public InputDialog() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/DMS.WPF/Views/VariableTableView.xaml b/DMS.WPF/Views/VariableTableView.xaml index 1d23201..9067ee3 100644 --- a/DMS.WPF/Views/VariableTableView.xaml +++ b/DMS.WPF/Views/VariableTableView.xaml @@ -186,6 +186,13 @@ + + + + +