CreateOrUpdateMapCommandHandler.cs 3.26 KB
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands;
using Rcs.Application.MessageBus.Responses;
using Rcs.Domain.Entities;
using Rcs.Domain.Repositories;

namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;

/// <summary>
/// 创建地图命令处理器
/// </summary>
public class CreateOrUpdateMapCommandHandler : IConsumer<CreateOrUpdateMapCommand>
{
    private readonly ILogger<CreateOrUpdateMapCommandHandler> _logger;
    private readonly IMapRepository _mapRepository;

    public CreateOrUpdateMapCommandHandler(
        ILogger<CreateOrUpdateMapCommandHandler> logger,
        IMapRepository mapRepository)
    {
        _logger = logger;
        _mapRepository = mapRepository;
    }

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

        try
        {
            if(!Guid.TryParse(command.MapId, out Guid mapId))
            {
                // 检查地图编码是否已存在
                var existingMap = await _mapRepository.GetByMapCodeAsync(
                    command.MapCode,
                    context.CancellationToken);

                if (existingMap != null)
                {
                    
                    throw new InvalidOperationException($"地图编码 {command.MapCode} 已存在");
                }

                // 创建地图实体
                var map = Map.Create
                (
                    command.MapCode,
                    command.MapName,
                    (MapTYPE)command.MapType,
                    command.Version,
                    command.Description,
                    command.Active,
                    command.ResourceUrl,
                    command.PointsUrl,
                    command.ResourceAutoSync,
                    command.ResourceSyncInterval,
                    command.PointsAutoSync,
                    command.PointsSyncInterval
                );

                // 保存地图到数据库
                await _mapRepository.AddAsync(map, context.CancellationToken);
            }
            else
            {
                var map = await _mapRepository.GetByIdAsync(
                    mapId,
                    context.CancellationToken);
                if (map == null)
                    throw new InvalidOperationException($"未找到地图ID为 {mapId} 的地图");

                map.Update
                (
                    command.MapCode,
                    command.MapName,
                    (MapTYPE)(command.MapType),
                    command.Version,
                    command.Description,
                    command.Active,
                    command.ResourceUrl,
                    command.PointsUrl,
                    command.ResourceAutoSync,
                    command.ResourceSyncInterval,
                    command.PointsAutoSync,
                    command.PointsSyncInterval
                );
                await _mapRepository.UpdateAsync(map, context.CancellationToken);
            }

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