完成修改更新频率

This commit is contained in:
2025-09-02 17:42:11 +08:00
parent 6d7636d664
commit 94aa9b0485
8 changed files with 118 additions and 54 deletions

View File

@@ -27,6 +27,11 @@ public interface IVariableAppService
/// </summary> /// </summary>
Task<int> UpdateVariableAsync(VariableDto variableDto); Task<int> UpdateVariableAsync(VariableDto variableDto);
/// <summary>
/// 异步更新一个已存在的变量。
/// </summary>
Task<int> UpdateVariablesAsync(List<VariableDto> variableDtos);
/// <summary> /// <summary>
/// 异步删除一个变量。 /// 异步删除一个变量。
/// </summary> /// </summary>

View File

@@ -100,6 +100,41 @@ public class VariableAppService : IVariableAppService
} }
} }
/// <summary>
/// 异步批量更新变量(事务性操作)。
/// </summary>
/// <param name="variableDtos">要更新的变量数据传输对象列表。</param>
/// <returns>受影响的行数。</returns>
/// <exception cref="ApplicationException">如果更新变量时发生错误。</exception>
public async Task<int> UpdateVariablesAsync(List<VariableDto> 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);
}
}
/// <summary> /// <summary>
/// 异步删除一个变量(事务性操作)。 /// 异步删除一个变量(事务性操作)。
/// </summary> /// </summary>

View File

@@ -20,6 +20,7 @@ namespace DMS.WPF.Services
{ typeof(ImportExcelDialogViewModel), typeof(ImportExcelDialog) }, { typeof(ImportExcelDialogViewModel), typeof(ImportExcelDialog) },
{ typeof(ImportOpcUaDialogViewModel), typeof(ImportOpcUaDialog) }, { typeof(ImportOpcUaDialogViewModel), typeof(ImportOpcUaDialog) },
{ typeof(VariableDialogViewModel), typeof(VariableDialog) }, { typeof(VariableDialogViewModel), typeof(VariableDialog) },
{ typeof(PollLevelDialogViewModel), typeof(PollLevelDialog) },
// { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here // { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // Add other mappings here
// ... other dialogs // ... other dialogs
}; };

View File

@@ -2,11 +2,12 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Core.Enums; using DMS.Core.Enums;
namespace DMS.WPF.ViewModels.Dialogs namespace DMS.WPF.ViewModels.Dialogs
{ {
public partial class PollLevelDialogViewModel : ObservableObject public partial class PollLevelDialogViewModel : DialogViewModelBase<PollLevelType?>
{ {
[ObservableProperty] [ObservableProperty]
private PollLevelType _selectedPollLevelType; private PollLevelType _selectedPollLevelType;
@@ -17,6 +18,20 @@ namespace DMS.WPF.ViewModels.Dialogs
{ {
PollLevelTypes = Enum.GetValues(typeof(PollLevelType)).Cast<PollLevelType>().ToList(); PollLevelTypes = Enum.GetValues(typeof(PollLevelType)).Cast<PollLevelType>().ToList();
SelectedPollLevelType = currentPollLevelType; SelectedPollLevelType = currentPollLevelType;
Title = "修改轮询频率";
PrimaryButText = "确定";
}
[RelayCommand]
private void PrimaryButton()
{
Close(SelectedPollLevelType);
}
[RelayCommand]
private void CancleButton()
{
Close(null);
} }
} }
} }

View File

@@ -277,7 +277,7 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
{ {
try try
{ {
if (CurrentVariableTable.Device==null) if (CurrentVariableTable.Device == null)
{ {
NotificationHelper.ShowError("当前变量表的Device对象为空请检查。"); NotificationHelper.ShowError("当前变量表的Device对象为空请检查。");
return; return;
@@ -454,37 +454,44 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable
/// </summary> /// </summary>
/// <param name="variablesToChange">要修改轮询频率的变量数据列表。</param> /// <param name="variablesToChange">要修改轮询频率的变量数据列表。</param>
[RelayCommand] [RelayCommand]
public async Task ChangePollLevel(IList<object> variablesToChange) public async Task ChangePollLevel()
{ {
// var validVariables = variablesToChange?.OfType<Variable>() // 检查是否有变量被选中
// .ToList(); if (SelectedVariables.Count == 0)
// {
// // 检查是否有变量被选中 NotificationHelper.ShowInfo("请选择要修改轮询频率的变量");
// if (validVariables == null || !validVariables.Any()) return;
// { }
// NotificationHelper.ShowInfo("请选择要修改轮询频率的变量");
// return; // 获取选中的变量列表
// } var validVariables = SelectedVariables.Cast<VariableItemViewModel>().ToList();
//
// // 显示轮询频率选择对话框,并传入第一个变量的当前轮询频率作为默认值 // 显示轮询频率选择对话框,并传入第一个变量的当前轮询频率作为默认值
// var newPollLevelType = await _dialogService.ShowPollLevelDialog(validVariables.First() PollLevelDialogViewModel viewModel = new PollLevelDialogViewModel(validVariables.First().PollLevel);
// .PollLevelType); var newPollLevelType = await _dialogService.ShowDialogAsync(viewModel);
// if (newPollLevelType.HasValue) if (newPollLevelType.HasValue)
// { {
// // 更新所有选定变量的轮询频率和修改状态 // 更新所有选定变量的轮询频率和修改状态
// foreach (var variable in validVariables) foreach (var variable in validVariables)
// { {
// variable.PollLevelType = newPollLevelType.Value; variable.PollLevel = newPollLevelType.Value;
// variable.IsModified = false; // 标记为未修改,因为已保存到数据库 variable.UpdatedAt = DateTime.Now;
// } }
//
// // 批量更新数据库中的变量数据 // 批量更新数据库中的变量数据
// await _varDataRepository.UpdateAsync(validVariables); var variableDtos = _mapper.Map<List<VariableDto>>(validVariables);
// var result = await _variableAppService.UpdateVariablesAsync(variableDtos);
// await RefreshDataView();
// // 显示成功通知 if (result > 0)
// NotificationHelper.ShowSuccess($"已成功更新 {validVariables.Count} 个变量的轮询频率"); {
// } // 显示成功通知
NotificationHelper.ShowSuccess($"已成功更新 {validVariables.Count} 个变量的轮询频率");
}
else
{
NotificationHelper.ShowError("更新轮询频率失败");
}
}
} }
/// <summary> /// <summary>

View File

@@ -1,28 +1,30 @@
<ui:ContentDialog x:Class="DMS.WPF.Views.Dialogs.PollLevelDialog" <ui:ContentDialog
x:Class="DMS.WPF.Views.Dialogs.PollLevelDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:enums="clr-namespace:DMS.Core.Enums" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:extensions="clr-namespace:DMS.Extensions"
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts" xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts"
mc:Ignorable="d" xmlns:vmd="clr-namespace:DMS.WPF.ViewModels.Dialogs"
Title="修改轮询频率" Title="{Binding Title}"
PrimaryButtonText="确定" d:DataContext="{d:DesignInstance vmd:PollLevelDialogViewModel}"
CloseButtonCommand="{Binding CancleButtonCommand}"
CloseButtonText="取消" CloseButtonText="取消"
d:DesignHeight="150" d:DesignWidth="300"> PrimaryButtonCommand="{Binding PrimaryButtonCommand}"
PrimaryButtonText="{Binding PrimaryButText}"
mc:Ignorable="d">
<ui:ContentDialog.Resources> <ui:ContentDialog.Resources>
<valueConverts:EnumDescriptionConverter x:Key="EnumDescriptionConverter" /> <valueConverts:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />
</ui:ContentDialog.Resources> </ui:ContentDialog.Resources>
<Grid> <Grid>
<ComboBox <ComboBox
Margin="20"
ItemsSource="{Binding PollLevelTypes}" ItemsSource="{Binding PollLevelTypes}"
SelectedItem="{Binding SelectedPollLevelType}" SelectedItem="{Binding SelectedPollLevelType}">
Margin="20">
<ComboBox.ItemTemplate> <ComboBox.ItemTemplate>
<DataTemplate> <DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}"/> <TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}" />
</DataTemplate> </DataTemplate>
</ComboBox.ItemTemplate> </ComboBox.ItemTemplate>
</ComboBox> </ComboBox>

View File

@@ -5,10 +5,9 @@ namespace DMS.WPF.Views.Dialogs
{ {
public partial class PollLevelDialog : ContentDialog public partial class PollLevelDialog : ContentDialog
{ {
public PollLevelDialog(PollLevelDialogViewModel viewModel) public PollLevelDialog()
{ {
InitializeComponent(); InitializeComponent();
DataContext = viewModel;
} }
} }
} }

View File

@@ -148,7 +148,7 @@
<DataGrid.ContextMenu> <DataGrid.ContextMenu>
<ContextMenu> <ContextMenu>
<MenuItem Command="{Binding AddVarDataCommand}" Header="添加变量"> <MenuItem Command="{Binding AddVariableCommand}" Header="添加变量">
<MenuItem.Icon> <MenuItem.Icon>
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" /> <ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" />
</MenuItem.Icon> </MenuItem.Icon>