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,38 @@
|
||||
"""积分制切换 · 限额字段 ×10(与 billing/0004 同窗口)。
|
||||
|
||||
- TeamMember 三档限额:0=不限,0×10=0 语义不变;
|
||||
- Team.monthly_credit_limit:**-1 是「不限」哨兵,乘 10 变 -10 会破坏前端 === -1 判断**,
|
||||
必须 WHERE > 0 才乘(0 保持不动);NULL 自然跳过;
|
||||
- Invitation.monthly_credit_limit:join_team 邀请码会把它落到新成员限额,漏乘会发 1/10 限额。
|
||||
|
||||
写法纪律(review 修正):RunPython + atomic 防 MySQL 中断重放 ×100;反向除 10.0 防 sqlite 整数截断。
|
||||
详见 billing/0004_points_rescale.py 同款说明。
|
||||
"""
|
||||
from django.db import migrations, transaction
|
||||
|
||||
RESCALE_STATEMENTS = [
|
||||
"UPDATE accounts_teammember SET daily_credit_limit = daily_credit_limit * 10, monthly_credit_limit = monthly_credit_limit * 10, total_credit_limit = total_credit_limit * 10",
|
||||
"UPDATE accounts_team SET monthly_credit_limit = monthly_credit_limit * 10 WHERE monthly_credit_limit > 0",
|
||||
"UPDATE accounts_invitation SET monthly_credit_limit = monthly_credit_limit * 10",
|
||||
]
|
||||
|
||||
REVERSE_STATEMENTS = [
|
||||
"UPDATE accounts_teammember SET daily_credit_limit = daily_credit_limit / 10.0, monthly_credit_limit = monthly_credit_limit / 10.0, total_credit_limit = total_credit_limit / 10.0",
|
||||
"UPDATE accounts_team SET monthly_credit_limit = monthly_credit_limit / 10.0 WHERE monthly_credit_limit > 0",
|
||||
"UPDATE accounts_invitation SET monthly_credit_limit = monthly_credit_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 = [("accounts", "0007_team_monthly_credit_limit")]
|
||||
operations = [migrations.RunPython(_run(RESCALE_STATEMENTS), _run(REVERSE_STATEMENTS))]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.1.15 on 2026-07-03 07:41
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("accounts", "0008_points_rescale"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="team",
|
||||
name="price_multiplier",
|
||||
field=models.DecimalField(decimal_places=2, default=1, max_digits=5),
|
||||
),
|
||||
]
|
||||
@@ -35,6 +35,11 @@ class Team(TimeStampedModel):
|
||||
# 团队级月限额(超管在团队页设置,自然月重置)。语义三态,与成员级 0=不限 不同:
|
||||
# None = 未设置(前端按成员月度额度累加作团队月限额)· -1 = 不限 · >=0 = 固定上限。
|
||||
monthly_credit_limit = models.DecimalField(max_digits=12, decimal_places=2, null=True, blank=True, default=None)
|
||||
# 团队价格系数(差异化调价,jimeng 同款诉求):最终积分价 = 标准挂牌价 × 系数(HALF_UP,最低 1 积分)。
|
||||
# <1 = 大客户折扣,>1 = 渠道加价;全部计费类型统一生效;对团队侧静默(只体现在预估/实扣数字里)。
|
||||
# 视频类按实结算用**下单时的快照系数**(request_payload.price_multiplier),中途改价不影响在途任务。
|
||||
# 仅平台超管可改(admin 团队定价端点,0.10~10.00 边界)。
|
||||
price_multiplier = models.DecimalField(max_digits=5, decimal_places=2, default=1)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
@@ -54,9 +54,15 @@ class AuthApiTests(TestCase):
|
||||
account = CreditAccount.objects.get(team=team)
|
||||
self.assertEqual(account.balance, trial)
|
||||
genesis = CreditLedger.objects.filter(team=team, ledger_type=CreditLedger.Type.RECHARGE)
|
||||
self.assertEqual(genesis.count(), 1)
|
||||
self.assertEqual(genesis.first().amount, trial)
|
||||
self.assertEqual(genesis.first().balance_after, trial) # 流水终点 == 账户余额,可对账
|
||||
if trial > 0:
|
||||
# 若运营期把赠送额度调回 >0,genesis 凭证契约必须成立(I8)
|
||||
self.assertEqual(genesis.count(), 1)
|
||||
self.assertEqual(genesis.first().amount, trial)
|
||||
self.assertEqual(genesis.first().balance_after, trial) # 流水终点 == 账户余额,可对账
|
||||
else:
|
||||
# 商业决策(2026-07-03):开户不赠送 → 余额 0 且不落赠送流水(I8 隐含余额 0 天然满足)
|
||||
self.assertEqual(trial, Decimal("0"))
|
||||
self.assertEqual(genesis.count(), 0)
|
||||
|
||||
|
||||
class InvitationFlowTests(TestCase):
|
||||
@@ -150,7 +156,7 @@ class MemberQuotaAndRoleTests(TestCase):
|
||||
r = self._register(APIClient(), "solo-owner", team_name="Solo", invite_code=make_create_team_code())
|
||||
self.assertEqual(r.data.get("role"), "owner")
|
||||
|
||||
def test_create_member_defaults_limit_100(self):
|
||||
def test_create_member_defaults_limit_1000(self):
|
||||
res = self.owner_client.post(
|
||||
"/api/auth/team/members/",
|
||||
{"username": "sub-a", "password": "strong-password", "role": "member"},
|
||||
@@ -158,7 +164,8 @@ class MemberQuotaAndRoleTests(TestCase):
|
||||
)
|
||||
self.assertEqual(res.status_code, 201, res.content)
|
||||
member = TeamMember.objects.get(user__username="sub-a", team=self.team)
|
||||
self.assertEqual(member.monthly_credit_limit, Decimal("100"))
|
||||
# 积分制:默认月限额跟随 ×10(¥100 → 1000 积分),否则新成员限额实际缩水 10 倍
|
||||
self.assertEqual(member.monthly_credit_limit, Decimal("1000"))
|
||||
|
||||
def test_create_member_explicit_limit_respected(self):
|
||||
res = self.owner_client.post(
|
||||
|
||||
@@ -27,7 +27,7 @@ from .serializers import (
|
||||
|
||||
|
||||
# 子账号默认月度限额(0=不限,只留给主账号/超管)
|
||||
DEFAULT_MEMBER_MONTHLY_LIMIT = 100
|
||||
DEFAULT_MEMBER_MONTHLY_LIMIT = 1000
|
||||
|
||||
|
||||
def member_role(user, team):
|
||||
|
||||
Reference in New Issue
Block a user