Files
Gmao-app-win11/src/App.Infrastructure/Data/Configurations/WorkOrderConfiguration.cs
2026-09-10 16:43:09 +02:00

60 lines
2.1 KiB
C#

using App.Core.Entities.Maintenance;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace App.Infrastructure.Data.Configurations;
public class WorkOrderConfiguration : IEntityTypeConfiguration<WorkOrder>
{
public void Configure(EntityTypeBuilder<WorkOrder> builder)
{
builder.Property(w => w.GeneratedForOccurrenceKey).HasMaxLength(50);
// Anti-doublon de génération du MaintenanceSchedulingEngine : un seul OT par occurrence
// d'une gamme donnée, quel que soit son statut. Filtré (pas d'index sur les OT curatifs,
// qui ont MaintenancePlanId = null).
builder.HasIndex(w => new { w.MaintenancePlanId, w.GeneratedForOccurrenceKey })
.IsUnique()
.HasFilter("MaintenancePlanId IS NOT NULL");
builder.HasIndex(w => new { w.Status, w.ScheduledDate }); // vue calendrier / dashboard
builder.HasOne<App.Core.Entities.Team.User>()
.WithMany()
.HasForeignKey(w => w.AssignedTechnicianUserId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasMany(w => w.TaskResults)
.WithOne(r => r.WorkOrder)
.HasForeignKey(r => r.WorkOrderId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasMany(w => w.PartsUsed)
.WithOne()
.HasForeignKey(p => p.WorkOrderId)
.OnDelete(DeleteBehavior.Cascade);
}
}
public class WorkOrderTaskResultConfiguration : IEntityTypeConfiguration<WorkOrderTaskResult>
{
public void Configure(EntityTypeBuilder<WorkOrderTaskResult> builder)
{
builder.HasOne<MaintenanceTask>()
.WithMany()
.HasForeignKey(r => r.MaintenanceTaskId)
.OnDelete(DeleteBehavior.Restrict);
}
}
public class WorkOrderPartUsedConfiguration : IEntityTypeConfiguration<WorkOrderPartUsed>
{
public void Configure(EntityTypeBuilder<WorkOrderPartUsed> builder)
{
builder.HasOne<App.Core.Entities.Inventory.Part>()
.WithMany()
.HasForeignKey(p => p.PartId)
.OnDelete(DeleteBehavior.Restrict);
}
}