2025-07-24 19:51:16 +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;
|
|
|
|
|
|
using DMS.Core.Enums;
|
2025-07-24 19:51:16 +08:00
|
|
|
|
using DMS.Core.Interfaces;
|
|
|
|
|
|
using DMS.Core.Models;
|
|
|
|
|
|
|
2025-09-16 12:29:09 +08:00
|
|
|
|
namespace DMS.Application.Services.Database
|
2025-07-24 19:51:16 +08:00
|
|
|
|
{
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 变量表应用服务,负责处理变量表相关的业务逻辑。
|
|
|
|
|
|
/// 实现 <see cref="IVariableTableAppService"/> 接口。
|
|
|
|
|
|
/// </summary>
|
2025-07-24 19:51:16 +08:00
|
|
|
|
public class VariableTableAppService : IVariableTableAppService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IRepositoryManager _repositoryManager;
|
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构造函数,注入仓储管理器和AutoMapper。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="repositoryManager">仓储管理器实例。</param>
|
|
|
|
|
|
/// <param name="mapper">AutoMapper 实例。</param>
|
2025-07-24 19:51:16 +08:00
|
|
|
|
public VariableTableAppService(IRepositoryManager repositoryManager, IMapper mapper)
|
|
|
|
|
|
{
|
|
|
|
|
|
_repositoryManager = repositoryManager;
|
|
|
|
|
|
_mapper = mapper;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步根据ID获取变量表。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">变量表ID。</param>
|
|
|
|
|
|
/// <returns>变量表数据传输对象。</returns>
|
2025-10-07 17:51:24 +08:00
|
|
|
|
public async Task<VariableTable> GetVariableTableByIdAsync(int id)
|
2025-07-24 19:51:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
var variableTable = await _repositoryManager.VariableTables.GetByIdAsync(id);
|
2025-10-07 17:51:24 +08:00
|
|
|
|
return _mapper.Map<VariableTable>(variableTable);
|
2025-07-24 19:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步获取所有变量表。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>变量表数据传输对象列表。</returns>
|
2025-10-07 17:51:24 +08:00
|
|
|
|
public async Task<List<VariableTable>> GetAllVariableTablesAsync()
|
2025-07-24 19:51:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
var variableTables = await _repositoryManager.VariableTables.GetAllAsync();
|
2025-10-07 17:51:24 +08:00
|
|
|
|
return _mapper.Map<List<VariableTable>>(variableTables);
|
2025-07-24 19:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步创建变量表,并可选择性地创建关联菜单。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="createDto">包含变量表和菜单信息的创建数据传输对象。</param>
|
|
|
|
|
|
/// <returns>创建后的变量表数据传输对象。</returns>
|
|
|
|
|
|
/// <exception cref="ApplicationException">如果添加变量表失败或找不到设备菜单。</exception>
|
2025-07-30 12:54:14 +08:00
|
|
|
|
public async Task<CreateVariableTableWithMenuDto> CreateVariableTableAsync(CreateVariableTableWithMenuDto createDto)
|
2025-07-24 19:51:16 +08:00
|
|
|
|
{
|
|
|
|
|
|
await _repositoryManager.BeginTranAsync();
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var variableTable = _mapper.Map<VariableTable>(createDto.VariableTable);
|
|
|
|
|
|
variableTable.DeviceId = createDto.DeviceId;
|
|
|
|
|
|
|
|
|
|
|
|
var createdVariableTable = await _repositoryManager.VariableTables.AddAsync(variableTable);
|
|
|
|
|
|
if (createdVariableTable.Id == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ApplicationException($"添加变量表失败,设备ID:{createDto.DeviceId},请检查。");
|
|
|
|
|
|
}
|
2025-07-30 12:54:14 +08:00
|
|
|
|
|
|
|
|
|
|
_mapper.Map(createdVariableTable, createDto.VariableTable);
|
2025-07-24 19:51:16 +08:00
|
|
|
|
|
|
|
|
|
|
if (createDto.Menu!=null)
|
|
|
|
|
|
{
|
2025-07-24 20:02:08 +08:00
|
|
|
|
// 获取设备菜单,作为变量表菜单的父级
|
2025-07-24 19:51:16 +08:00
|
|
|
|
var deviceMenu
|
|
|
|
|
|
= await _repositoryManager.Menus.GetMenuByTargetIdAsync(
|
|
|
|
|
|
MenuType.DeviceMenu, createDto.DeviceId);
|
|
|
|
|
|
if (deviceMenu == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ApplicationException($"添加变量表菜单时,找不到设备ID:{createDto.DeviceId},请检查。");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
// 映射菜单实体并设置关联信息
|
2025-07-24 19:51:16 +08:00
|
|
|
|
var menu = _mapper.Map<MenuBean>(createDto.Menu);
|
|
|
|
|
|
menu.ParentId = deviceMenu.Id;
|
|
|
|
|
|
menu.TargetId = createdVariableTable.Id;
|
|
|
|
|
|
menu.MenuType = MenuType.VariableTableMenu;
|
2025-07-30 12:54:14 +08:00
|
|
|
|
var addMenu= await _repositoryManager.Menus.AddAsync(menu);
|
|
|
|
|
|
_mapper.Map(addMenu, createDto.Menu);
|
2025-07-24 19:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await _repositoryManager.CommitAsync();
|
|
|
|
|
|
|
2025-07-30 12:54:14 +08:00
|
|
|
|
return createDto;
|
2025-07-24 19:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repositoryManager.RollbackAsync();
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步更新变量表。
|
|
|
|
|
|
/// </summary>
|
2025-10-07 17:51:24 +08:00
|
|
|
|
/// <param name="variableTable">要更新的变量表数据传输对象。</param>
|
2025-07-24 21:06:48 +08:00
|
|
|
|
/// <returns>受影响的行数。</returns>
|
|
|
|
|
|
/// <exception cref="ApplicationException">如果找不到变量表。</exception>
|
2025-10-07 17:51:24 +08:00
|
|
|
|
public async Task<int> UpdateVariableTableAsync(VariableTable variableTableDto)
|
2025-07-24 19:51:16 +08:00
|
|
|
|
{
|
2025-07-24 21:06:48 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repositoryManager.BeginTranAsync();
|
|
|
|
|
|
var variableTable = await _repositoryManager.VariableTables.GetByIdAsync(variableTableDto.Id);
|
|
|
|
|
|
if (variableTable == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new ApplicationException($"VariableTable with ID {variableTableDto.Id} not found.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_mapper.Map(variableTableDto, variableTable);
|
|
|
|
|
|
int res = await _repositoryManager.VariableTables.UpdateAsync(variableTable);
|
|
|
|
|
|
await _repositoryManager.CommitAsync();
|
|
|
|
|
|
return res;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repositoryManager.RollbackAsync();
|
|
|
|
|
|
// 可以在此记录日志
|
|
|
|
|
|
throw new ApplicationException($"更新变量表时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
|
|
|
|
|
|
}
|
2025-07-24 19:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
2025-07-24 21:01:03 +08:00
|
|
|
|
/// 异步根据ID删除变量表,包括其关联的变量、MQTT别名和菜单(事务性操作)。
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">要删除的变量表ID。</param>
|
2025-07-24 21:01:03 +08:00
|
|
|
|
/// <returns>如果删除成功则为 true,否则为 false。</returns>
|
|
|
|
|
|
/// <exception cref="InvalidOperationException">如果删除变量表失败。</exception>
|
|
|
|
|
|
/// <exception cref="ApplicationException">如果删除变量表时发生其他错误。</exception>
|
|
|
|
|
|
public async Task<bool> DeleteVariableTableAsync(int id)
|
2025-07-24 19:51:16 +08:00
|
|
|
|
{
|
2025-07-24 21:01:03 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repositoryManager.BeginTranAsync();
|
|
|
|
|
|
var delRes = await _repositoryManager.VariableTables.DeleteByIdAsync(id);
|
|
|
|
|
|
if (delRes == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException($"删除变量表失败:变量表ID:{id},请检查变量表Id是否存在");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 删除关联的变量
|
2025-09-19 07:27:56 +08:00
|
|
|
|
await _repositoryManager.Variables.DeleteByVariableTableIdAsync(id);
|
2025-07-24 21:01:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 删除关联的MQTT别名
|
2025-10-06 17:39:03 +08:00
|
|
|
|
// await _repositoryManager.MqttAlias.DeleteByVariableTableIdAsync(id);
|
2025-07-24 21:01:03 +08:00
|
|
|
|
|
|
|
|
|
|
// 删除关联的菜单树
|
|
|
|
|
|
await _repositoryManager.Menus.DeleteMenuTreeByTargetIdAsync(MenuType.VariableTableMenu, id);
|
|
|
|
|
|
|
|
|
|
|
|
await _repositoryManager.CommitAsync();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repositoryManager.RollbackAsync();
|
|
|
|
|
|
// 可以在此记录日志
|
|
|
|
|
|
throw new ApplicationException($"删除变量表时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
|
|
|
|
|
|
}
|
2025-07-24 19:51:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|