line-gestao-api/Data/AppDbContext.cs

158 lines
4.9 KiB
C#

using Microsoft.EntityFrameworkCore;
using line_gestao_api.Models;
namespace line_gestao_api.Data;
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<User> Users => Set<User>();
// ✅ tabela para espelhar a planilha (GERAL)
public DbSet<MobileLine> MobileLines => Set<MobileLine>();
// ✅ tabela para espelhar a aba MUREG
public DbSet<MuregLine> MuregLines => Set<MuregLine>();
// ✅ tabela para espelhar o FATURAMENTO (PF/PJ)
public DbSet<BillingClient> BillingClients => Set<BillingClient>();
// ✅ tabela DADOS DOS USUÁRIOS
public DbSet<UserData> UserDatas => Set<UserData>();
// ✅ tabela VIGÊNCIA
public DbSet<VigenciaLine> VigenciaLines => Set<VigenciaLine>();
// ✅ tabela TROCA DE NÚMERO
public DbSet<TrocaNumeroLine> TrocaNumeroLines => Set<TrocaNumeroLine>();
// ✅ tabela NOTIFICAÇÕES
public DbSet<Notification> Notifications => Set<Notification>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// =========================
// ✅ USER
// =========================
modelBuilder.Entity<User>()
.HasIndex(u => u.Email)
.IsUnique();
// =========================
// ✅ GERAL (MobileLine)
// =========================
modelBuilder.Entity<MobileLine>(e =>
{
// Mantém UNIQUE por Linha (se Linha puder ser null no banco, Postgres aceita múltiplos nulls)
e.HasIndex(x => x.Linha).IsUnique();
// performance
e.HasIndex(x => x.Chip);
e.HasIndex(x => x.Cliente);
e.HasIndex(x => x.Usuario);
e.HasIndex(x => x.Skil);
e.HasIndex(x => x.Status);
});
// =========================
// ✅ MUREG (FK para MobileLines)
// =========================
modelBuilder.Entity<MuregLine>(e =>
{
e.HasIndex(x => x.Item);
e.HasIndex(x => x.ICCID);
e.HasIndex(x => x.LinhaAntiga);
e.HasIndex(x => x.LinhaNova);
// FK + index
e.HasIndex(x => x.MobileLineId);
e.HasOne(x => x.MobileLine)
.WithMany(m => m.Muregs)
.HasForeignKey(x => x.MobileLineId)
.OnDelete(DeleteBehavior.Restrict);
});
// =========================
// ✅ FATURAMENTO (BillingClient)
// =========================
modelBuilder.Entity<BillingClient>(e =>
{
// ⚠️ só mantenha se seu banco realmente usa esse nome
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);
});
// =========================
// ✅ DADOS DOS USUÁRIOS (UserData)
// ✅ (SEM "Nome" pq não existe no model)
// =========================
modelBuilder.Entity<UserData>(e =>
{
e.HasIndex(x => x.Item);
e.HasIndex(x => x.Cliente);
e.HasIndex(x => x.Linha);
e.HasIndex(x => x.Cpf);
e.HasIndex(x => x.Email);
});
// =========================
// ✅ VIGÊNCIA
// =========================
modelBuilder.Entity<VigenciaLine>(e =>
{
e.HasIndex(x => x.Item);
e.HasIndex(x => x.Cliente);
e.HasIndex(x => x.Linha);
e.HasIndex(x => x.DtTerminoFidelizacao);
});
// =========================
// ✅ TROCA NÚMERO
// =========================
modelBuilder.Entity<TrocaNumeroLine>(e =>
{
e.HasIndex(x => x.Item);
e.HasIndex(x => x.LinhaAntiga);
e.HasIndex(x => x.LinhaNova);
e.HasIndex(x => x.ICCID);
e.HasIndex(x => x.DataTroca);
});
// =========================
// ✅ NOTIFICAÇÕES
// =========================
modelBuilder.Entity<Notification>(e =>
{
e.HasIndex(x => x.DedupKey).IsUnique();
e.HasIndex(x => x.UserId);
e.HasIndex(x => x.Cliente);
e.HasIndex(x => x.Lida);
e.HasIndex(x => x.Data);
e.HasIndex(x => x.VigenciaLineId);
e.HasOne(x => x.User)
.WithMany()
.HasForeignKey(x => x.UserId)
.OnDelete(DeleteBehavior.Restrict);
e.HasOne(x => x.VigenciaLine)
.WithMany()
.HasForeignKey(x => x.VigenciaLineId)
.OnDelete(DeleteBehavior.Restrict);
});
}
}