GetAlarmLogQueryHandler.cs
1.62 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
using MassTransit;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Rcs.Application.Common;
using Rcs.Application.DTOs;
using Rcs.Application.MessageBus.Commands;
using Rcs.Domain.Entities;
using Rcs.Infrastructure.DB.MsSql;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;
/// <summary>
/// 查询报警日志详情处理器
/// </summary>
public class GetAlarmLogQueryHandler : IConsumer<GetAlarmLogQuery>
{
private readonly ILogger<GetAlarmLogQueryHandler> _logger;
private readonly AppDbContext _dbContext;
public GetAlarmLogQueryHandler(
ILogger<GetAlarmLogQueryHandler> logger,
AppDbContext dbContext)
{
_logger = logger;
_dbContext = dbContext;
}
public async Task Consume(ConsumeContext<GetAlarmLogQuery> context)
{
var query = context.Message;
try
{
var entity = await _dbContext.Set<AlarmLog>()
.AsNoTracking()
.FirstOrDefaultAsync(x => x.AlarmLogId == query.AlarmLogId, context.CancellationToken);
if (entity == null)
{
await context.RespondAsync(ApiResponse<AlarmLogDto>.Failed("报警不存在", 404));
return;
}
await context.RespondAsync(ApiResponse<AlarmLogDto>.Successful(entity.ToDetailDto()));
}
catch (Exception ex)
{
_logger.LogError(ex, "查询报警日志详情失败: {AlarmLogId}", query.AlarmLogId);
await context.RespondAsync(ApiResponse<AlarmLogDto>.Failed($"查询报警详情失败: {ex.Message}", 500));
}
}
}