feat(product): complete combo and detail editing data model
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 47s

This commit is contained in:
2026-02-22 09:35:57 +08:00
parent f7c2ae4bac
commit d66879f5cf
22 changed files with 19099 additions and 75 deletions

View File

@@ -233,6 +233,14 @@ public sealed class TakeoutAppDbContext(
/// </summary>
public DbSet<ProductSku> ProductSkus => Set<ProductSku>();
/// <summary>
/// 套餐分组。
/// </summary>
public DbSet<ProductComboGroup> ProductComboGroups => Set<ProductComboGroup>();
/// <summary>
/// 套餐分组商品。
/// </summary>
public DbSet<ProductComboGroupItem> ProductComboGroupItems => Set<ProductComboGroupItem>();
/// <summary>
/// 加料分组。
/// </summary>
public DbSet<ProductAddonGroup> ProductAddonGroups => Set<ProductAddonGroup>();
@@ -477,6 +485,8 @@ public sealed class TakeoutAppDbContext(
ConfigureProductSchedule(modelBuilder.Entity<ProductSchedule>());
ConfigureProductScheduleProduct(modelBuilder.Entity<ProductScheduleProduct>());
ConfigureProductSku(modelBuilder.Entity<ProductSku>());
ConfigureProductComboGroup(modelBuilder.Entity<ProductComboGroup>());
ConfigureProductComboGroupItem(modelBuilder.Entity<ProductComboGroupItem>());
ConfigureProductAddonGroup(modelBuilder.Entity<ProductAddonGroup>());
ConfigureProductAddonOption(modelBuilder.Entity<ProductAddonOption>());
ConfigureProductPricingRule(modelBuilder.Entity<ProductPricingRule>());
@@ -747,6 +757,8 @@ public sealed class TakeoutAppDbContext(
builder.Property(x => x.CoverImage).HasMaxLength(256);
builder.Property(x => x.GalleryImages).HasMaxLength(1024);
builder.Property(x => x.Description).HasColumnType("text");
builder.Property(x => x.SortWeight).HasDefaultValue(0);
builder.Property(x => x.PackingFee).HasPrecision(18, 2);
builder.HasIndex(x => new { x.TenantId, x.StoreId });
builder.HasIndex(x => new { x.TenantId, x.SpuCode }).IsUnique();
}
@@ -1308,9 +1320,35 @@ public sealed class TakeoutAppDbContext(
builder.Property(x => x.OriginalPrice).HasPrecision(18, 2);
builder.Property(x => x.Weight).HasPrecision(10, 3);
builder.Property(x => x.AttributesJson).HasColumnType("text");
builder.Property(x => x.IsEnabled).HasDefaultValue(true);
builder.HasIndex(x => new { x.TenantId, x.SkuCode }).IsUnique();
}
private static void ConfigureProductComboGroup(EntityTypeBuilder<ProductComboGroup> builder)
{
builder.ToTable("product_combo_groups");
builder.HasKey(x => x.Id);
builder.Property(x => x.ProductId).IsRequired();
builder.Property(x => x.Name).HasMaxLength(64).IsRequired();
builder.Property(x => x.MinSelect).IsRequired();
builder.Property(x => x.MaxSelect).IsRequired();
builder.Property(x => x.SortOrder).IsRequired();
builder.HasIndex(x => new { x.TenantId, x.ProductId, x.SortOrder });
builder.HasIndex(x => new { x.TenantId, x.ProductId, x.Name });
}
private static void ConfigureProductComboGroupItem(EntityTypeBuilder<ProductComboGroupItem> builder)
{
builder.ToTable("product_combo_group_items");
builder.HasKey(x => x.Id);
builder.Property(x => x.ComboGroupId).IsRequired();
builder.Property(x => x.ProductId).IsRequired();
builder.Property(x => x.Quantity).IsRequired();
builder.Property(x => x.SortOrder).IsRequired();
builder.HasIndex(x => new { x.TenantId, x.ComboGroupId, x.SortOrder });
builder.HasIndex(x => new { x.TenantId, x.ComboGroupId, x.ProductId }).IsUnique();
}
private static void ConfigureProductAddonGroup(EntityTypeBuilder<ProductAddonGroup> builder)
{
builder.ToTable("product_addon_groups");

View File

@@ -493,6 +493,77 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
return skus;
}
/// <inheritdoc />
public async Task<IReadOnlyList<ProductComboGroup>> GetComboGroupsAsync(long productId, long tenantId, CancellationToken cancellationToken = default)
{
var groups = await context.ProductComboGroups
.AsNoTracking()
.Where(x => x.TenantId == tenantId && x.ProductId == productId)
.OrderBy(x => x.SortOrder)
.ThenBy(x => x.Id)
.ToListAsync(cancellationToken);
return groups;
}
/// <inheritdoc />
public async Task<IReadOnlyList<ProductComboGroup>> GetComboGroupsByProductIdsAsync(IReadOnlyCollection<long> productIds, long tenantId, CancellationToken cancellationToken = default)
{
if (productIds.Count == 0)
{
return Array.Empty<ProductComboGroup>();
}
var groups = await context.ProductComboGroups
.AsNoTracking()
.Where(x => x.TenantId == tenantId && productIds.Contains(x.ProductId))
.OrderBy(x => x.SortOrder)
.ThenBy(x => x.Id)
.ToListAsync(cancellationToken);
return groups;
}
/// <inheritdoc />
public async Task<IReadOnlyList<ProductComboGroupItem>> GetComboGroupItemsAsync(long productId, long tenantId, CancellationToken cancellationToken = default)
{
var groupIds = await context.ProductComboGroups
.AsNoTracking()
.Where(x => x.TenantId == tenantId && x.ProductId == productId)
.Select(x => x.Id)
.ToListAsync(cancellationToken);
if (groupIds.Count == 0)
{
return Array.Empty<ProductComboGroupItem>();
}
var items = await context.ProductComboGroupItems
.AsNoTracking()
.Where(x => x.TenantId == tenantId && groupIds.Contains(x.ComboGroupId))
.OrderBy(x => x.SortOrder)
.ThenBy(x => x.Id)
.ToListAsync(cancellationToken);
return items;
}
/// <inheritdoc />
public async Task<IReadOnlyList<ProductComboGroupItem>> GetComboGroupItemsByGroupIdsAsync(IReadOnlyCollection<long> comboGroupIds, long tenantId, CancellationToken cancellationToken = default)
{
if (comboGroupIds.Count == 0)
{
return Array.Empty<ProductComboGroupItem>();
}
var items = await context.ProductComboGroupItems
.AsNoTracking()
.Where(x => x.TenantId == tenantId && comboGroupIds.Contains(x.ComboGroupId))
.OrderBy(x => x.SortOrder)
.ThenBy(x => x.Id)
.ToListAsync(cancellationToken);
return items;
}
/// <inheritdoc />
public async Task<IReadOnlyList<ProductAddonGroup>> GetAddonGroupsAsync(long productId, long tenantId, CancellationToken cancellationToken = default)
{
@@ -731,6 +802,14 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
return context.ProductSkus.AddRangeAsync(skus, cancellationToken);
}
/// <inheritdoc />
public Task AddComboGroupsAsync(IEnumerable<ProductComboGroup> groups, IEnumerable<ProductComboGroupItem> items, CancellationToken cancellationToken = default)
{
var addGroupsTask = context.ProductComboGroups.AddRangeAsync(groups, cancellationToken);
var addItemsTask = context.ProductComboGroupItems.AddRangeAsync(items, cancellationToken);
return Task.WhenAll(addGroupsTask, addItemsTask);
}
/// <inheritdoc />
public Task AddAddonGroupsAsync(IEnumerable<ProductAddonGroup> groups, IEnumerable<ProductAddonOption> options, CancellationToken cancellationToken = default)
{
@@ -779,6 +858,7 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
await RemoveMediaAssetsAsync(productId, tenantId, cancellationToken);
await RemoveAttributeGroupsAsync(productId, tenantId, cancellationToken);
await RemoveAddonGroupsAsync(productId, tenantId, cancellationToken);
await RemoveComboGroupsAsync(productId, tenantId, cancellationToken);
await RemoveSkusAsync(productId, tenantId, cancellationToken);
var existing = await context.Products
@@ -902,6 +982,38 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
context.ProductSkus.RemoveRange(skus);
}
/// <inheritdoc />
public async Task RemoveComboGroupsAsync(long productId, long tenantId, CancellationToken cancellationToken = default)
{
var groupIds = await context.ProductComboGroups
.Where(x => x.TenantId == tenantId && x.ProductId == productId)
.Select(x => x.Id)
.ToListAsync(cancellationToken);
if (groupIds.Count == 0)
{
return;
}
var items = await context.ProductComboGroupItems
.Where(x => x.TenantId == tenantId && groupIds.Contains(x.ComboGroupId))
.ToListAsync(cancellationToken);
if (items.Count > 0)
{
context.ProductComboGroupItems.RemoveRange(items);
}
var groups = await context.ProductComboGroups
.Where(x => groupIds.Contains(x.Id))
.ToListAsync(cancellationToken);
if (groups.Count > 0)
{
context.ProductComboGroups.RemoveRange(groups);
}
}
/// <inheritdoc />
public async Task RemoveSpecTemplateOptionsAsync(long templateId, long tenantId, CancellationToken cancellationToken = default)
{

View File

@@ -0,0 +1,96 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace TakeoutSaaS.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class AddProductComboGroups : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "product_combo_group_items",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ComboGroupId = table.Column<long>(type: "bigint", nullable: false, comment: "所属套餐分组 ID。"),
ProductId = table.Column<long>(type: "bigint", nullable: false, comment: "商品 ID。"),
Quantity = table.Column<int>(type: "integer", nullable: false, comment: "数量。"),
SortOrder = table.Column<int>(type: "integer", nullable: false, comment: "排序值。"),
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间UTC。"),
UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间UTC从未更新时为 null。"),
DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间UTC未删除时为 null。"),
CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识,匿名或系统操作时为 null。"),
UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识,匿名或系统操作时为 null。"),
DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识(软删除),未删除时为 null。"),
TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
},
constraints: table =>
{
table.PrimaryKey("PK_product_combo_group_items", x => x.Id);
},
comment: "套餐分组内商品。");
migrationBuilder.CreateTable(
name: "product_combo_groups",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
ProductId = table.Column<long>(type: "bigint", nullable: false, comment: "套餐商品 ID。"),
Name = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "分组名称。"),
MinSelect = table.Column<int>(type: "integer", nullable: false, comment: "最小选择数。"),
MaxSelect = table.Column<int>(type: "integer", nullable: false, comment: "最大选择数。"),
SortOrder = table.Column<int>(type: "integer", nullable: false, comment: "排序值。"),
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false, comment: "创建时间UTC。"),
UpdatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "最近一次更新时间UTC从未更新时为 null。"),
DeletedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true, comment: "软删除时间UTC未删除时为 null。"),
CreatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "创建人用户标识,匿名或系统操作时为 null。"),
UpdatedBy = table.Column<long>(type: "bigint", nullable: true, comment: "最后更新人用户标识,匿名或系统操作时为 null。"),
DeletedBy = table.Column<long>(type: "bigint", nullable: true, comment: "删除人用户标识(软删除),未删除时为 null。"),
TenantId = table.Column<long>(type: "bigint", nullable: false, comment: "所属租户 ID。")
},
constraints: table =>
{
table.PrimaryKey("PK_product_combo_groups", x => x.Id);
},
comment: "套餐分组。");
migrationBuilder.CreateIndex(
name: "IX_product_combo_group_items_TenantId_ComboGroupId_ProductId",
table: "product_combo_group_items",
columns: new[] { "TenantId", "ComboGroupId", "ProductId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_product_combo_group_items_TenantId_ComboGroupId_SortOrder",
table: "product_combo_group_items",
columns: new[] { "TenantId", "ComboGroupId", "SortOrder" });
migrationBuilder.CreateIndex(
name: "IX_product_combo_groups_TenantId_ProductId_Name",
table: "product_combo_groups",
columns: new[] { "TenantId", "ProductId", "Name" });
migrationBuilder.CreateIndex(
name: "IX_product_combo_groups_TenantId_ProductId_SortOrder",
table: "product_combo_groups",
columns: new[] { "TenantId", "ProductId", "SortOrder" });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "product_combo_group_items");
migrationBuilder.DropTable(
name: "product_combo_groups");
}
}
}

