AlarmLogMappings.cs 3.6 KB
using Rcs.Application.DTOs;
using Rcs.Domain.Entities;

namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;

internal static class AlarmLogMappings
{
    public static AlarmLogListItemDto ToListItemDto(this AlarmLog entity)
    {
        return new AlarmLogListItemDto
        {
            Id = entity.AlarmLogId.ToString(),
            AlarmCode = entity.AlarmCode,
            Title = entity.Title,
            Content = entity.Message,
            Severity = MapSeverity(entity.Level),
            Status = MapStatus(entity),
            Source = MapSource(entity.SourceType),
            TargetCode = entity.SourceCode ?? string.Empty,
            TargetName = entity.SourceName ?? string.Empty,
            OccurredAt = ToIsoOffsetString(entity.OccurredAt),
            Acknowledged = entity.IsAcknowledged,
            AcknowledgedBy = entity.AcknowledgedBy,
            AcknowledgedAt = ToIsoOffsetString(entity.AcknowledgedAt),
            RecoveredAt = ToIsoOffsetString(entity.ResolvedAt),
            RecoveryNote = null
        };
    }

    public static AlarmLogDto ToDetailDto(this AlarmLog entity)
    {
        return new AlarmLogDto
        {
            Id = entity.AlarmLogId.ToString(),
            AlarmCode = entity.AlarmCode,
            Title = entity.Title,
            Content = entity.Message,
            Severity = MapSeverity(entity.Level),
            Status = MapStatus(entity),
            Source = MapSource(entity.SourceType),
            TargetCode = entity.SourceCode ?? string.Empty,
            TargetName = entity.SourceName ?? string.Empty,
            OccurredAt = ToIsoOffsetString(entity.OccurredAt),
            Acknowledged = entity.IsAcknowledged,
            AcknowledgedBy = entity.AcknowledgedBy,
            AcknowledgedAt = ToIsoOffsetString(entity.AcknowledgedAt),
            RecoveredAt = ToIsoOffsetString(entity.ResolvedAt),
            RecoveryNote = null,
            Details = entity.Details,
            ExtraData = entity.ExtraData,
            RawMessage = entity.Message,
            AlarmType = entity.AlarmType,
            SourceType = entity.SourceType,
            SourceCode = entity.SourceCode,
            SourceName = entity.SourceName,
            AcknowledgeRemark = entity.AcknowledgeRemark,
            CreatedAt = ToIsoOffsetString(entity.CreatedAt),
            UpdatedAt = ToIsoOffsetString(entity.UpdatedAt)
        };
    }

    public static string MapSeverity(string? level)
    {
        return level?.Trim().ToLowerInvariant() switch
        {
            "critical" => "critical",
            "error" => "critical",
            "warning" => "warning",
            "warn" => "warning",
            "info" => "info",
            _ => "warning"
        };
    }

    public static string MapStatus(AlarmLog entity)
    {
        if (entity.ResolvedAt.HasValue)
        {
            return "resolved";
        }

        return entity.IsAcknowledged ? "acknowledged" : "active";
    }

    public static string MapSource(string? sourceType)
    {
        return sourceType?.Trim().ToLowerInvariant() switch
        {
            "robot" => "robot",
            "charging" => "charging",
            "storage" => "storage",
            "protocol" => "system",
            "system" => "system",
            _ => "system"
        };
    }

    public static string? ToIsoOffsetString(DateTime? value)
    {
        if (!value.HasValue)
        {
            return null;
        }

        var unspecified = DateTime.SpecifyKind(value.Value, DateTimeKind.Unspecified);
        return new DateTimeOffset(unspecified, TimeSpan.FromHours(8))
            .ToString("yyyy-MM-ddTHH:mm:sszzz");
    }
}