Repository navigation
feat(billing): DB-driven product templates replace hardcoded contract constants - #9
Merged
Merged
Conversation
Replace the hardcoded frozen contract numbers inside ValidateV1Contract with the new product_templates table: seven seeded core templates (wallet, monthly, ds_glm/qwen/k3 packs, classroom, bigfish) now carry duration, multiplier, model/channel scope, purchase limits and price rules. Seed is insert-only and idempotent so operational edits survive restarts; locked templates can only change through the migration-only write path. subscription_plans gains template_id, pinned during validation and backfilled for existing plans.
The service package TestMain builds its own schema; add ProductTemplate to its AutoMigrate list and seed the core templates so V1 plan validation (billing_pool_isolation e2e) resolves its template row.
Author
|
维护者评审通过(2026-10-21):
AI 辅助实现,维护者已核对。合并后触发生产发布管线(平台侧需 repin 子模块,另起 PR)。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Important
本 PR 为 AI 辅助生成(Kimi Code),已经过人工要求的设计评审点核对;请审计者重点复核
model/cilai_contract.go校验规则与模板 seed 数值的等价性。📝 变更描述 / Description
把
ValidateV1Contract里的硬编码"冻结契约"下沉为数据库驱动的product_templates表,这是"管理员后台可操作商品配置"改造的第一阶段(模板编辑 UI 与毛利校验在后续阶段)。product_templates表(三库兼容:SQLite/MySQL/PostgreSQL,注册进migrateDB/migrateDBFast两个 AutoMigrate 清单):product_type 唯一索引、时长、plan 级倍率、模型集合、渠道绑定、成本上限快照、限购、支付/兑换/课程开关、price_rules定价项列表(月付 tier→价格、bigfish code→价格、classroom 固定价)、family/price_version 默认值、min_margin_percent(Phase 2 预留)、display_metadata(Phase 4 预留)、locked标记。SeedProductTemplates()把 7 个核心模板(wallet/monthly/pack_ds_glm/pack_qwen/pack_k3/classroom/bigfish)按现有常量原样落库:只在模板不存在时 INSERT,绝不覆盖运营期修改;在MigrateCilaiV1Contracts()之后随主迁移执行。bigfish 模板从validateBigfishPlan规则反推。硬编码常量(frozenV1BillingMultipliers、月付 tier 价格、bigfish code→价格等)全部保留为 seed 定义来源。ValidateV1Contract保持函数签名与规则语义不变,数值来源改为模板行:空/legacy product_type 放行不变;模板缺失报"未知产品类型"错误;价格从等值比较改为必须命中模板price_rules对应 code/tier 条目(严格度不变);classroom/bigfish 的模型集合与渠道精确绑定、bigfish 的限购/禁兑换/禁余额支付/禁分组变更均从模板字段校验。subscription_plans新增template_id bigint列(SQLite 走 PRAGMA+ADD COLUMN 模式,MySQL/PG 走 AutoMigrate);plan 校验时自动钉住并校验与 product_type 一致;迁移按 product_type 回填存量 plan。locked=true的核心模板行走独立的MigrateLockedProductTemplate高权限路径,普通运营写路径UpdateProductTemplate拒绝修改 locked 模板(管理端 API 与 UI 属 Phase 3,本阶段只保证模型层完整)。validateV1ContractTx(tx),模板查询复用事务连接,避免 SQLite 单连接下的池死锁。🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
min_margin_percent(Phase 2)、前台展示元数据display_metadata(Phase 4)。✅ 提交前检查项 / Checklist
📸 运行证明 / Proof of Work
新增测试覆盖:seed 幂等且不覆盖手改(
TestSeedProductTemplatesIsIdempotentAndPreservesOperatorEdits)、seed 定义与冻结常量一致性(TestProductTemplateSeedDefinitionsMatchFrozenContractConstants)、既有商品(月付三档/classroom/bigfish 三档)全部通过新校验、模板缺失/价格不在 price_rules/倍率漂移均拒绝、template_id 钉住/不一致拒绝/回填、locked 模板仅迁移路径可改。存量TestValidateV1ContractRejectsFrozenRuleDrift语义保持不变并继续通过。