diff --git a/DMS.Application/Interfaces/IVariableAppService.cs b/DMS.Application/Interfaces/IVariableAppService.cs index efd3bf8..df1952c 100644 --- a/DMS.Application/Interfaces/IVariableAppService.cs +++ b/DMS.Application/Interfaces/IVariableAppService.cs @@ -27,6 +27,11 @@ public interface IVariableAppService /// Task UpdateVariableAsync(VariableDto variableDto); + /// + /// 异步更新一个已存在的变量。 + /// + Task UpdateVariablesAsync(List variableDtos); + /// /// 异步删除一个变量。 /// diff --git a/DMS.Application/Services/VariableAppService.cs b/DMS.Application/Services/VariableAppService.cs index c0c5a6c..20b93c2 100644 --- a/DMS.Application/Services/VariableAppService.cs +++ b/DMS.Application/Services/VariableAppService.cs @@ -100,6 +100,41 @@ public class VariableAppService : IVariableAppService } } + /// + /// 异步批量更新变量(事务性操作)。 + /// + /// 要更新的变量数据传输对象列表。 + /// 受影响的行数。 + /// 如果更新变量时发生错误。 + public async Task UpdateVariablesAsync(List variableDtos) + { + try + { + await _repoManager.BeginTranAsync(); + int totalAffected = 0; + + foreach (var variableDto in variableDtos) + { + var variable = await _repoManager.Variables.GetByIdAsync(variableDto.Id); + if (variable == null) + { + throw new ApplicationException($"Variable with ID {variableDto.Id} not found."); + } + _mapper.Map(variableDto, variable); + int res = await _repoManager.Variables.UpdateAsync(variable); + totalAffected += res; + } + + await _repoManager.CommitAsync(); + return totalAffected; + } + catch (Exception ex) + { + await _repoManager.RollbackAsync(); + throw new ApplicationException($"批量更新变量时发生错误,操作已回滚,错误信息:{ex.Message}", ex); + } + } + /// /// 异步删除一个变量(事务性操作)。 /// diff --git a/DMS.WPF/Services/DialogService.cs b/DMS.WPF/Services/DialogService.cs index d1f31f1..0413fd9 100644 --- a/DMS.WPF/Services/DialogService.cs +++ b/DMS.WPF/Services/DialogService.cs @@ -20,6 +20,7 @@ namespace DMS.WPF.Services { typeof(ImportExcelDialogViewModel), typeof(ImportExcelDialog) }, { typeof(ImportOpcUaDialogViewModel), typeof(ImportOpcUaDialog) }, { typeof(VariableDialogViewModel), typeof(VariableDialog) }, + { typeof(PollLevelDialogViewModel), typeof(PollLevelDialog) }, // { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here // ... other dialogs }; diff --git a/DMS.WPF/ViewModels/Dialogs/PollLevelDialogViewModel.cs b/DMS.WPF/ViewModels/Dialogs/PollLevelDialogViewModel.cs index 327793c..1d47aa7 100644 --- a/DMS.WPF/ViewModels/Dialogs/PollLevelDialogViewModel.cs +++ b/DMS.WPF/ViewModels/Dialogs/PollLevelDialogViewModel.cs @@ -2,11 +2,12 @@ using System; using System.Collections.Generic; using System.Linq; using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; using DMS.Core.Enums; namespace DMS.WPF.ViewModels.Dialogs { - public partial class PollLevelDialogViewModel : ObservableObject + public partial class PollLevelDialogViewModel : DialogViewModelBase { [ObservableProperty] private PollLevelType _selectedPollLevelType; @@ -17,6 +18,20 @@ namespace DMS.WPF.ViewModels.Dialogs { PollLevelTypes = Enum.GetValues(typeof(PollLevelType)).Cast().ToList(); SelectedPollLevelType = currentPollLevelType; + Title = "修改轮询频率"; + PrimaryButText = "确定"; + } + + [RelayCommand] + private void PrimaryButton() + { + Close(SelectedPollLevelType); + } + + [RelayCommand] + private void CancleButton() + { + Close(null); } } } diff --git a/DMS.WPF/ViewModels/VariableTableViewModel.cs b/DMS.WPF/ViewModels/VariableTableViewModel.cs index 5bb5719..cddd786 100644 --- a/DMS.WPF/ViewModels/VariableTableViewModel.cs +++ b/DMS.WPF/ViewModels/VariableTableViewModel.cs @@ -277,7 +277,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable { try { - if (CurrentVariableTable.Device==null) + if (CurrentVariableTable.Device == null) { NotificationHelper.ShowError("当前变量表的Device对象为空,请检查。"); return; @@ -454,37 +454,44 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable /// /// 要修改轮询频率的变量数据列表。 [RelayCommand] - public async Task ChangePollLevel(IList variablesToChange) + public async Task ChangePollLevel() { - // var validVariables = variablesToChange?.OfType() - // .ToList(); - // - // // 检查是否有变量被选中 - // if (validVariables == null || !validVariables.Any()) - // { - // NotificationHelper.ShowInfo("请选择要修改轮询频率的变量"); - // return; - // } - // - // // 显示轮询频率选择对话框,并传入第一个变量的当前轮询频率作为默认值 - // var newPollLevelType = await _dialogService.ShowPollLevelDialog(validVariables.First() - // .PollLevelType); - // if (newPollLevelType.HasValue) - // { - // // 更新所有选定变量的轮询频率和修改状态 - // foreach (var variable in validVariables) - // { - // variable.PollLevelType = newPollLevelType.Value; - // variable.IsModified = false; // 标记为未修改,因为已保存到数据库 - // } - // - // // 批量更新数据库中的变量数据 - // await _varDataRepository.UpdateAsync(validVariables); - // - // await RefreshDataView(); - // // 显示成功通知 - // NotificationHelper.ShowSuccess($"已成功更新 {validVariables.Count} 个变量的轮询频率"); - // } + // 检查是否有变量被选中 + if (SelectedVariables.Count == 0) + { + NotificationHelper.ShowInfo("请选择要修改轮询频率的变量"); + return; + } + + // 获取选中的变量列表 + var validVariables = SelectedVariables.Cast().ToList(); + + // 显示轮询频率选择对话框,并传入第一个变量的当前轮询频率作为默认值 + PollLevelDialogViewModel viewModel = new PollLevelDialogViewModel(validVariables.First().PollLevel); + var newPollLevelType = await _dialogService.ShowDialogAsync(viewModel); + if (newPollLevelType.HasValue) + { + // 更新所有选定变量的轮询频率和修改状态 + foreach (var variable in validVariables) + { + variable.PollLevel = newPollLevelType.Value; + variable.UpdatedAt = DateTime.Now; + } + + // 批量更新数据库中的变量数据 + var variableDtos = _mapper.Map>(validVariables); + var result = await _variableAppService.UpdateVariablesAsync(variableDtos); + + if (result > 0) + { + // 显示成功通知 + NotificationHelper.ShowSuccess($"已成功更新 {validVariables.Count} 个变量的轮询频率"); + } + else + { + NotificationHelper.ShowError("更新轮询频率失败"); + } + } } /// diff --git a/DMS.WPF/Views/Dialogs/PollLevelDialog.xaml b/DMS.WPF/Views/Dialogs/PollLevelDialog.xaml index e6b9dbf..30d28b1 100644 --- a/DMS.WPF/Views/Dialogs/PollLevelDialog.xaml +++ b/DMS.WPF/Views/Dialogs/PollLevelDialog.xaml @@ -1,28 +1,30 @@ - + - + - + diff --git a/DMS.WPF/Views/Dialogs/PollLevelDialog.xaml.cs b/DMS.WPF/Views/Dialogs/PollLevelDialog.xaml.cs index 4979a0a..f9856f5 100644 --- a/DMS.WPF/Views/Dialogs/PollLevelDialog.xaml.cs +++ b/DMS.WPF/Views/Dialogs/PollLevelDialog.xaml.cs @@ -5,10 +5,9 @@ namespace DMS.WPF.Views.Dialogs { public partial class PollLevelDialog : ContentDialog { - public PollLevelDialog(PollLevelDialogViewModel viewModel) + public PollLevelDialog() { InitializeComponent(); - DataContext = viewModel; } } } diff --git a/DMS.WPF/Views/VariableTableView.xaml b/DMS.WPF/Views/VariableTableView.xaml index b17a7ee..6132f99 100644 --- a/DMS.WPF/Views/VariableTableView.xaml +++ b/DMS.WPF/Views/VariableTableView.xaml @@ -148,7 +148,7 @@ - +