169 lines
8.3 KiB
C#
169 lines
8.3 KiB
C#
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<PlatformDbContext> options) : base(options) { }
|
|
|
|
public DbSet<WarehouseArea> WarehouseAreas => Set<WarehouseArea>();
|
|
public DbSet<Storage> Storages => Set<Storage>();
|
|
public DbSet<Container> Containers => Set<Container>();
|
|
public DbSet<Material> Materials => Set<Material>();
|
|
public DbSet<ContainerLocation> ContainerLocations => Set<ContainerLocation>();
|
|
public DbSet<ContainerMaterial> ContainerMaterials => Set<ContainerMaterial>();
|
|
public DbSet<ContainerLocationHistory> ContainerLocationHistories => Set<ContainerLocationHistory>();
|
|
public DbSet<ContainerMaterialHistory> ContainerMaterialHistories => Set<ContainerMaterialHistory>();
|
|
public DbSet<SimpleField> SimpleFields => Set<SimpleField>();
|
|
public DbSet<UserDashboardShortcut> UserDashboardShortcuts => Set<UserDashboardShortcut>();
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
var guid = new ValueConverter<Guid, string>(
|
|
v => v.ToString("D"),
|
|
v => Guid.Parse(v));
|
|
var nullableGuid = new ValueConverter<Guid?, string?>(
|
|
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<WarehouseArea>(modelBuilder, "wms_areas");
|
|
ConfigureEntityBase<Storage>(modelBuilder, "wms_storages");
|
|
ConfigureEntityBase<Container>(modelBuilder, "wms_containers");
|
|
ConfigureEntityBase<Material>(modelBuilder, "wms_materials");
|
|
ConfigureEntityBase<ContainerLocation>(modelBuilder, "wms_container_locations");
|
|
ConfigureEntityBase<ContainerMaterial>(modelBuilder, "wms_container_materials");
|
|
|
|
ConfigureHistory<ContainerLocationHistory>(modelBuilder, "wms_container_location_history");
|
|
ConfigureHistory<ContainerMaterialHistory>(modelBuilder, "wms_container_material_history");
|
|
|
|
modelBuilder.Entity<WarehouseArea>().HasIndex(x => x.Code).IsUnique();
|
|
modelBuilder.Entity<Storage>().HasIndex(x => x.Code).IsUnique();
|
|
modelBuilder.Entity<Storage>().HasIndex(x => x.AreaId);
|
|
modelBuilder.Entity<Container>().HasIndex(x => x.Code).IsUnique();
|
|
modelBuilder.Entity<Material>().HasIndex(x => x.Code).IsUnique();
|
|
modelBuilder.Entity<ContainerLocation>().HasIndex(x => x.ContainerId).IsUnique();
|
|
modelBuilder.Entity<ContainerLocation>().HasIndex(x => new { x.LocationType, x.LocationId });
|
|
modelBuilder.Entity<ContainerMaterial>().HasIndex(x => new { x.ContainerId, x.MaterialId, x.BatchNo, x.SerialNo }).IsUnique();
|
|
|
|
modelBuilder.Entity<ContainerMaterial>().Property(x => x.Quantity).HasPrecision(18, 4);
|
|
modelBuilder.Entity<ContainerMaterialHistory>().Property(x => x.QuantityDelta).HasPrecision(18, 4);
|
|
|
|
ConfigureSimpleField(modelBuilder);
|
|
ConfigureUserDashboardShortcut(modelBuilder);
|
|
}
|
|
|
|
private static void ConfigureUserDashboardShortcut(ModelBuilder modelBuilder)
|
|
{
|
|
var e = modelBuilder.Entity<UserDashboardShortcut>();
|
|
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<DateTimeOffset, string>(
|
|
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<SimpleField>();
|
|
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<DateTimeOffset, string>(
|
|
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<int> 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<EntityBase>())
|
|
{
|
|
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<T>(ModelBuilder modelBuilder, string table) where T : EntityBase
|
|
{
|
|
var e = modelBuilder.Entity<T>();
|
|
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<T>(ModelBuilder modelBuilder, string table) where T : WarehouseHistoryBase
|
|
{
|
|
var e = modelBuilder.Entity<T>();
|
|
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);
|
|
}
|
|
}
|