修改了历史记录页面

This commit is contained in:
2025-09-12 08:35:35 +08:00
parent 5680c38fa9
commit 8afb989486
2 changed files with 143 additions and 34 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections; using System.Collections;
using System.Collections.ObjectModel;
using AutoMapper; using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
@@ -38,7 +39,7 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
/// 建议的变量列表 /// 建议的变量列表
/// </summary> /// </summary>
[ObservableProperty] [ObservableProperty]
private List<VariableHistoryDto> _suggestedVariables; private ObservableCollection<VariableHistoryDto> _suggestedVariables;
/// <summary> /// <summary>
/// 历史记录条数限制 /// 历史记录条数限制
@@ -58,6 +59,12 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
[ObservableProperty] [ObservableProperty]
private DateTime? _endTime; private DateTime? _endTime;
/// <summary>
/// 选中的变量历史记录
/// </summary>
[ObservableProperty]
private VariableHistoryDto _selectedVariable;
/// <summary> /// <summary>
/// 变量历史记录列表 /// 变量历史记录列表
/// </summary> /// </summary>
@@ -85,12 +92,13 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
_variableHistorySynchronizedView = _variableHistoryList.CreateView(v => v); _variableHistorySynchronizedView = _variableHistoryList.CreateView(v => v);
VariableHistories = _variableHistorySynchronizedView.ToNotifyCollectionChanged(); VariableHistories = _variableHistorySynchronizedView.ToNotifyCollectionChanged();
_allVariableHistories = new List<VariableHistoryDto>(); _allVariableHistories = new List<VariableHistoryDto>();
_suggestedVariables = new List<VariableHistoryDto>(); _suggestedVariables = new ObservableCollection<VariableHistoryDto>();
// 初始化默认值 // 初始化默认值
_historyLimit = 1000; // 默认限制1000条记录 _historyLimit = 1000; // 默认限制1000条记录
_startTime = null; _startTime = null;
_endTime = null; _endTime = null;
_selectedVariable = new VariableHistoryDto();
} }
/// <summary> /// <summary>
@@ -123,19 +131,27 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
/// </summary> /// </summary>
private void UpdateSuggestedVariables() private void UpdateSuggestedVariables()
{ {
// 清空现有建议列表
_suggestedVariables.Clear();
if (string.IsNullOrWhiteSpace(SearchText)) if (string.IsNullOrWhiteSpace(SearchText))
{ {
// 如果搜索文本为空,显示所有唯一的变量名 // 如果搜索文本为空,显示所有唯一的变量名
_suggestedVariables = _allVariableHistories var uniqueVariables = _allVariableHistories
.GroupBy(h => h.VariableName) .GroupBy(h => h.VariableName)
.Select(g => g.First()) .Select(g => g.First())
.Take(10) .Take(10)
.ToList(); .ToList();
foreach (var variable in uniqueVariables)
{
_suggestedVariables.Add(variable);
}
} }
else else
{ {
// 根据搜索文本过滤建议列表 // 根据搜索文本过滤建议列表
_suggestedVariables = _allVariableHistories var filteredVariables = _allVariableHistories
.Where(h => .Where(h =>
h.VariableName?.Contains(SearchText, StringComparison.OrdinalIgnoreCase) == h.VariableName?.Contains(SearchText, StringComparison.OrdinalIgnoreCase) ==
true) true)
@@ -143,12 +159,40 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
.Select(g => g.First()) .Select(g => g.First())
.Take(10) .Take(10)
.ToList(); .ToList();
foreach (var variable in filteredVariables)
{
_suggestedVariables.Add(variable);
}
}
} }
public async Task OnNavigatedToAsync(MenuItemViewModel menu)
{
// 加载所有变量的历史记录
LoadAllVariableHistories(HistoryLimit, StartTime, EndTime);
} }
/// <summary> /// <summary>
/// 搜索变量历史记录 /// 重新加载历史记录命令
/// </summary>
[RelayCommand]
private void Reload()
{
LoadAllVariableHistories(HistoryLimit, StartTime, EndTime);
}
/// <summary>
/// 更新建议列表命令
/// </summary>
[RelayCommand]
private void UpdateSuggestions()
{
UpdateSuggestedVariables();
}
/// <summary>
/// 当搜索文本改变时触发
/// </summary> /// </summary>
/// <param name="value"></param> /// <param name="value"></param>
partial void OnSearchTextChanged(string value) partial void OnSearchTextChanged(string value)
@@ -156,7 +200,7 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
// 更新建议列表 // 更新建议列表
UpdateSuggestedVariables(); UpdateSuggestedVariables();
if (string.IsNullOrWhiteSpace(SearchText)) if (string.IsNullOrWhiteSpace(value))
{ {
// 如果搜索文本为空,显示所有历史记录 // 如果搜索文本为空,显示所有历史记录
_variableHistoryList.Clear(); _variableHistoryList.Clear();
@@ -168,7 +212,7 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
var filteredHistories = _allVariableHistories var filteredHistories = _allVariableHistories
.Where(h => .Where(h =>
h.VariableName?.Contains( h.VariableName?.Contains(
SearchText, StringComparison.OrdinalIgnoreCase) == true) value, StringComparison.OrdinalIgnoreCase) == true)
.ToList(); .ToList();
_variableHistoryList.Clear(); _variableHistoryList.Clear();
@@ -176,10 +220,30 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable
} }
} }
public async Task OnNavigatedToAsync(MenuItemViewModel menu) /// <summary>
/// 根据搜索文本过滤历史记录
/// </summary>
/// <param name="searchText"></param>
private void FilterHistoriesBySearchText(string searchText)
{ {
// 加载所有变量的历史记录 if (string.IsNullOrWhiteSpace(searchText))
LoadAllVariableHistories(HistoryLimit, StartTime, EndTime); {
// 如果搜索文本为空,显示所有历史记录
_variableHistoryList.Clear();
_variableHistoryList.AddRange(_allVariableHistories);
}
else
{
// 根据搜索文本过滤历史记录
var filteredHistories = _allVariableHistories
.Where(h =>
h.VariableName?.Contains(
searchText, StringComparison.OrdinalIgnoreCase) == true)
.ToList();
_variableHistoryList.Clear();
_variableHistoryList.AddRange(filteredHistories);
}
} }
/// <summary> /// <summary>

