RobotTaskHistoryConfiguration.cs 3.68 KB
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Rcs.Domain.Entities;

namespace Rcs.Infrastructure.DB.Configuration.Domain;

/// <summary>
/// RobotTaskHistory 实体 EF Core 配置
/// </summary>
public class RobotTaskHistoryConfiguration : IEntityTypeConfiguration<RobotTaskHistory>
{
    public void Configure(EntityTypeBuilder<RobotTaskHistory> builder)
    {
        builder.ToTable("robot_task_histories");

        builder.HasKey(e => e.TaskId);

        builder.Property(e => e.TaskId)
            .HasColumnName("task_id")
            .ValueGeneratedNever();

        builder.Property(e => e.TaskCode)
            .HasColumnName("task_code")
            .HasMaxLength(50)
            .IsRequired();

        builder.Property(e => e.TaskName)
            .HasColumnName("task_name")
            .HasMaxLength(100);

        builder.Property(e => e.RobotCode)
            .HasColumnName("robot_code")
            .HasMaxLength(50);

        builder.Property(e => e.TaskTemplateCode)
            .HasColumnName("task_template_code")
            .HasMaxLength(50);

        builder.Property(e => e.BeginLocationCode)
            .HasColumnName("begin_location_code")
            .HasMaxLength(50);

        builder.Property(e => e.EndLocationCode)
            .HasColumnName("end_location_code")
            .HasMaxLength(50);

        builder.Property(e => e.Status)
            .HasColumnName("status")
            .IsRequired();

        builder.Property(e => e.Pause)
            .HasColumnName("pause")
            .HasDefaultValue(false);

        builder.Property(e => e.Priority)
            .HasColumnName("priority")
            .HasDefaultValue(99);

        builder.Property(e => e.Source)
            .HasColumnName("source")
            .HasMaxLength(300);

        builder.Property(e => e.Relation)
            .HasColumnName("relation")
            .HasMaxLength(300);

        builder.Property(e => e.ShelfCode)
            .HasColumnName("shelf_code")
            .HasMaxLength(50);

        builder.Property(e => e.ContainerID)
            .HasColumnName("container_id")
            .HasMaxLength(50);

        builder.Property(e => e.ErrorInfo)
            .HasColumnName("error_info")
            .HasColumnType("text");

        builder.Property(e => e.CreatedAt)
            .HasColumnName("created_at")
            .HasColumnType("timestamp without time zone")
            .IsRequired();

        builder.Property(e => e.UpdatedAt)
            .HasColumnName("updated_at")
            .HasColumnType("timestamp without time zone");

        builder.Property(e => e.ArchivedAt)
            .HasColumnName("archived_at")
            .HasColumnType("timestamp without time zone")
            .IsRequired();

        builder.HasAlternateKey(e => e.TaskCode)
            .HasName("ak_task_history_task_code");

        builder.HasIndex(e => e.TaskCode)
            .HasDatabaseName("idx_task_history_task_code");

        builder.HasIndex(e => e.Status)
            .HasDatabaseName("idx_task_history_status");

        builder.HasIndex(e => e.RobotCode)
            .HasDatabaseName("idx_task_history_robot_code");

        builder.HasIndex(e => e.BeginLocationCode)
            .HasDatabaseName("idx_task_history_begin_location_code");

        builder.HasIndex(e => e.EndLocationCode)
            .HasDatabaseName("idx_task_history_end_location_code");

        builder.HasIndex(e => e.ArchivedAt)
            .HasDatabaseName("idx_task_history_archived_at");

        builder.HasMany(e => e.SubTaskHistories)
            .WithOne(e => e.TaskHistory)
            .HasForeignKey(e => e.TaskCode)
            .HasPrincipalKey(e => e.TaskCode)
            .OnDelete(DeleteBehavior.Cascade);
    }
}