feat(billing): 计费商业化重构(积分制)+ 团队差异化调价
统一计价引擎 apps/billing/pricing.py:1积分=¥0.1,平台成本(¥)与用户价(积分)双记账, 视频按火山真实 usage.total_tokens 结算(true-up,终结¥1/段倒贴)+ 首发×1.5毛利系数。 Team.price_multiplier 差异化调价(jimeng同款):挂牌价×系数两步HALF_UP取整,视频类 按下单时的价格/汇率快照结算(中途改配不影响在途任务)。 BillingConfig 单例(汇率/毛利系数/预留buffer,admin可调即刻生效)。存量数据 ×10 rescale 迁移(RunPython+atomic,MySQL 迁移不可中断重跑)。开户赠送归零(DEFAULT_TRIAL_CREDITS=0, 商业决策)。audit_billing 加 I9(卖亏审计)。271 条测试 + tsc/build 全绿。 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
# Generated by Django 5.1.15 on 2026-07-03 03:08
|
||||
|
||||
import uuid
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("billing", "0002_remove_creditledger_billing_cre_team_id_e0f18f_idx_and_more"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="BillingConfig",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.UUIDField(
|
||||
default=uuid.uuid4,
|
||||
editable=False,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
),
|
||||
),
|
||||
("created_at", models.DateTimeField(auto_now_add=True)),
|
||||
("updated_at", models.DateTimeField(auto_now=True)),
|
||||
(
|
||||
"points_per_yuan",
|
||||
models.DecimalField(decimal_places=2, default=10, max_digits=8),
|
||||
),
|
||||
(
|
||||
"video_margin_multiplier",
|
||||
models.DecimalField(decimal_places=2, default=1.5, max_digits=6),
|
||||
),
|
||||
(
|
||||
"video_reserve_buffer",
|
||||
models.DecimalField(decimal_places=2, default=1.1, max_digits=4),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"abstract": False,
|
||||
},
|
||||
),
|
||||
# seed 默认配置行(幂等):1¥=10积分 · 视频毛利×1.5(首发,商业决策) · 预留 buffer×1.10
|
||||
migrations.RunPython(
|
||||
lambda apps, schema_editor: apps.get_model("billing", "BillingConfig").objects.exists()
|
||||
or apps.get_model("billing", "BillingConfig").objects.create(),
|
||||
migrations.RunPython.noop,
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,44 @@
|
||||
"""积分制切换 · 账本全量 ×10 线性缩放(1 积分 = ¥0.1,points_per_yuan=10)。
|
||||
|
||||
全部金额列同乘 10:线性变换保持 audit_billing I1~I8 全部不变量。
|
||||
**不缩放**:CreditLedger.metadata 内的 paid_amount/bonus(真实支付¥,JSON 列不受影响)。
|
||||
|
||||
写法纪律(review 修正):
|
||||
· 用 RunPython + transaction.atomic 而非裸 RunSQL —— MySQL 的迁移不包事务
|
||||
(can_rollback_ddl=False),裸 RunSQL 逐条 autocommit,中途被杀后 django_migrations
|
||||
未记录 → 重跑会把已乘过的表再乘 10(×100 错账)。atomic 包住纯 DML(InnoDB 可回滚)
|
||||
后要么全做并记录、要么全回滚,重跑安全。
|
||||
· 反向除法用 10.0 —— sqlite NUMERIC 亲和下整数值 `/ 10` 是整数除法,回滚会静默截断。
|
||||
|
||||
⚠️ 部署纪律:必须与积分制新代码同一次停机窗口上线(短停机一段式)。
|
||||
"""
|
||||
from django.db import migrations, transaction
|
||||
|
||||
RESCALE_STATEMENTS = [
|
||||
"UPDATE billing_creditaccount SET balance = balance * 10, reserved_balance = reserved_balance * 10",
|
||||
"UPDATE billing_creditledger SET amount = amount * 10, balance_after = balance_after * 10",
|
||||
"UPDATE billing_creditreservation SET amount = amount * 10",
|
||||
"UPDATE billing_quotapolicy SET monthly_limit = monthly_limit * 10, project_limit = project_limit * 10, per_task_limit = per_task_limit * 10",
|
||||
]
|
||||
|
||||
REVERSE_STATEMENTS = [
|
||||
"UPDATE billing_creditaccount SET balance = balance / 10.0, reserved_balance = reserved_balance / 10.0",
|
||||
"UPDATE billing_creditledger SET amount = amount / 10.0, balance_after = balance_after / 10.0",
|
||||
"UPDATE billing_creditreservation SET amount = amount / 10.0",
|
||||
"UPDATE billing_quotapolicy SET monthly_limit = monthly_limit / 10.0, project_limit = project_limit / 10.0, per_task_limit = per_task_limit / 10.0",
|
||||
]
|
||||
|
||||
|
||||
def _run(statements):
|
||||
def apply(apps, schema_editor):
|
||||
with transaction.atomic(using=schema_editor.connection.alias):
|
||||
with schema_editor.connection.cursor() as cursor:
|
||||
for sql in statements:
|
||||
cursor.execute(sql)
|
||||
|
||||
return apply
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [("billing", "0003_billingconfig")]
|
||||
operations = [migrations.RunPython(_run(RESCALE_STATEMENTS), _run(REVERSE_STATEMENTS))]
|
||||
Reference in New Issue
Block a user