feat(product): add product schedule management api
All checks were successful
Build and Deploy TenantApi / build-and-deploy (push) Successful in 46s

This commit is contained in:
2026-02-21 11:46:55 +08:00
parent ad65ef3bf6
commit d41f69045f
21 changed files with 9760 additions and 0 deletions

View File

@@ -221,6 +221,14 @@ public sealed class TakeoutAppDbContext(
/// </summary>
public DbSet<ProductLabelProduct> ProductLabelProducts => Set<ProductLabelProduct>();
/// <summary>
/// 商品时段规则。
/// </summary>
public DbSet<ProductSchedule> ProductSchedules => Set<ProductSchedule>();
/// <summary>
/// 时段规则与商品关联。
/// </summary>
public DbSet<ProductScheduleProduct> ProductScheduleProducts => Set<ProductScheduleProduct>();
/// <summary>
/// SKU 实体。
/// </summary>
public DbSet<ProductSku> ProductSkus => Set<ProductSku>();
@@ -466,6 +474,8 @@ public sealed class TakeoutAppDbContext(
ConfigureProductSpecTemplateProduct(modelBuilder.Entity<ProductSpecTemplateProduct>());
ConfigureProductLabel(modelBuilder.Entity<ProductLabel>());
ConfigureProductLabelProduct(modelBuilder.Entity<ProductLabelProduct>());
ConfigureProductSchedule(modelBuilder.Entity<ProductSchedule>());
ConfigureProductScheduleProduct(modelBuilder.Entity<ProductScheduleProduct>());
ConfigureProductSku(modelBuilder.Entity<ProductSku>());
ConfigureProductAddonGroup(modelBuilder.Entity<ProductAddonGroup>());
ConfigureProductAddonOption(modelBuilder.Entity<ProductAddonOption>());
@@ -1255,6 +1265,31 @@ public sealed class TakeoutAppDbContext(
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ProductId });
}
private static void ConfigureProductSchedule(EntityTypeBuilder<ProductSchedule> builder)
{
builder.ToTable("product_schedules");
builder.HasKey(x => x.Id);
builder.Property(x => x.StoreId).IsRequired();
builder.Property(x => x.Name).HasMaxLength(64).IsRequired();
builder.Property(x => x.StartTime).IsRequired();
builder.Property(x => x.EndTime).IsRequired();
builder.Property(x => x.WeekDaysMask).IsRequired();
builder.Property(x => x.IsEnabled).IsRequired();
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.Name }).IsUnique();
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.IsEnabled });
}
private static void ConfigureProductScheduleProduct(EntityTypeBuilder<ProductScheduleProduct> builder)
{
builder.ToTable("product_schedule_products");
builder.HasKey(x => x.Id);
builder.Property(x => x.StoreId).IsRequired();
builder.Property(x => x.ScheduleId).IsRequired();
builder.Property(x => x.ProductId).IsRequired();
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ScheduleId, x.ProductId }).IsUnique();
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ProductId });
}
private static void ConfigureProductSku(EntityTypeBuilder<ProductSku> builder)
{
builder.ToTable("product_skus");

View File

@@ -172,6 +172,17 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
.ToListAsync(cancellationToken);
}
/// <inheritdoc />
public async Task<IReadOnlyList<ProductSchedule>> GetSchedulesByStoreAsync(long tenantId, long storeId, CancellationToken cancellationToken = default)
{
return await context.ProductSchedules
.AsNoTracking()
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
.OrderBy(x => x.Name)
.ThenBy(x => x.Id)
.ToListAsync(cancellationToken);
}
/// <inheritdoc />
public Task<ProductSpecTemplate?> FindSpecTemplateByIdAsync(long templateId, long tenantId, CancellationToken cancellationToken = default)
{
@@ -202,6 +213,14 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
.FirstOrDefaultAsync(cancellationToken);
}
/// <inheritdoc />
public Task<ProductSchedule?> FindScheduleByIdAsync(long scheduleId, long tenantId, CancellationToken cancellationToken = default)
{
return context.ProductSchedules
.Where(x => x.TenantId == tenantId && x.Id == scheduleId)
.FirstOrDefaultAsync(cancellationToken);
}
/// <inheritdoc />
public Task<bool> ExistsSpecTemplateNameAsync(long tenantId, long storeId, string name, long? excludeTemplateId = null, CancellationToken cancellationToken = default)
{
@@ -264,6 +283,26 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
return query.AnyAsync(cancellationToken);
}
/// <inheritdoc />
public Task<bool> ExistsScheduleNameAsync(long tenantId, long storeId, string name, long? excludeScheduleId = null, CancellationToken cancellationToken = default)
{
var normalizedName = (name ?? string.Empty).Trim();
var normalizedLower = normalizedName.ToLowerInvariant();
var query = context.ProductSchedules
.AsNoTracking()
.Where(x =>
x.TenantId == tenantId &&
x.StoreId == storeId &&
x.Name.ToLower() == normalizedLower);
if (excludeScheduleId.HasValue)
{
query = query.Where(x => x.Id != excludeScheduleId.Value);
}
return query.AnyAsync(cancellationToken);
}
/// <inheritdoc />
public async Task<IReadOnlyList<ProductSpecTemplateOption>> GetSpecTemplateOptionsByTemplateIdsAsync(IReadOnlyCollection<long> templateIds, long tenantId, CancellationToken cancellationToken = default)
{
@@ -318,6 +357,25 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
.ToDictionaryAsync(item => item.LabelId, item => item.Count, cancellationToken);
}
/// <inheritdoc />
public async Task<IReadOnlyList<ProductScheduleProduct>> GetScheduleProductsByScheduleIdsAsync(IReadOnlyCollection<long> scheduleIds, long tenantId, long storeId, CancellationToken cancellationToken = default)
{
if (scheduleIds.Count == 0)
{
return Array.Empty<ProductScheduleProduct>();
}
return await context.ProductScheduleProducts
.AsNoTracking()
.Where(x =>
x.TenantId == tenantId &&
x.StoreId == storeId &&
scheduleIds.Contains(x.ScheduleId))
.OrderBy(x => x.ScheduleId)
.ThenBy(x => x.ProductId)
.ToListAsync(cancellationToken);
}
/// <inheritdoc />
public async Task<IReadOnlyList<long>> FilterExistingProductIdsAsync(long tenantId, long storeId, IReadOnlyCollection<long> productIds, CancellationToken cancellationToken = default)
{
@@ -649,6 +707,18 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
return context.ProductLabels.AddAsync(label, cancellationToken).AsTask();
}
/// <inheritdoc />
public Task AddScheduleAsync(ProductSchedule schedule, CancellationToken cancellationToken = default)
{
return context.ProductSchedules.AddAsync(schedule, cancellationToken).AsTask();
}
/// <inheritdoc />
public Task AddScheduleProductsAsync(IEnumerable<ProductScheduleProduct> relations, CancellationToken cancellationToken = default)
{
return context.ProductScheduleProducts.AddRangeAsync(relations, cancellationToken);
}
/// <inheritdoc />
public Task AddProductAsync(Product product, CancellationToken cancellationToken = default)
{
@@ -744,6 +814,13 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
return Task.CompletedTask;
}
/// <inheritdoc />
public Task UpdateScheduleAsync(ProductSchedule schedule, CancellationToken cancellationToken = default)
{
context.ProductSchedules.Update(schedule);
return Task.CompletedTask;
}
/// <inheritdoc />
public async Task DeleteCategoryAsync(long categoryId, long tenantId, CancellationToken cancellationToken = default)
{
@@ -793,6 +870,23 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
.ExecuteDeleteAsync(cancellationToken);
}
/// <inheritdoc />
public async Task DeleteScheduleAsync(long scheduleId, long tenantId, CancellationToken cancellationToken = default)
{
var existing = await context.ProductSchedules
.Where(x => x.TenantId == tenantId && x.Id == scheduleId)
.FirstOrDefaultAsync(cancellationToken);
if (existing is null)
{
return;
}
await context.ProductSchedules
.Where(x => x.TenantId == tenantId && x.Id == scheduleId)
.ExecuteDeleteAsync(cancellationToken);
}
/// <inheritdoc />
public async Task RemoveSkusAsync(long productId, long tenantId, CancellationToken cancellationToken = default)
{
@@ -838,6 +932,17 @@ public sealed class EfProductRepository(TakeoutAppDbContext context) : IProductR
.ExecuteDeleteAsync(cancellationToken);
}
/// <inheritdoc />
public async Task RemoveScheduleProductsAsync(long scheduleId, long tenantId, long storeId, CancellationToken cancellationToken = default)
{
await context.ProductScheduleProducts
.Where(x =>
x.TenantId == tenantId &&
x.StoreId == storeId &&
x.ScheduleId == scheduleId)
.ExecuteDeleteAsync(cancellationToken);
}
/// <inheritdoc />
public async Task RemoveAddonGroupsAsync(long productId, long tenantId, CancellationToken cancellationToken = default)
{

View File

@@ -0,0 +1,97 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace TakeoutSaaS.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class AddProductSchedules : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "product_schedule_products",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
StoreId = table.Column<long>(type: "bigint", nullable: false, comment: "所属门店。"),
ScheduleId = table.Column<long>(type: "bigint", nullable: false, comment: "时段规则 ID。"),
ProductId = table.Column<long>(type: "bigint", nullable: false, comment: "商品 ID。"),
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_schedule_products", x => x.Id);
},
comment: "时段规则与商品关联关系。");
migrationBuilder.CreateTable(
name: "product_schedules",
columns: table => new
{
Id = table.Column<long>(type: "bigint", nullable: false, comment: "实体唯一标识。")
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
StoreId = table.Column<long>(type: "bigint", nullable: false, comment: "所属门店。"),
Name = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false, comment: "规则名称。"),
StartTime = table.Column<TimeSpan>(type: "interval", nullable: false, comment: "开始时间。"),
EndTime = table.Column<TimeSpan>(type: "interval", nullable: false, comment: "结束时间。"),
WeekDaysMask = table.Column<int>(type: "integer", nullable: false, comment: "星期位掩码(周一到周日)。"),
IsEnabled = table.Column<bool>(type: "boolean", 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_schedules", x => x.Id);
},
comment: "商品时段规则(门店维度)。");
migrationBuilder.CreateIndex(
name: "IX_product_schedule_products_TenantId_StoreId_ProductId",
table: "product_schedule_products",
columns: new[] { "TenantId", "StoreId", "ProductId" });
migrationBuilder.CreateIndex(
name: "IX_product_schedule_products_TenantId_StoreId_ScheduleId_Produ~",
table: "product_schedule_products",
columns: new[] { "TenantId", "StoreId", "ScheduleId", "ProductId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_product_schedules_TenantId_StoreId_IsEnabled",
table: "product_schedules",
columns: new[] { "TenantId", "StoreId", "IsEnabled" });
migrationBuilder.CreateIndex(
name: "IX_product_schedules_TenantId_StoreId_Name",
table: "product_schedules",
columns: new[] { "TenantId", "StoreId", "Name" },
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "product_schedule_products");
migrationBuilder.DropTable(
name: "product_schedules");
}
}
}

