2025-07-21 14:35:17 +08:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using DMS.Core.Models;
|
|
|
|
|
|
using DMS.Application.DTOs;
|
|
|
|
|
|
using DMS.Application.Interfaces;
|
|
|
|
|
|
using DMS.Core.Enums;
|
2025-07-21 18:49:49 +08:00
|
|
|
|
using DMS.Core.Interfaces;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
|
|
|
|
|
|
namespace DMS.Application.Services;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 实现设备管理的应用服务。
|
|
|
|
|
|
/// </summary>
|
2025-07-21 22:02:42 +08:00
|
|
|
|
public class DeviceService : IDeviceAppService
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IRepositoryManager _repoManager;
|
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
|
|
|
|
|
/// </summary>
|
2025-07-21 22:02:42 +08:00
|
|
|
|
public DeviceService(IRepositoryManager repoManager, IMapper mapper)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
_repoManager = repoManager;
|
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步根据ID获取设备DTO。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<DeviceDto> GetDeviceByIdAsync(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var device = await _repoManager.Devices.GetByIdAsync(id);
|
|
|
|
|
|
return _mapper.Map<DeviceDto>(device);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步获取所有设备DTO列表。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<List<DeviceDto>> GetAllDevicesAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var devices = await _repoManager.Devices.GetAllAsync();
|
|
|
|
|
|
return _mapper.Map<List<DeviceDto>>(devices);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步创建一个新设备及其关联的变量表和菜单(事务性操作)。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<int> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-24 15:07:03 +08:00
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-07-21 14:35:17 +08:00
|
|
|
|
|
|
|
|
|
|
var device = _mapper.Map<Device>(dto.Device);
|
|
|
|
|
|
device.IsActive = true; // 默认激活
|
2025-07-24 15:07:03 +08:00
|
|
|
|
var addDevice = await _repoManager.Devices.AddAsync(device);
|
|
|
|
|
|
if (addDevice == null || addDevice.Id == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException($"添加设备失败:{addDevice}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MenuBean addDeviceMenu = null;
|
|
|
|
|
|
|
|
|
|
|
|
// 假设有设备菜单
|
|
|
|
|
|
if (dto.DeviceMenu != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var deviceMenu = _mapper.Map<MenuBean>(dto.DeviceMenu);
|
|
|
|
|
|
deviceMenu.ParentId = 2;
|
|
|
|
|
|
deviceMenu.MenuType = MenuType.DeviceMenu;
|
|
|
|
|
|
deviceMenu.TargetId = addDevice.Id;
|
|
|
|
|
|
addDeviceMenu=await _repoManager.Menus.AddAsync(deviceMenu);
|
|
|
|
|
|
if (addDeviceMenu == null || addDeviceMenu.Id == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException($"添加设备菜单失败:{addDeviceMenu}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-21 14:35:17 +08:00
|
|
|
|
|
|
|
|
|
|
// 假设 CreateDeviceWithDetailsDto 包含了变量表和菜单信息
|
|
|
|
|
|
if (dto.VariableTable != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var variableTable = _mapper.Map<VariableTable>(dto.VariableTable);
|
|
|
|
|
|
variableTable.DeviceId = device.Id; // 关联新设备ID
|
2025-07-24 15:07:03 +08:00
|
|
|
|
variableTable.Protocol=device.Protocol;
|
|
|
|
|
|
var addVariableTable = await _repoManager.VariableTables.AddAsync(variableTable);
|
|
|
|
|
|
if (addVariableTable == null || addVariableTable.Id == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException($"添加设备变量表失败,设备:{device.Name},变量表:{variableTable.Name}");
|
|
|
|
|
|
}
|
2025-07-21 14:35:17 +08:00
|
|
|
|
|
2025-07-24 15:07:03 +08:00
|
|
|
|
// 假设有设备菜单
|
|
|
|
|
|
if (dto.VariableTableMenu != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var menu = _mapper.Map<MenuBean>(dto.VariableTableMenu);
|
|
|
|
|
|
menu.ParentId = addDeviceMenu.Id;
|
|
|
|
|
|
menu.MenuType = MenuType.VariableTableMenu;
|
|
|
|
|
|
menu.TargetId = addVariableTable.Id;
|
|
|
|
|
|
var addVariableTableMenu= await _repoManager.Menus.AddAsync(menu);
|
|
|
|
|
|
if (addVariableTableMenu == null || addVariableTableMenu.Id == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException($"添加设备变量表菜单失败,变量表:{variableTable.Name},变量表菜单:{menu.Header}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-21 14:35:17 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
|
|
|
|
|
|
2025-07-24 15:07:03 +08:00
|
|
|
|
return addDevice.Id;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.RollbackAsync();
|
|
|
|
|
|
// 可以在此记录日志
|
2025-07-24 15:07:03 +08:00
|
|
|
|
throw new ApplicationException($"创建设备时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步更新一个已存在的设备。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task UpdateDeviceAsync(UpdateDeviceDto deviceDto)
|
|
|
|
|
|
{
|
|
|
|
|
|
var device = await _repoManager.Devices.GetByIdAsync(deviceDto.Id);
|
|
|
|
|
|
if (device == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ApplicationException($"Device with ID {deviceDto.Id} not found.");
|
|
|
|
|
|
}
|
2025-07-24 15:07:03 +08:00
|
|
|
|
|
2025-07-21 14:35:17 +08:00
|
|
|
|
_mapper.Map(deviceDto, device);
|
|
|
|
|
|
await _repoManager.Devices.UpdateAsync(device);
|
|
|
|
|
|
await _repoManager.CommitAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步删除一个设备。
|
|
|
|
|
|
/// </summary>
|
2025-07-21 18:49:49 +08:00
|
|
|
|
public async Task DeleteDeviceAsync(Device device)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
await _repoManager.Devices.DeleteAsync(device);
|
2025-07-21 14:35:17 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步切换设备的激活状态。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task ToggleDeviceActiveStateAsync(int id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var device = await _repoManager.Devices.GetByIdAsync(id);
|
|
|
|
|
|
if (device == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ApplicationException($"Device with ID {id} not found.");
|
|
|
|
|
|
}
|
2025-07-24 15:07:03 +08:00
|
|
|
|
|
2025-07-21 14:35:17 +08:00
|
|
|
|
device.IsActive = !device.IsActive;
|
|
|
|
|
|
await _repoManager.Devices.UpdateAsync(device);
|
|
|
|
|
|
await _repoManager.CommitAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步获取指定协议类型的设备列表。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async Task<List<DeviceDto>> GetDevicesByProtocolAsync(ProtocolType protocol)
|
|
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var devices = await _repoManager.Devices.GetAllAsync();
|
2025-07-21 14:35:17 +08:00
|
|
|
|
return _mapper.Map<List<DeviceDto>>(devices);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|