CreateOrUpdateNetActionPropertysCommandHandler.cs 4.59 KB
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands;
using Rcs.Domain.Entities;
using Rcs.Domain.Enums;
using Rcs.Domain.Repositories;

namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;

/// <summary>
/// 创建或更新网络动作属性命令处理器
/// @author zzy
/// </summary>
public class CreateOrUpdateNetActionPropertysCommandHandler : IConsumer<CreateOrUpdateNetActionPropertysCommand>
{
    private readonly ILogger<CreateOrUpdateNetActionPropertysCommandHandler> _logger;
    private readonly INetActionPropertysRepository _repository;

    public CreateOrUpdateNetActionPropertysCommandHandler(
        ILogger<CreateOrUpdateNetActionPropertysCommandHandler> logger,
        INetActionPropertysRepository repository)
    {
        _logger = logger;
        _repository = repository;
    }

    public async Task Consume(ConsumeContext<CreateOrUpdateNetActionPropertysCommand> context)
    {
        var command = context.Message;
        try
        {
            if (!Guid.TryParse(command.NetActionId, out Guid netActionId))
            {
                // 创建新实体
                var existing = await _repository.FirstOrDefaultAsync(
                    x => x.ActionName == command.ActionName, 
                    context.CancellationToken);
                    
                if (existing != null)
                {
                    throw new InvalidOperationException($"动作名称 {command.ActionName} 已存在");
                }

                var entity = new NetActionPropertys
                {
                    NetActionId = Guid.NewGuid(),
                    ActionName = command.ActionName,
                    IsActive = command.IsActive,
                    RequestMethod = command.RequestMethod,
                    RequestUrl = command.RequestUrl,
                    RequestParams = command.RequestParams,
                    RepeatCount = command.RepeatCount,
                    IntervalTimeMs = command.IntervalTimeMs,
                    WaitResponseType = (ResponseWaitType)command.WaitResponseType,
                    ResponseValidationRule = command.ResponseValidationRule,
                    Description = command.Description,
                    ExtraProperties = command.ExtraProperties,
                    CreatedAt = DateTime.Now,
                    UpdatedAt = DateTime.Now
                };

                await _repository.AddAsync(entity, context.CancellationToken);
                await _repository.SaveChangesAsync(context.CancellationToken);
            }
            else
            {
                // 更新现有实体
                var entity = await _repository.GetByIdAsync(netActionId, context.CancellationToken);
                
                if (entity == null)
                {
                    throw new InvalidOperationException($"未找到ID为 {netActionId} 的网络动作属性");
                }

                // 检查名称是否与其他记录冲突
                var existing = await _repository.FirstOrDefaultAsync(
                    x => x.ActionName == command.ActionName && x.NetActionId != netActionId, 
                    context.CancellationToken);
                    
                if (existing != null)
                {
                    throw new InvalidOperationException($"动作名称 {command.ActionName} 已存在");
                }

                entity.ActionName = command.ActionName;
                entity.IsActive = command.IsActive;
                entity.RequestMethod = command.RequestMethod;
                entity.RequestUrl = command.RequestUrl;
                entity.RequestParams = command.RequestParams;
                entity.RepeatCount = command.RepeatCount;
                entity.IntervalTimeMs = command.IntervalTimeMs;
                entity.WaitResponseType = command?.WaitResponseType == null
                    ? ResponseWaitType.None
                    : (ResponseWaitType)command.WaitResponseType;
                entity.ResponseValidationRule = command.ResponseValidationRule;
                entity.Description = command.Description;
                entity.ExtraProperties = command.ExtraProperties;
                entity.UpdatedAt = DateTime.Now;

                await _repository.UpdateAsync(entity, context.CancellationToken);
                await _repository.SaveChangesAsync(context.CancellationToken);
            }

            await context.RespondAsync(ApiResponse.Successful());
        }
        catch (Exception ex)
        {
            await context.RespondAsync(ApiResponse.Failed(ex.Message));
        }
    }
}