diff --git a/Document/13_AppSeed说明.md b/Document/13_AppSeed说明.md index 8ab02e7..95e2d0e 100644 --- a/Document/13_AppSeed说明.md +++ b/Document/13_AppSeed说明.md @@ -1,52 +1,62 @@ # App 数据种子使用说明(App:Seed) - > 作用:在启动时自动创建默认租户与基础字典,便于本地/测试环境快速落地必备数据。由 `AppDataSeeder` 执行,支持幂等多次运行。 ## 配置入口 -- 文件位置:`appsettings.{Environment}.json`(示例已写入 AdminApi 的 Development 配置)。 -- 配置节:`App:Seed`。 +- 文件位置:`appsettings.Seed.{Environment}.json`(AdminApi 下新增独立种子文件,示例已写入 Development) +- 配置节:`App:Seed` -示例(已写入 `src/Api/TakeoutSaaS.AdminApi/appsettings.Development.json`): +示例(已写入 `src/Api/TakeoutSaaS.AdminApi/appsettings.Seed.Development.json`): ```json -"App": { - "Seed": { - "Enabled": true, - "DefaultTenant": { - "TenantId": 1000000000001, - "Code": "demo", - "Name": "Demo租户", - "ShortName": "Demo", - "ContactName": "DemoAdmin", - "ContactPhone": "13800000000" - }, - "DictionaryGroups": [ - { - "Code": "order_status", - "Name": "订单状态", - "Scope": "Business", - "Items": [ - { "Key": "pending", "Value": "待支付", "SortOrder": 10 }, - { "Key": "paid", "Value": "已支付", "SortOrder": 20 }, - { "Key": "finished", "Value": "已完成", "SortOrder": 30 } - ] - } - ] +{ + "App": { + "Seed": { + "Enabled": true, + "DefaultTenant": { + "TenantId": 1000000000001, + "Code": "demo", + "Name": "Demo租户", + "ShortName": "Demo", + "ContactName": "DemoAdmin", + "ContactPhone": "13800000000" + }, + "DictionaryGroups": [ + { + "Code": "order_status", + "Name": "订单状态", + "Scope": "Business", + "Items": [ + { "Key": "pending", "Value": "待支付", "SortOrder": 10 }, + { "Key": "paid", "Value": "已支付", "SortOrder": 20 }, + { "Key": "finished", "Value": "已完成", "SortOrder": 30 } + ] + }, + { + "Code": "store_tags", + "Name": "门店标签", + "Scope": "Business", + "Items": [ + { "Key": "hot", "Value": "热门", "SortOrder": 10 }, + { "Key": "new", "Value": "新店", "SortOrder": 20 } + ] + } + ] + } } } ``` -字段说明: -- `Enabled`: 是否启用种子。 -- `DefaultTenant`: 默认租户(使用雪花 long ID,0 表示让雪花生成)。 -- `DictionaryGroups`: 基础字典,`Scope` 可选 `System`/`Business`,`Items` 支持重复执行更新。 +## 字段说明 +- `Enabled`: 是否启用种子 +- `DefaultTenant`: 默认租户(使用雪花 long ID;0 表示让雪花生成) +- `DictionaryGroups`: 基础字典,`Scope` 可选 `System`/`Business`,`Items` 支持幂等运行更新 ## 运行方式 -1. 确保 Admin API 已调用 `AddAppInfrastructure`(已在 Program.cs 中注册,会启动 `AppDataSeeder`)。 -2. 修改 `appsettings.{Environment}.json` 的 `App:Seed` 后,启动 Admin API,即会自动执行种子逻辑(幂等)。 +1. 确保 Admin API 已调用 `AddAppInfrastructure`(Program.cs 已注册,会启用 `AppDataSeeder`)。 +2. 修改 `appsettings.Seed.{Environment}.json` 的 `App:Seed` 后,启动 Admin API,即会自动执行种子逻辑(幂等)。 3. 查看日志:`AppSeed` 前缀会输出创建/更新结果。 ## 注意事项 -- ID 必须为 long(雪花),不要再使用 Guid/自增。 +- ID 必须用 long(雪花),不要再使用 Guid/自增。 - 系统租户使用 `TenantId = 0`;业务租户请填写实际雪花 ID。 - 字典分组编码需唯一;重复运行会按编码合并更新。 -- 生产环境请按需开启 `Enabled`,避免误写入。 +- 生产环境请按需开启 `Enabled`,避免误写入。 diff --git a/src/Api/TakeoutSaaS.AdminApi/Program.cs b/src/Api/TakeoutSaaS.AdminApi/Program.cs index 14eb59d..1cdf937 100644 --- a/src/Api/TakeoutSaaS.AdminApi/Program.cs +++ b/src/Api/TakeoutSaaS.AdminApi/Program.cs @@ -25,6 +25,10 @@ using TakeoutSaaS.Shared.Web.Swagger; var builder = WebApplication.CreateBuilder(args); +builder.Configuration + .AddJsonFile("appsettings.Seed.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.Seed.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true); + builder.Host.UseSerilog((context, _, configuration) => { configuration diff --git a/src/Api/TakeoutSaaS.AdminApi/appsettings.Development.json b/src/Api/TakeoutSaaS.AdminApi/appsettings.Development.json index 01c5725..4eb2c0e 100644 --- a/src/Api/TakeoutSaaS.AdminApi/appsettings.Development.json +++ b/src/Api/TakeoutSaaS.AdminApi/appsettings.Development.json @@ -149,39 +149,5 @@ "WorkerCount": 5, "DashboardEnabled": false, "DashboardPath": "/hangfire" - }, - "App": { - "Seed": { - "Enabled": true, - "DefaultTenant": { - "TenantId": 1000000000001, - "Code": "demo", - "Name": "Demo租户", - "ShortName": "Demo", - "ContactName": "DemoAdmin", - "ContactPhone": "13800000000" - }, - "DictionaryGroups": [ - { - "Code": "order_status", - "Name": "订单状态", - "Scope": "Business", - "Items": [ - { "Key": "pending", "Value": "待支付", "SortOrder": 10 }, - { "Key": "paid", "Value": "已支付", "SortOrder": 20 }, - { "Key": "finished", "Value": "已完成", "SortOrder": 30 } - ] - }, - { - "Code": "store_tags", - "Name": "门店标签", - "Scope": "Business", - "Items": [ - { "Key": "hot", "Value": "热门", "SortOrder": 10 }, - { "Key": "new", "Value": "新店", "SortOrder": 20 } - ] - } - ] - } } } diff --git a/src/Api/TakeoutSaaS.AdminApi/appsettings.Seed.Development.json b/src/Api/TakeoutSaaS.AdminApi/appsettings.Seed.Development.json new file mode 100644 index 0000000..4a20620 --- /dev/null +++ b/src/Api/TakeoutSaaS.AdminApi/appsettings.Seed.Development.json @@ -0,0 +1,36 @@ +{ + "App": { + "Seed": { + "Enabled": true, + "DefaultTenant": { + "TenantId": 1000000000001, + "Code": "demo", + "Name": "Demo租户", + "ShortName": "Demo", + "ContactName": "DemoAdmin", + "ContactPhone": "13800000000" + }, + "DictionaryGroups": [ + { + "Code": "order_status", + "Name": "订单状态", + "Scope": "Business", + "Items": [ + { "Key": "pending", "Value": "待支付", "SortOrder": 10 }, + { "Key": "paid", "Value": "已支付", "SortOrder": 20 }, + { "Key": "finished", "Value": "已完成", "SortOrder": 30 } + ] + }, + { + "Code": "store_tags", + "Name": "门店标签", + "Scope": "Business", + "Items": [ + { "Key": "hot", "Value": "热门", "SortOrder": 10 }, + { "Key": "new", "Value": "新店", "SortOrder": 20 } + ] + } + ] + } + } +}