添加中文注释
This commit is contained in:
@@ -8,7 +8,8 @@ using DMS.Core.Interfaces;
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 实现设备管理的应用服务。
|
||||
/// 设备应用服务,负责处理设备相关的业务逻辑。
|
||||
/// 实现 <see cref="IDeviceAppService"/> 接口。
|
||||
/// </summary>
|
||||
public class DeviceAppService : IDeviceAppService
|
||||
{
|
||||
@@ -18,6 +19,8 @@ public class DeviceAppService : IDeviceAppService
|
||||
/// <summary>
|
||||
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
||||
/// </summary>
|
||||
/// <param name="repoManager">仓储管理器实例。</param>
|
||||
/// <param name="mapper">AutoMapper 实例。</param>
|
||||
public DeviceAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||
{
|
||||
_repoManager = repoManager;
|
||||
@@ -25,8 +28,10 @@ public class DeviceAppService : IDeviceAppService
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID获取设备DTO。
|
||||
/// 异步根据ID获取设备数据传输对象。
|
||||
/// </summary>
|
||||
/// <param name="id">设备ID。</param>
|
||||
/// <returns>设备数据传输对象。</returns>
|
||||
public async Task<DeviceDto> GetDeviceByIdAsync(int id)
|
||||
{
|
||||
var device = await _repoManager.Devices.GetByIdAsync(id);
|
||||
@@ -34,8 +39,9 @@ public class DeviceAppService : IDeviceAppService
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有设备DTO列表。
|
||||
/// 异步获取所有设备数据传输对象列表。
|
||||
/// </summary>
|
||||
/// <returns>设备数据传输对象列表。</returns>
|
||||
public async Task<List<DeviceDto>> GetAllDevicesAsync()
|
||||
{
|
||||
var devices = await _repoManager.Devices.GetAllAsync();
|
||||
@@ -45,6 +51,10 @@ public class DeviceAppService : IDeviceAppService
|
||||
/// <summary>
|
||||
/// 异步创建一个新设备及其关联的变量表和菜单(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="dto">包含设备、变量表和菜单信息的创建数据传输对象。</param>
|
||||
/// <returns>新创建设备的ID。</returns>
|
||||
/// <exception cref="InvalidOperationException">如果添加设备、设备菜单或变量表失败。</exception>
|
||||
/// <exception cref="ApplicationException">如果创建设备时发生其他错误。</exception>
|
||||
public async Task<int> CreateDeviceWithDetailsAsync(CreateDeviceWithDetailsDto dto)
|
||||
{
|
||||
try
|
||||
@@ -65,7 +75,7 @@ public class DeviceAppService : IDeviceAppService
|
||||
if (dto.DeviceMenu != null)
|
||||
{
|
||||
var deviceMenu = _mapper.Map<MenuBean>(dto.DeviceMenu);
|
||||
deviceMenu.ParentId = 2;
|
||||
deviceMenu.ParentId = 2; // 假设父菜单ID为2
|
||||
deviceMenu.MenuType = MenuType.DeviceMenu;
|
||||
deviceMenu.TargetId = addDevice.Id;
|
||||
addDeviceMenu = await _repoManager.Menus.AddAsync(deviceMenu);
|
||||
@@ -92,7 +102,7 @@ public class DeviceAppService : IDeviceAppService
|
||||
if (dto.VariableTableMenu != null)
|
||||
{
|
||||
var menu = _mapper.Map<MenuBean>(dto.VariableTableMenu);
|
||||
menu.ParentId = addDeviceMenu.Id;
|
||||
menu.ParentId = addDeviceMenu.Id; // 关联设备菜单作为父级
|
||||
menu.MenuType = MenuType.VariableTableMenu;
|
||||
menu.TargetId = addVariableTable.Id;
|
||||
var addVariableTableMenu = await _repoManager.Menus.AddAsync(menu);
|
||||
@@ -119,6 +129,9 @@ public class DeviceAppService : IDeviceAppService
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的设备。
|
||||
/// </summary>
|
||||
/// <param name="deviceDto">要更新的设备数据传输对象。</param>
|
||||
/// <returns>受影响的行数。</returns>
|
||||
/// <exception cref="ApplicationException">如果找不到设备。</exception>
|
||||
public async Task<int> UpdateDeviceAsync(UpdateDeviceDto deviceDto)
|
||||
{
|
||||
await _repoManager.BeginTranAsync();
|
||||
@@ -137,11 +150,20 @@ public class DeviceAppService : IDeviceAppService
|
||||
/// <summary>
|
||||
/// 异步删除一个设备。
|
||||
/// </summary>
|
||||
/// <param name="device">要删除的设备实体。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
public async Task DeleteDeviceAsync(Device device)
|
||||
{
|
||||
await DeleteDeviceByIdAsync(device.Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID删除一个设备,包括其关联的变量表和菜单(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="deviceId">要删除设备的ID。</param>
|
||||
/// <returns>如果删除成功则为 true,否则为 false。</returns>
|
||||
/// <exception cref="InvalidOperationException">如果删除设备失败。</exception>
|
||||
/// <exception cref="ApplicationException">如果删除设备时发生其他错误。</exception>
|
||||
public async Task<bool> DeleteDeviceByIdAsync(int deviceId)
|
||||
{
|
||||
try
|
||||
@@ -153,8 +175,10 @@ public class DeviceAppService : IDeviceAppService
|
||||
throw new InvalidOperationException($"删除设备失败:设备ID:{deviceId},请检查设备Id是否存在");
|
||||
}
|
||||
|
||||
// 删除关联的变量表
|
||||
await _repoManager.VariableTables.DeleteByDeviceIdAsync(deviceId);
|
||||
|
||||
// 删除关联的菜单树
|
||||
await _repoManager.Menus.DeleteMenuTreeByTargetIdAsync(MenuType.DeviceMenu,deviceId);
|
||||
|
||||
await _repoManager.CommitAsync();
|
||||
@@ -172,6 +196,9 @@ public class DeviceAppService : IDeviceAppService
|
||||
/// <summary>
|
||||
/// 异步切换设备的激活状态。
|
||||
/// </summary>
|
||||
/// <param name="id">设备的ID。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
/// <exception cref="ApplicationException">如果找不到设备。</exception>
|
||||
public async Task ToggleDeviceActiveStateAsync(int id)
|
||||
{
|
||||
var device = await _repoManager.Devices.GetByIdAsync(id);
|
||||
@@ -188,6 +215,8 @@ public class DeviceAppService : IDeviceAppService
|
||||
/// <summary>
|
||||
/// 异步获取指定协议类型的设备列表。
|
||||
/// </summary>
|
||||
/// <param name="protocol">协议类型。</param>
|
||||
/// <returns>设备数据传输对象列表。</returns>
|
||||
public async Task<List<DeviceDto>> GetDevicesByProtocolAsync(ProtocolType protocol)
|
||||
{
|
||||
var devices = await _repoManager.Devices.GetAllAsync();
|
||||
|
||||
@@ -2,25 +2,41 @@ using DMS.Core.Interfaces.Repositories;
|
||||
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化服务,负责应用程序启动时的数据库和菜单初始化。
|
||||
/// </summary>
|
||||
public class InitializeService
|
||||
{
|
||||
private readonly IInitializeRepository _repository;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数,注入初始化仓储。
|
||||
/// </summary>
|
||||
/// <param name="repository">初始化仓储实例。</param>
|
||||
public InitializeService(IInitializeRepository repository )
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化数据库表。
|
||||
/// </summary>
|
||||
public void InitializeTables()
|
||||
{
|
||||
_repository.InitializeTables();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化数据库表索引。
|
||||
/// </summary>
|
||||
public void InitializeTableIndex()
|
||||
{
|
||||
_repository.InitializeTableIndex();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化默认菜单。
|
||||
/// </summary>
|
||||
public void InitializeMenus()
|
||||
{
|
||||
_repository.InitializeMenus();
|
||||
|
||||
@@ -7,36 +7,57 @@ using DMS.Application.Interfaces;
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 实现菜单管理的应用服务。
|
||||
/// 菜单应用服务,负责处理菜单相关的业务逻辑。
|
||||
/// 实现 <see cref="IMenuService"/> 接口。
|
||||
/// </summary>
|
||||
public class MenuService : IMenuService
|
||||
{
|
||||
private readonly IRepositoryManager _repoManager;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
||||
/// </summary>
|
||||
/// <param name="repoManager">仓储管理器实例。</param>
|
||||
/// <param name="mapper">AutoMapper 实例。</param>
|
||||
public MenuService(IRepositoryManager repoManager, IMapper mapper)
|
||||
{
|
||||
_repoManager = repoManager;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID获取菜单数据传输对象。
|
||||
/// </summary>
|
||||
/// <param name="id">菜单ID。</param>
|
||||
/// <returns>菜单数据传输对象。</returns>
|
||||
public async Task<MenuBeanDto> GetMenuByIdAsync(int id)
|
||||
{
|
||||
var menu = await _repoManager.Menus.GetByIdAsync(id);
|
||||
return _mapper.Map<MenuBeanDto>(menu);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有菜单数据传输对象列表。
|
||||
/// </summary>
|
||||
/// <returns>菜单数据传输对象列表。</returns>
|
||||
public async Task<List<MenuBeanDto>> GetAllMenusAsync()
|
||||
{
|
||||
var menus = await _repoManager.Menus.GetAllAsync();
|
||||
return _mapper.Map<List<MenuBeanDto>>(menus);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步创建一个新菜单(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="menuDto">要创建的菜单数据传输对象。</param>
|
||||
/// <returns>新创建菜单的ID。</returns>
|
||||
/// <exception cref="ApplicationException">如果创建菜单时发生错误。</exception>
|
||||
public async Task<int> CreateMenuAsync(MenuBeanDto menuDto)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
var menu = _mapper.Map<MenuBean>(menuDto);
|
||||
await _repoManager.Menus.AddAsync(menu);
|
||||
await _repoManager.CommitAsync();
|
||||
@@ -49,11 +70,17 @@ public class MenuService : IMenuService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的菜单(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="menuDto">要更新的菜单数据传输对象。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
/// <exception cref="ApplicationException">如果找不到菜单或更新菜单时发生错误。</exception>
|
||||
public async Task UpdateMenuAsync(MenuBeanDto menuDto)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
var menu = await _repoManager.Menus.GetByIdAsync(menuDto.Id);
|
||||
if (menu == null)
|
||||
{
|
||||
@@ -70,11 +97,17 @@ public class MenuService : IMenuService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个菜单(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="id">要删除菜单的ID。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
/// <exception cref="ApplicationException">如果删除菜单时发生错误。</exception>
|
||||
public async Task DeleteMenuAsync(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
await _repoManager.Menus.DeleteByIdAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
@@ -7,36 +7,57 @@ using DMS.Application.Interfaces;
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 实现MQTT别名管理的应用服务。
|
||||
/// MQTT别名应用服务,负责处理MQTT别名相关的业务逻辑。
|
||||
/// 实现 <see cref="IMqttAliasAppService"/> 接口。
|
||||
/// </summary>
|
||||
public class MqttAliasAppService : IMqttAliasAppService
|
||||
{
|
||||
private readonly IRepositoryManager _repoManager;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
||||
/// </summary>
|
||||
/// <param name="repoManager">仓储管理器实例。</param>
|
||||
/// <param name="mapper">AutoMapper 实例。</param>
|
||||
public MqttAliasAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||
{
|
||||
_repoManager = repoManager;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID获取MQTT别名数据传输对象。
|
||||
/// </summary>
|
||||
/// <param name="id">MQTT别名ID。</param>
|
||||
/// <returns>MQTT别名数据传输对象。</returns>
|
||||
public async Task<VariableMqttAliasDto> GetMqttAliasByIdAsync(int id)
|
||||
{
|
||||
var mqttAlias = await _repoManager.VariableMqttAliases.GetByIdAsync(id);
|
||||
return _mapper.Map<VariableMqttAliasDto>(mqttAlias);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有MQTT别名数据传输对象列表。
|
||||
/// </summary>
|
||||
/// <returns>MQTT别名数据传输对象列表。</returns>
|
||||
public async Task<List<VariableMqttAliasDto>> GetAllMqttAliasesAsync()
|
||||
{
|
||||
var mqttAliases = await _repoManager.VariableMqttAliases.GetAllAsync();
|
||||
return _mapper.Map<List<VariableMqttAliasDto>>(mqttAliases);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步创建一个新MQTT别名(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="mqttAliasDto">要创建的MQTT别名数据传输对象。</param>
|
||||
/// <returns>新创建MQTT别名的ID。</returns>
|
||||
/// <exception cref="ApplicationException">如果创建MQTT别名时发生错误。</exception>
|
||||
public async Task<int> CreateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
var mqttAlias = _mapper.Map<VariableMqttAlias>(mqttAliasDto);
|
||||
await _repoManager.VariableMqttAliases.AddAsync(mqttAlias);
|
||||
await _repoManager.CommitAsync();
|
||||
@@ -49,11 +70,17 @@ public class MqttAliasAppService : IMqttAliasAppService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的MQTT别名(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="mqttAliasDto">要更新的MQTT别名数据传输对象。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
/// <exception cref="ApplicationException">如果找不到MQTT别名或更新MQTT别名时发生错误。</exception>
|
||||
public async Task UpdateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
var mqttAlias = await _repoManager.VariableMqttAliases.GetByIdAsync(mqttAliasDto.Id);
|
||||
if (mqttAlias == null)
|
||||
{
|
||||
@@ -70,11 +97,17 @@ public class MqttAliasAppService : IMqttAliasAppService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个MQTT别名(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="id">要删除MQTT别名的ID。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
/// <exception cref="ApplicationException">如果删除MQTT别名时发生错误。</exception>
|
||||
public async Task DeleteMqttAliasAsync(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
await _repoManager.VariableMqttAliases.DeleteByIdAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
@@ -7,36 +7,57 @@ using DMS.Application.Interfaces;
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 实现MQTT服务器管理的应用服务。
|
||||
/// MQTT应用服务,负责处理MQTT服务器相关的业务逻辑。
|
||||
/// 实现 <see cref="IMqttAppService"/> 接口。
|
||||
/// </summary>
|
||||
public class MqttAppService : IMqttAppService
|
||||
{
|
||||
private readonly IRepositoryManager _repoManager;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
||||
/// </summary>
|
||||
/// <param name="repoManager">仓储管理器实例。</param>
|
||||
/// <param name="mapper">AutoMapper 实例。</param>
|
||||
public MqttAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||
{
|
||||
_repoManager = repoManager;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID获取MQTT服务器数据传输对象。
|
||||
/// </summary>
|
||||
/// <param name="id">MQTT服务器ID。</param>
|
||||
/// <returns>MQTT服务器数据传输对象。</returns>
|
||||
public async Task<MqttServerDto> GetMqttServerByIdAsync(int id)
|
||||
{
|
||||
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(id);
|
||||
return _mapper.Map<MqttServerDto>(mqttServer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有MQTT服务器数据传输对象列表。
|
||||
/// </summary>
|
||||
/// <returns>MQTT服务器数据传输对象列表。</returns>
|
||||
public async Task<List<MqttServerDto>> GetAllMqttServersAsync()
|
||||
{
|
||||
var mqttServers = await _repoManager.MqttServers.GetAllAsync();
|
||||
return _mapper.Map<List<MqttServerDto>>(mqttServers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步创建一个新MQTT服务器(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="mqttServerDto">要创建的MQTT服务器数据传输对象。</param>
|
||||
/// <returns>新创建MQTT服务器的ID。</returns>
|
||||
/// <exception cref="ApplicationException">如果创建MQTT服务器时发生错误。</exception>
|
||||
public async Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
var mqttServer = _mapper.Map<MqttServer>(mqttServerDto);
|
||||
await _repoManager.MqttServers.AddAsync(mqttServer);
|
||||
await _repoManager.CommitAsync();
|
||||
@@ -49,11 +70,17 @@ public class MqttAppService : IMqttAppService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的MQTT服务器(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="mqttServerDto">要更新的MQTT服务器数据传输对象。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
/// <exception cref="ApplicationException">如果找不到MQTT服务器或更新MQTT服务器时发生错误。</exception>
|
||||
public async Task UpdateMqttServerAsync(MqttServerDto mqttServerDto)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerDto.Id);
|
||||
if (mqttServer == null)
|
||||
{
|
||||
@@ -70,11 +97,17 @@ public class MqttAppService : IMqttAppService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个MQTT服务器(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="id">要删除MQTT服务器的ID。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
/// <exception cref="ApplicationException">如果删除MQTT服务器时发生错误。</exception>
|
||||
public async Task DeleteMqttServerAsync(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
await _repoManager.MqttServers.DeleteByIdAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
@@ -7,36 +7,57 @@ using DMS.Application.Interfaces;
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 实现变量管理的应用服务。
|
||||
/// 变量应用服务,负责处理变量相关的业务逻辑。
|
||||
/// 实现 <see cref="IVariableAppService"/> 接口。
|
||||
/// </summary>
|
||||
public class VariableAppService : IVariableAppService
|
||||
{
|
||||
private readonly IRepositoryManager _repoManager;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
||||
/// </summary>
|
||||
/// <param name="repoManager">仓储管理器实例。</param>
|
||||
/// <param name="mapper">AutoMapper 实例。</param>
|
||||
public VariableAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||
{
|
||||
_repoManager = repoManager;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID获取变量数据传输对象。
|
||||
/// </summary>
|
||||
/// <param name="id">变量ID。</param>
|
||||
/// <returns>变量数据传输对象。</returns>
|
||||
public async Task<VariableDto> GetVariableByIdAsync(int id)
|
||||
{
|
||||
var variable = await _repoManager.Variables.GetByIdAsync(id);
|
||||
return _mapper.Map<VariableDto>(variable);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有变量数据传输对象列表。
|
||||
/// </summary>
|
||||
/// <returns>变量数据传输对象列表。</returns>
|
||||
public async Task<List<VariableDto>> GetAllVariablesAsync()
|
||||
{
|
||||
var variables = await _repoManager.Variables.GetAllAsync();
|
||||
return _mapper.Map<List<VariableDto>>(variables);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步创建一个新变量(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="variableDto">要创建的变量数据传输对象。</param>
|
||||
/// <returns>新创建变量的ID。</returns>
|
||||
/// <exception cref="ApplicationException">如果创建变量时发生错误。</exception>
|
||||
public async Task<int> CreateVariableAsync(VariableDto variableDto)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
var variable = _mapper.Map<Variable>(variableDto);
|
||||
await _repoManager.Variables.AddAsync(variable);
|
||||
await _repoManager.CommitAsync();
|
||||
@@ -49,11 +70,17 @@ public class VariableAppService : IVariableAppService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新一个已存在的变量(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="variableDto">要更新的变量数据传输对象。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
/// <exception cref="ApplicationException">如果找不到变量或更新变量时发生错误。</exception>
|
||||
public async Task UpdateVariableAsync(VariableDto variableDto)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
var variable = await _repoManager.Variables.GetByIdAsync(variableDto.Id);
|
||||
if (variable == null)
|
||||
{
|
||||
@@ -70,11 +97,17 @@ public class VariableAppService : IVariableAppService
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个变量(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="id">要删除变量的ID。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
/// <exception cref="ApplicationException">如果删除变量时发生错误。</exception>
|
||||
public async Task DeleteVariableAsync(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.BeginTranAsync();
|
||||
await _repoManager.Variables.DeleteByIdAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
@@ -9,29 +9,53 @@ using DMS.Core.Enums;
|
||||
|
||||
namespace DMS.Application.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// 变量表应用服务,负责处理变量表相关的业务逻辑。
|
||||
/// 实现 <see cref="IVariableTableAppService"/> 接口。
|
||||
/// </summary>
|
||||
public class VariableTableAppService : IVariableTableAppService
|
||||
{
|
||||
private readonly IRepositoryManager _repositoryManager;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数,注入仓储管理器和AutoMapper。
|
||||
/// </summary>
|
||||
/// <param name="repositoryManager">仓储管理器实例。</param>
|
||||
/// <param name="mapper">AutoMapper 实例。</param>
|
||||
public VariableTableAppService(IRepositoryManager repositoryManager, IMapper mapper)
|
||||
{
|
||||
_repositoryManager = repositoryManager;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID获取变量表。
|
||||
/// </summary>
|
||||
/// <param name="id">变量表ID。</param>
|
||||
/// <returns>变量表数据传输对象。</returns>
|
||||
public async Task<VariableTableDto> GetVariableTableByIdAsync(int id)
|
||||
{
|
||||
var variableTable = await _repositoryManager.VariableTables.GetByIdAsync(id);
|
||||
return _mapper.Map<VariableTableDto>(variableTable);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步获取所有变量表。
|
||||
/// </summary>
|
||||
/// <returns>变量表数据传输对象列表。</returns>
|
||||
public async Task<List<VariableTableDto>> GetAllVariableTablesAsync()
|
||||
{
|
||||
var variableTables = await _repositoryManager.VariableTables.GetAllAsync();
|
||||
return _mapper.Map<List<VariableTableDto>>(variableTables);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步创建变量表,并可选择性地创建关联菜单。
|
||||
/// </summary>
|
||||
/// <param name="createDto">包含变量表和菜单信息的创建数据传输对象。</param>
|
||||
/// <returns>创建后的变量表数据传输对象。</returns>
|
||||
/// <exception cref="ApplicationException">如果添加变量表失败或找不到设备菜单。</exception>
|
||||
public async Task<VariableTableDto> CreateVariableTableAsync(CreateVariableTableWithMenuDto createDto)
|
||||
{
|
||||
await _repositoryManager.BeginTranAsync();
|
||||
@@ -48,6 +72,7 @@ namespace DMS.Application.Services
|
||||
|
||||
if (createDto.Menu!=null)
|
||||
{
|
||||
// 获取设备菜单,作为变量表菜单的父级
|
||||
var deviceMenu
|
||||
= await _repositoryManager.Menus.GetMenuByTargetIdAsync(
|
||||
MenuType.DeviceMenu, createDto.DeviceId);
|
||||
@@ -56,6 +81,7 @@ namespace DMS.Application.Services
|
||||
throw new ApplicationException($"添加变量表菜单时,找不到设备ID:{createDto.DeviceId},请检查。");
|
||||
}
|
||||
|
||||
// 映射菜单实体并设置关联信息
|
||||
var menu = _mapper.Map<MenuBean>(createDto.Menu);
|
||||
menu.ParentId = deviceMenu.Id;
|
||||
menu.TargetId = createdVariableTable.Id;
|
||||
@@ -76,12 +102,22 @@ namespace DMS.Application.Services
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步更新变量表。
|
||||
/// </summary>
|
||||
/// <param name="variableTableDto">要更新的变量表数据传输对象。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
public async Task UpdateVariableTableAsync(VariableTableDto variableTableDto)
|
||||
{
|
||||
var variableTable = _mapper.Map<VariableTable>(variableTableDto);
|
||||
await _repositoryManager.VariableTables.UpdateAsync(variableTable);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步根据ID删除变量表。
|
||||
/// </summary>
|
||||
/// <param name="id">要删除的变量表ID。</param>
|
||||
/// <returns>表示异步操作的任务。</returns>
|
||||
public async Task DeleteVariableTableAsync(int id)
|
||||
{
|
||||
await _repositoryManager.VariableTables.DeleteByIdAsync(id);
|
||||
|
||||
Reference in New Issue
Block a user