189 lines
5.6 KiB
C#
189 lines
5.6 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
|
using Prefab.Base.Catalog.Options;
|
|
using Prefab.Catalog.Domain.Entities;
|
|
|
|
namespace Prefab.Catalog.Data.Configs;
|
|
|
|
public sealed class OptionDefinitionConfig() : Prefab.Data.Configs.EntityConfig<OptionDefinition>("OptionDefinitions", IModuleDb.SchemaName.ToLower())
|
|
{
|
|
public override void Configure(EntityTypeBuilder<OptionDefinition> builder)
|
|
{
|
|
base.Configure(builder);
|
|
|
|
builder.HasKey(o => o.Id);
|
|
|
|
builder.Property(o => o.RowVersion)
|
|
.IsRowVersion();
|
|
|
|
builder.Property(o => o.Name)
|
|
.HasMaxLength(OptionDefinitionRules.NameMaxLength)
|
|
.IsRequired();
|
|
|
|
builder.Property(o => o.Code)
|
|
.HasMaxLength(OptionDefinitionRules.CodeMaxLength)
|
|
.IsRequired();
|
|
|
|
builder.Property(o => o.Unit)
|
|
.HasMaxLength(OptionDefinitionRules.UnitMaxLength);
|
|
|
|
builder.Property(o => o.Min)
|
|
.HasPrecision(18, 4);
|
|
|
|
builder.Property(o => o.Max)
|
|
.HasPrecision(18, 4);
|
|
|
|
builder.Property(o => o.Step)
|
|
.HasPrecision(18, 4);
|
|
|
|
builder.Property(o => o.PricePerUnit)
|
|
.HasPrecision(18, 4);
|
|
|
|
builder.Property(o => o.PercentScope)
|
|
.HasConversion(
|
|
v => v.HasValue ? (int)v.Value : (int?)null,
|
|
v => v.HasValue ? (PercentScope)v.Value : null);
|
|
|
|
builder.HasIndex(o => new { o.ProductId, o.Code })
|
|
.IsUnique();
|
|
|
|
builder.HasOne(o => o.Product)
|
|
.WithMany(p => p.Options)
|
|
.HasForeignKey(o => o.ProductId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
builder.HasMany(o => o.Values)
|
|
.WithOne(v => v.OptionDefinition)
|
|
.HasForeignKey(v => v.OptionDefinitionId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
builder.HasMany(o => o.Tiers)
|
|
.WithOne(t => t.OptionDefinition)
|
|
.HasForeignKey(t => t.OptionDefinitionId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
}
|
|
}
|
|
|
|
public sealed class OptionValueConfig() : Prefab.Data.Configs.EntityConfig<OptionValue>("OptionValues", IModuleDb.SchemaName.ToLower())
|
|
{
|
|
public override void Configure(EntityTypeBuilder<OptionValue> builder)
|
|
{
|
|
base.Configure(builder);
|
|
|
|
builder.HasKey(v => v.Id);
|
|
|
|
builder.Property(v => v.RowVersion)
|
|
.IsRowVersion();
|
|
|
|
builder.Property(v => v.Code)
|
|
.HasMaxLength(OptionValueRules.CodeMaxLength)
|
|
.IsRequired();
|
|
|
|
builder.Property(v => v.Label)
|
|
.HasMaxLength(OptionValueRules.LabelMaxLength)
|
|
.IsRequired();
|
|
|
|
builder.Property(v => v.PriceDelta)
|
|
.HasPrecision(9, 4);
|
|
|
|
builder.HasIndex(v => new { v.OptionDefinitionId, v.Code })
|
|
.IsUnique();
|
|
}
|
|
}
|
|
|
|
public sealed class OptionTierConfig() : Prefab.Data.Configs.EntityConfig<OptionTier>("OptionTiers", IModuleDb.SchemaName.ToLower())
|
|
{
|
|
public override void Configure(EntityTypeBuilder<OptionTier> builder)
|
|
{
|
|
base.Configure(builder);
|
|
|
|
builder.HasKey(t => t.Id);
|
|
|
|
builder.Property(t => t.RowVersion)
|
|
.IsRowVersion();
|
|
|
|
builder.Property(t => t.FromInclusive)
|
|
.HasPrecision(18, 4);
|
|
|
|
builder.Property(t => t.ToInclusive)
|
|
.HasPrecision(18, 4);
|
|
|
|
builder.Property(t => t.UnitRate)
|
|
.HasPrecision(18, 4);
|
|
|
|
builder.Property(t => t.FlatDelta)
|
|
.HasPrecision(18, 2);
|
|
|
|
builder.HasIndex(t => new { t.OptionDefinitionId, t.FromInclusive, t.ToInclusive });
|
|
}
|
|
}
|
|
|
|
public sealed class OptionRuleSetConfig() : Prefab.Data.Configs.EntityConfig<OptionRuleSet>("OptionRuleSets", IModuleDb.SchemaName.ToLower())
|
|
{
|
|
public override void Configure(EntityTypeBuilder<OptionRuleSet> builder)
|
|
{
|
|
base.Configure(builder);
|
|
|
|
builder.HasKey(r => r.Id);
|
|
|
|
builder.Property(r => r.RowVersion)
|
|
.IsRowVersion();
|
|
|
|
builder.Property(r => r.TargetKind)
|
|
.HasConversion<byte>();
|
|
|
|
builder.Property(r => r.Effect)
|
|
.HasConversion<byte>();
|
|
|
|
builder.Property(r => r.Mode)
|
|
.HasConversion<byte>();
|
|
|
|
builder.HasIndex(r => new { r.ProductId, r.TargetKind, r.TargetId });
|
|
|
|
builder.HasOne(r => r.Product)
|
|
.WithMany(p => p.RuleSets)
|
|
.HasForeignKey(r => r.ProductId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
}
|
|
}
|
|
|
|
public sealed class OptionRuleConditionConfig() : Prefab.Data.Configs.EntityConfig<OptionRuleCondition>("OptionRuleConditions", IModuleDb.SchemaName.ToLower())
|
|
{
|
|
public override void Configure(EntityTypeBuilder<OptionRuleCondition> builder)
|
|
{
|
|
base.Configure(builder);
|
|
|
|
builder.HasKey(c => c.Id);
|
|
|
|
builder.Property(c => c.RowVersion)
|
|
.IsRowVersion();
|
|
|
|
builder.Property(c => c.Operator)
|
|
.HasConversion<byte>();
|
|
|
|
builder.Property(c => c.RightNumber)
|
|
.HasPrecision(18, 4);
|
|
|
|
builder.Property(c => c.RightMin)
|
|
.HasPrecision(18, 4);
|
|
|
|
builder.Property(c => c.RightMax)
|
|
.HasPrecision(18, 4);
|
|
|
|
builder.HasOne(c => c.RuleSet)
|
|
.WithMany(r => r.Conditions)
|
|
.HasForeignKey(c => c.RuleSetId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
builder.HasOne(c => c.LeftOptionDefinition)
|
|
.WithMany()
|
|
.HasForeignKey(c => c.LeftOptionDefinitionId)
|
|
.OnDelete(DeleteBehavior.NoAction);
|
|
|
|
builder.HasOne(c => c.RightOptionValue)
|
|
.WithMany()
|
|
.HasForeignKey(c => c.RightOptionValueId)
|
|
.OnDelete(DeleteBehavior.NoAction);
|
|
}
|
|
}
|