Repository navigation
feat(billing): margin guard — verified channel costs, save-time margin check, daily margin report - #10
Merged
Conversation
…n check, daily margin report
Author
|
维护者评审通过(2026-10-21):
AI 辅助实现,维护者已核对。 |
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
Note
本 PR 的代码由 AI 辅助生成(提交者非仓库历史核心开发者),已按仓库约定声明;描述为人工整理,请审计者重点核对下方「审计注意点」。
📝 变更描述 / Description
「管理员后台可操作商品配置」Phase 2:真防亏本三件套。
1. 渠道成本人工核验留痕
channels表新增cost_verified_at bigint/cost_verified_by int两列(走 GORM AutoMigrate,与upstream_costs列的历史迁移模式一致,三库均为 ADD COLUMN)。upstream_costs与现值不同,自动写入当前时间戳与操作人 id;同值重存不动 stamp。channelReadOnlyFields并在clearChannelReadOnlyFields中清零,客户端无法伪造。2. 保存时毛利校验 + 下单复检
validateV1ContractTx末尾新增共用校验validatePlanMarginTx:仅对 enabled plan 且模板min_margin > 0生效,逐模型逐分量检查cost ≤ sale × (1 − min_margin/100)(epsilon 1e-9,恰好等于地板通过),违反时聚合所有分量报错(含模型与分量明细)。validateV1ProductPriceCatalogTx保证价格行存在且启用,因此下单复检天然严格,未在createSubscriptionOrderTx重复调用)。migrateProductTemplateMinMargins()挂入MigrateProductTemplates():monthly / pack_ds_glm / pack_qwen / pack_k3 / classroom / bigfish 六个 strict 模板的min_margin通过MigrateLockedProductTemplate收敛为 10(幂等自愈),wallet 保持 0。已验算全部真实 seed 价格在 10% 地板下通过。3. 运行时成本超限告警 + 每日毛利报表
channel_select两处过滤点:渠道 enabled 且在分组允许集内、但因成本超限(ChannelSatisfiesPolicy失败)被排除时,common.SysError告警,含 product_type / 模型 / 渠道 id 及 cost vs ceiling 分量明细;按product_type|model|channelId节流 1 小时。margin_reportSystemTask(仿billing_refund_task.go):每 24h 流式聚合 consume logs,按other.product_type分桶,用渠道当前upstream_costs估算成本 vs 收入;任一模板 floor > 0 的 product_type 毛利低于地板时逐条 SysError 并 NotifyUser root。精度边界(历史成本漂移、退款、无成本数据的请求偏向上报漏报而非误报)已在RunMarginReport文档注释中说明。🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
✅ 提交前检查项 / Checklist
Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。📸 运行证明 / Proof of Work
本地验收全部通过(SQLite;postgres 由 CI postgres-accounting 覆盖,新测试模式已加入该 job 三个 package 的正则并集):
新增测试:
model/margin_guard_test.go— TestMarginGuard*(低于地板拒绝 / 恰等地板通过 / 零地板模板跳过 / disabled plan 跳过 / 下单复检拒薄利 / 模板迁移幂等)、TestChannelCost*(新列存在 / 成本超限告警节流与触发条件)service/margin_report_task_test.go— TestMarginReportAggregatesAndAlertsOnBreach、TestMarginReportHealthyMarginPassescontroller/channel_cost_verified_test.go— TestChannelCostVerifiedStampedOnUpstreamCostsUpdate(stamp 写入 / 伪造被忽略 / 同值重存不动 stamp / 真变更动 stamp)🔍 审计注意点(请审计者重点核对)
MigrateProductTemplates会把六个 strict 模板的min_margin收敛为 10。若线上既有 enabled 商品的价格行毛利不足 10%,其保存/下单将开始被拒——这是本特性意图,但请先核对线上 pack 类价格行的 selling vs cost。upstream_costs历史模式一致),未使用 PRAGMA 特殊路径。validateV1ContractTx内共用函数,createSubscriptionOrderTx原本即调用该校验,未重复添加调用点。provisionPlanFromOrder(订单完成供货)会带livePlan.Enabled重跑校验;价格行不可变故正常不翻转,但若模板地板在下单与支付回调之间被迁移调高,理论上会卡住已支付订单的供货。知悉即可。margin_report为粗粒度估算,精度边界见RunMarginReport注释。