Files
DMS/DMS.WPF/ViewModels/VariableHistoryViewModel.cs

273 lines
9.4 KiB
C#
Raw Normal View History

2025-09-11 11:04:07 +08:00
using System.Collections;
2025-09-12 08:35:35 +08:00
using System.Collections.ObjectModel;
2025-09-11 11:04:07 +08:00
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Core.Models;
using DMS.WPF.Interfaces;
using DMS.WPF.ViewModels.Items;
using Microsoft.Extensions.DependencyInjection;
using ObservableCollections;
using System.Linq;
2025-09-16 12:29:09 +08:00
using DMS.Application.Interfaces.Database;
2025-09-11 11:04:07 +08:00
namespace DMS.WPF.ViewModels;
partial class VariableHistoryViewModel : ViewModelBase, INavigatable
2025-09-11 11:04:07 +08:00
{
private readonly IMapper _mapper;
private readonly IDialogService _dialogService;
private readonly IHistoryAppService _historyAppService;
2025-09-11 11:04:07 +08:00
private readonly IWPFDataService _wpfDataService;
private readonly IDataStorageService _dataStorageService;
private readonly INotificationService _notificationService;
/// <summary>
/// 用于过滤变量的搜索文本
2025-09-11 11:04:07 +08:00
/// </summary>
[ObservableProperty]
private string _searchText;
2025-09-11 11:04:07 +08:00
/// <summary>
/// 是否打开建议列表
2025-09-11 11:04:07 +08:00
/// </summary>
[ObservableProperty]
private bool _isSuggestionListOpen;
2025-09-11 11:04:07 +08:00
/// <summary>
/// 建议的变量列表
2025-09-11 11:04:07 +08:00
/// </summary>
[ObservableProperty]
private ObservableCollection<string> _suggestedVariables;
2025-09-11 11:04:07 +08:00
/// <summary>
/// 历史记录条数限制
2025-09-11 11:04:07 +08:00
/// </summary>
[ObservableProperty]
private int? _historyLimit;
2025-09-11 11:04:07 +08:00
/// <summary>
/// 历史记录开始时间
2025-09-11 11:04:07 +08:00
/// </summary>
[ObservableProperty]
private DateTime? _startTime;
2025-09-11 11:04:07 +08:00
/// <summary>
/// 历史记录结束时间
2025-09-11 11:04:07 +08:00
/// </summary>
[ObservableProperty]
private DateTime? _endTime;
2025-09-12 08:35:35 +08:00
/// <summary>
/// 选中的变量历史记录
/// </summary>
[ObservableProperty]
private VariableHistoryDto _selectedVariable;
2025-09-11 11:04:07 +08:00
/// <summary>
/// 变量历史记录列表
/// </summary>
public NotifyCollectionChangedSynchronizedViewList<VariableHistoryDto> VariableHistories { get; }
private readonly ObservableList<VariableHistoryDto> _variableHistoryList;
private readonly ISynchronizedView<VariableHistoryDto, VariableHistoryDto> _variableHistorySynchronizedView;
/// <summary>
/// 所有变量的缓存列表,用于搜索
/// </summary>
private List<VariableHistoryDto> _allVariableHistories;
public VariableHistoryViewModel(IMapper mapper, IDialogService dialogService, IHistoryAppService historyAppService,
IWPFDataService wpfDataService, IDataStorageService dataStorageService,
INotificationService notificationService)
2025-09-11 11:04:07 +08:00
{
_mapper = mapper;
_dialogService = dialogService;
_historyAppService = historyAppService;
2025-09-11 11:04:07 +08:00
_wpfDataService = wpfDataService;
_dataStorageService = dataStorageService;
_notificationService = notificationService;
_variableHistoryList = new ObservableList<VariableHistoryDto>();
_variableHistorySynchronizedView = _variableHistoryList.CreateView(v => v);
VariableHistories = _variableHistorySynchronizedView.ToNotifyCollectionChanged();
_allVariableHistories = new List<VariableHistoryDto>();
_suggestedVariables = new ObservableCollection<string>();
// 初始化默认值
_historyLimit = 1000; // 默认限制1000条记录
_startTime = null;
_endTime = null;
2025-09-12 08:35:35 +08:00
_selectedVariable = new VariableHistoryDto();
2025-09-11 11:04:07 +08:00
}
/// <summary>
/// 加载所有变量的历史记录
2025-09-11 11:04:07 +08:00
/// </summary>
/// <param name="limit">返回记录的最大数量null表示无限制</param>
/// <param name="startTime">开始时间null表示无限制</param>
/// <param name="endTime">结束时间null表示无限制</param>
private async void LoadAllVariableHistories(int? limit = null, DateTime? startTime = null, DateTime? endTime = null)
2025-09-11 11:04:07 +08:00
{
try
2025-09-11 11:04:07 +08:00
{
_variableHistoryList.Clear();
var allHistories = await _historyAppService.GetAllVariableHistoriesAsync(limit, startTime, endTime);
_allVariableHistories = allHistories.ToList();
_variableHistoryList.AddRange(_allVariableHistories);
// 更新建议列表
UpdateSuggestedVariables();
2025-09-11 11:04:07 +08:00
}
catch (Exception ex)
2025-09-11 11:04:07 +08:00
{
// 记录更详细的错误信息
_notificationService.ShowError($"加载变量历史记录失败: {ex.Message}", ex);
2025-09-11 11:04:07 +08:00
}
}
/// <summary>
/// 更新建议的变量列表
/// </summary>
private void UpdateSuggestedVariables()
2025-09-11 11:04:07 +08:00
{
// 清空现有建议列表
_suggestedVariables.Clear();
2025-09-12 08:35:35 +08:00
if (!string.IsNullOrWhiteSpace(SearchText))
2025-09-11 11:04:07 +08:00
{
// // 根据搜索文本过滤建议列表
// var filteredVariables = _dataStorageService.Variables
// .Where(v =>
// v.Name?.Contains(
// SearchText, StringComparison.OrdinalIgnoreCase) ==
// true)
// .Select(v => v.Name)
// .Take(10)
// .ToList();
//
// foreach (var variable in filteredVariables)
// {
// _suggestedVariables.Add(variable);
// }
2025-09-11 11:04:07 +08:00
}
}
2025-09-12 08:35:35 +08:00
public async Task OnNavigatedToAsync(MenuItemViewModel menu)
{
// 加载所有变量的历史记录
LoadAllVariableHistories(HistoryLimit, StartTime, EndTime);
}
2025-09-12 08:35:35 +08:00
/// <summary>
/// 重新加载历史记录命令
/// </summary>
[RelayCommand]
private void Reload()
{
LoadAllVariableHistories(HistoryLimit, StartTime, EndTime);
}
2025-09-12 08:35:35 +08:00
/// <summary>
/// 更新建议列表命令
/// </summary>
[RelayCommand]
private void UpdateSuggestions()
{
UpdateSuggestedVariables();
}
2025-09-11 11:04:07 +08:00
/// <summary>
2025-09-12 08:35:35 +08:00
/// 当搜索文本改变时触发
2025-09-11 11:04:07 +08:00
/// </summary>
/// <param name="value"></param>
partial void OnSearchTextChanged(string value)
{
// 添加调试信息
System.Diagnostics.Debug.WriteLine($"OnSearchTextChanged called with value: '{value}'");
// 更新建议列表
UpdateSuggestedVariables();
2025-09-12 08:35:35 +08:00
if (string.IsNullOrWhiteSpace(value))
2025-09-11 11:04:07 +08:00
{
// 如果搜索文本为空,显示所有历史记录
_variableHistoryList.Clear();
_variableHistoryList.AddRange(_allVariableHistories);
2025-09-11 11:04:07 +08:00
}
else
{
// 根据搜索文本过滤历史记录
var filteredHistories = _allVariableHistories
.Where(h =>
h.VariableName?.Contains(
2025-09-12 08:35:35 +08:00
value, StringComparison.OrdinalIgnoreCase) == true)
.ToList();
_variableHistoryList.Clear();
_variableHistoryList.AddRange(filteredHistories);
2025-09-11 11:04:07 +08:00
}
}
2025-09-12 08:35:35 +08:00
/// <summary>
/// 根据搜索文本过滤历史记录
/// </summary>
/// <param name="searchText"></param>
private void FilterHistoriesBySearchText(string searchText)
{
2025-09-12 08:35:35 +08:00
if (string.IsNullOrWhiteSpace(searchText))
{
// 如果搜索文本为空,显示所有历史记录
_variableHistoryList.Clear();
_variableHistoryList.AddRange(_allVariableHistories);
}
else
{
// 根据搜索文本过滤历史记录
var filteredHistories = _allVariableHistories
.Where(h =>
h.VariableName?.Contains(
searchText, StringComparison.OrdinalIgnoreCase) == true)
.ToList();
_variableHistoryList.Clear();
_variableHistoryList.AddRange(filteredHistories);
}
}
2025-09-11 11:04:07 +08:00
/// <summary>
/// 重新加载历史记录,使用当前设置的限制和时间范围
2025-09-11 11:04:07 +08:00
/// </summary>
public void ReloadHistories()
2025-09-11 11:04:07 +08:00
{
LoadAllVariableHistories(HistoryLimit, StartTime, EndTime);
2025-09-11 11:04:07 +08:00
}
/// <summary>
/// 根据变量ID加载历史记录
/// </summary>
/// <param name="variableId">变量ID</param>
/// <param name="limit">返回记录的最大数量null表示无限制</param>
/// <param name="startTime">开始时间null表示无限制</param>
/// <param name="endTime">结束时间null表示无限制</param>
public async Task LoadVariableHistoriesAsync(int variableId, int? limit = null, DateTime? startTime = null,
DateTime? endTime = null)
2025-09-11 11:04:07 +08:00
{
try
2025-09-11 11:04:07 +08:00
{
_variableHistoryList.Clear();
var histories = await _historyAppService.GetVariableHistoriesAsync(variableId, limit, startTime, endTime);
_variableHistoryList.AddRange(histories);
// 更新建议列表
UpdateSuggestedVariables();
}
catch (Exception ex)
{
// 记录更详细的错误信息
_notificationService.ShowError($"加载变量历史记录失败: {ex.Message}", ex);
2025-09-11 11:04:07 +08:00
}
}
}