View File

@@ -4709,6 +4709,144 @@ namespace TakeoutSaaS.Infrastructure.Migrations
});
});
modelBuilder.Entity("TakeoutSaaS.Domain.Products.Entities.ProductSchedule", 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<TimeSpan>("EndTime")
.HasColumnType("interval")
.HasComment("结束时间。");
b.Property<bool>("IsEnabled")
.HasColumnType("boolean")
.HasComment("是否启用。");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("character varying(64)")
.HasComment("规则名称。");
b.Property<TimeSpan>("StartTime")
.HasColumnType("interval")
.HasComment("开始时间。");
b.Property<long>("StoreId")
.HasColumnType("bigint")
.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.Property<int>("WeekDaysMask")
.HasColumnType("integer")
.HasComment("星期位掩码(周一到周日)。");
b.HasKey("Id");
b.HasIndex("TenantId", "StoreId", "IsEnabled");
b.HasIndex("TenantId", "StoreId", "Name")
.IsUnique();
b.ToTable("product_schedules", null, t =>
{
t.HasComment("商品时段规则(门店维度)。");
});
});
modelBuilder.Entity("TakeoutSaaS.Domain.Products.Entities.ProductScheduleProduct", 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<long>("ProductId")
.HasColumnType("bigint")
.HasComment("商品 ID。");
b.Property<long>("ScheduleId")
.HasColumnType("bigint")
.HasComment("时段规则 ID。");
b.Property<long>("StoreId")
.HasColumnType("bigint")
.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", "StoreId", "ProductId");
b.HasIndex("TenantId", "StoreId", "ScheduleId", "ProductId")
.IsUnique();
b.ToTable("product_schedule_products", null, t =>
{
t.HasComment("时段规则与商品关联关系。");
});
});
modelBuilder.Entity("TakeoutSaaS.Domain.Products.Entities.ProductSku", b =>
{
b.Property<long>("Id")