fix: 修复套餐新增草稿默认值与开关落库

This commit is contained in:
2025-12-15 21:06:23 +08:00
parent 17a697b9c8
commit fd377624d8
5 changed files with 6831 additions and 5 deletions

View File

@@ -81,7 +81,7 @@ public sealed class TenantPackage : AuditableEntityBase
/// <summary>
/// 发布状态(草稿/已发布)。
/// </summary>
public TenantPackagePublishStatus PublishStatus { get; set; } = TenantPackagePublishStatus.Published;
public TenantPackagePublishStatus PublishStatus { get; set; } = TenantPackagePublishStatus.Draft;
/// <summary>
/// 展示排序,数值越小越靠前。

View File

@@ -677,9 +677,21 @@ public sealed class TakeoutAppDbContext(
builder.Property(x => x.Name).HasMaxLength(128).IsRequired();
builder.Property(x => x.Description).HasMaxLength(512);
builder.Property(x => x.FeaturePoliciesJson).HasColumnType("text");
builder.Property(x => x.PublishStatus).HasConversion<int>().HasDefaultValue(TenantPackagePublishStatus.Published).HasComment("发布状态0=草稿1=已发布。");
builder.Property(x => x.IsPublicVisible).HasDefaultValue(true).HasComment("是否对外可见(展示页/套餐列表可见性)。");
builder.Property(x => x.IsAllowNewTenantPurchase).HasDefaultValue(true).HasComment("是否允许新租户购买/选择(仅影响新购)。");
builder.Property(x => x.PublishStatus)
.HasConversion<int>()
.HasDefaultValue(TenantPackagePublishStatus.Draft)
.HasComment("发布状态0=草稿1=已发布。");
// 1. 解决 EF Core 默认值哨兵问题:当我们希望插入 false/0 时,若数据库配置了 default 且 EF 认为该值是“未设置”,会导致 insert 省略列,最终落库为默认值。
// 2. (空行后) 将哨兵值设置为数据库默认值true 作为哨兵false 才会被显式写入,从而保证“可见性/可售开关”在新增时可正确落库。
builder.Property(x => x.IsPublicVisible)
.HasDefaultValue(true)
.HasSentinel(true)
.HasComment("是否对外可见(展示页/套餐列表可见性)。");
builder.Property(x => x.IsAllowNewTenantPurchase)
.HasDefaultValue(true)
.HasSentinel(true)
.HasComment("是否允许新租户购买/选择(仅影响新购)。");
builder.Property(x => x.SortOrder).HasDefaultValue(0).HasComment("展示排序,数值越小越靠前。");
builder.HasIndex(x => new { x.IsActive, x.SortOrder });
builder.HasIndex(x => new { x.PublishStatus, x.IsActive, x.IsPublicVisible, x.IsAllowNewTenantPurchase, x.SortOrder });

View File

@@ -0,0 +1,42 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace TakeoutSaaS.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class ChangeTenantPackageDefaults : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<int>(
name: "PublishStatus",
table: "tenant_packages",
type: "integer",
nullable: false,
defaultValue: 0,
comment: "发布状态0=草稿1=已发布。",
oldClrType: typeof(int),
oldType: "integer",
oldDefaultValue: 1,
oldComment: "发布状态0=草稿1=已发布。");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<int>(
name: "PublishStatus",
table: "tenant_packages",
type: "integer",
nullable: false,
defaultValue: 1,
comment: "发布状态0=草稿1=已发布。",
oldClrType: typeof(int),
oldType: "integer",
oldDefaultValue: 0,
oldComment: "发布状态0=草稿1=已发布。");
}
}
}

View File

@@ -6298,7 +6298,7 @@ namespace TakeoutSaaS.Infrastructure.Migrations
b.Property<int>("PublishStatus")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasDefaultValue(1)
.HasDefaultValue(0)
.HasComment("发布状态0=草稿1=已发布。");
b.Property<int>("SortOrder")