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;
|
|
|
|
|
|
using DMS.Core.Interfaces;
|
|
|
|
|
|
using DMS.Core.Models;
|
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="IVariableAppService"/> 接口。
|
2025-07-21 14:35:17 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class VariableAppService : IVariableAppService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IRepositoryManager _repoManager;
|
|
|
|
|
|
private readonly IMapper _mapper;
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="repoManager">仓储管理器实例。</param>
|
|
|
|
|
|
/// <param name="mapper">AutoMapper 实例。</param>
|
2025-07-21 14:35:17 +08:00
|
|
|
|
public VariableAppService(IRepositoryManager repoManager, IMapper mapper)
|
|
|
|
|
|
{
|
|
|
|
|
|
_repoManager = repoManager;
|
|
|
|
|
|
_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<Variable> GetVariableByIdAsync(int id)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
var variable = await _repoManager.Variables.GetByIdAsync(id);
|
2025-10-07 17:51:24 +08:00
|
|
|
|
return variable;
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-05 13:24:03 +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<Variable>> GetAllVariablesAsync()
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
|
|
|
|
|
var variables = await _repoManager.Variables.GetAllAsync();
|
2025-10-07 17:51:24 +08:00
|
|
|
|
return _mapper.Map<List<Variable>>(variables);
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步创建一个新变量(事务性操作)。
|
|
|
|
|
|
/// </summary>
|
2025-10-22 14:06:16 +08:00
|
|
|
|
/// <param name="variable">要创建的变量数据传输对象。</param>
|
|
|
|
|
|
public async Task<Variable> AddAsync(Variable variable)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-24 20:02:08 +08:00
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-10-22 14:06:16 +08:00
|
|
|
|
var addedVariable = await _repoManager.Variables.AddAsync(variable); await _repoManager.CommitAsync();
|
2025-10-07 17:51:24 +08:00
|
|
|
|
return _mapper.Map<Variable>(addedVariable);
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.RollbackAsync();
|
2025-07-24 21:41:00 +08:00
|
|
|
|
throw new ApplicationException($"创建变量时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步更新一个已存在的变量(事务性操作)。
|
|
|
|
|
|
/// </summary>
|
2025-10-22 14:06:16 +08:00
|
|
|
|
/// <param name="variable">要更新的变量数据传输对象。</param>
|
|
|
|
|
|
public async Task<int> UpdateAsync(Variable variable)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-24 20:02:08 +08:00
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-10-07 17:51:24 +08:00
|
|
|
|
var existingVariable = await _repoManager.Variables.GetByIdAsync(variable.Id);
|
|
|
|
|
|
if (existingVariable == null)
|
2025-07-21 18:49:49 +08:00
|
|
|
|
{
|
2025-10-07 17:51:24 +08:00
|
|
|
|
throw new ApplicationException($"Variable with ID {variable.Id} not found.");
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
2025-10-07 17:51:24 +08:00
|
|
|
|
_mapper.Map(variable, existingVariable);
|
2025-07-24 21:41:00 +08:00
|
|
|
|
int res = await _repoManager.Variables.UpdateAsync(variable);
|
2025-07-21 18:49:49 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
2025-07-24 21:41:00 +08:00
|
|
|
|
return res;
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
await _repoManager.RollbackAsync();
|
2025-07-24 21:41:00 +08:00
|
|
|
|
throw new ApplicationException($"更新变量时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-02 17:42:11 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步批量更新变量(事务性操作)。
|
|
|
|
|
|
/// </summary>
|
2025-10-07 17:51:24 +08:00
|
|
|
|
/// <param name="variables">要更新的变量数据传输对象列表。</param>
|
2025-09-02 17:42:11 +08:00
|
|
|
|
/// <returns>受影响的行数。</returns>
|
|
|
|
|
|
/// <exception cref="ApplicationException">如果更新变量时发生错误。</exception>
|
2025-10-22 14:06:16 +08:00
|
|
|
|
public async Task<int> UpdateAsync(List<Variable> variables)
|
2025-09-02 17:42:11 +08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.BeginTranAsync();
|
|
|
|
|
|
int totalAffected = 0;
|
2025-10-22 14:06:16 +08:00
|
|
|
|
|
2025-10-07 17:51:24 +08:00
|
|
|
|
foreach (var variable in variables)
|
2025-09-02 17:42:11 +08:00
|
|
|
|
{
|
2025-10-07 17:51:24 +08:00
|
|
|
|
var existingVariable = await _repoManager.Variables.GetByIdAsync(variable.Id);
|
|
|
|
|
|
if (existingVariable == null)
|
2025-09-02 17:42:11 +08:00
|
|
|
|
{
|
2025-10-07 17:51:24 +08:00
|
|
|
|
throw new ApplicationException($"Variable with ID {variable.Id} not found.");
|
2025-09-02 17:42:11 +08:00
|
|
|
|
}
|
2025-10-07 17:51:24 +08:00
|
|
|
|
_mapper.Map(variable, existingVariable);
|
|
|
|
|
|
int res = await _repoManager.Variables.UpdateAsync(existingVariable);
|
2025-09-02 17:42:11 +08:00
|
|
|
|
totalAffected += res;
|
|
|
|
|
|
}
|
2025-10-22 14:06:16 +08:00
|
|
|
|
|
2025-09-02 17:42:11 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
|
|
|
|
|
return totalAffected;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.RollbackAsync();
|
|
|
|
|
|
throw new ApplicationException($"批量更新变量时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-24 20:02:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步删除一个变量(事务性操作)。
|
|
|
|
|
|
/// </summary>
|
2025-10-22 14:06:16 +08:00
|
|
|
|
/// <param name="variable1"></param>
|
2025-07-24 21:41:00 +08:00
|
|
|
|
/// <returns>如果删除成功则为 true,否则为 false。</returns>
|
|
|
|
|
|
/// <exception cref="InvalidOperationException">如果删除变量失败。</exception>
|
|
|
|
|
|
/// <exception cref="ApplicationException">如果删除变量时发生其他错误。</exception>
|
2025-10-22 14:06:16 +08:00
|
|
|
|
public async Task<bool> DeleteAsync(Variable variable)
|
2025-07-21 14:35:17 +08:00
|
|
|
|
{
|
2025-07-21 18:49:49 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2025-07-24 20:02:08 +08:00
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-10-21 13:01:27 +08:00
|
|
|
|
var delRes = await _repoManager.Variables.DeleteAsync(variable);
|
2025-07-24 21:41:00 +08:00
|
|
|
|
if (delRes == 0)
|
|
|
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
|
throw new InvalidOperationException($"删除变量失败:变量ID:{variable.Id},请检查变量Id是否存在");
|
2025-07-24 21:41:00 +08:00
|
|
|
|
}
|
2025-07-21 18:49:49 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
2025-07-24 21:41:00 +08:00
|
|
|
|
return true;
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.RollbackAsync();
|
2025-07-24 21:41:00 +08:00
|
|
|
|
throw new ApplicationException($"删除变量时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
|
2025-07-21 18:49:49 +08:00
|
|
|
|
}
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|
2025-08-24 17:48:33 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 异步批量删除变量(事务性操作)。
|
|
|
|
|
|
/// </summary>
|
2025-10-22 14:06:16 +08:00
|
|
|
|
/// <param name="variables"></param>
|
2025-08-24 17:48:33 +08:00
|
|
|
|
/// <returns>如果删除成功则为 true,否则为 false。</returns>
|
|
|
|
|
|
/// <exception cref="ArgumentException">如果ID列表为空或null。</exception>
|
|
|
|
|
|
/// <exception cref="ApplicationException">如果删除变量时发生错误。</exception>
|
2025-10-22 14:06:16 +08:00
|
|
|
|
public async Task<bool> DeleteAsync(List<Variable> variables)
|
2025-08-24 17:48:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.BeginTranAsync();
|
2025-10-22 14:06:16 +08:00
|
|
|
|
|
2025-08-24 17:48:33 +08:00
|
|
|
|
// 批量删除变量
|
2025-10-22 14:06:16 +08:00
|
|
|
|
var deletedCount = await _repoManager.Variables.DeleteAsync(variables);
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否所有变量都被成功删除
|
|
|
|
|
|
if (deletedCount != variables.Count)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new InvalidOperationException($"删除变量失败:请求删除 {variables.Count} 个变量,实际删除 {deletedCount} 个变量");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-24 17:48:33 +08:00
|
|
|
|
await _repoManager.CommitAsync();
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repoManager.RollbackAsync();
|
|
|
|
|
|
throw new ApplicationException($"批量删除变量时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-22 20:24:09 +08:00
|
|
|
|
|
2025-10-22 14:06:16 +08:00
|
|
|
|
public async Task<List<Variable>> AddAsync(List<Variable> variables)
|
2025-08-22 20:24:09 +08:00
|
|
|
|
{
|
2025-10-22 14:06:16 +08:00
|
|
|
|
return await _repoManager.Variables.AddAsync(variables);
|
2025-08-24 14:42:31 +08:00
|
|
|
|
}
|
2025-07-21 14:35:17 +08:00
|
|
|
|
}
|