2025-07-21 14:35:17 +08:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
|
using DMS.Application.DTOs;
|
|
|
|
|
|
using DMS.Application.Interfaces;
|
2025-09-16 12:29:09 +08:00
|
|
|
|
using DMS.Application.Interfaces.Database;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
using DMS.Core.Enums;
|
2025-07-21 18:49:49 +08:00
|
|
|
|
using DMS.Core.Interfaces;
|
2025-09-16 12:29:09 +08:00
|
|
|
|
using DMS.Core.Models;
|
2025-10-20 19:39:17 +08:00
|
|
|
|
using System.ComponentModel;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
|
2025-09-16 12:29:09 +08:00
|
|
|
|
namespace DMS.Application.Services.Database;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// 设备应用服务,负责处理设备相关的业务逻辑。
|
|
|
|
|
|
/// 实现 <see cref="IDeviceAppService"/> 接口。
|
2025-07-21 14:35:17 +08:00
|
|
|
|
/// </summary>
|
2025-07-24 19:51:16 +08:00
|
|
|
|
public class DeviceAppService : IDeviceAppService
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly IRepositoryManager _repoManager;
|
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
|
|
|
|
|
/// </summary>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <param name="repoManager">仓储管理器实例。</param>
|
|
|
|
|
|
/// <param name="mapper">AutoMapper 实例。</param>
|
2025-07-24 19:51:16 +08:00
|
|
|
|
public DeviceAppService(IRepositoryManager repoManager, IMapper mapper)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
_repoManager = repoManager;
|
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// 异步根据ID获取设备数据传输对象。
|
2025-07-21 14:35:17 +08:00
|
|
|
|
/// </summary>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <param name="id">设备ID。</param>
|
|
|
|
|
|
/// <returns>设备数据传输对象。</returns>
|
2025-10-21 12:27:45 +08:00
|
|
|
|
public async Task<Device> GetDeviceByIdAsync(int id)
|
|
|
|
|
|
{
|
2025-07-21 14:35:17 +08:00
|
|
|
|
var device = await _repoManager.Devices.GetByIdAsync(id);
|
2025-10-07 17:51:24 +08:00
|
|
|
|
return device;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// 异步获取所有设备数据传输对象列表。
|
2025-07-21 14:35:17 +08:00
|
|
|
|
/// </summary>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <returns>设备数据传输对象列表。</returns>
|
2025-10-07 17:51:24 +08:00
|
|
|
|
public async Task<List<Device>> GetAllDevicesAsync()
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
var devices = await _repoManager.Devices.GetAllAsync();
|
2025-10-07 17:51:24 +08:00
|
|
|
|
return devices;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步创建一个新设备及其关联的变量表和菜单(事务性操作)。
|
|
|
|
|
|
/// </summary>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <param name="dto">包含设备、变量表和菜单信息的创建数据传输对象。</param>
|
|
|
|
|
|
/// <returns>新创建设备的ID。</returns>
|
|
|
|
|
|
/// <exception cref="InvalidOperationException">如果添加设备、设备菜单或变量表失败。</exception>
|
|
|
|
|
|
/// <exception cref="ApplicationException">如果创建设备时发生其他错误。</exception>
|
2025-07-27 21:08:58 +08:00
|
|
|
|
public async Task<CreateDeviceWithDetailsDto> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-24 15:07:03 +08:00
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-07-21 14:35:17 +08:00
|
|
|
|
|
2025-10-20 19:39:17 +08:00
|
|
|
|
dto.Device = await _repoManager.Devices.AddAsync(dto.Device);
|
2025-07-24 18:09:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-24 15:07:03 +08:00
|
|
|
|
// 假设有设备菜单
|
2025-10-20 19:39:17 +08:00
|
|
|
|
if (dto.DeviceMenu is not null)
|
2025-07-24 15:07:03 +08:00
|
|
|
|
{
|
2025-10-20 22:47:22 +08:00
|
|
|
|
dto.DeviceMenu.TargetId = dto.Device.Id;
|
2025-10-20 19:39:17 +08:00
|
|
|
|
dto.DeviceMenu = await _repoManager.Menus.AddAsync(dto.DeviceMenu);
|
2025-07-24 15:07:03 +08:00
|
|
|
|
}
|
2025-07-24 18:09:46 +08:00
|
|
|
|
|
2025-07-21 14:35:17 +08:00
|
|
|
|
|
|
|
|
|
|
// 假设 CreateDeviceWithDetailsDto 包含了变量表和菜单信息
|
2025-10-20 19:39:17 +08:00
|
|
|
|
if (dto.VariableTable is not null)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-10-20 19:39:17 +08:00
|
|
|
|
dto.VariableTable.DeviceId = dto.Device.Id; // 关联新设备ID
|
|
|
|
|
|
dto.VariableTable.Protocol = dto.Device.Protocol;
|
|
|
|
|
|
dto.VariableTable = await _repoManager.VariableTables.AddAsync(dto.VariableTable);
|
|
|
|
|
|
if (dto.VariableTable == null || dto.VariableTable.Id == 0)
|
2025-07-24 15:07:03 +08:00
|
|
|
|
{
|
2025-10-21 12:27:45 +08:00
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
|
$"添加设备变量表失败,设备:{dto.Device.Name},变量表:{dto?.VariableTable?.Name}");
|
2025-07-24 15:07:03 +08:00
|
|
|
|
}
|
2025-10-21 12:27:45 +08:00
|
|
|
|
|
2025-10-20 22:47:22 +08:00
|
|
|
|
dto.VariableTable.Device = dto.Device;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
|
2025-07-24 15:07:03 +08:00
|
|
|
|
// 假设有设备菜单
|
2025-10-20 19:39:17 +08:00
|
|
|
|
if (dto.VariableTableMenu is not null && dto.VariableTableMenu is not null)
|
2025-07-24 15:07:03 +08:00
|
|
|
|
{
|
2025-10-20 19:39:17 +08:00
|
|
|
|
dto.VariableTableMenu.ParentId = dto.DeviceMenu.Id; // 关联设备菜单作为父级
|
|
|
|
|
|
dto.VariableTableMenu.TargetId = dto.VariableTable.Id;
|
|
|
|
|
|
dto.VariableTableMenu = await _repoManager.Menus.AddAsync(dto.VariableTableMenu);
|
|
|
|
|
|
if (dto.VariableTableMenu == null || dto.VariableTableMenu.Id == 0)
|
2025-07-24 15:07:03 +08:00
|
|
|
|
{
|
2025-07-24 18:09:46 +08:00
|
|
|
|
throw new InvalidOperationException(
|
2025-10-20 19:39:17 +08:00
|
|
|
|
$"添加设备变量表菜单失败,变量表:{dto.VariableTable.Name},变量表菜单:{dto.VariableTableMenu.Header}");
|
2025-07-24 15:07:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-24 18:09:46 +08:00
|
|
|
|
|
2025-07-21 14:35:17 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
|
|
|
|
|
|
2025-07-27 21:08:58 +08:00
|
|
|
|
return dto;
|
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>
|
2025-10-07 17:51:24 +08:00
|
|
|
|
/// <param name="device">要更新的设备。</param>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <returns>受影响的行数。</returns>
|
|
|
|
|
|
/// <exception cref="ApplicationException">如果找不到设备。</exception>
|
2025-10-07 17:51:24 +08:00
|
|
|
|
public async Task<int> UpdateDeviceAsync(Device device)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-24 18:52:29 +08:00
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-10-07 17:51:24 +08:00
|
|
|
|
var existingDevice = await _repoManager.Devices.GetByIdAsync(device.Id);
|
|
|
|
|
|
if (existingDevice == null)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-10-07 17:51:24 +08:00
|
|
|
|
throw new ApplicationException($"Device with ID {device.Id} not found.");
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
2025-07-24 15:07:03 +08:00
|
|
|
|
|
2025-10-07 17:51:24 +08:00
|
|
|
|
_mapper.Map(device, existingDevice);
|
2025-10-21 12:27:45 +08:00
|
|
|
|
int res = await _repoManager.Devices.UpdateAsync(existingDevice);
|
|
|
|
|
|
var menu = await _repoManager.Menus.GetMenuByTargetIdAsync(MenuType.DeviceMenu, device.Id);
|
2025-07-29 20:02:09 +08:00
|
|
|
|
if (menu != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
menu.Header = device.Name;
|
2025-10-21 12:27:45 +08:00
|
|
|
|
await _repoManager.Menus.UpdateAsync(menu);
|
2025-07-29 20:02:09 +08:00
|
|
|
|
}
|
2025-10-21 12:27:45 +08:00
|
|
|
|
|
2025-07-21 14:35:17 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
2025-07-24 18:52:29 +08:00
|
|
|
|
return res;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 18:09:46 +08:00
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步根据ID删除一个设备,包括其关联的变量表和菜单(事务性操作)。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="deviceId">要删除设备的ID。</param>
|
|
|
|
|
|
/// <returns>如果删除成功则为 true,否则为 false。</returns>
|
|
|
|
|
|
/// <exception cref="InvalidOperationException">如果删除设备失败。</exception>
|
|
|
|
|
|
/// <exception cref="ApplicationException">如果删除设备时发生其他错误。</exception>
|
2025-10-20 22:47:22 +08:00
|
|
|
|
public async Task<bool> DeleteAsync(Device device)
|
2025-07-24 18:09:46 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-10-20 22:47:22 +08:00
|
|
|
|
var delRes = await _repoManager.Devices.DeleteAsync(device);
|
2025-07-24 18:09:46 +08:00
|
|
|
|
if (delRes == 0)
|
|
|
|
|
|
{
|
2025-10-20 22:47:22 +08:00
|
|
|
|
throw new InvalidOperationException($"删除设备失败:设备ID:{device.Id},请检查设备Id是否存在");
|
2025-07-24 18:09:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-21 12:27:45 +08:00
|
|
|
|
// 删除关联的变量
|
|
|
|
|
|
await _repoManager.Variables.DeleteByVariableTableIdAsync(device.Id);
|
2025-07-24 20:02:08 +08:00
|
|
|
|
// 删除关联的变量表
|
2025-10-20 22:47:22 +08:00
|
|
|
|
await _repoManager.VariableTables.DeleteAsync(device.VariableTables);
|
2025-10-21 12:27:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
// 删除关联的菜单树
|
2025-10-21 12:27:45 +08:00
|
|
|
|
await _repoManager.Menus.DeleteMenuTreeByTargetIdAsync(MenuType.DeviceMenu, device.Id);
|
2025-07-24 18:09:46 +08:00
|
|
|
|
|
|
|
|
|
|
await _repoManager.CommitAsync();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.RollbackAsync();
|
|
|
|
|
|
// 可以在此记录日志
|
|
|
|
|
|
throw new ApplicationException($"删除设备时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
|
|
|
|
|
|
}
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步切换设备的激活状态。
|
|
|
|
|
|
/// </summary>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <param name="id">设备的ID。</param>
|
|
|
|
|
|
/// <returns>表示异步操作的任务。</returns>
|
|
|
|
|
|
/// <exception cref="ApplicationException">如果找不到设备。</exception>
|
2025-07-21 14:35:17 +08:00
|
|
|
|
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>
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <param name="protocol">协议类型。</param>
|
|
|
|
|
|
/// <returns>设备数据传输对象列表。</returns>
|
2025-10-07 17:51:24 +08:00
|
|
|
|
public async Task<List<Device>> GetDevicesByProtocolAsync(ProtocolType protocol)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
var devices = await _repoManager.Devices.GetAllAsync();
|
2025-10-07 17:51:24 +08:00
|
|
|
|
return devices;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|