ApiMapper.cs 4.23 KB
using RobotProductionSystem.Api.Contracts;
using RobotProductionSystem.Api.Domain;

namespace RobotProductionSystem.Api.Services;

public static class ApiMapper
{
    public static AuthUserDto ToAuthUser(User user) =>
        new(user.Id, user.Username, user.Name, user.Email, user.Roles.Select(x => x.Role).ToArray(), user.Station,
            user.AvatarSrc is null && user.AvatarAlt is null ? null : new AvatarDto(user.AvatarSrc, user.AvatarAlt));

    public static object ToMember(Member member) => new
    {
        member.Name,
        member.Username,
        member.Role,
        Avatar = new { Src = member.AvatarSrc, Alt = member.AvatarAlt },
        member.Email,
        member.Bio
    };

    public static SettingsProfileDto ToProfile(SettingsProfile profile) =>
        new(profile.Name, profile.Email, profile.Username, profile.Avatar, profile.Bio);

    public static SettingsNotificationsDto ToNotifications(SettingsNotification notifications) => new()
    {
        Email = notifications.Email,
        Desktop = notifications.Desktop,
        ProductUpdates = notifications.ProductUpdates,
        WeeklyDigest = notifications.WeeklyDigest,
        ImportantUpdates = notifications.ImportantUpdates
    };

    public static object ToDeviceType(DeviceType item) => new
    {
        item.Id,
        item.Name,
        item.Model,
        item.Category,
        item.LengthMm,
        item.WidthMm,
        item.HeightMm,
        item.WeightKg,
        item.HasBattery,
        item.BatterySpec,
        item.Description,
        UpdatedAt = item.UpdatedAt.ToString("O")
    };

    public static object ToWorkOrder(WorkOrder item) => new
    {
        item.Id,
        item.OrderNo,
        item.DeviceCode,
        item.BatchNo,
        item.Line,
        item.OwnerUsername,
        item.OwnerName,
        PlannedDate = item.PlannedDate.ToString("yyyy-MM-dd"),
        item.Status,
        Progress = new { item.CompletedSn, item.TotalSn },
        Audit = new
        {
            item.CreatedBy,
            CreatedAt = item.CreatedAt.ToString("O"),
            item.UpdatedBy,
            UpdatedAt = item.UpdatedAt.ToString("O"),
            item.LastAction,
            LastActionAt = item.LastActionAt.ToString("O"),
            item.LastActionBy
        },
        Events = item.Events.OrderByDescending(x => x.At).Select(x => new
        {
            Id = x.EventId,
            x.Action,
            x.FromStatus,
            x.ToStatus,
            x.Operator,
            At = x.At.ToString("O"),
            x.Remark
        })
    };

    public static object ToSnItem(SnItem item) => new
    {
        item.Id,
        item.Sn,
        item.WorkOrderNo,
        item.Status,
        CurrentStep = ProcessStepMapper.ToApiValue(item.CurrentStep),
        item.ExceptionStatus,
        item.FreezeReason,
        item.ScrapReason,
        Audit = new
        {
            item.CreatedBy,
            CreatedAt = item.CreatedAt.ToString("O"),
            item.UpdatedBy,
            UpdatedAt = item.UpdatedAt.ToString("O"),
            item.LastAction,
            LastActionAt = item.LastActionAt.ToString("O"),
            item.LastActionBy
        },
        Events = item.Events.OrderByDescending(x => x.At).Select(x => new
        {
            Id = x.EventId,
            x.Action,
            x.FromStatus,
            x.ToStatus,
            x.Operator,
            At = x.At.ToString("O"),
            x.Reason,
            CurrentStep = ProcessStepMapper.ToApiValue(x.CurrentStep),
            x.ExceptionStatus
        })
    };

    public static object ToOperationTask(OperationTask item) => new
    {
        item.Id,
        item.WorkOrderNo,
        item.Sn,
        StepName = ProcessStepMapper.ToApiValue(item.StepName),
        item.Workstation,
        item.Device,
        item.Operator,
        StartedAt = item.StartedAt?.ToString("O"),
        EndedAt = item.EndedAt?.ToString("O"),
        item.Result,
        item.Status,
        item.NextAction,
        AuditEvents = item.AuditEvents.OrderByDescending(x => x.At).Select(x => new
        {
            Id = x.EventId,
            x.Action,
            x.FromStatus,
            x.ToStatus,
            x.Operator,
            At = x.At.ToString("O"),
            x.Remark,
            x.NextAction
        })
    };
}