using AutoMapper;
using DMS.Core.Interfaces;
using DMS.Core.Models;
using DMS.Application.DTOs;
using DMS.Application.Interfaces;
using DMS.Core.Enums;
namespace DMS.Application.Services;
///
/// 实现设备管理的应用服务。
///
public class DeviceAppService : IDeviceAppService
{
private readonly IRepositoryManager _repoManager;
private readonly IMapper _mapper;
///
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
///
public DeviceAppService(IRepositoryManager repoManager, IMapper mapper)
{
_repoManager = repoManager;
_mapper = mapper;
}
///
/// 异步根据ID获取设备DTO。
///
public async Task GetDeviceByIdAsync(int id)
{
var device = await _repoManager.Devices.GetByIdAsync(id);
return _mapper.Map(device);
}
///
/// 异步获取所有设备DTO列表。
///
public async Task> GetAllDevicesAsync()
{
var devices = await _repoManager.Devices.GetAllAsync();
return _mapper.Map>(devices);
}
///
/// 异步创建一个新设备及其关联的变量表和菜单(事务性操作)。
///
public async Task CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
{
try
{
_repoManager.BeginTransaction();
var device = _mapper.Map(dto.Device);
device.IsActive = true; // 默认激活
await _repoManager.Devices.AddAsync(device);
// 假设 CreateDeviceWithDetailsDto 包含了变量表和菜单信息
if (dto.VariableTable != null)
{
var variableTable = _mapper.Map(dto.VariableTable);
variableTable.DeviceId = device.Id; // 关联新设备ID
await _repoManager.VariableTables.AddAsync(variableTable);
}
// 假设有菜单服务或仓储
// if (dto.Menu != null)
// {
// var menu = _mapper.Map