UpdateMapFileSettingsCommandHandler.cs 2.48 KB
using MassTransit;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.MessageBus.Commands;
using Rcs.Domain.Repositories;

namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;

/// <summary>
/// 更新地图背景图设置命令处理器
/// </summary>
public class UpdateMapFileSettingsCommandHandler : IConsumer<UpdateMapFileSettingsCommand>
{
    private readonly ILogger<UpdateMapFileSettingsCommandHandler> _logger;
    private readonly IMapFileRepository _mapFileRepository;

    public UpdateMapFileSettingsCommandHandler(
        ILogger<UpdateMapFileSettingsCommandHandler> logger,
        IMapFileRepository mapFileRepository)
    {
        _logger = logger;
        _mapFileRepository = mapFileRepository;
    }

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

        try
        {
            // 查找该地图的文件记录
            var existingMapFile = await _mapFileRepository.GetByMapIdAsync(command.MapId, context.CancellationToken);
            
            if (existingMapFile == null)
            {
                await context.RespondAsync(ApiResponse.Failed("未找到地图背景图文件"));
                return;
            }

            // 更新设置
            existingMapFile.Opacity = command.Opacity;
            existingMapFile.Scale = command.Scale;
            existingMapFile.Rotation = command.Rotation;
            existingMapFile.OffsetX = command.OffsetX;
            existingMapFile.OffsetY = command.OffsetY;

            // 保存到数据库
            await _mapFileRepository.UpdateAsync(existingMapFile, context.CancellationToken);
            await _mapFileRepository.SaveChangesAsync(context.CancellationToken);

            _logger.LogInformation(
                "地图背景图设置更新成功: MapId={MapId}, Opacity={Opacity}, Scale={Scale}, Rotation={Rotation}, OffsetX={OffsetX}, OffsetY={OffsetY}",
                command.MapId,
                command.Opacity,
                command.Scale,
                command.Rotation,
                command.OffsetX,
                command.OffsetY);

            await context.RespondAsync(ApiResponse.Successful("设置更新成功"));
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "更新地图背景图设置失败: MapId={MapId}", command.MapId);
            await context.RespondAsync(ApiResponse.Failed($"更新失败: {ex.Message}"));
        }
    }
}