using DMS.Application.DTOs;
using DMS.Application.Events;
using DMS.Application.Interfaces;
using DMS.Application.Interfaces.Database;
using DMS.Application.Interfaces.Management;
using DMS.Core.Enums;
namespace DMS.Application.Services.Management;
///
/// 设备管理服务,负责设备相关的业务逻辑。
///
public class DeviceManagementService : IDeviceManagementService
{
private readonly IDeviceAppService _deviceAppService;
private readonly IAppDataStorageService _appDataStorageService;
private readonly IEventService _eventService;
public DeviceManagementService(IDeviceAppService deviceAppService, IAppDataStorageService appDataStorageService, IEventService eventService)
{
_deviceAppService = deviceAppService;
_appDataStorageService = appDataStorageService;
_eventService = eventService;
}
///
/// 异步根据ID获取设备DTO。
///
public async Task GetDeviceByIdAsync(int id)
{
return await _deviceAppService.GetDeviceByIdAsync(id);
}
///
/// 异步获取所有设备DTO列表。
///
public async Task> GetAllDevicesAsync()
{
return await _deviceAppService.GetAllDevicesAsync();
}
///
/// 异步创建一个新设备及其关联的变量表和菜单(事务性操作)。
///
public async Task CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
{
var result = await _deviceAppService.CreateDeviceWithDetailsAsync(dto);
// 创建成功后,将设备添加到内存中
if (result?.Device != null)
{
if (_appDataStorageService.Devices.TryAdd(result.Device.Id, result.Device))
{
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Added, result.Device));
}
}
return result;
}
///
/// 异步更新一个已存在的设备。
///
public async Task UpdateDeviceAsync(DeviceDto deviceDto)
{
var result = await _deviceAppService.UpdateDeviceAsync(deviceDto);
// 更新成功后,更新内存中的设备
if (result > 0 && deviceDto != null)
{
_appDataStorageService.Devices.AddOrUpdate(deviceDto.Id, deviceDto, (key, oldValue) => deviceDto);
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Updated, deviceDto));
}
return result;
}
///
/// 异步删除一个设备。
///
public async Task DeleteDeviceByIdAsync(int deviceId)
{
var device = await _deviceAppService.GetDeviceByIdAsync(deviceId); // 获取设备信息用于内存删除
var result = await _deviceAppService.DeleteDeviceByIdAsync(deviceId);
// 删除成功后,从内存中移除设备
if (result && device != null)
{
if (_appDataStorageService.Devices.TryGetValue(deviceId, out var deviceDto))
{
foreach (var variableTable in deviceDto.VariableTables)
{
foreach (var variable in variableTable.Variables)
{
_appDataStorageService.Variables.TryRemove(variable.Id, out _);
}
_appDataStorageService.VariableTables.TryRemove(variableTable.Id, out _);
}
_appDataStorageService.Devices.TryRemove(deviceId, out _);
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Deleted, deviceDto));
}
}
return result;
}
///
/// 异步切换设备的激活状态。
///
public async Task ToggleDeviceActiveStateAsync(int id)
{
await _deviceAppService.ToggleDeviceActiveStateAsync(id);
// 更新内存中的设备状态
var device = await _deviceAppService.GetDeviceByIdAsync(id);
if (device != null)
{
_appDataStorageService.Devices.AddOrUpdate(device.Id, device, (key, oldValue) => device);
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Updated, device));
}
}
}