From 82c355a3926185010731138c69c14fbe028eed99 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Wed, 17 Sep 2025 13:32:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90=E5=8D=95?= =?UTF-8?q?=E4=B8=AA=E5=8F=98=E9=87=8F=E7=9A=84=E5=8E=86=E5=8F=B2=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/BaseRepository.cs | 32 +-- .../Repositories/DeviceRepository.cs | 2 +- .../Repositories/MenuRepository.cs | 20 +- .../Repositories/MqttServerRepository.cs | 2 +- .../Repositories/NlogRepository.cs | 2 +- .../Repositories/TriggerRepository.cs | 4 +- .../Repositories/UserRepository.cs | 2 +- .../Repositories/VariableHistoryRepository.cs | 8 +- .../VariableMqttAliasRepository.cs | 14 +- .../Repositories/VariableRepository.cs | 20 +- .../Repositories/VariableTableRepository.cs | 4 +- .../Converters/NullToVisibilityConverter.cs | 24 +++ .../ViewModels/VariableHistoryViewModel.cs | 124 ++--------- DMS.WPF/Views/VariableHistoryView.xaml | 193 +++++++++++------- 14 files changed, 212 insertions(+), 239 deletions(-) create mode 100644 DMS.WPF/Converters/NullToVisibilityConverter.cs diff --git a/DMS.Infrastructure/Repositories/BaseRepository.cs b/DMS.Infrastructure/Repositories/BaseRepository.cs index fc0d870..7edd60f 100644 --- a/DMS.Infrastructure/Repositories/BaseRepository.cs +++ b/DMS.Infrastructure/Repositories/BaseRepository.cs @@ -14,7 +14,7 @@ namespace DMS.Infrastructure.Repositories; public abstract class BaseRepository where TEntity : class, new() { - private readonly SqlSugarDbContext _dbContext; + protected readonly SqlSugarDbContext _dbContext; protected readonly ILogger> _logger; /// @@ -45,7 +45,7 @@ public abstract class BaseRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Insertable(entity) + var result = await _dbContext.GetInstance().Insertable(entity) .ExecuteReturnEntityAsync(); stopwatch.Stop(); _logger.LogInformation($"Add {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -61,7 +61,7 @@ public abstract class BaseRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Updateable(entity) + var result = await _dbContext.GetInstance().Updateable(entity) .ExecuteCommandAsync(); stopwatch.Stop(); _logger.LogInformation($"Update {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -77,7 +77,7 @@ public abstract class BaseRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable(entity) + var result = await _dbContext.GetInstance().Deleteable(entity) .ExecuteCommandAsync(); stopwatch.Stop(); _logger.LogInformation($"Delete {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -93,7 +93,7 @@ public abstract class BaseRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var entities = await Db.Queryable() + var entities = await _dbContext.GetInstance().Queryable() .ToListAsync(); stopwatch.Stop(); _logger.LogInformation($"GetAll {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -111,7 +111,7 @@ public abstract class BaseRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var entity = await Db.Queryable() + var entity = await _dbContext.GetInstance().Queryable() .In(id) .FirstAsync(); stopwatch.Stop(); @@ -128,7 +128,7 @@ public abstract class BaseRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var entity = await Db.Queryable() + var entity = await _dbContext.GetInstance().Queryable() .In(id) .FirstAsync(); stopwatch.Stop(); @@ -145,7 +145,7 @@ public abstract class BaseRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable() + var result = await _dbContext.GetInstance().Deleteable() .In(ids) .ExecuteCommandAsync(); stopwatch.Stop(); @@ -162,7 +162,7 @@ public abstract class BaseRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var entity = await Db.Queryable() + var entity = await _dbContext.GetInstance().Queryable() .FirstAsync(expression); stopwatch.Stop(); _logger.LogInformation($"GetByCondition {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -178,7 +178,7 @@ public abstract class BaseRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Queryable() + var result = await _dbContext.GetInstance().Queryable() .AnyAsync(expression); stopwatch.Stop(); _logger.LogInformation($"Exists {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -191,7 +191,7 @@ public abstract class BaseRepository /// 表示异步操作的任务。 public async Task BeginTranAsync() { - await Db.BeginTranAsync(); + await _dbContext.GetInstance().BeginTranAsync(); } /// @@ -200,7 +200,7 @@ public abstract class BaseRepository /// 表示异步操作的任务。 public async Task CommitTranAsync() { - await Db.CommitTranAsync(); + await _dbContext.GetInstance().CommitTranAsync(); } @@ -210,7 +210,7 @@ public abstract class BaseRepository /// 表示异步操作的任务。 public async Task RollbackTranAsync() { - await Db.RollbackTranAsync(); + await _dbContext.GetInstance().RollbackTranAsync(); } /// @@ -222,7 +222,7 @@ public abstract class BaseRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var entity = await Db.Queryable().Take(number).ToListAsync(); + var entity = await _dbContext.GetInstance().Queryable().Take(number).ToListAsync(); stopwatch.Stop(); _logger.LogInformation($"TakeAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -238,7 +238,7 @@ public abstract class BaseRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable() + var result = await _dbContext.GetInstance().Deleteable() .In(id) .ExecuteCommandAsync(); stopwatch.Stop(); @@ -253,7 +253,7 @@ public abstract class BaseRepository var retrunEntities = new List(); foreach (var entity in entities) { - var result = await Db.Insertable(entity).ExecuteReturnEntityAsync(); + var result = await _dbContext.GetInstance().Insertable(entity).ExecuteReturnEntityAsync(); retrunEntities.Add(result); } diff --git a/DMS.Infrastructure/Repositories/DeviceRepository.cs b/DMS.Infrastructure/Repositories/DeviceRepository.cs index fef565c..937ca8f 100644 --- a/DMS.Infrastructure/Repositories/DeviceRepository.cs +++ b/DMS.Infrastructure/Repositories/DeviceRepository.cs @@ -84,7 +84,7 @@ public class DeviceRepository : BaseRepository, IDeviceRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable(new DbDevice() { Id = id }) + var result = await _dbContext.GetInstance().Deleteable(new DbDevice() { Id = id }) .ExecuteCommandAsync(); stopwatch.Stop(); _logger.LogInformation($"Delete {typeof(DbDevice)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); diff --git a/DMS.Infrastructure/Repositories/MenuRepository.cs b/DMS.Infrastructure/Repositories/MenuRepository.cs index 9e46270..572fd64 100644 --- a/DMS.Infrastructure/Repositories/MenuRepository.cs +++ b/DMS.Infrastructure/Repositories/MenuRepository.cs @@ -37,7 +37,7 @@ public class MenuRepository : BaseRepository, IMenuRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var dbMenuTree = await Db.Queryable() + var dbMenuTree = await _dbContext.GetInstance().Queryable() .ToTreeAsync(dm => dm.Childrens, dm => dm.ParentId, 0); stopwatch.Stop(); _logger.LogInformation($"获取菜单树耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -99,7 +99,7 @@ public class MenuRepository : BaseRepository, IMenuRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable(new DbMenu { Id = id }) + var result = await _dbContext.GetInstance().Deleteable(new DbMenu { Id = id }) .ExecuteCommandAsync(); stopwatch.Stop(); _logger.LogInformation($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -116,11 +116,11 @@ public class MenuRepository : BaseRepository, IMenuRepository var stopwatch = new Stopwatch(); stopwatch.Start(); int delConut = 0; - var childList = await Db.Queryable() + var childList = await _dbContext.GetInstance().Queryable() .ToChildListAsync(c => c.ParentId, id); - delConut = await Db.Deleteable(childList) + delConut = await _dbContext.GetInstance().Deleteable(childList) .ExecuteCommandAsync(); - delConut += await Db.Deleteable() + delConut += await _dbContext.GetInstance().Deleteable() .Where(m => m.Id == id) .ExecuteCommandAsync(); stopwatch.Stop(); @@ -138,13 +138,13 @@ public class MenuRepository : BaseRepository, IMenuRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var menu = await Db.Queryable().FirstAsync(m => m.MenuType == menuType && m.TargetId == targetId); + var menu = await _dbContext.GetInstance().Queryable().FirstAsync(m => m.MenuType == menuType && m.TargetId == targetId); if (menu == null) return 0; - var childList = await Db.Queryable() + var childList = await _dbContext.GetInstance().Queryable() .ToChildListAsync(c => c.ParentId, menu.Id); - var delConut = await Db.Deleteable(childList) + var delConut = await _dbContext.GetInstance().Deleteable(childList) .ExecuteCommandAsync(); - delConut += await Db.Deleteable() + delConut += await _dbContext.GetInstance().Deleteable() .Where(m => m.Id == menu.Id) .ExecuteCommandAsync(); stopwatch.Stop(); @@ -160,7 +160,7 @@ public class MenuRepository : BaseRepository, IMenuRepository /// 对应的菜单实体,如果不存在则为null。 public async Task GetMenuByTargetIdAsync(MenuType menuType, int targetId) { - var dbMenu = await Db.Queryable().FirstAsync(m => m.MenuType == menuType && m.TargetId == targetId); + var dbMenu = await _dbContext.GetInstance().Queryable().FirstAsync(m => m.MenuType == menuType && m.TargetId == targetId); return _mapper.Map(dbMenu); } diff --git a/DMS.Infrastructure/Repositories/MqttServerRepository.cs b/DMS.Infrastructure/Repositories/MqttServerRepository.cs index 7c152b5..1a30151 100644 --- a/DMS.Infrastructure/Repositories/MqttServerRepository.cs +++ b/DMS.Infrastructure/Repositories/MqttServerRepository.cs @@ -83,7 +83,7 @@ public class MqttServerRepository : BaseRepository, IMqttServerRep { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable(new DbMqttServer() { Id = id }) + var result = await _dbContext.GetInstance().Deleteable(new DbMqttServer() { Id = id }) .ExecuteCommandAsync(); stopwatch.Stop(); _logger.LogInformation($"Delete {typeof(DbMqttServer)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); diff --git a/DMS.Infrastructure/Repositories/NlogRepository.cs b/DMS.Infrastructure/Repositories/NlogRepository.cs index 64cdbc1..795e843 100644 --- a/DMS.Infrastructure/Repositories/NlogRepository.cs +++ b/DMS.Infrastructure/Repositories/NlogRepository.cs @@ -32,7 +32,7 @@ public class NlogRepository : BaseRepository, INlogRepository /// public async Task DeleteAllAsync() { - await Db.Deleteable().ExecuteCommandAsync(); + await _dbContext.GetInstance().Deleteable().ExecuteCommandAsync(); } // Nlog 通常是只读或追加的日志,因此像 AddAsync, UpdateAsync, DeleteAsync 这样的修改方法 diff --git a/DMS.Infrastructure/Repositories/TriggerRepository.cs b/DMS.Infrastructure/Repositories/TriggerRepository.cs index f04d131..55c3929 100644 --- a/DMS.Infrastructure/Repositories/TriggerRepository.cs +++ b/DMS.Infrastructure/Repositories/TriggerRepository.cs @@ -71,7 +71,7 @@ namespace DMS.Infrastructure.Repositories { var stopwatch = new Stopwatch(); stopwatch.Start(); - var rowsAffected = await Db.Deleteable() + var rowsAffected = await _dbContext.GetInstance().Deleteable() .In(id) .ExecuteCommandAsync(); stopwatch.Stop(); @@ -86,7 +86,7 @@ namespace DMS.Infrastructure.Repositories { var stopwatch = new Stopwatch(); stopwatch.Start(); - var dbList = await Db.Queryable() + var dbList = await _dbContext.GetInstance().Queryable() .Where(t => t.VariableId == variableId) .ToListAsync(); stopwatch.Stop(); diff --git a/DMS.Infrastructure/Repositories/UserRepository.cs b/DMS.Infrastructure/Repositories/UserRepository.cs index 24e0f0e..61c9139 100644 --- a/DMS.Infrastructure/Repositories/UserRepository.cs +++ b/DMS.Infrastructure/Repositories/UserRepository.cs @@ -86,7 +86,7 @@ public class UserRepository : BaseRepository, IUserRepository { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable(new DbUser() { Id = id }) + var result = await _dbContext.GetInstance().Deleteable(new DbUser() { Id = id }) .ExecuteCommandAsync(); stopwatch.Stop(); _logger.LogInformation($"Delete {typeof(DbUser)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); diff --git a/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs b/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs index 911d485..462fc0c 100644 --- a/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs @@ -85,7 +85,7 @@ public class VariableHistoryRepository : BaseRepository, IVar { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable(new DbVariableHistory() { Id = id }) + var result = await _dbContext.GetInstance().Deleteable(new DbVariableHistory() { Id = id }) .ExecuteCommandAsync(); stopwatch.Stop(); _logger.LogInformation($"Delete {typeof(DbVariableHistory)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -118,7 +118,7 @@ public class VariableHistoryRepository : BaseRepository, IVar /// 变量历史记录列表 public async Task> GetByVariableIdAsync(int variableId) { - var dbList = await Db.Queryable() + var dbList = await _dbContext.GetInstance().Queryable() .Where(h => h.VariableId == variableId) .OrderBy(h => h.Timestamp, SqlSugar.OrderByType.Desc) .ToListAsync(); @@ -135,7 +135,7 @@ public class VariableHistoryRepository : BaseRepository, IVar /// 变量历史记录列表 public async Task> GetByVariableIdAsync(int variableId, int? limit = null, DateTime? startTime = null, DateTime? endTime = null) { - var query = Db.Queryable() + var query = _dbContext.GetInstance().Queryable() .Where(h => h.VariableId == variableId); // 添加时间范围筛选 @@ -165,7 +165,7 @@ public class VariableHistoryRepository : BaseRepository, IVar /// 所有历史记录列表 public new async Task> GetAllAsync(int? limit = null, DateTime? startTime = null, DateTime? endTime = null) { - var query = Db.Queryable(); + var query = _dbContext.GetInstance().Queryable(); // 添加时间范围筛选 if (startTime.HasValue) diff --git a/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs b/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs index 47768d7..71dc76c 100644 --- a/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs @@ -86,7 +86,7 @@ public class VariableMqttAliasRepository : BaseRepository, { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable(new DbVariableMqttAlias() { Id = id }) + var result = await _dbContext.GetInstance().Deleteable(new DbVariableMqttAlias() { Id = id }) .ExecuteCommandAsync(); stopwatch.Stop(); _logger.LogInformation($"Delete {typeof(DbVariableMqttAlias)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -118,7 +118,7 @@ public class VariableMqttAliasRepository : BaseRepository, public async Task> GetAliasesForVariableAsync(int variableId) { // 查询别名关联,并包含关联的Variable和MqttServer信息 - var dbList = await Db.Queryable() + var dbList = await _dbContext.GetInstance().Queryable() .Where(x => x.VariableId == variableId) .ToListAsync(); @@ -126,11 +126,11 @@ public class VariableMqttAliasRepository : BaseRepository, var variableIds = dbList.Select(x => x.VariableId).Distinct().ToList(); var mqttServerIds = dbList.Select(x => x.MqttServerId).Distinct().ToList(); - var variables = await Db.Queryable() + var variables = await _dbContext.GetInstance().Queryable() .In(x => x.Id, variableIds) .ToListAsync(); - var mqttServers = await Db.Queryable() + var mqttServers = await _dbContext.GetInstance().Queryable() .In(x => x.Id, mqttServerIds) .ToListAsync(); @@ -161,7 +161,7 @@ public class VariableMqttAliasRepository : BaseRepository, /// public async Task GetByVariableAndServerAsync(int variableId, int mqttServerId) { - var dbAlias = await Db.Queryable() + var dbAlias = await _dbContext.GetInstance().Queryable() .Where(x => x.VariableId == variableId && x.MqttServerId == mqttServerId) .FirstAsync(); @@ -169,11 +169,11 @@ public class VariableMqttAliasRepository : BaseRepository, return null; // 手动加载关联的Variable和MqttServer实体 - var variable = await Db.Queryable() + var variable = await _dbContext.GetInstance().Queryable() .Where(x => x.Id == variableId) .FirstAsync(); - var mqttServer = await Db.Queryable() + var mqttServer = await _dbContext.GetInstance().Queryable() .Where(x => x.Id == mqttServerId) .FirstAsync(); diff --git a/DMS.Infrastructure/Repositories/VariableRepository.cs b/DMS.Infrastructure/Repositories/VariableRepository.cs index f7aa231..b4ba7cd 100644 --- a/DMS.Infrastructure/Repositories/VariableRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableRepository.cs @@ -38,7 +38,7 @@ public class VariableRepository : BaseRepository, IVariableRepositor /// 成功添加或更新关联的数量。 public async Task AddMqttToVariablesAsync(IEnumerable variableMqttList) { - await Db.BeginTranAsync(); + await _dbContext.GetInstance().BeginTranAsync(); try { @@ -47,7 +47,7 @@ public class VariableRepository : BaseRepository, IVariableRepositor var mqttIds = variableMqttList.Select(vm => vm.Mqtt.Id).Distinct().ToList(); // 1. 一次性查询所有相关的现有别名 - var existingAliases = await Db.Queryable() + var existingAliases = await _dbContext.GetInstance().Queryable() .Where(it => variableIds.Contains(it.VariableId) && mqttIds.Contains(it.MqttId)) .ToListAsync(); @@ -87,24 +87,24 @@ public class VariableRepository : BaseRepository, IVariableRepositor // 2. 批量更新 if (toUpdate.Any()) { - var updateResult = await Db.Updateable(toUpdate).ExecuteCommandAsync(); + var updateResult = await _dbContext.GetInstance().Updateable(toUpdate).ExecuteCommandAsync(); affectedCount += updateResult; } // 3. 批量插入 if (toInsert.Any()) { - var insertResult = await Db.Insertable(toInsert).ExecuteCommandAsync(); + var insertResult = await _dbContext.GetInstance().Insertable(toInsert).ExecuteCommandAsync(); affectedCount += insertResult; } - await Db.CommitTranAsync(); + await _dbContext.GetInstance().CommitTranAsync(); //_logger.LogInformation($"成功为 {variableMqttList.Count()} 个变量请求添加/更新了MQTT服务器关联,实际影响 {affectedCount} 个。"); return affectedCount; } catch (Exception ex) { - await Db.RollbackTranAsync(); + await _dbContext.GetInstance().RollbackTranAsync(); //_logger.LogError(ex, $"为变量添加MQTT服务器关联时发生错误: {ex.Message}"); // 根据需要,可以向上层抛出异常 throw; @@ -167,7 +167,7 @@ public class VariableRepository : BaseRepository, IVariableRepositor { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable(new DbVariable() { Id = id }) + var result = await _dbContext.GetInstance().Deleteable(new DbVariable() { Id = id }) .ExecuteCommandAsync(); stopwatch.Stop(); _logger.LogInformation($"Delete {typeof(DbVariable)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -183,7 +183,7 @@ public class VariableRepository : BaseRepository, IVariableRepositor { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable() + var result = await _dbContext.GetInstance().Deleteable() .In(ids) .ExecuteCommandAsync(); stopwatch.Stop(); @@ -217,7 +217,7 @@ public class VariableRepository : BaseRepository, IVariableRepositor /// 找到的变量实体,如果不存在则返回null。 public async Task GetByOpcUaNodeIdAsync(string opcUaNodeId) { - var dbVariable = await Db.Queryable() + var dbVariable = await _dbContext.GetInstance().Queryable() .Where(v => v.OpcUaNodeId == opcUaNodeId) .FirstAsync(); return dbVariable == null ? null : _mapper.Map(dbVariable); @@ -230,7 +230,7 @@ public class VariableRepository : BaseRepository, IVariableRepositor /// 找到的变量实体列表。 public async Task> GetByOpcUaNodeIdsAsync(List opcUaNodeIds) { - var dbVariables = await Db.Queryable() + var dbVariables = await _dbContext.GetInstance().Queryable() .Where(v => opcUaNodeIds.Contains(v.OpcUaNodeId)) .ToListAsync(); return _mapper.Map>(dbVariables); diff --git a/DMS.Infrastructure/Repositories/VariableTableRepository.cs b/DMS.Infrastructure/Repositories/VariableTableRepository.cs index 22c740b..e4980e4 100644 --- a/DMS.Infrastructure/Repositories/VariableTableRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableTableRepository.cs @@ -85,7 +85,7 @@ public class VariableTableRepository : BaseRepository, IVariabl { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable(new DbVariableTable() { Id = id }) + var result = await _dbContext.GetInstance().Deleteable(new DbVariableTable() { Id = id }) .ExecuteCommandAsync(); stopwatch.Stop(); _logger.LogInformation($"Delete {typeof(DbVariableTable)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms"); @@ -120,7 +120,7 @@ public class VariableTableRepository : BaseRepository, IVariabl { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Deleteable() + var result = await _dbContext.GetInstance().Deleteable() .Where(it => it.DeviceId == deviceId) .ExecuteCommandAsync(); stopwatch.Stop(); diff --git a/DMS.WPF/Converters/NullToVisibilityConverter.cs b/DMS.WPF/Converters/NullToVisibilityConverter.cs new file mode 100644 index 0000000..a233eb3 --- /dev/null +++ b/DMS.WPF/Converters/NullToVisibilityConverter.cs @@ -0,0 +1,24 @@ +using System; +using System.Globalization; +using System.Windows; +using System.Windows.Data; + +namespace DMS.WPF.Converters +{ + /// + /// Null值到可见性转换器。当绑定的值不为null时,返回Visible,否则返回Collapsed。 + /// + public class NullToVisibilityConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + // 如果值不为null,则可见 + return value != null ? Visibility.Visible : Visibility.Collapsed; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/DMS.WPF/ViewModels/VariableHistoryViewModel.cs b/DMS.WPF/ViewModels/VariableHistoryViewModel.cs index a10c775..59fff1a 100644 --- a/DMS.WPF/ViewModels/VariableHistoryViewModel.cs +++ b/DMS.WPF/ViewModels/VariableHistoryViewModel.cs @@ -24,24 +24,6 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable private readonly IDataStorageService _dataStorageService; private readonly INotificationService _notificationService; - /// - /// 用于过滤变量的搜索文本 - /// - [ObservableProperty] - private string _searchText; - - /// - /// 是否打开建议列表 - /// - [ObservableProperty] - private bool _isSuggestionListOpen; - - /// - /// 建议的变量列表 - /// - [ObservableProperty] - private ObservableCollection _suggestedVariables; - /// /// 历史记录条数限制 /// @@ -64,7 +46,7 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable /// 选中的变量历史记录 /// [ObservableProperty] - private VariableHistoryDto _selectedVariable; + private VariableItemViewModel _currentVariable; /// /// 变量历史记录列表 @@ -94,13 +76,11 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable _variableHistorySynchronizedView = _variableHistoryList.CreateView(v => v); VariableHistories = _variableHistorySynchronizedView.ToNotifyCollectionChanged(); _allVariableHistories = new List(); - _suggestedVariables = new ObservableCollection(); // 初始化默认值 _historyLimit = 1000; // 默认限制1000条记录 _startTime = null; _endTime = null; - _selectedVariable = new VariableHistoryDto(); } /// @@ -109,17 +89,16 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable /// 返回记录的最大数量,null表示无限制 /// 开始时间,null表示无限制 /// 结束时间,null表示无限制 - private async void LoadAllVariableHistories(int? limit = null, DateTime? startTime = null, DateTime? endTime = null) + private async void LoadAllVariableHistories(int variableId,int? limit = null, DateTime? startTime = null, DateTime? endTime = null) { + + try { _variableHistoryList.Clear(); - var allHistories = await _historyAppService.GetAllVariableHistoriesAsync(limit, startTime, endTime); + var allHistories = await _historyAppService.GetVariableHistoriesAsync(variableId,limit, startTime, endTime); _allVariableHistories = allHistories.ToList(); _variableHistoryList.AddRange(_allVariableHistories); - - // 更新建议列表 - UpdateSuggestedVariables(); } catch (Exception ex) { @@ -128,37 +107,17 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable } } - /// - /// 更新建议的变量列表 - /// - private void UpdateSuggestedVariables() - { - // 清空现有建议列表 - _suggestedVariables.Clear(); - - if (!string.IsNullOrWhiteSpace(SearchText)) - { - // // 根据搜索文本过滤建议列表 - // 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); - // } - } - } public async Task OnNavigatedToAsync(MenuItemViewModel menu) { - // 加载所有变量的历史记录 - LoadAllVariableHistories(HistoryLimit, StartTime, EndTime); + if (_dataStorageService.Variables.TryGetValue(menu.TargetId, out VariableItemViewModel variableItem)) + { + CurrentVariable = variableItem; + // 加载所有变量的历史记录 + LoadAllVariableHistories(variableItem.Id,HistoryLimit, StartTime, EndTime); + + } + } /// @@ -167,50 +126,14 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable [RelayCommand] private void Reload() { - LoadAllVariableHistories(HistoryLimit, StartTime, EndTime); - } - - /// - /// 更新建议列表命令 - /// - [RelayCommand] - private void UpdateSuggestions() - { - UpdateSuggestedVariables(); - } - - /// - /// 当搜索文本改变时触发 - /// - /// - partial void OnSearchTextChanged(string value) - { - // 添加调试信息 - System.Diagnostics.Debug.WriteLine($"OnSearchTextChanged called with value: '{value}'"); + if (CurrentVariable!=null) + { + LoadAllVariableHistories( CurrentVariable.Id,HistoryLimit, StartTime, EndTime); + } - // 更新建议列表 - UpdateSuggestedVariables(); - - if (string.IsNullOrWhiteSpace(value)) - { - // 如果搜索文本为空,显示所有历史记录 - _variableHistoryList.Clear(); - _variableHistoryList.AddRange(_allVariableHistories); - } - else - { - // 根据搜索文本过滤历史记录 - var filteredHistories = _allVariableHistories - .Where(h => - h.VariableName?.Contains( - value, StringComparison.OrdinalIgnoreCase) == true) - .ToList(); - - _variableHistoryList.Clear(); - _variableHistoryList.AddRange(filteredHistories); - } } + /// /// 根据搜索文本过滤历史记录 /// @@ -237,14 +160,6 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable } } - /// - /// 重新加载历史记录,使用当前设置的限制和时间范围 - /// - public void ReloadHistories() - { - LoadAllVariableHistories(HistoryLimit, StartTime, EndTime); - } - /// /// 根据变量ID加载历史记录 /// @@ -260,9 +175,6 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable _variableHistoryList.Clear(); var histories = await _historyAppService.GetVariableHistoriesAsync(variableId, limit, startTime, endTime); _variableHistoryList.AddRange(histories); - - // 更新建议列表 - UpdateSuggestedVariables(); } catch (Exception ex) { diff --git a/DMS.WPF/Views/VariableHistoryView.xaml b/DMS.WPF/Views/VariableHistoryView.xaml index 9cccca7..a19c9a1 100644 --- a/DMS.WPF/Views/VariableHistoryView.xaml +++ b/DMS.WPF/Views/VariableHistoryView.xaml @@ -14,111 +14,148 @@ xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern" xmlns:valueConverts="clr-namespace:DMS.WPF.ValueConverts" xmlns:vm="clr-namespace:DMS.WPF.ViewModels" + xmlns:converters="clr-namespace:DMS.WPF.Converters" d:DataContext="{d:DesignInstance vm:VariableHistoryViewModel}" d:DesignHeight="600" d:DesignWidth="800" mc:Ignorable="d"> - - - - - - - - + + + + + + + + + + - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + - + - + -