UpdateMapFileSettingsCommandHandler.cs
2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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}"));
}
}
}