View File

@@ -6,6 +6,7 @@
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;assembly=DMS.Core" xmlns:enums="clr-namespace:DMS.Core.Enums;assembly=DMS.Core"
xmlns:ex="clr-namespace:DMS.Extensions" xmlns:ex="clr-namespace:DMS.Extensions"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:helper="clr-namespace:DMS.WPF.Helper" xmlns:helper="clr-namespace:DMS.WPF.Helper"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf" xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
@@ -48,6 +49,7 @@
Spacing="10"> Spacing="10">
<TextBlock Style="{StaticResource VarHistoryLabelStyle}" Text="搜索变量:" /> <TextBlock Style="{StaticResource VarHistoryLabelStyle}" Text="搜索变量:" />
<ui:AutoSuggestBox <ui:AutoSuggestBox
x:Name="SearchAutoSuggestBox"
Width="200" Width="200"
ui:ControlHelper.PlaceholderText="搜索变量..." ui:ControlHelper.PlaceholderText="搜索变量..."
Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"
@@ -58,6 +60,48 @@
</ikw:SimpleStackPanel> </ikw:SimpleStackPanel>
</ikw:SimpleStackPanel> </ikw:SimpleStackPanel>
</GroupBox> </GroupBox>
<!-- 时间范围和详细信息区域 -->
<GroupBox Header="详细信息">
<ikw:SimpleStackPanel Margin="5" Spacing="10">
<!-- 变量详细信息 -->
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Style="{StaticResource VarHistoryLabelStyle}" Text="变量ID:" />
<TextBlock Grid.Row="0" Grid.Column="1" Style="{StaticResource VarHistoryValueStyle}" Text="{Binding SelectedVariable.Id}" />
<TextBlock Grid.Row="0" Grid.Column="2" Style="{StaticResource VarHistoryLabelStyle}" Text="变量名:" />
<TextBlock Grid.Row="0" Grid.Column="3" Style="{StaticResource VarHistoryValueStyle}" Text="{Binding SelectedVariable.VariableName}" />
<TextBlock Grid.Row="1" Grid.Column="0" Style="{StaticResource VarHistoryLabelStyle}" Text="当前值:" />
<TextBlock Grid.Row="1" Grid.Column="1" Style="{StaticResource VarHistoryValueStyle}" Text="{Binding SelectedVariable.Value}" />
<TextBlock Grid.Row="1" Grid.Column="2" Style="{StaticResource VarHistoryLabelStyle}" Text="更新时间:" />
<TextBlock Grid.Row="1" Grid.Column="3" Style="{StaticResource VarHistoryValueStyle}" Text="{Binding SelectedVariable.Timestamp, StringFormat='{}{0:yyyy-MM-dd HH:mm:ss.fff}'}" />
</Grid>
<!-- 时间范围选择 -->
<ikw:SimpleStackPanel Orientation="Horizontal" Spacing="10">
<TextBlock Style="{StaticResource VarHistoryLabelStyle}" Text="开始时间:" />
<hc:DateTimePicker Width="200" SelectedDateTime="{Binding StartTime}" />
<TextBlock Style="{StaticResource VarHistoryLabelStyle}" Text="结束时间:" />
<hc:DateTimePicker Width="200" SelectedDateTime="{Binding EndTime}" />
<Button Content="重新加载" Command="{Binding ReloadCommand}" />
</ikw:SimpleStackPanel>
</ikw:SimpleStackPanel>
</GroupBox>
</ikw:SimpleStackPanel> </ikw:SimpleStackPanel>
<DataGrid <DataGrid
@@ -67,6 +111,7 @@
CanUserSortColumns="False" CanUserSortColumns="False"
IsReadOnly="True" IsReadOnly="True"
ItemsSource="{Binding VariableHistories}" ItemsSource="{Binding VariableHistories}"
SelectedItem="{Binding SelectedVariable}"
SelectionMode="Single" SelectionMode="Single"
Style="{StaticResource DataGridBaseStyle}"> Style="{StaticResource DataGridBaseStyle}">
<DataGrid.Columns> <DataGrid.Columns>