2025-10-22 14:06:16 +08:00
|
|
|
using AutoMapper;
|
2025-09-07 21:16:56 +08:00
|
|
|
using DMS.Application.DTOs;
|
2025-09-16 14:42:23 +08:00
|
|
|
using DMS.Application.Events;
|
2025-09-07 21:16:56 +08:00
|
|
|
using DMS.Application.Interfaces;
|
2025-09-16 12:29:09 +08:00
|
|
|
using DMS.Application.Interfaces.Database;
|
|
|
|
|
using DMS.Application.Interfaces.Management;
|
|
|
|
|
using DMS.Core.Enums;
|
2025-10-07 17:51:24 +08:00
|
|
|
using DMS.Core.Models;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
2025-09-16 12:29:09 +08:00
|
|
|
namespace DMS.Application.Services.Management;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设备管理服务,负责设备相关的业务逻辑。
|
|
|
|
|
/// </summary>
|
2025-09-09 15:28:07 +08:00
|
|
|
public class DeviceManagementService : IDeviceManagementService
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
private readonly IMapper _mapper;
|
2025-09-07 21:16:56 +08:00
|
|
|
private readonly IDeviceAppService _deviceAppService;
|
2025-10-22 14:06:16 +08:00
|
|
|
private readonly IAppDataService _appDataService;
|
2025-10-01 18:15:51 +08:00
|
|
|
private readonly IEventService _eventService;
|
2025-09-07 21:16:56 +08:00
|
|
|
|
2025-10-22 14:06:16 +08:00
|
|
|
public DeviceManagementService(IMapper mapper, IDeviceAppService deviceAppService, IAppDataService appStorageService, IEventService eventService)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
_mapper = mapper;
|
2025-09-07 21:16:56 +08:00
|
|
|
_deviceAppService = deviceAppService;
|
2025-10-22 14:06:16 +08:00
|
|
|
_appDataService = appStorageService;
|
2025-10-01 18:15:51 +08:00
|
|
|
_eventService = eventService;
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步根据ID获取设备DTO。
|
|
|
|
|
/// </summary>
|
2025-10-07 17:51:24 +08:00
|
|
|
public async Task<Device> GetDeviceByIdAsync(int id)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
|
|
|
|
return await _deviceAppService.GetDeviceByIdAsync(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步获取所有设备DTO列表。
|
|
|
|
|
/// </summary>
|
2025-10-07 17:51:24 +08:00
|
|
|
public async Task<List<Device>> GetAllDevicesAsync()
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
|
|
|
|
return await _deviceAppService.GetAllDevicesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步创建一个新设备及其关联的变量表和菜单(事务性操作)。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<CreateDeviceWithDetailsDto> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
|
|
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
|
2025-10-01 18:09:30 +08:00
|
|
|
var result = await _deviceAppService.CreateDeviceWithDetailsAsync(dto);
|
2025-10-22 14:06:16 +08:00
|
|
|
|
2025-10-01 18:09:30 +08:00
|
|
|
// 创建成功后,将设备添加到内存中
|
|
|
|
|
if (result?.Device != null)
|
|
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
if (_appDataService.Devices.TryAdd(result.Device.Id, result.Device))
|
2025-10-20 22:47:22 +08:00
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
|
|
|
|
|
if (result.VariableTable is not null)
|
2025-10-20 22:47:22 +08:00
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
if (_appDataService.VariableTables.TryAdd(result.VariableTable.Id, result.VariableTable))
|
|
|
|
|
{
|
|
|
|
|
result.VariableTable.Device = result.Device;
|
|
|
|
|
//将默认变量表添加到设备中
|
|
|
|
|
result.Device.VariableTables.Add(result.VariableTable);
|
|
|
|
|
|
|
|
|
|
_eventService.RaiseVariableTableChanged(this, new VariableTableChangedEventArgs(DataChangeType.Added, result.VariableTable));
|
|
|
|
|
}
|
2025-10-20 22:47:22 +08:00
|
|
|
}
|
2025-10-22 14:06:16 +08:00
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
//关联菜单
|
|
|
|
|
if (result.DeviceMenu is not null)
|
2025-10-22 14:06:16 +08:00
|
|
|
{
|
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
var deviceRootMenu = _appDataService.Menus.Values.FirstOrDefault(m => m.TargetViewKey == "DevicesViewModel" && m.TargetId == 0);
|
|
|
|
|
if (deviceRootMenu is not null)
|
2025-10-22 14:06:16 +08:00
|
|
|
{
|
2025-10-23 16:25:22 +08:00
|
|
|
_appDataService.Menus.TryAdd(result.DeviceMenu.Id, result.DeviceMenu);
|
|
|
|
|
//将设备菜单添加到根菜单中
|
|
|
|
|
deviceRootMenu.Children.Add(result.DeviceMenu);
|
|
|
|
|
|
|
|
|
|
if (result.VariableTableMenu is not null)
|
|
|
|
|
{
|
|
|
|
|
_appDataService.Menus.TryAdd(result.VariableTableMenu.Id, result.VariableTableMenu);
|
|
|
|
|
//将变量表菜单添加到设备菜单中
|
|
|
|
|
result.DeviceMenu.Children.Add(result.VariableTableMenu);
|
|
|
|
|
}
|
2025-10-22 14:06:16 +08:00
|
|
|
}
|
2025-10-23 16:25:22 +08:00
|
|
|
|
2025-10-22 14:06:16 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Added, result.Device));
|
2025-10-04 00:42:59 +08:00
|
|
|
}
|
2025-10-20 22:47:22 +08:00
|
|
|
|
2025-10-23 16:25:22 +08:00
|
|
|
|
|
|
|
|
|
2025-10-20 22:47:22 +08:00
|
|
|
|
2025-10-22 14:06:16 +08:00
|
|
|
|
|
|
|
|
|
2025-10-01 18:09:30 +08:00
|
|
|
}
|
2025-10-22 14:06:16 +08:00
|
|
|
|
2025-10-01 18:09:30 +08:00
|
|
|
return result;
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步更新一个已存在的设备。
|
|
|
|
|
/// </summary>
|
2025-10-07 17:51:24 +08:00
|
|
|
public async Task<int> UpdateDeviceAsync(Device device)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-07 17:51:24 +08:00
|
|
|
var result = await _deviceAppService.UpdateDeviceAsync(device);
|
2025-10-22 14:06:16 +08:00
|
|
|
|
2025-10-01 18:09:30 +08:00
|
|
|
// 更新成功后,更新内存中的设备
|
2025-10-22 14:06:16 +08:00
|
|
|
if (result > 0)
|
2025-10-01 18:09:30 +08:00
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
var deviceMenu = _appDataService.Menus.Values.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu && m.TargetId == device.Id);
|
|
|
|
|
if (deviceMenu is not null && deviceMenu.Header != device.Name)
|
|
|
|
|
{
|
|
|
|
|
deviceMenu.Header = device.Name;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-07 17:51:24 +08:00
|
|
|
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Updated, device));
|
2025-10-01 18:09:30 +08:00
|
|
|
}
|
2025-10-22 14:06:16 +08:00
|
|
|
|
2025-10-01 18:09:30 +08:00
|
|
|
return result;
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步删除一个设备。
|
|
|
|
|
/// </summary>
|
2025-10-20 22:47:22 +08:00
|
|
|
public async Task<bool> DeleteAsync(Device device)
|
2025-09-07 21:16:56 +08:00
|
|
|
{
|
2025-10-21 12:27:45 +08:00
|
|
|
var result = await _deviceAppService.DeleteAsync(device);
|
2025-10-22 14:06:16 +08:00
|
|
|
|
2025-10-01 18:09:30 +08:00
|
|
|
// 删除成功后,从内存中移除设备
|
2025-10-22 14:06:16 +08:00
|
|
|
if (result)
|
2025-10-01 18:09:30 +08:00
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
if (_appDataService.Devices.TryGetValue(device.Id, out var deviceInStorage))
|
2025-10-01 19:06:12 +08:00
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
//删除设备的所有变量表和变量
|
2025-10-07 17:51:24 +08:00
|
|
|
foreach (var variableTable in deviceInStorage.VariableTables)
|
2025-10-01 19:06:12 +08:00
|
|
|
{
|
|
|
|
|
foreach (var variable in variableTable.Variables)
|
|
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
_appDataService.Variables.TryRemove(variable.Id, out _);
|
2025-10-01 19:06:12 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-22 14:06:16 +08:00
|
|
|
_appDataService.VariableTables.TryRemove(variableTable.Id, out _);
|
2025-10-01 19:06:12 +08:00
|
|
|
}
|
2025-10-22 14:06:16 +08:00
|
|
|
//删除菜单
|
|
|
|
|
var deviceMenu = _appDataService.Menus.Values.FirstOrDefault(m => m.MenuType == MenuType.DeviceMenu && m.TargetId == device.Id);
|
|
|
|
|
if (deviceMenu is not null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var menu in deviceMenu.Children)
|
|
|
|
|
{
|
|
|
|
|
_appDataService.Menus.TryRemove(menu.Id, out _);
|
|
|
|
|
}
|
2025-10-01 19:06:12 +08:00
|
|
|
|
2025-10-22 14:06:16 +08:00
|
|
|
_appDataService.Menus.TryRemove(deviceMenu.Id, out _);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//删除设备
|
|
|
|
|
_appDataService.Devices.TryRemove(device.Id, out _);
|
2025-10-01 19:06:12 +08:00
|
|
|
|
2025-10-07 17:51:24 +08:00
|
|
|
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Deleted, deviceInStorage));
|
2025-10-01 19:06:12 +08:00
|
|
|
}
|
2025-10-01 18:09:30 +08:00
|
|
|
}
|
2025-10-22 14:06:16 +08:00
|
|
|
|
2025-10-01 18:09:30 +08:00
|
|
|
return result;
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 异步切换设备的激活状态。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task ToggleDeviceActiveStateAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
await _deviceAppService.ToggleDeviceActiveStateAsync(id);
|
2025-10-22 14:06:16 +08:00
|
|
|
|
2025-10-01 18:09:30 +08:00
|
|
|
// 更新内存中的设备状态
|
|
|
|
|
var device = await _deviceAppService.GetDeviceByIdAsync(id);
|
|
|
|
|
if (device != null)
|
|
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
_appDataService.Devices.AddOrUpdate(device.Id, device, (key, oldValue) => device);
|
2025-10-01 19:06:12 +08:00
|
|
|
_eventService.RaiseDeviceChanged(this, new DeviceChangedEventArgs(DataChangeType.Updated, device));
|
2025-10-01 18:09:30 +08:00
|
|
|
}
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|
|
|
|
|
|
2025-10-18 17:59:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 异步加载所有设备数据到内存中。
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task LoadAllDevicesAsync()
|
|
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
_appDataService.Devices.Clear();
|
2025-10-18 17:59:21 +08:00
|
|
|
var devices = await _deviceAppService.GetAllDevicesAsync();
|
2025-10-22 14:06:16 +08:00
|
|
|
|
2025-10-18 17:59:21 +08:00
|
|
|
// 建立设备与变量表的关联
|
|
|
|
|
foreach (var device in devices)
|
|
|
|
|
{
|
|
|
|
|
// 将设备添加到安全字典
|
2025-10-22 14:06:16 +08:00
|
|
|
_appDataService.Devices.TryAdd(device.Id, device);
|
2025-10-18 17:59:21 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-09 15:28:07 +08:00
|
|
|
|
2025-09-07 21:16:56 +08:00
|
|
|
}
|