diff --git a/DMS.Application/Interfaces/IVariableAppService.cs b/DMS.Application/Interfaces/IVariableAppService.cs index 2411d61..47786c8 100644 --- a/DMS.Application/Interfaces/IVariableAppService.cs +++ b/DMS.Application/Interfaces/IVariableAppService.cs @@ -55,7 +55,7 @@ public interface IVariableAppService /// /// 异步批量导入变量。 /// - Task BatchImportVariablesAsync(List variables); + Task> BatchImportVariablesAsync(List variables); /// /// 检测一组变量是否已存在。 diff --git a/DMS.Application/Services/VariableAppService.cs b/DMS.Application/Services/VariableAppService.cs index 7cc15f0..61148f0 100644 --- a/DMS.Application/Services/VariableAppService.cs +++ b/DMS.Application/Services/VariableAppService.cs @@ -221,13 +221,13 @@ public class VariableAppService : IVariableAppService } } - public async Task BatchImportVariablesAsync(List variables) + public async Task> BatchImportVariablesAsync(List variables) { try { var variableModels = _mapper.Map>(variables); - var result = await _repoManager.Variables.AddBatchAsync(variableModels); - return result; + var addedVariables = await _repoManager.Variables.AddBatchAsync(variableModels); + return _mapper.Map>(addedVariables); } catch (Exception ex) { diff --git a/DMS.Application/Services/VariableManagementService.cs b/DMS.Application/Services/VariableManagementService.cs index cc07e6f..47e0e9e 100644 --- a/DMS.Application/Services/VariableManagementService.cs +++ b/DMS.Application/Services/VariableManagementService.cs @@ -19,6 +19,7 @@ public class VariableManagementService : IVariableManagementService { private readonly IVariableAppService _variableAppService; private readonly IAppDataStorageService _appDataStorageService; + private readonly IDataProcessingService _dataProcessingService; /// /// 当变量数据发生变化时触发 @@ -31,10 +32,12 @@ public class VariableManagementService : IVariableManagementService public event EventHandler OnVariableValueChanged; public VariableManagementService(IVariableAppService variableAppService, - IAppDataStorageService appDataStorageService) + IAppDataStorageService appDataStorageService, + IDataProcessingService dataProcessingService) { _variableAppService = variableAppService; _appDataStorageService = appDataStorageService; + _dataProcessingService = dataProcessingService; } /// @@ -151,6 +154,7 @@ public class VariableManagementService : IVariableManagementService public void VariableValueChanged(VariableValueChangedEventArgs eventArgs) { + // 触发事件,通知DataEventService等监听者 OnVariableValueChanged?.Invoke(this, eventArgs); } } \ No newline at end of file diff --git a/DMS.Core/Interfaces/Repositories/IBaseRepository.cs b/DMS.Core/Interfaces/Repositories/IBaseRepository.cs index 8e7c00e..05afad6 100644 --- a/DMS.Core/Interfaces/Repositories/IBaseRepository.cs +++ b/DMS.Core/Interfaces/Repositories/IBaseRepository.cs @@ -59,5 +59,5 @@ public interface IBaseRepository where T : class /// 异步批量添加实体。 /// /// 要添加的实体列表。 - Task AddBatchAsync(List entities); + Task> AddBatchAsync(List entities); } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/BaseRepository.cs b/DMS.Infrastructure/Repositories/BaseRepository.cs index a1a937b..fc0d870 100644 --- a/DMS.Infrastructure/Repositories/BaseRepository.cs +++ b/DMS.Infrastructure/Repositories/BaseRepository.cs @@ -246,13 +246,22 @@ public abstract class BaseRepository return result; } - public async Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var stopwatch = new Stopwatch(); stopwatch.Start(); - var result = await Db.Insertable(entities).ExecuteCommandAsync(); + var retrunEntities = new List(); + foreach (var entity in entities) + { + var result = await Db.Insertable(entity).ExecuteReturnEntityAsync(); + retrunEntities.Add(result); + } + stopwatch.Stop(); _logger.LogInformation($"AddBatchAsync {typeof(TEntity).Name}耗时:{stopwatch.ElapsedMilliseconds}ms"); - return result > 0; + + + + return retrunEntities; } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/DeviceRepository.cs b/DMS.Infrastructure/Repositories/DeviceRepository.cs index aef8bbd..fef565c 100644 --- a/DMS.Infrastructure/Repositories/DeviceRepository.cs +++ b/DMS.Infrastructure/Repositories/DeviceRepository.cs @@ -103,9 +103,10 @@ public class DeviceRepository : BaseRepository, IDeviceRepository } - public Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - return base.AddBatchAsync(dbEntities); + var addedEntities = await base.AddBatchAsync(dbEntities); + return _mapper.Map>(addedEntities); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/EmailAccountRepository.cs b/DMS.Infrastructure/Repositories/EmailAccountRepository.cs index d92801d..2cf7321 100644 --- a/DMS.Infrastructure/Repositories/EmailAccountRepository.cs +++ b/DMS.Infrastructure/Repositories/EmailAccountRepository.cs @@ -146,13 +146,19 @@ namespace DMS.Infrastructure.Repositories /// /// 异步批量添加实体。 /// - public async Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - var result = await Db.Insertable(dbEntities) - .ExecuteCommandAsync(); + var insertedEntities = new List(); - return result > 0; + // 使用循环逐个插入实体,这样可以确保返回每个插入的实体 + foreach (var entity in dbEntities) + { + var insertedEntity = await Db.Insertable(entity).ExecuteReturnEntityAsync(); + insertedEntities.Add(insertedEntity); + } + + return _mapper.Map>(insertedEntities); } } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/EmailLogRepository.cs b/DMS.Infrastructure/Repositories/EmailLogRepository.cs index ad61c0b..482ecef 100644 --- a/DMS.Infrastructure/Repositories/EmailLogRepository.cs +++ b/DMS.Infrastructure/Repositories/EmailLogRepository.cs @@ -122,13 +122,19 @@ namespace DMS.Infrastructure.Repositories /// /// 异步批量添加实体。 /// - public async Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - var result = await Db.Insertable(dbEntities) - .ExecuteCommandAsync(); + var insertedEntities = new List(); - return result > 0; + // 使用循环逐个插入实体,这样可以确保返回每个插入的实体 + foreach (var entity in dbEntities) + { + var insertedEntity = await Db.Insertable(entity).ExecuteReturnEntityAsync(); + insertedEntities.Add(insertedEntity); + } + + return _mapper.Map>(insertedEntities); } /// diff --git a/DMS.Infrastructure/Repositories/EmailMessageRepository.cs b/DMS.Infrastructure/Repositories/EmailMessageRepository.cs index 4f98dbe..49b51cd 100644 --- a/DMS.Infrastructure/Repositories/EmailMessageRepository.cs +++ b/DMS.Infrastructure/Repositories/EmailMessageRepository.cs @@ -122,13 +122,19 @@ namespace DMS.Infrastructure.Repositories /// /// 异步批量添加实体。 /// - public async Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - var result = await Db.Insertable(dbEntities) - .ExecuteCommandAsync(); + var insertedEntities = new List(); - return result > 0; + // 使用循环逐个插入实体,这样可以确保返回每个插入的实体 + foreach (var entity in dbEntities) + { + var insertedEntity = await Db.Insertable(entity).ExecuteReturnEntityAsync(); + insertedEntities.Add(insertedEntity); + } + + return _mapper.Map>(insertedEntities); } /// diff --git a/DMS.Infrastructure/Repositories/EmailTemplateRepository.cs b/DMS.Infrastructure/Repositories/EmailTemplateRepository.cs index b96af4a..5b8d836 100644 --- a/DMS.Infrastructure/Repositories/EmailTemplateRepository.cs +++ b/DMS.Infrastructure/Repositories/EmailTemplateRepository.cs @@ -122,13 +122,19 @@ namespace DMS.Infrastructure.Repositories /// /// 异步批量添加实体。 /// - public async Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - var result = await Db.Insertable(dbEntities) - .ExecuteCommandAsync(); + var insertedEntities = new List(); - return result > 0; + // 使用循环逐个插入实体,这样可以确保返回每个插入的实体 + foreach (var entity in dbEntities) + { + var insertedEntity = await Db.Insertable(entity).ExecuteReturnEntityAsync(); + insertedEntities.Add(insertedEntity); + } + + return _mapper.Map>(insertedEntities); } /// diff --git a/DMS.Infrastructure/Repositories/InitializeRepository.cs b/DMS.Infrastructure/Repositories/InitializeRepository.cs index 9730bff..89f08aa 100644 --- a/DMS.Infrastructure/Repositories/InitializeRepository.cs +++ b/DMS.Infrastructure/Repositories/InitializeRepository.cs @@ -158,14 +158,20 @@ public class InitializeRepository : IInitializeRepository }, new DbMenu { - Id = 8, Header = "设置", Icon = "\uE713", ParentId = 0, - MenuType = MenuType.MainMenu, TargetViewKey = "SettingView", + Id = 8, Header = "变量历史", Icon = "\uE81C", ParentId = 0, + MenuType = MenuType.MainMenu, TargetViewKey = "VariableHistoryView", DisplayOrder = 8 }, new DbMenu { - Id = 9, Header = "关于", Icon = "\uE946", ParentId = 0, - MenuType = MenuType.MainMenu, TargetViewKey = "", DisplayOrder = 9 + Id = 9, Header = "设置", Icon = "\uE713", ParentId = 0, + MenuType = MenuType.MainMenu, TargetViewKey = "SettingView", + DisplayOrder = 9 + }, + new DbMenu + { + Id = 10, Header = "关于", Icon = "\uE946", ParentId = 0, + MenuType = MenuType.MainMenu, TargetViewKey = "", DisplayOrder = 10 } // 假设有一个AboutView }; diff --git a/DMS.Infrastructure/Repositories/MenuRepository.cs b/DMS.Infrastructure/Repositories/MenuRepository.cs index d6b91c4..9e46270 100644 --- a/DMS.Infrastructure/Repositories/MenuRepository.cs +++ b/DMS.Infrastructure/Repositories/MenuRepository.cs @@ -175,9 +175,10 @@ public class MenuRepository : BaseRepository, IMenuRepository return _mapper.Map>(dbList); } - public Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - return base.AddBatchAsync(dbEntities); + var addedEntities = await base.AddBatchAsync(dbEntities); + return _mapper.Map>(addedEntities); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/MqttServerRepository.cs b/DMS.Infrastructure/Repositories/MqttServerRepository.cs index f6fa4ef..7c152b5 100644 --- a/DMS.Infrastructure/Repositories/MqttServerRepository.cs +++ b/DMS.Infrastructure/Repositories/MqttServerRepository.cs @@ -102,9 +102,10 @@ public class MqttServerRepository : BaseRepository, IMqttServerRep } - public Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - return base.AddBatchAsync(dbEntities); + var addedEntities = await base.AddBatchAsync(dbEntities); + return _mapper.Map>(addedEntities); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/NlogRepository.cs b/DMS.Infrastructure/Repositories/NlogRepository.cs index 6ff34f2..64cdbc1 100644 --- a/DMS.Infrastructure/Repositories/NlogRepository.cs +++ b/DMS.Infrastructure/Repositories/NlogRepository.cs @@ -129,10 +129,11 @@ public class NlogRepository : BaseRepository, INlogRepository /// 此方法主要用于满足接口契约,实际使用应谨慎。 /// /// 要添加的Nlog日志实体列表。 - /// 操作是否成功。 - public new async Task AddBatchAsync(List entities) + /// 添加的Nlog日志实体列表。 + public new async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - return await base.AddBatchAsync(dbEntities); + var addedEntities = await base.AddBatchAsync(dbEntities); + return _mapper.Map>(addedEntities); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/UserRepository.cs b/DMS.Infrastructure/Repositories/UserRepository.cs index 7ef1888..24e0f0e 100644 --- a/DMS.Infrastructure/Repositories/UserRepository.cs +++ b/DMS.Infrastructure/Repositories/UserRepository.cs @@ -105,9 +105,10 @@ public class UserRepository : BaseRepository, IUserRepository } - public Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - return base.AddBatchAsync(dbEntities); + var addedEntities = await base.AddBatchAsync(dbEntities); + return _mapper.Map>(addedEntities); } } \ No newline at end of file diff --git a/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs b/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs index 68ee3ac..911d485 100644 --- a/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableHistoryRepository.cs @@ -104,10 +104,11 @@ public class VariableHistoryRepository : BaseRepository, IVar } - public Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - return base.AddBatchAsync(dbEntities); + var addedEntities = await base.AddBatchAsync(dbEntities); + return _mapper.Map>(addedEntities); } /// diff --git a/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs b/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs index 2b2afd5..47768d7 100644 --- a/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableMqttAliasRepository.cs @@ -105,10 +105,11 @@ public class VariableMqttAliasRepository : BaseRepository, } - public Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - return base.AddBatchAsync(dbEntities); + var addedEntities = await base.AddBatchAsync(dbEntities); + return _mapper.Map>(addedEntities); } /// diff --git a/DMS.Infrastructure/Repositories/VariableRepository.cs b/DMS.Infrastructure/Repositories/VariableRepository.cs index 5af3def..f7aa231 100644 --- a/DMS.Infrastructure/Repositories/VariableRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableRepository.cs @@ -203,10 +203,11 @@ public class VariableRepository : BaseRepository, IVariableRepositor } - public Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - return base.AddBatchAsync(dbEntities); + var addedEntities = await base.AddBatchAsync(dbEntities); + return _mapper.Map>(addedEntities); } /// diff --git a/DMS.Infrastructure/Repositories/VariableTableRepository.cs b/DMS.Infrastructure/Repositories/VariableTableRepository.cs index 4dfeb6e..22c740b 100644 --- a/DMS.Infrastructure/Repositories/VariableTableRepository.cs +++ b/DMS.Infrastructure/Repositories/VariableTableRepository.cs @@ -104,10 +104,11 @@ public class VariableTableRepository : BaseRepository, IVariabl } - public Task AddBatchAsync(List entities) + public async Task> AddBatchAsync(List entities) { var dbEntities = _mapper.Map>(entities); - return base.AddBatchAsync(dbEntities); + var addedEntities = await base.AddBatchAsync(dbEntities); + return _mapper.Map>(addedEntities); } /// diff --git a/DMS.Infrastructure/Services/S7ServiceManager.cs b/DMS.Infrastructure/Services/S7ServiceManager.cs index 038cd53..a5d4136 100644 --- a/DMS.Infrastructure/Services/S7ServiceManager.cs +++ b/DMS.Infrastructure/Services/S7ServiceManager.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using DMS.Application.DTOs; +using DMS.Application.Events; using DMS.Application.Interfaces; using DMS.Core.Enums; using DMS.Infrastructure.Interfaces.Services; @@ -19,6 +20,7 @@ namespace DMS.Infrastructure.Services public class S7ServiceManager : IS7ServiceManager { private readonly ILogger _logger; + private readonly IEventService _eventService; private readonly IDataProcessingService _dataProcessingService; private readonly IAppDataCenterService _appDataCenterService; private readonly IS7ServiceFactory _s7ServiceFactory; @@ -28,11 +30,13 @@ namespace DMS.Infrastructure.Services public S7ServiceManager( ILogger logger, + IEventService eventService, IDataProcessingService dataProcessingService, IAppDataCenterService appDataCenterService, IS7ServiceFactory s7ServiceFactory) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _eventService = eventService; _dataProcessingService = dataProcessingService ?? throw new ArgumentNullException(nameof(dataProcessingService)); _appDataCenterService = appDataCenterService ?? throw new ArgumentNullException(nameof(appDataCenterService)); _s7ServiceFactory = s7ServiceFactory ?? throw new ArgumentNullException(nameof(s7ServiceFactory)); @@ -167,18 +171,25 @@ namespace DMS.Infrastructure.Services if (context.S7Service.IsConnected) { context.IsConnected = true; + + _logger.LogInformation("设备 {DeviceName} 连接成功", context.Device.Name); } else { _logger.LogWarning("设备 {DeviceName} 连接失败", context.Device.Name); } + _eventService.RaiseDeviceConnectChanged( + this, new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, false, context.IsConnected)); } catch (Exception ex) { _logger.LogError(ex, "连接设备 {DeviceName} 时发生错误: {ErrorMessage}", context.Device.Name, ex.Message); context.IsConnected = false; + + _eventService.RaiseDeviceConnectChanged( + this, new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, false, context.IsConnected)); } finally { @@ -199,6 +210,9 @@ namespace DMS.Infrastructure.Services _logger.LogInformation("正在断开设备 {DeviceName} 的连接", context.Device.Name); await context.S7Service.DisconnectAsync(); context.IsConnected = false; + + _eventService.RaiseDeviceConnectChanged( + this, new DeviceConnectChangedEventArgs(context.Device.Id, context.Device.Name, false, context.IsConnected)); _logger.LogInformation("设备 {DeviceName} 连接已断开", context.Device.Name); } catch (Exception ex) diff --git a/DMS.WPF/App.xaml.cs b/DMS.WPF/App.xaml.cs index 1a66eb0..e6b3e9b 100644 --- a/DMS.WPF/App.xaml.cs +++ b/DMS.WPF/App.xaml.cs @@ -79,6 +79,8 @@ public partial class App : System.Windows.Application dataProcessingService.AddProcessor(Host.Services.GetRequiredService()); // 添加报警处理器 dataProcessingService.AddProcessor(Host.Services.GetRequiredService()); + // 添加触发器处理器 + dataProcessingService.AddProcessor(Host.Services.GetRequiredService()); } catch (Exception exception) { @@ -201,6 +203,7 @@ public partial class App : System.Windows.Application services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); // 注册触发器处理器 // 注册Core中的仓库 services.AddSingleton(); diff --git a/DMS.WPF/DMS.WPF.csproj b/DMS.WPF/DMS.WPF.csproj index 8f78734..52ccd13 100644 --- a/DMS.WPF/DMS.WPF.csproj +++ b/DMS.WPF/DMS.WPF.csproj @@ -176,7 +176,6 @@ - diff --git a/DMS.WPF/Services/DataEventService.cs b/DMS.WPF/Services/DataEventService.cs index 6aef794..bf28dda 100644 --- a/DMS.WPF/Services/DataEventService.cs +++ b/DMS.WPF/Services/DataEventService.cs @@ -5,7 +5,6 @@ using CommunityToolkit.Mvvm.Messaging; using DMS.Application.DTOs; using DMS.Application.DTOs.Events; using DMS.Application.Interfaces; -using DMS.Application.Services.Triggers; // 添加触发器服务引用 using DMS.Core.Enums; using DMS.Core.Models; using DMS.Message; @@ -23,7 +22,6 @@ public class DataEventService : IDataEventService private readonly IDataStorageService _dataStorageService; private readonly IAppDataCenterService _appDataCenterService; private readonly IWPFDataService _wpfDataService; - private readonly ITriggerEvaluationService _triggerEvaluationService; // 新增依赖 /// /// DataEventService类的构造函数。 @@ -31,15 +29,12 @@ public class DataEventService : IDataEventService public DataEventService(IMapper mapper, IDataStorageService dataStorageService, IAppDataCenterService appDataCenterService, - IWPFDataService wpfDataService, - ITriggerEvaluationService triggerEvaluationService // 新增参数 - ) + IWPFDataService wpfDataService) { _mapper = mapper; _dataStorageService = dataStorageService; _appDataCenterService = appDataCenterService; _wpfDataService = wpfDataService; - _triggerEvaluationService = triggerEvaluationService; // 赋值 // 监听变量值变更事件 _appDataCenterService.VariableManagementService.OnVariableValueChanged += OnVariableValueChanged; @@ -65,7 +60,7 @@ public class DataEventService : IDataEventService /// /// 处理变量值变更事件。 /// - private async void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e) // 改为 async void 以便调用 await + private void OnVariableValueChanged(object? sender, VariableValueChangedEventArgs e) { // 在UI线程上更新变量值 App.Current.Dispatcher.BeginInvoke(new Action(() => @@ -79,27 +74,6 @@ public class DataEventService : IDataEventService variableToUpdate.UpdatedAt = e.UpdateTime; } })); - - // 在后台线程上调用触发器评估服务 - // 使用 Task.Run 将其放到线程池线程上执行,避免阻塞 UI 线程 - // 注意:这里调用的是 Fire-and-forget,因为我们不等待结果。 - // 如果将来需要处理执行结果或错误,可以考虑使用 async Task 并在适当的地方等待。 - _ = Task.Run(async () => - { - try - { - await _triggerEvaluationService.EvaluateTriggersAsync(e.VariableId, e.NewValue); - } - catch (Exception ex) - { - // Log the exception appropriately. - // Since this is fire-and-forget, we must handle exceptions internally. - // You might have a logging service injected or use a static logger. - // For now, let's assume a static logger or inline comment. - System.Diagnostics.Debug.WriteLine($"Error evaluating triggers for variable {e.VariableId}: {ex}"); - // Consider integrating with your logging framework (e.g., NLog) here. - } - }); } diff --git a/DMS.WPF/ViewModels/VariableHistoryViewModel.cs b/DMS.WPF/ViewModels/VariableHistoryViewModel.cs index 1414ac4..a116ef2 100644 --- a/DMS.WPF/ViewModels/VariableHistoryViewModel.cs +++ b/DMS.WPF/ViewModels/VariableHistoryViewModel.cs @@ -39,26 +39,26 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable /// 建议的变量列表 /// [ObservableProperty] - private ObservableCollection _suggestedVariables; - + private ObservableCollection _suggestedVariables; + /// /// 历史记录条数限制 /// [ObservableProperty] private int? _historyLimit; - + /// /// 历史记录开始时间 /// [ObservableProperty] private DateTime? _startTime; - + /// /// 历史记录结束时间 /// [ObservableProperty] private DateTime? _endTime; - + /// /// 选中的变量历史记录 /// @@ -79,7 +79,8 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable private List _allVariableHistories; public VariableHistoryViewModel(IMapper mapper, IDialogService dialogService, IHistoryAppService historyAppService, - IWPFDataService wpfDataService, IDataStorageService dataStorageService, INotificationService notificationService) + IWPFDataService wpfDataService, IDataStorageService dataStorageService, + INotificationService notificationService) { _mapper = mapper; _dialogService = dialogService; @@ -92,8 +93,8 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable _variableHistorySynchronizedView = _variableHistoryList.CreateView(v => v); VariableHistories = _variableHistorySynchronizedView.ToNotifyCollectionChanged(); _allVariableHistories = new List(); - _suggestedVariables = new ObservableCollection(); - + _suggestedVariables = new ObservableCollection(); + // 初始化默认值 _historyLimit = 1000; // 默认限制1000条记录 _startTime = null; @@ -115,7 +116,7 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable var allHistories = await _historyAppService.GetAllVariableHistoriesAsync(limit, startTime, endTime); _allVariableHistories = allHistories.ToList(); _variableHistoryList.AddRange(_allVariableHistories); - + // 更新建议列表 UpdateSuggestedVariables(); } @@ -127,52 +128,38 @@ partial class VariableHistoryViewModel : ViewModelBase, INavigatable } /// -/// 更新建议的变量列表 -/// -private void UpdateSuggestedVariables() -{ - // 清空现有建议列表 - _suggestedVariables.Clear(); - - if (string.IsNullOrWhiteSpace(SearchText)) + /// 更新建议的变量列表 + /// + private void UpdateSuggestedVariables() { - // 如果搜索文本为空,显示所有唯一的变量名 - var uniqueVariables = _allVariableHistories - .GroupBy(h => h.VariableName) - .Select(g => g.First()) - .Take(10) - .ToList(); + // 清空现有建议列表 + _suggestedVariables.Clear(); - foreach (var variable in uniqueVariables) + if (!string.IsNullOrWhiteSpace(SearchText)) { - _suggestedVariables.Add(variable); + // 根据搜索文本过滤建议列表 + 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); + } } } - else - { - // 根据搜索文本过滤建议列表 - var filteredVariables = _allVariableHistories - .Where(h => - h.VariableName?.Contains(SearchText, StringComparison.OrdinalIgnoreCase) == - true) - .GroupBy(h => h.VariableName) - .Select(g => g.First()) - .Take(10) - .ToList(); - - foreach (var variable in filteredVariables) - { - _suggestedVariables.Add(variable); - } - } -} public async Task OnNavigatedToAsync(MenuItemViewModel menu) { // 加载所有变量的历史记录 LoadAllVariableHistories(HistoryLimit, StartTime, EndTime); } - + /// /// 重新加载历史记录命令 /// @@ -181,7 +168,7 @@ private void UpdateSuggestedVariables() { LoadAllVariableHistories(HistoryLimit, StartTime, EndTime); } - + /// /// 更新建议列表命令 /// @@ -190,16 +177,19 @@ private void UpdateSuggestedVariables() { UpdateSuggestedVariables(); } - + /// /// 当搜索文本改变时触发 /// /// partial void OnSearchTextChanged(string value) { + // 添加调试信息 + System.Diagnostics.Debug.WriteLine($"OnSearchTextChanged called with value: '{value}'"); + // 更新建议列表 UpdateSuggestedVariables(); - + if (string.IsNullOrWhiteSpace(value)) { // 如果搜索文本为空,显示所有历史记录 @@ -219,7 +209,7 @@ private void UpdateSuggestedVariables() _variableHistoryList.AddRange(filteredHistories); } } - + /// /// 根据搜索文本过滤历史记录 /// @@ -245,7 +235,7 @@ private void UpdateSuggestedVariables() _variableHistoryList.AddRange(filteredHistories); } } - + /// /// 重新加载历史记录,使用当前设置的限制和时间范围 /// @@ -253,7 +243,7 @@ private void UpdateSuggestedVariables() { LoadAllVariableHistories(HistoryLimit, StartTime, EndTime); } - + /// /// 根据变量ID加载历史记录 /// @@ -261,14 +251,15 @@ private void UpdateSuggestedVariables() /// 返回记录的最大数量,null表示无限制 /// 开始时间,null表示无限制 /// 结束时间,null表示无限制 - public async Task LoadVariableHistoriesAsync(int variableId, int? limit = null, DateTime? startTime = null, DateTime? endTime = null) + public async Task LoadVariableHistoriesAsync(int variableId, int? limit = null, DateTime? startTime = null, + DateTime? endTime = null) { try { _variableHistoryList.Clear(); var histories = await _historyAppService.GetVariableHistoriesAsync(variableId, limit, startTime, endTime); _variableHistoryList.AddRange(histories); - + // 更新建议列表 UpdateSuggestedVariables(); } diff --git a/DMS.WPF/ViewModels/VariableTableViewModel.cs b/DMS.WPF/ViewModels/VariableTableViewModel.cs index 02d25c1..12bc2d0 100644 --- a/DMS.WPF/ViewModels/VariableTableViewModel.cs +++ b/DMS.WPF/ViewModels/VariableTableViewModel.cs @@ -261,10 +261,10 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable if (improtVariableDtos.Count != 0) { - var isSuccess = await _variableAppService.BatchImportVariablesAsync(improtVariableDtos); - if (isSuccess) + var addVariableDtos = await _variableAppService.BatchImportVariablesAsync(improtVariableDtos); + if (addVariableDtos is { Count: > 0 }) { - _variableItemList.AddRange(_mapper.Map>(improtVariableDtos)); + _variableItemList.AddRange(_mapper.Map>(addVariableDtos)); _notificationService.ShowSuccess($"从Excel导入变量成功,共导入变量:{improtVariableDtos.Count}个"); } } @@ -338,12 +338,9 @@ partial class VariableTableViewModel : ViewModelBase, INavigatable // 如果还有变量需要导入,则执行导入操作 if (importedVariableDtos.Count != 0) { - var isSuccess = await _variableAppService.BatchImportVariablesAsync(importedVariableDtos); - if (isSuccess) + var addVariableDtos = await _variableAppService.BatchImportVariablesAsync(importedVariableDtos); + if (addVariableDtos is { Count: > 0 }) { - var addVariableDtos = await _variableAppService.GetVariableByOpcUaNodeIdsAsync( - importedVariableDtos.Select(v => v.OpcUaNodeId) - .ToList()); _variableItemList.AddRange(_mapper.Map>(addVariableDtos)); _notificationService.ShowSuccess($"从OPC UA服务器导入变量成功,共导入变量:{importedVariableDtos.Count}个"); } diff --git a/DMS.WPF/Views/DevicesView.xaml b/DMS.WPF/Views/DevicesView.xaml index 7842814..5d64e2f 100644 --- a/DMS.WPF/Views/DevicesView.xaml +++ b/DMS.WPF/Views/DevicesView.xaml @@ -98,7 +98,7 @@ - - @@ -267,6 +267,7 @@