完成修改更新频率
This commit is contained in:
@@ -27,6 +27,11 @@ public interface IVariableAppService
|
||||
/// </summary>
|
||||
Task<int> UpdateVariableAsync(VariableDto variableDto);
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的变量。
|
||||
/// </summary>
|
||||
Task<int> UpdateVariablesAsync(List<VariableDto> variableDtos);
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个变量。
|
||||
/// </summary>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
@@ -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<PollLevelType?>
|
||||
{
|
||||
[ObservableProperty]
|
||||
private PollLevelType _selectedPollLevelType;
|
||||
@@ -17,6 +18,20 @@ namespace DMS.WPF.ViewModels.Dialogs
|
||||
{
|
||||
PollLevelTypes = Enum.GetValues(typeof(PollLevelType)).Cast<PollLevelType>().ToList();
|
||||
SelectedPollLevelType = currentPollLevelType;
|
||||
Title = "修改轮询频率";
|
||||
PrimaryButText = "确定";
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void PrimaryButton()
|
||||
{
|
||||
Close(SelectedPollLevelType);
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void CancleButton()
|
||||
{
|
||||
Close(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
/// </summary>
|
||||
/// <param name="variablesToChange">要修改轮询频率的变量数据列表。</param>
|
||||
[RelayCommand]
|
||||
public async Task ChangePollLevel(IList<object> variablesToChange)
|
||||
public async Task ChangePollLevel()
|
||||
{
|
||||
// var validVariables = variablesToChange?.OfType<Variable>()
|
||||
// .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<VariableItemViewModel>().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<List<VariableDto>>(validVariables);
|
||||
var result = await _variableAppService.UpdateVariablesAsync(variableDtos);
|
||||
|
||||
if (result > 0)
|
||||
{
|
||||
// 显示成功通知
|
||||
NotificationHelper.ShowSuccess($"已成功更新 {validVariables.Count} 个变量的轮询频率");
|
||||
}
|
||||
else
|
||||
{
|
||||
NotificationHelper.ShowError("更新轮询频率失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -1,28 +1,30 @@
|
||||
<ui:ContentDialog x:Class="DMS.WPF.Views.Dialogs.PollLevelDialog"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
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:enums="clr-namespace:DMS.Core.Enums"
|
||||
xmlns:extensions="clr-namespace:DMS.Extensions"
|
||||
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
|
||||
xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts"
|
||||
mc:Ignorable="d"
|
||||
Title="修改轮询频率"
|
||||
PrimaryButtonText="确定"
|
||||
CloseButtonText="取消"
|
||||
d:DesignHeight="150" d:DesignWidth="300">
|
||||
<ui:ContentDialog
|
||||
x:Class="DMS.WPF.Views.Dialogs.PollLevelDialog"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
|
||||
xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts"
|
||||
xmlns:vmd="clr-namespace:DMS.WPF.ViewModels.Dialogs"
|
||||
Title="{Binding Title}"
|
||||
d:DataContext="{d:DesignInstance vmd:PollLevelDialogViewModel}"
|
||||
CloseButtonCommand="{Binding CancleButtonCommand}"
|
||||
CloseButtonText="取消"
|
||||
PrimaryButtonCommand="{Binding PrimaryButtonCommand}"
|
||||
PrimaryButtonText="{Binding PrimaryButText}"
|
||||
mc:Ignorable="d">
|
||||
<ui:ContentDialog.Resources>
|
||||
<valueConverts:EnumDescriptionConverter x:Key="EnumDescriptionConverter" />
|
||||
</ui:ContentDialog.Resources>
|
||||
<Grid>
|
||||
<ComboBox
|
||||
ItemsSource="{Binding PollLevelTypes}"
|
||||
SelectedItem="{Binding SelectedPollLevelType}"
|
||||
Margin="20">
|
||||
Margin="20"
|
||||
ItemsSource="{Binding PollLevelTypes}"
|
||||
SelectedItem="{Binding SelectedPollLevelType}">
|
||||
<ComboBox.ItemTemplate>
|
||||
<DataTemplate>
|
||||
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}"/>
|
||||
<TextBlock Text="{Binding Converter={StaticResource EnumDescriptionConverter}}" />
|
||||
</DataTemplate>
|
||||
</ComboBox.ItemTemplate>
|
||||
</ComboBox>
|
||||
|
||||
@@ -5,10 +5,9 @@ namespace DMS.WPF.Views.Dialogs
|
||||
{
|
||||
public partial class PollLevelDialog : ContentDialog
|
||||
{
|
||||
public PollLevelDialog(PollLevelDialogViewModel viewModel)
|
||||
public PollLevelDialog()
|
||||
{
|
||||
InitializeComponent();
|
||||
DataContext = viewModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,7 +148,7 @@
|
||||
|
||||
<DataGrid.ContextMenu>
|
||||
<ContextMenu>
|
||||
<MenuItem Command="{Binding AddVarDataCommand}" Header="添加变量">
|
||||
<MenuItem Command="{Binding AddVariableCommand}" Header="添加变量">
|
||||
<MenuItem.Icon>
|
||||
<ui:FontIcon Icon="{x:Static ui:SegoeFluentIcons.Add}" />
|
||||
</MenuItem.Icon>
|
||||
|
||||
Reference in New Issue
Block a user