From 226c6c73879bd9836053d26e0a6df25b236abcb8 Mon Sep 17 00:00:00 2001 From: "David P.G" Date: Wed, 1 Oct 2025 19:21:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BB=E8=A6=81=E5=8F=98=E6=9B=B4=EF=BC=9A?= =?UTF-8?q?=20=20=20=201.=20=E5=B0=86=E5=86=85=E5=AD=98=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E5=92=8C=E4=BA=8B=E4=BB=B6=E8=A7=A6=E5=8F=91=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E5=88=B0=E6=95=B0=E6=8D=AE=E5=BA=93=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E6=96=B9=E6=B3=95=E4=B8=AD=20=20=20=202.=20=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=BA=86=E7=8B=AC=E7=AB=8B=E7=9A=84=E5=86=85=E5=AD=98?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E6=96=B9=E6=B3=95=EF=BC=88AddVariableToMemor?= =?UTF-8?q?y=E3=80=81UpdateVariableInMemory=E3=80=81RemoveVariableFromMemo?= =?UTF-8?q?ry=EF=BC=89=20=20=20=203.=20=E6=9B=B4=E6=96=B0=E4=BA=86?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89=EF=BC=8C=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E4=BA=86=E5=86=85=E5=AD=98=E6=93=8D=E4=BD=9C=E6=96=B9=E6=B3=95?= =?UTF-8?q?=20=20=20=204.=20=E7=A1=AE=E4=BF=9D=E4=BA=86=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E6=93=8D=E4=BD=9C=E5=92=8C=E5=86=85=E5=AD=98=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E4=BF=9D=E6=8C=81=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Management/IVariableManagementService.cs | 15 -- .../Management/VariableManagementService.cs | 157 +++++++++++------- 2 files changed, 100 insertions(+), 72 deletions(-) diff --git a/DMS.Application/Interfaces/Management/IVariableManagementService.cs b/DMS.Application/Interfaces/Management/IVariableManagementService.cs index fb9aacf..a1b693d 100644 --- a/DMS.Application/Interfaces/Management/IVariableManagementService.cs +++ b/DMS.Application/Interfaces/Management/IVariableManagementService.cs @@ -39,19 +39,4 @@ public interface IVariableManagementService /// 异步批量删除变量。 /// Task DeleteVariablesAsync(List ids); - - /// - /// 在内存中添加变量 - /// - void AddVariableToMemory(VariableDto variableDto, ConcurrentDictionary variableTables); - - /// - /// 在内存中更新变量 - /// - void UpdateVariableInMemory(VariableDto variableDto, ConcurrentDictionary variableTables); - - /// - /// 在内存中删除变量 - /// - void RemoveVariableFromMemory(int variableId, ConcurrentDictionary variableTables); } \ No newline at end of file diff --git a/DMS.Application/Services/Management/VariableManagementService.cs b/DMS.Application/Services/Management/VariableManagementService.cs index 6956583..56c7fea 100644 --- a/DMS.Application/Services/Management/VariableManagementService.cs +++ b/DMS.Application/Services/Management/VariableManagementService.cs @@ -51,7 +51,27 @@ public class VariableManagementService : IVariableManagementService /// public async Task CreateVariableAsync(VariableDto variableDto) { - return await _variableAppService.CreateVariableAsync(variableDto); + var result = await _variableAppService.CreateVariableAsync(variableDto); + + // 创建成功后,将变量添加到内存中 + if (result != null) + { + VariableTableDto variableTableDto = null; + if (_appDataStorageService.VariableTables.TryGetValue(result.VariableTableId, out var variableTable)) + { + variableTableDto = variableTable; + result.VariableTable = variableTableDto; + variableTable.Variables.Add(result); + } + + if (_appDataStorageService.Variables.TryAdd(result.Id, result)) + { + _eventService.RaiseVariableChanged( + this, new VariableChangedEventArgs(DataChangeType.Added, result, variableTableDto)); + } + } + + return result; } /// @@ -59,7 +79,23 @@ public class VariableManagementService : IVariableManagementService /// public async Task UpdateVariableAsync(VariableDto variableDto) { - return await _variableAppService.UpdateVariableAsync(variableDto); + var result = await _variableAppService.UpdateVariableAsync(variableDto); + + // 更新成功后,更新内存中的变量 + if (result > 0 && variableDto != null) + { + VariableTableDto variableTableDto = null; + if (_appDataStorageService.VariableTables.TryGetValue(variableDto.VariableTableId, out var variableTable)) + { + variableTableDto = variableTable; + } + + _appDataStorageService.Variables.AddOrUpdate(variableDto.Id, variableDto, (key, oldValue) => variableDto); + _eventService.RaiseVariableChanged( + this, new VariableChangedEventArgs(DataChangeType.Updated, variableDto, variableTableDto)); + } + + return result; } /// @@ -67,7 +103,26 @@ public class VariableManagementService : IVariableManagementService /// public async Task UpdateVariablesAsync(List variableDtos) { - return await _variableAppService.UpdateVariablesAsync(variableDtos); + var result = await _variableAppService.UpdateVariablesAsync(variableDtos); + + // 批量更新成功后,更新内存中的变量 + if (result > 0 && variableDtos != null) + { + foreach (var variableDto in variableDtos) + { + VariableTableDto variableTableDto = null; + if (_appDataStorageService.VariableTables.TryGetValue(variableDto.VariableTableId, out var variableTable)) + { + variableTableDto = variableTable; + } + + _appDataStorageService.Variables.AddOrUpdate(variableDto.Id, variableDto, (key, oldValue) => variableDto); + _eventService.RaiseVariableChanged( + this, new VariableChangedEventArgs(DataChangeType.Updated, variableDto, variableTableDto)); + } + } + + return result; } /// @@ -75,7 +130,27 @@ public class VariableManagementService : IVariableManagementService /// public async Task DeleteVariableAsync(int id) { - return await _variableAppService.DeleteVariableAsync(id); + var variable = await _variableAppService.GetVariableByIdAsync(id); // 获取变量信息用于内存删除 + var result = await _variableAppService.DeleteVariableAsync(id); + + // 删除成功后,从内存中移除变量 + if (result && variable != null) + { + if (_appDataStorageService.Variables.TryRemove(id, out var variableDto)) + { + VariableTableDto variableTableDto = null; + if (variableDto != null && _appDataStorageService.VariableTables.TryGetValue(variableDto.VariableTableId, out var variableTable)) + { + variableTableDto = variableTable; + variableTable.Variables.Remove(variableDto); + } + + _eventService.RaiseVariableChanged( + this, new VariableChangedEventArgs(DataChangeType.Deleted, variableDto, variableTableDto)); + } + } + + return result; } /// @@ -83,62 +158,30 @@ public class VariableManagementService : IVariableManagementService /// public async Task DeleteVariablesAsync(List ids) { - return await _variableAppService.DeleteVariablesAsync(ids); - } - - /// - /// 在内存中添加变量 - /// - public void AddVariableToMemory(VariableDto variableDto, ConcurrentDictionary variableTables) - { - VariableTableDto variableTableDto = null; - if (variableTables.TryGetValue(variableDto.VariableTableId, out var variableTable)) + var result = await _variableAppService.DeleteVariablesAsync(ids); + + // 批量删除成功后,从内存中移除变量 + if (result && ids != null) { - variableTableDto = variableTable; - variableDto.VariableTable = variableTableDto; - variableTable.Variables.Add(variableDto); - } - - if (_appDataStorageService.Variables.TryAdd(variableDto.Id, variableDto)) - { - _eventService.RaiseVariableChanged( - this, new VariableChangedEventArgs(DataChangeType.Added, variableDto, variableTableDto)); - } - } - - /// - /// 在内存中更新变量 - /// - public void UpdateVariableInMemory(VariableDto variableDto, - ConcurrentDictionary variableTables) - { - VariableTableDto variableTableDto = null; - if (variableTables.TryGetValue(variableDto.VariableTableId, out var variableTable)) - { - variableTableDto = variableTable; - } - - _appDataStorageService.Variables.AddOrUpdate(variableDto.Id, variableDto, (key, oldValue) => variableDto); - _eventService.RaiseVariableChanged( - this, new VariableChangedEventArgs(DataChangeType.Updated, variableDto, variableTableDto)); - } - - /// - /// 在内存中删除变量 - /// - public void RemoveVariableFromMemory(int variableId, ConcurrentDictionary variableTables) - { - if (_appDataStorageService.Variables.TryRemove(variableId, out var variableDto)) - { - VariableTableDto variableTableDto = null; - if (variableDto != null && variableTables.TryGetValue(variableDto.VariableTableId, out var variableTable)) + foreach (var id in ids) { - variableTableDto = variableTable; - variableTable.Variables.Remove(variableDto); - } + if (_appDataStorageService.Variables.TryRemove(id, out var variableDto)) + { + VariableTableDto variableTableDto = null; + if (variableDto != null && _appDataStorageService.VariableTables.TryGetValue(variableDto.VariableTableId, out var variableTable)) + { + variableTableDto = variableTable; + variableTable.Variables.Remove(variableDto); + } - _eventService.RaiseVariableChanged( - this, new VariableChangedEventArgs(DataChangeType.Deleted, variableDto, variableTableDto)); + _eventService.RaiseVariableChanged( + this, new VariableChangedEventArgs(DataChangeType.Deleted, variableDto, variableTableDto)); + } + } } + + return result; } + + } \ No newline at end of file