using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; using MiGu.Server.Dashboard; using MiGu.Server.Wms; using MiGu.Server.SimpleFields; namespace MiGu.Server.Persistence; public sealed class PlatformDbContext : DbContext { public PlatformDbContext(DbContextOptions options) : base(options) { } public DbSet WarehouseAreas => Set(); public DbSet Storages => Set(); public DbSet Containers => Set(); public DbSet Materials => Set(); public DbSet ContainerLocations => Set(); public DbSet ContainerMaterials => Set(); public DbSet ContainerLocationHistories => Set(); public DbSet ContainerMaterialHistories => Set(); public DbSet SimpleFields => Set(); public DbSet UserDashboardShortcuts => Set(); protected override void OnModelCreating(ModelBuilder modelBuilder) { var guid = new ValueConverter( v => v.ToString("D"), v => Guid.Parse(v)); var nullableGuid = new ValueConverter( v => v.HasValue ? v.Value.ToString("D") : null, v => string.IsNullOrWhiteSpace(v) ? null : Guid.Parse(v)); foreach (var entity in modelBuilder.Model.GetEntityTypes()) { foreach (var p in entity.ClrType.GetProperties().Where(p => p.PropertyType == typeof(Guid))) modelBuilder.Entity(entity.ClrType).Property(p.Name).HasConversion(guid).HasMaxLength(36); foreach (var p in entity.ClrType.GetProperties().Where(p => p.PropertyType == typeof(Guid?))) modelBuilder.Entity(entity.ClrType).Property(p.Name).HasConversion(nullableGuid).HasMaxLength(36); } ConfigureEntityBase(modelBuilder, "wms_areas"); ConfigureEntityBase(modelBuilder, "wms_storages"); ConfigureEntityBase(modelBuilder, "wms_containers"); ConfigureEntityBase(modelBuilder, "wms_materials"); ConfigureEntityBase(modelBuilder, "wms_container_locations"); ConfigureEntityBase(modelBuilder, "wms_container_materials"); ConfigureHistory(modelBuilder, "wms_container_location_history"); ConfigureHistory(modelBuilder, "wms_container_material_history"); modelBuilder.Entity().HasIndex(x => x.Code).IsUnique(); modelBuilder.Entity().HasIndex(x => x.Code).IsUnique(); modelBuilder.Entity().HasIndex(x => x.AreaId); modelBuilder.Entity().HasIndex(x => x.Code).IsUnique(); modelBuilder.Entity().HasIndex(x => x.Code).IsUnique(); modelBuilder.Entity().HasIndex(x => x.ContainerId).IsUnique(); modelBuilder.Entity().HasIndex(x => new { x.LocationType, x.LocationId }); modelBuilder.Entity().HasIndex(x => new { x.ContainerId, x.MaterialId, x.BatchNo, x.SerialNo }).IsUnique(); modelBuilder.Entity().Property(x => x.Quantity).HasPrecision(18, 4); modelBuilder.Entity().Property(x => x.QuantityDelta).HasPrecision(18, 4); ConfigureSimpleField(modelBuilder); ConfigureUserDashboardShortcut(modelBuilder); } private static void ConfigureUserDashboardShortcut(ModelBuilder modelBuilder) { var e = modelBuilder.Entity(); e.ToTable("user_dashboard_shortcuts"); e.HasKey(x => new { x.UserId, x.Scope }); e.Property(x => x.UserId).HasColumnName("user_id").HasMaxLength(64); e.Property(x => x.Scope).HasColumnName("scope").HasMaxLength(32); e.Property(x => x.KeysJson).HasColumnName("keys_json").HasColumnType("text"); var dateTime = new ValueConverter( v => v.UtcDateTime.ToString("O"), v => DateTimeOffset.Parse(v)); e.Property(x => x.UpdatedAt).HasColumnName("updated_at").HasConversion(dateTime).HasMaxLength(40); } private static void ConfigureSimpleField(ModelBuilder modelBuilder) { var e = modelBuilder.Entity(); e.ToTable("simple_fields"); e.HasKey(x => x.Id); e.Property(x => x.Id).HasColumnName("id"); e.Property(x => x.CarType).HasColumnName("car_type").HasMaxLength(64); e.Property(x => x.FieldType).HasColumnName("field_type").HasMaxLength(64); e.Property(x => x.Key).HasColumnName("key").HasMaxLength(128); e.Property(x => x.Value).HasColumnName("value"); e.Property(x => x.DataType).HasColumnName("data_type").HasMaxLength(128); e.Property(x => x.Chinese).HasColumnName("chinese").HasMaxLength(256).IsRequired(false); e.Property(x => x.English).HasColumnName("english").HasMaxLength(256).IsRequired(false); e.Property(x => x.Other).HasColumnName("other").HasMaxLength(512); e.Property(x => x.IsDefault).HasColumnName("is_default"); var dateTime = new ValueConverter( v => SimpleFieldDateTime.ToStorage(v), v => SimpleFieldDateTime.FromStorage(v)); e.Property(x => x.CreateTime).HasColumnName("create_time").HasConversion(dateTime).HasMaxLength(19); e.Property(x => x.UpdateTime).HasColumnName("update_time").HasConversion(dateTime).HasMaxLength(19); e.HasIndex(x => new { x.CarType, x.FieldType, x.Key }).IsUnique(); } public override int SaveChanges(bool acceptAllChangesOnSuccess) { StampEntities(); return base.SaveChanges(acceptAllChangesOnSuccess); } public override Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { StampEntities(); return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); } private void StampEntities() { var now = DateTimeOffset.UtcNow; foreach (var e in ChangeTracker.Entries()) { if (e.State == EntityState.Added) { if (e.Entity.Id == Guid.Empty) e.Entity.Id = Guid.NewGuid(); e.Entity.CreatedAt = now; e.Entity.UpdatedAt = now; e.Entity.Version = Math.Max(1, e.Entity.Version); if (string.IsNullOrWhiteSpace(e.Entity.Extend)) e.Entity.Extend = "{}"; } else if (e.State == EntityState.Modified) { e.Entity.UpdatedAt = now; e.Entity.Version += 1; if (string.IsNullOrWhiteSpace(e.Entity.Extend)) e.Entity.Extend = "{}"; } } } private static void ConfigureEntityBase(ModelBuilder modelBuilder, string table) where T : EntityBase { var e = modelBuilder.Entity(); e.ToTable(table); e.HasKey(x => x.Id); e.Property(x => x.CreatedBy).HasMaxLength(128); e.Property(x => x.UpdatedBy).HasMaxLength(128); e.Property(x => x.DeletedBy).HasMaxLength(128); e.Property(x => x.Remark).HasMaxLength(1000); e.Property(x => x.Extend).HasColumnType("text"); e.HasQueryFilter(x => !x.IsDeleted); } private static void ConfigureHistory(ModelBuilder modelBuilder, string table) where T : WarehouseHistoryBase { var e = modelBuilder.Entity(); e.ToTable(table); e.HasKey(x => x.Id); e.Property(x => x.EventType).HasMaxLength(64); e.Property(x => x.BeforeJson).HasColumnType("text"); e.Property(x => x.AfterJson).HasColumnType("text"); e.Property(x => x.Operator).HasMaxLength(128); e.Property(x => x.Source).HasMaxLength(32); e.Property(x => x.Reason).HasMaxLength(500); e.Property(x => x.Remark).HasMaxLength(1000); e.Property(x => x.Extend).HasColumnType("text"); e.HasIndex(x => x.ContainerId); e.HasIndex(x => x.OperatedAt); e.HasIndex(x => x.EventType); } }