refactor:将AppDataCenterService改为AppCenterService,将AppDataStorageService改为AppStorageService,将触发器的增删改成合并
This commit is contained in:
@@ -14,13 +14,13 @@ namespace DMS.Application.Services.Management;
|
||||
public class DeviceManagementService : IDeviceManagementService
|
||||
{
|
||||
private readonly IDeviceAppService _deviceAppService;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IAppStorageService _appStorageService;
|
||||
private readonly IEventService _eventService;
|
||||
|
||||
public DeviceManagementService(IDeviceAppService deviceAppService, IAppDataStorageService appDataStorageService, IEventService eventService)
|
||||
public DeviceManagementService(IDeviceAppService deviceAppService, IAppStorageService appStorageService, IEventService eventService)
|
||||
{
|
||||
_deviceAppService = deviceAppService;
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_appStorageService = appStorageService;
|
||||
_eventService = eventService;
|
||||
}
|
||||
|
||||
@@ -50,11 +50,11 @@ public class DeviceManagementService : IDeviceManagementService
|
||||
// 创建成功后,将设备添加到内存中
|
||||
if (result?.Device != null)
|
||||
{
|
||||
if (_appDataStorageService.Devices.TryAdd(result.Device.Id, result.Device))
|
||||
if (_appStorageService.Devices.TryAdd(result.Device.Id, result.Device))
|
||||
{
|
||||
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Added, result.Device));
|
||||
}
|
||||
if (_appDataStorageService.VariableTables.TryAdd(result.VariableTable.Id, result.VariableTable))
|
||||
if (_appStorageService.VariableTables.TryAdd(result.VariableTable.Id, result.VariableTable))
|
||||
{
|
||||
_eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs(DataChangeType.Added, result.VariableTable));
|
||||
}
|
||||
@@ -73,7 +73,7 @@ public class DeviceManagementService : IDeviceManagementService
|
||||
// 更新成功后,更新内存中的设备
|
||||
if (result > 0 && device != null)
|
||||
{
|
||||
_appDataStorageService.Devices.AddOrUpdate(device.Id, device, (key, oldValue) => device);
|
||||
_appStorageService.Devices.AddOrUpdate(device.Id, device, (key, oldValue) => device);
|
||||
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Updated, device));
|
||||
}
|
||||
|
||||
@@ -91,19 +91,19 @@ public class DeviceManagementService : IDeviceManagementService
|
||||
// 删除成功后,从内存中移除设备
|
||||
if (result && device != null)
|
||||
{
|
||||
if (_appDataStorageService.Devices.TryGetValue(deviceId, out var deviceInStorage))
|
||||
if (_appStorageService.Devices.TryGetValue(deviceId, out var deviceInStorage))
|
||||
{
|
||||
foreach (var variableTable in deviceInStorage.VariableTables)
|
||||
{
|
||||
foreach (var variable in variableTable.Variables)
|
||||
{
|
||||
_appDataStorageService.Variables.TryRemove(variable.Id, out _);
|
||||
_appStorageService.Variables.TryRemove(variable.Id, out _);
|
||||
}
|
||||
|
||||
_appDataStorageService.VariableTables.TryRemove(variableTable.Id, out _);
|
||||
_appStorageService.VariableTables.TryRemove(variableTable.Id, out _);
|
||||
}
|
||||
|
||||
_appDataStorageService.Devices.TryRemove(deviceId, out _);
|
||||
_appStorageService.Devices.TryRemove(deviceId, out _);
|
||||
|
||||
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Deleted, deviceInStorage));
|
||||
}
|
||||
@@ -123,7 +123,7 @@ public class DeviceManagementService : IDeviceManagementService
|
||||
var device = await _deviceAppService.GetDeviceByIdAsync(id);
|
||||
if (device != null)
|
||||
{
|
||||
_appDataStorageService.Devices.AddOrUpdate(device.Id, device, (key, oldValue) => device);
|
||||
_appStorageService.Devices.AddOrUpdate(device.Id, device, (key, oldValue) => device);
|
||||
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Updated, device));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,17 +13,17 @@ namespace DMS.Application.Services.Management;
|
||||
public class LogManagementService : ILogManagementService
|
||||
{
|
||||
private readonly INlogAppService _nlogAppService;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IAppStorageService _appStorageService;
|
||||
|
||||
/// <summary>
|
||||
/// 当日志数据发生变化时触发
|
||||
/// </summary>
|
||||
public event EventHandler<NlogChangedEventArgs> OnLogChanged;
|
||||
|
||||
public LogManagementService(INlogAppService nlogAppService,IAppDataStorageService appDataStorageService)
|
||||
public LogManagementService(INlogAppService nlogAppService,IAppStorageService appStorageService)
|
||||
{
|
||||
_nlogAppService = nlogAppService;
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_appStorageService = appStorageService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -63,7 +63,7 @@ public class LogManagementService : ILogManagementService
|
||||
/// </summary>
|
||||
public void AddNlogToMemory(NlogDto nlogDto)
|
||||
{
|
||||
if (_appDataStorageService.Nlogs.TryAdd(nlogDto.Id, nlogDto))
|
||||
if (_appStorageService.Nlogs.TryAdd(nlogDto.Id, nlogDto))
|
||||
{
|
||||
OnLogChanged?.Invoke(this,new NlogChangedEventArgs(DataChangeType.Added, nlogDto));
|
||||
}
|
||||
@@ -74,7 +74,7 @@ public class LogManagementService : ILogManagementService
|
||||
/// </summary>
|
||||
public void UpdateNlogInMemory(NlogDto nlogDto)
|
||||
{
|
||||
_appDataStorageService.Nlogs.AddOrUpdate(nlogDto.Id, nlogDto, (key, oldValue) => nlogDto);
|
||||
_appStorageService.Nlogs.AddOrUpdate(nlogDto.Id, nlogDto, (key, oldValue) => nlogDto);
|
||||
OnLogChanged?.Invoke(this,new NlogChangedEventArgs(DataChangeType.Updated, nlogDto));
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ public class LogManagementService : ILogManagementService
|
||||
/// </summary>
|
||||
public void RemoveNlogFromMemory(int nlogId)
|
||||
{
|
||||
if (_appDataStorageService.Nlogs.TryRemove(nlogId, out var nlogDto))
|
||||
if (_appStorageService.Nlogs.TryRemove(nlogId, out var nlogDto))
|
||||
{
|
||||
OnLogChanged?.Invoke(this,new NlogChangedEventArgs(DataChangeType.Deleted, nlogDto));
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace DMS.Application.Services.Management;
|
||||
public class MenuManagementService : IMenuManagementService
|
||||
{
|
||||
private readonly IMenuAppService _menuService;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IAppStorageService _appStorageService;
|
||||
private readonly IEventService _eventService;
|
||||
|
||||
/// <summary>
|
||||
@@ -20,10 +20,10 @@ public class MenuManagementService : IMenuManagementService
|
||||
/// </summary>
|
||||
public event EventHandler<MenuChangedEventArgs> MenuChanged;
|
||||
|
||||
public MenuManagementService(IMenuAppService menuService, IAppDataStorageService appDataStorageService, IEventService eventService)
|
||||
public MenuManagementService(IMenuAppService menuService, IAppStorageService appStorageService, IEventService eventService)
|
||||
{
|
||||
_menuService = menuService;
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_appStorageService = appStorageService;
|
||||
_eventService = eventService;
|
||||
}
|
||||
|
||||
@@ -54,10 +54,10 @@ public class MenuManagementService : IMenuManagementService
|
||||
if (result > 0)
|
||||
{
|
||||
menu.Id = result; // 假设返回的ID是新创建的
|
||||
if (_appDataStorageService.Menus.TryAdd(menu.Id, menu))
|
||||
if (_appStorageService.Menus.TryAdd(menu.Id, menu))
|
||||
{
|
||||
MenuBean parentMenu = null;
|
||||
if (menu.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menu.ParentId.Value, out var parent))
|
||||
if (menu.ParentId > 0 && _appStorageService.Menus.TryGetValue(menu.ParentId.Value, out var parent))
|
||||
{
|
||||
parentMenu = parent;
|
||||
parent.Children.Add(menu);
|
||||
@@ -80,7 +80,7 @@ public class MenuManagementService : IMenuManagementService
|
||||
// 更新成功后,更新内存中的菜单
|
||||
if (result > 0 && menu != null)
|
||||
{
|
||||
_appDataStorageService.Menus.AddOrUpdate(menu.Id, menu, (key, oldValue) => menu);
|
||||
_appStorageService.Menus.AddOrUpdate(menu.Id, menu, (key, oldValue) => menu);
|
||||
|
||||
|
||||
_eventService.RaiseMenuChanged(this, new MenuChangedEventArgs(DataChangeType.Updated, menu));
|
||||
@@ -100,10 +100,10 @@ public class MenuManagementService : IMenuManagementService
|
||||
// 删除成功后,从内存中移除菜单
|
||||
if (result && menu != null)
|
||||
{
|
||||
if (_appDataStorageService.Menus.TryRemove(id, out var menuData))
|
||||
if (_appStorageService.Menus.TryRemove(id, out var menuData))
|
||||
{
|
||||
// 从父菜单中移除子菜单
|
||||
if (menuData.ParentId > 0 && _appDataStorageService.Menus.TryGetValue(menuData.ParentId.Value, out var parentMenu))
|
||||
if (menuData.ParentId > 0 && _appStorageService.Menus.TryGetValue(menuData.ParentId.Value, out var parentMenu))
|
||||
{
|
||||
parentMenu.Children.Remove(menuData);
|
||||
}
|
||||
@@ -120,7 +120,7 @@ public class MenuManagementService : IMenuManagementService
|
||||
/// </summary>
|
||||
public List<MenuBean> GetRootMenus()
|
||||
{
|
||||
return _appDataStorageService.Menus.Values.Where(m => m.ParentId == 0)
|
||||
return _appStorageService.Menus.Values.Where(m => m.ParentId == 0)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ public class MenuManagementService : IMenuManagementService
|
||||
/// <returns>子菜单列表</returns>
|
||||
public List<MenuBean> GetChildMenus(int parentId)
|
||||
{
|
||||
return _appDataStorageService.Menus.Values.Where(m => m.ParentId == parentId)
|
||||
return _appStorageService.Menus.Values.Where(m => m.ParentId == parentId)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
@@ -141,7 +141,7 @@ public class MenuManagementService : IMenuManagementService
|
||||
public void BuildMenuTree()
|
||||
{
|
||||
// 清空现有菜单树
|
||||
_appDataStorageService.MenuTrees.Clear();
|
||||
_appStorageService.MenuTrees.Clear();
|
||||
|
||||
// 获取所有根菜单
|
||||
var rootMenus = GetRootMenus();
|
||||
@@ -149,7 +149,7 @@ public class MenuManagementService : IMenuManagementService
|
||||
// 将根菜单添加到菜单树中
|
||||
foreach (var rootMenu in rootMenus)
|
||||
{
|
||||
_appDataStorageService.MenuTrees.TryAdd(rootMenu.Id, rootMenu);
|
||||
_appStorageService.MenuTrees.TryAdd(rootMenu.Id, rootMenu);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,11 +12,11 @@ public class MqttAliasManagementService : IMqttAliasManagementService
|
||||
{
|
||||
private readonly IMqttAliasAppService _appService;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly IAppDataStorageService _storageService;
|
||||
private readonly IAppStorageService _storageService;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public MqttAliasManagementService(IMqttAliasAppService appService, IEventService eventService,
|
||||
IAppDataStorageService storageService, IMapper mapper)
|
||||
IAppStorageService storageService, IMapper mapper)
|
||||
{
|
||||
_appService = appService;
|
||||
_eventService = eventService;
|
||||
|
||||
@@ -15,15 +15,15 @@ namespace DMS.Application.Services.Management;
|
||||
public class MqttManagementService : IMqttManagementService
|
||||
{
|
||||
private readonly IMqttAppService _mqttAppService;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IAppStorageService _appStorageService;
|
||||
private readonly IEventService _eventService;
|
||||
|
||||
public MqttManagementService(IMqttAppService mqttAppService,
|
||||
IAppDataStorageService appDataStorageService,
|
||||
IAppStorageService appStorageService,
|
||||
IEventService eventService)
|
||||
{
|
||||
_mqttAppService = mqttAppService;
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_appStorageService = appStorageService;
|
||||
_eventService = eventService;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public class MqttManagementService : IMqttManagementService
|
||||
/// </summary>
|
||||
public async Task<MqttServer> GetMqttServerByIdAsync(int id)
|
||||
{
|
||||
if (_appDataStorageService.MqttServers.TryGetValue(id,out var mqttServer))
|
||||
if (_appStorageService.MqttServers.TryGetValue(id,out var mqttServer))
|
||||
{
|
||||
return mqttServer;
|
||||
}
|
||||
@@ -44,7 +44,7 @@ public class MqttManagementService : IMqttManagementService
|
||||
/// </summary>
|
||||
public async Task<List<MqttServer>> GetAllMqttServersAsync()
|
||||
{
|
||||
return _appDataStorageService.MqttServers.Values.ToList();
|
||||
return _appStorageService.MqttServers.Values.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -67,7 +67,7 @@ public class MqttManagementService : IMqttManagementService
|
||||
{
|
||||
foreach (var mqttServer in mqttServers)
|
||||
{
|
||||
if (_appDataStorageService.MqttServers.TryGetValue(mqttServer.Id, out var mMqttServer))
|
||||
if (_appStorageService.MqttServers.TryGetValue(mqttServer.Id, out var mMqttServer))
|
||||
{
|
||||
// 比较旧值和新值,确定哪个属性发生了变化
|
||||
var changedProperties = GetChangedProperties(mMqttServer, mqttServer);
|
||||
@@ -99,7 +99,7 @@ public class MqttManagementService : IMqttManagementService
|
||||
else
|
||||
{
|
||||
// 如果内存中不存在该MQTT服务器,则直接添加
|
||||
_appDataStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
|
||||
_appStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer);
|
||||
_eventService.RaiseMqttServerChanged(
|
||||
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServer, MqttServerPropertyType.All));
|
||||
}
|
||||
@@ -120,7 +120,7 @@ public class MqttManagementService : IMqttManagementService
|
||||
// 删除成功后,从内存中移除MQTT服务器
|
||||
if (result && mqttServer != null)
|
||||
{
|
||||
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServerFromCache))
|
||||
if (_appStorageService.MqttServers.TryRemove(id, out var mqttServerFromCache))
|
||||
{
|
||||
_eventService.RaiseMqttServerChanged(
|
||||
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServerFromCache));
|
||||
@@ -142,7 +142,7 @@ public class MqttManagementService : IMqttManagementService
|
||||
{
|
||||
foreach (var id in ids)
|
||||
{
|
||||
if (_appDataStorageService.MqttServers.TryRemove(id, out var mqttServer))
|
||||
if (_appStorageService.MqttServers.TryRemove(id, out var mqttServer))
|
||||
{
|
||||
_eventService.RaiseMqttServerChanged(
|
||||
this, new MqttServerChangedEventArgs(ActionChangeType.Deleted, mqttServer));
|
||||
@@ -167,7 +167,7 @@ public class MqttManagementService : IMqttManagementService
|
||||
|
||||
|
||||
// 将MQTT服务器添加到内存中
|
||||
if (_appDataStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer))
|
||||
if (_appStorageService.MqttServers.TryAdd(mqttServer.Id, mqttServer))
|
||||
{
|
||||
_eventService.RaiseMqttServerChanged(
|
||||
this, new MqttServerChangedEventArgs(ActionChangeType.Added, mqttServer));
|
||||
|
||||
@@ -13,39 +13,39 @@ namespace DMS.Application.Services.Management
|
||||
/// </summary>
|
||||
public class TriggerManagementService : ITriggerManagementService
|
||||
{
|
||||
private readonly IAppStorageService _appStorageService;
|
||||
private readonly IRepositoryManager _repositoryManager;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
|
||||
public TriggerManagementService(IRepositoryManager repositoryManager, IMapper mapper, IAppDataStorageService appDataStorageService)
|
||||
public TriggerManagementService(IAppStorageService appStorageService,IRepositoryManager repositoryManager, IMapper mapper)
|
||||
{
|
||||
_repositoryManager = repositoryManager ?? throw new ArgumentNullException(nameof(repositoryManager));
|
||||
_appStorageService = appStorageService;
|
||||
_repositoryManager = repositoryManager;
|
||||
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
_appDataStorageService = appDataStorageService ?? throw new ArgumentNullException(nameof(appDataStorageService));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有触发器定义
|
||||
/// </summary>
|
||||
public async Task<List<TriggerDefinitionDto>> GetAllTriggersAsync()
|
||||
public List<TriggerDefinition> GetAllTriggersAsync()
|
||||
{
|
||||
var triggers = await _repositoryManager.Triggers.GetAllAsync();
|
||||
return _mapper.Map<List<TriggerDefinitionDto>>(triggers);
|
||||
var triggers = _appStorageService.Triggers.Values.ToList();
|
||||
return _mapper.Map<List<TriggerDefinition>>(triggers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 ID 获取触发器定义
|
||||
/// </summary>
|
||||
public async Task<TriggerDefinitionDto?> GetTriggerByIdAsync(int id)
|
||||
public async Task<TriggerDefinition?> GetTriggerByIdAsync(int id)
|
||||
{
|
||||
var trigger = await _repositoryManager.Triggers.GetByIdAsync(id);
|
||||
return trigger != null ? _mapper.Map<TriggerDefinitionDto>(trigger) : null;
|
||||
_appStorageService.Triggers.TryGetValue(id, out var trigger);
|
||||
return trigger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建一个新的触发器定义
|
||||
/// </summary>
|
||||
public async Task<TriggerDefinitionDto> CreateTriggerAsync(TriggerDefinitionDto triggerDto)
|
||||
public async Task<TriggerDefinition> CreateTriggerAsync(TriggerDefinition triggerDto)
|
||||
{
|
||||
// 1. 验证 DTO (可以在应用层或领域层做)
|
||||
ValidateTriggerDto(triggerDto);
|
||||
@@ -59,10 +59,10 @@ namespace DMS.Application.Services.Management
|
||||
var createdTrigger = await _repositoryManager.Triggers.AddAsync(triggerEntity);
|
||||
|
||||
// 4. 转换回 DTO 并返回
|
||||
var result = _mapper.Map<TriggerDefinitionDto>(createdTrigger);
|
||||
var result = _mapper.Map<TriggerDefinition>(createdTrigger);
|
||||
|
||||
// 5. 同步更新AppDataStorageService中的Triggers字典
|
||||
_appDataStorageService.Triggers[result.Id] = result;
|
||||
_appStorageService.Triggers[result.Id] = result;
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -70,7 +70,7 @@ namespace DMS.Application.Services.Management
|
||||
/// <summary>
|
||||
/// 更新一个已存在的触发器定义
|
||||
/// </summary>
|
||||
public async Task<TriggerDefinitionDto?> UpdateTriggerAsync(int id, TriggerDefinitionDto triggerDto)
|
||||
public async Task<TriggerDefinition?> UpdateTriggerAsync(int id, TriggerDefinition triggerDto)
|
||||
{
|
||||
// 1. 获取现有实体
|
||||
var existingTrigger = await _repositoryManager.Triggers.GetByIdAsync(id);
|
||||
@@ -90,10 +90,10 @@ namespace DMS.Application.Services.Management
|
||||
return null;
|
||||
|
||||
// 5. 转换回 DTO 并返回
|
||||
var result = _mapper.Map<TriggerDefinitionDto>(updatedTrigger);
|
||||
var result = _mapper.Map<TriggerDefinition>(updatedTrigger);
|
||||
|
||||
// 6. 同步更新AppDataStorageService中的Triggers字典
|
||||
_appDataStorageService.Triggers[result.Id] = result;
|
||||
_appStorageService.Triggers[result.Id] = result;
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -103,33 +103,46 @@ namespace DMS.Application.Services.Management
|
||||
/// </summary>
|
||||
public async Task<bool> DeleteTriggerAsync(int id)
|
||||
{
|
||||
var result = await _repositoryManager.Triggers.DeleteAsync(id);
|
||||
|
||||
// 如果删除成功,也从AppDataStorageService中的Triggers字典中移除
|
||||
if (result)
|
||||
{
|
||||
_appDataStorageService.Triggers.TryRemove(id, out _);
|
||||
}
|
||||
|
||||
return result;
|
||||
// var result = await _repositoryManager.Triggers.DeleteAsync(id);
|
||||
//
|
||||
// // 如果删除成功,也从AppDataStorageService中的Triggers字典中移除
|
||||
// if (result)
|
||||
// {
|
||||
// _appStorageService.Triggers.TryRemove(id, out _);
|
||||
// }
|
||||
//
|
||||
// return result;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取与指定变量关联的所有触发器定义
|
||||
/// </summary>
|
||||
public async Task<List<TriggerDefinitionDto>> GetTriggersForVariableAsync(int variableId)
|
||||
public async Task<List<TriggerDefinition>> GetTriggersForVariableAsync(int variableId)
|
||||
{
|
||||
var triggers = await _repositoryManager.Triggers.GetByVariableIdAsync(variableId);
|
||||
return _mapper.Map<List<TriggerDefinitionDto>>(triggers);
|
||||
// var triggers = await _repositoryManager.Triggers.GetByVariableIdAsync(variableId);
|
||||
// return _mapper.Map<List<TriggerDefinition>>(triggers);
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task LoadAllTriggersAsync()
|
||||
{
|
||||
_appStorageService.Triggers.Clear();
|
||||
var triggerDefinitions = await _repositoryManager.Triggers.GetAllAsync();
|
||||
foreach (var triggerDefinition in triggerDefinitions)
|
||||
{
|
||||
_appStorageService.Triggers.TryAdd(triggerDefinition.Id, triggerDefinition);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内部方法:验证 TriggerDefinitionDto 的有效性
|
||||
/// 内部方法:验证 TriggerDefinition 的有效性
|
||||
/// </summary>
|
||||
private void ValidateTriggerDto(TriggerDefinitionDto dto)
|
||||
private void ValidateTriggerDto(TriggerDefinition dto)
|
||||
{
|
||||
// 检查是否至少关联了一个变量
|
||||
if (dto.VariableIds == null || !dto.VariableIds.Any())
|
||||
if (dto.Variables == null || !dto.Variables.Any())
|
||||
throw new ArgumentException("触发器必须至少关联一个变量。");
|
||||
|
||||
// 添加必要的验证逻辑
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
using DMS.Application.Events;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Application.Interfaces.Database;
|
||||
using DMS.Application.Interfaces.Management;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Models.Triggers;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DMS.Application.Services.Management;
|
||||
|
||||
public class TriggerVariableManagementService : ITriggerVariableManagementService
|
||||
{
|
||||
private readonly ITriggerVariableAppService _triggerVariableAppService;
|
||||
private readonly IAppStorageService _appStorageService;
|
||||
private readonly IEventService _eventService;
|
||||
|
||||
public TriggerVariableManagementService(ITriggerVariableAppService triggerVariableAppService,IAppStorageService appStorageService,IEventService eventService)
|
||||
{
|
||||
_triggerVariableAppService = triggerVariableAppService;
|
||||
_appStorageService = appStorageService;
|
||||
_eventService = eventService;
|
||||
}
|
||||
|
||||
public async Task<TriggerVariable> AssignTriggerVariableAsync(TriggerVariable triggerVariable)
|
||||
{
|
||||
var newTriggerVariable = await _triggerVariableAppService.AssignTriggerVariableAsync(triggerVariable);
|
||||
if (newTriggerVariable != null)
|
||||
{
|
||||
// Add to cache
|
||||
_appStorageService.TriggerVariables.TryAdd(newTriggerVariable.Id, newTriggerVariable);
|
||||
|
||||
_eventService.RaiseTriggerVariableChanged(this, new TriggerVariableChangedEventArgs(ActionChangeType.Added, newTriggerVariable));
|
||||
}
|
||||
|
||||
return newTriggerVariable;
|
||||
}
|
||||
|
||||
public async Task<List<TriggerVariable>> LoadAllTriggerVariablesAsync()
|
||||
{
|
||||
var triggerVariables = await _triggerVariableAppService.GetAllAsync();
|
||||
foreach (var triggerVariable in triggerVariables)
|
||||
{
|
||||
// Add to cache
|
||||
_appStorageService.TriggerVariables.TryAdd(triggerVariable.Id, triggerVariable);
|
||||
|
||||
|
||||
if (_appStorageService.Triggers.TryGetValue(triggerVariable.TriggerDefinitionId, out var trigger))
|
||||
{
|
||||
if (_appStorageService.Variables.TryGetValue(triggerVariable.VariableId, out var variable))
|
||||
{
|
||||
trigger.Variables.Add(variable);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
_eventService.RaiseTriggerVariableChanged(this, new TriggerVariableChangedEventArgs(ActionChangeType.Added, triggerVariable));
|
||||
}
|
||||
|
||||
return triggerVariables;
|
||||
}
|
||||
|
||||
public async Task<int> UpdateAsync(TriggerVariable triggerVariable)
|
||||
{
|
||||
int res = await _triggerVariableAppService.UpdateTriggerVariableAsync(triggerVariable);
|
||||
if (res > 0)
|
||||
{
|
||||
// Update cache
|
||||
if (_appStorageService.TriggerVariables.TryGetValue(triggerVariable.Id, out var existingTriggerVariable))
|
||||
{
|
||||
existingTriggerVariable.TriggerDefinitionId = triggerVariable.TriggerDefinitionId;
|
||||
existingTriggerVariable.VariableId = triggerVariable.VariableId;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteAsync(int id)
|
||||
{
|
||||
var result = await _triggerVariableAppService.RemoveTriggerVariableAsync(id);
|
||||
if (result == 0) return false;
|
||||
|
||||
if (_appStorageService.TriggerVariables.TryGetValue(id, out var triggerVariable))
|
||||
{
|
||||
_appStorageService.TriggerVariables.TryRemove(triggerVariable.Id, out _);
|
||||
_eventService.RaiseTriggerVariableChanged(
|
||||
this, new TriggerVariableChangedEventArgs(ActionChangeType.Deleted, triggerVariable));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// public async Task<List<int>> GetVariableIdsByTriggerIdAsync(int triggerId)
|
||||
// {
|
||||
// return await _triggerVariableAppService.GetVariableIdsByTriggerIdAsync(triggerId);
|
||||
// }
|
||||
|
||||
// public async Task<List<int>> GetTriggerIdsByVariableIdAsync(int variableId)
|
||||
// {
|
||||
// return await _triggerVariableAppService.GetTriggerIdsByVariableIdAsync(variableId);
|
||||
// }
|
||||
|
||||
public async Task<List<TriggerVariable>> AddTriggerVariablesAsync(List<TriggerVariable> triggerVariables)
|
||||
{
|
||||
var addedTriggerVariables = await _triggerVariableAppService.AddTriggerVariablesAsync(triggerVariables);
|
||||
foreach (var triggerVariable in addedTriggerVariables)
|
||||
{
|
||||
// Add to cache
|
||||
_appStorageService.TriggerVariables.TryAdd(triggerVariable.Id, triggerVariable);
|
||||
_eventService.RaiseTriggerVariableChanged(this, new TriggerVariableChangedEventArgs(ActionChangeType.Added, triggerVariable));
|
||||
}
|
||||
|
||||
return addedTriggerVariables;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteByTriggerIdAsync(int triggerId)
|
||||
{
|
||||
// var result = await _triggerVariableAppService.RemoveTriggerVariablesByTriggerIdAsync(triggerId);
|
||||
// 注意:这里可能需要额外的缓存管理逻辑,因为删除的是多个条目
|
||||
// 可能需要根据triggerId获取这些变量ID并从缓存中移除
|
||||
// 为简化实现,我们先不处理缓存中的逐个删除,而依赖于后续的重新加载
|
||||
// return result != null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -18,20 +18,20 @@ public class VariableManagementService : IVariableManagementService
|
||||
private readonly IVariableAppService _variableAppService;
|
||||
private readonly IEventService _eventService;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IAppStorageService _appStorageService;
|
||||
private readonly IDataProcessingService _dataProcessingService;
|
||||
|
||||
|
||||
public VariableManagementService(IVariableAppService variableAppService,
|
||||
IEventService eventService,
|
||||
IMapper mapper,
|
||||
IAppDataStorageService appDataStorageService,
|
||||
IAppStorageService appStorageService,
|
||||
IDataProcessingService dataProcessingService)
|
||||
{
|
||||
_variableAppService = variableAppService;
|
||||
_eventService = eventService;
|
||||
_mapper = mapper;
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_appStorageService = appStorageService;
|
||||
_dataProcessingService = dataProcessingService;
|
||||
}
|
||||
|
||||
@@ -61,13 +61,13 @@ public class VariableManagementService : IVariableManagementService
|
||||
// 创建成功后,将变量添加到内存中
|
||||
if (result != null)
|
||||
{
|
||||
if (_appDataStorageService.VariableTables.TryGetValue(result.VariableTableId, out var variableTable))
|
||||
if (_appStorageService.VariableTables.TryGetValue(result.VariableTableId, out var variableTable))
|
||||
{
|
||||
result.VariableTable = variableTable;
|
||||
variableTable.Variables.Add(result);
|
||||
}
|
||||
|
||||
if (_appDataStorageService.Variables.TryAdd(result.Id, result))
|
||||
if (_appStorageService.Variables.TryAdd(result.Id, result))
|
||||
{
|
||||
_eventService.RaiseVariableChanged(
|
||||
this, new VariableChangedEventArgs(ActionChangeType.Added, result));
|
||||
@@ -97,7 +97,7 @@ public class VariableManagementService : IVariableManagementService
|
||||
{
|
||||
foreach (var variable in variables)
|
||||
{
|
||||
if (_appDataStorageService.Variables.TryGetValue(variable.Id, out var mVariable))
|
||||
if (_appStorageService.Variables.TryGetValue(variable.Id, out var mVariable))
|
||||
{
|
||||
// 比较旧值和新值,确定哪个属性发生了变化
|
||||
var changedProperties = GetChangedProperties(mVariable, variable);
|
||||
@@ -122,7 +122,7 @@ public class VariableManagementService : IVariableManagementService
|
||||
else
|
||||
{
|
||||
// 如果内存中不存在该变量,则直接添加
|
||||
_appDataStorageService.Variables.TryAdd(variable.Id, variable);
|
||||
_appStorageService.Variables.TryAdd(variable.Id, variable);
|
||||
_eventService.RaiseVariableChanged(
|
||||
this, new VariableChangedEventArgs(ActionChangeType.Added, variable, VariablePropertyType.All));
|
||||
}
|
||||
@@ -142,9 +142,9 @@ public class VariableManagementService : IVariableManagementService
|
||||
// 删除成功后,从内存中移除变量
|
||||
if (result)
|
||||
{
|
||||
if (_appDataStorageService.Variables.TryRemove(id, out var variable))
|
||||
if (_appStorageService.Variables.TryRemove(id, out var variable))
|
||||
{
|
||||
if (variable != null && _appDataStorageService.VariableTables.TryGetValue(variable.VariableTableId, out var variableTable))
|
||||
if (variable != null && _appStorageService.VariableTables.TryGetValue(variable.VariableTableId, out var variableTable))
|
||||
{
|
||||
variableTable.Variables.Remove(variable);
|
||||
|
||||
@@ -166,7 +166,7 @@ public class VariableManagementService : IVariableManagementService
|
||||
var result = await _variableAppService.BatchImportVariablesAsync(variables);
|
||||
foreach (var variable in result)
|
||||
{
|
||||
if (_appDataStorageService.VariableTables.TryGetValue(variable.VariableTableId ,out var variableTable))
|
||||
if (_appStorageService.VariableTables.TryGetValue(variable.VariableTableId ,out var variableTable))
|
||||
{
|
||||
variable.VariableTable = variableTable;
|
||||
}
|
||||
@@ -258,9 +258,9 @@ public class VariableManagementService : IVariableManagementService
|
||||
{
|
||||
foreach (var id in ids)
|
||||
{
|
||||
if (_appDataStorageService.Variables.TryRemove(id, out var variable))
|
||||
if (_appStorageService.Variables.TryRemove(id, out var variable))
|
||||
{
|
||||
if (variable != null && _appDataStorageService.VariableTables.TryGetValue(variable.VariableTableId, out var variableTable))
|
||||
if (variable != null && _appStorageService.VariableTables.TryGetValue(variable.VariableTableId, out var variableTable))
|
||||
{
|
||||
variableTable.Variables.Remove(variable);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace DMS.Application.Services.Management;
|
||||
public class VariableTableManagementService : IVariableTableManagementService
|
||||
{
|
||||
private readonly IVariableTableAppService _variableTableAppService;
|
||||
private readonly IAppDataStorageService _appDataStorageService;
|
||||
private readonly IAppStorageService _appStorageService;
|
||||
private readonly IEventService _eventService;
|
||||
|
||||
/// <summary>
|
||||
@@ -23,11 +23,11 @@ public class VariableTableManagementService : IVariableTableManagementService
|
||||
public event EventHandler<VariableTableChangedEventArgs> OnVariableTableChanged;
|
||||
|
||||
public VariableTableManagementService(IVariableTableAppService variableTableAppService,
|
||||
IAppDataStorageService appDataStorageService,
|
||||
IAppStorageService appStorageService,
|
||||
IEventService eventService)
|
||||
{
|
||||
_variableTableAppService = variableTableAppService;
|
||||
_appDataStorageService = appDataStorageService;
|
||||
_appStorageService = appStorageService;
|
||||
_eventService = eventService;
|
||||
}
|
||||
|
||||
@@ -58,8 +58,8 @@ public class VariableTableManagementService : IVariableTableManagementService
|
||||
if (result?.VariableTable != null)
|
||||
{
|
||||
// 添加null检查
|
||||
if (_appDataStorageService.Devices != null &&
|
||||
_appDataStorageService.Devices.TryGetValue(result.VariableTable.DeviceId, out var device))
|
||||
if (_appStorageService.Devices != null &&
|
||||
_appStorageService.Devices.TryGetValue(result.VariableTable.DeviceId, out var device))
|
||||
{
|
||||
// 确保VariableTables不为null
|
||||
if (device.VariableTables == null)
|
||||
@@ -73,7 +73,7 @@ public class VariableTableManagementService : IVariableTableManagementService
|
||||
}
|
||||
|
||||
// 确保_variableTables和result.VariableTable不为null
|
||||
if (_appDataStorageService.VariableTables.TryAdd(result.VariableTable.Id, result.VariableTable))
|
||||
if (_appStorageService.VariableTables.TryAdd(result.VariableTable.Id, result.VariableTable))
|
||||
{
|
||||
_eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs(
|
||||
DataChangeType.Added,
|
||||
@@ -94,7 +94,7 @@ public class VariableTableManagementService : IVariableTableManagementService
|
||||
// 更新成功后,更新内存中的变量表
|
||||
if (result > 0 && variableTable != null)
|
||||
{
|
||||
_appDataStorageService.VariableTables.AddOrUpdate(variableTable.Id, variableTable, (key, oldValue) => variableTable);
|
||||
_appStorageService.VariableTables.AddOrUpdate(variableTable.Id, variableTable, (key, oldValue) => variableTable);
|
||||
_eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs(
|
||||
DataChangeType.Updated,
|
||||
variableTable));
|
||||
@@ -113,9 +113,9 @@ public class VariableTableManagementService : IVariableTableManagementService
|
||||
// 删除成功后,从内存中移除变量表
|
||||
if (result )
|
||||
{
|
||||
if (_appDataStorageService.VariableTables.TryRemove(id, out var variableTable))
|
||||
if (_appStorageService.VariableTables.TryRemove(id, out var variableTable))
|
||||
{
|
||||
if (variableTable != null && _appDataStorageService.Devices.TryGetValue(variableTable.DeviceId, out var device))
|
||||
if (variableTable != null && _appStorageService.Devices.TryGetValue(variableTable.DeviceId, out var device))
|
||||
{
|
||||
if (device.VariableTables != null)
|
||||
device.VariableTables.Remove(variableTable);
|
||||
|
||||
Reference in New Issue
Block a user