View File

@@ -0,0 +1,66 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace TakeoutSaaS.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class AddProductDetailRichFields : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<decimal>(
name: "PackingFee",
table: "products",
type: "numeric(18,2)",
precision: 18,
scale: 2,
nullable: true,
comment: "打包费(元/份)。");
migrationBuilder.AddColumn<int>(
name: "SortWeight",
table: "products",
type: "integer",
nullable: false,
defaultValue: 0,
comment: "排序权重,越大越靠前。");
migrationBuilder.AddColumn<int>(
name: "WarningStock",
table: "products",
type: "integer",
nullable: true,
comment: "库存预警值。");
migrationBuilder.AddColumn<bool>(
name: "IsEnabled",
table: "product_skus",
type: "boolean",
nullable: false,
defaultValue: true,
comment: "是否启用。");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "PackingFee",
table: "products");
migrationBuilder.DropColumn(
name: "SortWeight",
table: "products");
migrationBuilder.DropColumn(
name: "WarningStock",
table: "products");
migrationBuilder.DropColumn(
name: "IsEnabled",
table: "product_skus");
}
}
}

View File

@@ -4007,6 +4007,11 @@ namespace TakeoutSaaS.Infrastructure.Migrations
.HasColumnType("numeric(18,2)")
.HasComment("原价。");
b.Property<decimal?>("PackingFee")
.HasPrecision(18, 2)
.HasColumnType("numeric(18,2)")
.HasComment("打包费(元/份)。");
b.Property<decimal>("Price")
.HasPrecision(18, 2)
.HasColumnType("numeric(18,2)")
@@ -4035,6 +4040,12 @@ namespace TakeoutSaaS.Infrastructure.Migrations
.HasColumnType("character varying(256)")
.HasComment("沽清原因。");
b.Property<int>("SortWeight")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasDefaultValue(0)
.HasComment("排序权重,越大越靠前。");
b.Property<string>("SpuCode")
.IsRequired()
.HasMaxLength(32)
@@ -4091,6 +4102,10 @@ namespace TakeoutSaaS.Infrastructure.Migrations
.HasColumnType("bigint")
.HasComment("最后更新人用户标识,匿名或系统操作时为 null。");
b.Property<int?>("WarningStock")
.HasColumnType("integer")
.HasComment("库存预警值。");
b.HasKey("Id");
b.HasIndex("TenantId", "SpuCode")
@@ -4475,6 +4490,143 @@ namespace TakeoutSaaS.Infrastructure.Migrations
});
});
modelBuilder.Entity("TakeoutSaaS.Domain.Products.Entities.ProductComboGroup", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasComment("实体唯一标识。");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone")
.HasComment("创建时间UTC。");
b.Property<long?>("CreatedBy")
.HasColumnType("bigint")
.HasComment("创建人用户标识,匿名或系统操作时为 null。");
b.Property<DateTime?>("DeletedAt")
.HasColumnType("timestamp with time zone")
.HasComment("软删除时间UTC未删除时为 null。");
b.Property<long?>("DeletedBy")
.HasColumnType("bigint")
.HasComment("删除人用户标识(软删除),未删除时为 null。");
b.Property<int>("MaxSelect")
.HasColumnType("integer")
.HasComment("最大选择数。");
b.Property<int>("MinSelect")
.HasColumnType("integer")
.HasComment("最小选择数。");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("character varying(64)")
.HasComment("分组名称。");
b.Property<long>("ProductId")
.HasColumnType("bigint")
.HasComment("套餐商品 ID。");
b.Property<int>("SortOrder")
.HasColumnType("integer")
.HasComment("排序值。");
b.Property<long>("TenantId")
.HasColumnType("bigint")
.HasComment("所属租户 ID。");
b.Property<DateTime?>("UpdatedAt")
.HasColumnType("timestamp with time zone")
.HasComment("最近一次更新时间UTC从未更新时为 null。");
b.Property<long?>("UpdatedBy")
.HasColumnType("bigint")
.HasComment("最后更新人用户标识,匿名或系统操作时为 null。");
b.HasKey("Id");
b.HasIndex("TenantId", "ProductId", "Name");
b.HasIndex("TenantId", "ProductId", "SortOrder");
b.ToTable("product_combo_groups", null, t =>
{
t.HasComment("套餐分组。");
});
});
modelBuilder.Entity("TakeoutSaaS.Domain.Products.Entities.ProductComboGroupItem", b =>
{
b.Property<long>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("bigint")
.HasComment("实体唯一标识。");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<long>("Id"));
b.Property<long>("ComboGroupId")
.HasColumnType("bigint")
.HasComment("所属套餐分组 ID。");
b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone")
.HasComment("创建时间UTC。");
b.Property<long?>("CreatedBy")
.HasColumnType("bigint")
.HasComment("创建人用户标识,匿名或系统操作时为 null。");
b.Property<DateTime?>("DeletedAt")
.HasColumnType("timestamp with time zone")
.HasComment("软删除时间UTC未删除时为 null。");
b.Property<long?>("DeletedBy")
.HasColumnType("bigint")
.HasComment("删除人用户标识(软删除),未删除时为 null。");
b.Property<long>("ProductId")
.HasColumnType("bigint")
.HasComment("商品 ID。");
b.Property<int>("Quantity")
.HasColumnType("integer")
.HasComment("数量。");
b.Property<int>("SortOrder")
.HasColumnType("integer")
.HasComment("排序值。");
b.Property<long>("TenantId")
.HasColumnType("bigint")
.HasComment("所属租户 ID。");
b.Property<DateTime?>("UpdatedAt")
.HasColumnType("timestamp with time zone")
.HasComment("最近一次更新时间UTC从未更新时为 null。");
b.Property<long?>("UpdatedBy")
.HasColumnType("bigint")
.HasComment("最后更新人用户标识,匿名或系统操作时为 null。");
b.HasKey("Id");
b.HasIndex("TenantId", "ComboGroupId", "ProductId")
.IsUnique();
b.HasIndex("TenantId", "ComboGroupId", "SortOrder");
b.ToTable("product_combo_group_items", null, t =>
{
t.HasComment("套餐分组内商品。");
});
});
modelBuilder.Entity("TakeoutSaaS.Domain.Products.Entities.ProductLabel", b =>
{
b.Property<long>("Id")
@@ -4933,6 +5085,12 @@ namespace TakeoutSaaS.Infrastructure.Migrations
.HasColumnType("bigint")
.HasComment("删除人用户标识(软删除),未删除时为 null。");
b.Property<bool>("IsEnabled")
.ValueGeneratedOnAdd()
.HasColumnType("boolean")
.HasDefaultValue(true)
.HasComment("是否启用。");
b.Property<decimal?>("OriginalPrice")
.HasPrecision(18, 2)
.HasColumnType("numeric(18,2)")