using Microsoft.EntityFrameworkCore; using line_gestao_api.Models; namespace line_gestao_api.Data; public class AppDbContext : DbContext { public AppDbContext(DbContextOptions options) : base(options) { } public DbSet Users => Set(); // ✅ tabela para espelhar a planilha (GERAL) public DbSet MobileLines => Set(); // ✅ tabela para espelhar a aba MUREG public DbSet MuregLines => Set(); // ✅ tabela para espelhar o FATURAMENTO (PF/PJ) public DbSet BillingClients => Set(); public DbSet UserDatas => Set(); public DbSet VigenciaLines { get; set; } = default!; public DbSet TrocaNumeroLines => Set(); // ✅ PARCELAMENTO public DbSet ParcelamentoLines => Set(); public DbSet ParcelamentoMonthValues => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // ========================= // ✅ USER // ========================= modelBuilder.Entity() .HasIndex(u => u.Email) .IsUnique(); // ========================= // ✅ GERAL (MobileLine) // ========================= modelBuilder.Entity() .HasIndex(x => x.Linha) .IsUnique(); // ========================= // ✅ MUREG // ========================= modelBuilder.Entity().HasIndex(x => x.Item); modelBuilder.Entity().HasIndex(x => x.Cliente); modelBuilder.Entity().HasIndex(x => x.ICCID); modelBuilder.Entity().HasIndex(x => x.LinhaNova); // ========================================================== // ✅ FATURAMENTO (BillingClient) - mantém seu mapeamento // ========================================================== modelBuilder.Entity(e => { e.ToTable("billing_clients"); e.HasKey(x => x.Id); e.Property(x => x.Tipo).HasMaxLength(2); e.Property(x => x.Cliente).HasMaxLength(255); e.HasIndex(x => x.Tipo); e.HasIndex(x => x.Cliente); e.HasIndex(x => new { x.Tipo, x.Cliente }); e.HasIndex(x => x.Item); }); // ========================================================== // ✅ PARCELAMENTO - MAPEAMENTO COMPLETO // ========================================================== modelBuilder.Entity(e => { // Nome físico fixo no Postgres e.ToTable("parcelamento_lines"); e.HasKey(x => x.Id); e.Property(x => x.Linha).HasMaxLength(32); e.Property(x => x.Cliente).HasMaxLength(120); e.Property(x => x.QtParcelas).HasMaxLength(32); // índices úteis para filtro e performance e.HasIndex(x => x.Cliente); e.HasIndex(x => x.Linha); e.HasIndex(x => x.AnoRef); // se você quiser evitar duplicar importação da mesma linha/cliente/item: // (deixa comentado pra não quebrar caso existam repetições legítimas) // e.HasIndex(x => new { x.AnoRef, x.Item, x.Linha, x.Cliente }).IsUnique(); }); modelBuilder.Entity(e => { e.ToTable("parcelamento_month_values"); e.HasKey(x => x.Id); // relação 1:N (ParcelamentoLine -> Meses) e.HasOne(x => x.ParcelamentoLine) .WithMany(x => x.Meses) .HasForeignKey(x => x.ParcelamentoLineId) .OnDelete(DeleteBehavior.Cascade); // índices para gráfico e consultas por mês e.HasIndex(x => x.Competencia); e.HasIndex(x => x.ParcelamentoLineId); // garante 1 valor por mês por linha (evita duplicar mês) e.HasIndex(x => new { x.ParcelamentoLineId, x.Competencia }) .IsUnique(); }); } }