DeleteActionConfigurationCommandHandler.cs 3.42 KB
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;

namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;

public class DeleteActionConfigurationCommandHandler : IConsumer<DeleteActionConfigurationCommand>
{
    private readonly ILogger<DeleteActionConfigurationCommandHandler> _logger;
    private readonly IActionConfigurationRepository _repository;
    private readonly IRepository<StepAction> _stepActionRepository;

    public DeleteActionConfigurationCommandHandler(
        ILogger<DeleteActionConfigurationCommandHandler> logger,
        IActionConfigurationRepository repository,
        IRepository<StepAction> stepActionRepository)
    {
        _logger = logger;
        _repository = repository;
        _stepActionRepository = stepActionRepository;
    }

#if false
    public async Task Consume(ConsumeContext<DeleteActionConfigurationCommand> context)
    {
        var command = context.Message;

        try
        {
            var entity = await _repository.GetByIdAsync(command.ActionConfigId, context.CancellationToken);
            if (entity == null)
            {
                throw new InvalidOperationException($"动作配置ID {command.ActionConfigId} 不存在");
                throw new InvalidOperationException($"动作配置ID {command.ActionConfigId} 不存在");
            }

            }
            var hasStepActionReference = await _stepActionRepository.AnyAsync(
                x => x.ActionConfigId == command.ActionConfigId,
                context.CancellationToken);

            if (hasStepActionReference)
            {
                throw new InvalidOperationException("动作配置已被步骤动作引用,无法删除,请先解除关联");
            }

            await _repository.DeleteByIdAsync(command.ActionConfigId, context.CancellationToken);
            await context.RespondAsync(ApiResponse.Successful());
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "删除动作配置失败: {ActionConfigId}", command.ActionConfigId);
            await context.RespondAsync(ApiResponse.Failed(ex.Message));
        }
    }
#endif

    public async Task Consume(ConsumeContext<DeleteActionConfigurationCommand> context)
    {
        var command = context.Message;

        try
        {
            var entity = await _repository.GetByIdAsync(command.ActionConfigId, context.CancellationToken);
            if (entity == null)
            {
                throw new InvalidOperationException($"动作配置ID {command.ActionConfigId} 不存在");
            }

            var hasStepActionReference = await _stepActionRepository.AnyAsync(
                x => x.ActionConfigId == command.ActionConfigId,
                context.CancellationToken);

            if (hasStepActionReference)
            {
                throw new InvalidOperationException("动作配置已被步骤动作引用,无法删除,请先解除关联");
            }

            await _repository.DeleteByIdAsync(command.ActionConfigId, context.CancellationToken);
            await context.RespondAsync(ApiResponse.Successful());
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "删除动作配置失败: {ActionConfigId}", command.ActionConfigId);
            await context.RespondAsync(ApiResponse.Failed(ex.Message));
        }
    }
}