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:
@@ -1,6 +1,6 @@
|
||||
"""平台超管后台 · 跨团队端点。所有视图统一挂 IsPlatformAdmin,非超管一律 403,写操作记审计。"""
|
||||
|
||||
from decimal import Decimal
|
||||
from decimal import Decimal, ROUND_HALF_UP
|
||||
|
||||
from django.db.models import Count, F, Q
|
||||
from rest_framework import status
|
||||
@@ -15,7 +15,8 @@ from apps.accounts.serializers import InvitationSerializer
|
||||
from apps.ai.models import AITask, ModelConfig, ModelProvider, PromptTemplate, QualityWord
|
||||
from apps.assets.models import Asset
|
||||
from apps.assets.review import poll_asset_review, submit_asset_for_review
|
||||
from apps.billing.models import CreditLedger, QuotaPolicy
|
||||
from apps.billing.models import BillingConfig, CreditLedger, QuotaPolicy
|
||||
from apps.billing.pricing import get_billing_config, invalidate_billing_config_cache
|
||||
from apps.billing.services.ledger import adjust_credit
|
||||
from apps.common.pagination import DefaultPagination
|
||||
from apps.products.models import Product
|
||||
@@ -160,6 +161,38 @@ def admin_team_toggle(request, team_id):
|
||||
# ─────────────────────────── 用户管理 ───────────────────────────
|
||||
|
||||
|
||||
@api_view(["POST"])
|
||||
@permission_classes([IsPlatformAdmin])
|
||||
def admin_team_pricing(request, team_id):
|
||||
"""团队差异化调价(jimeng 同款诉求):设置团队价格系数(0.10~10.00)。
|
||||
最终积分价 = 挂牌价 × 系数;全部计费类型统一生效;视频在途任务用下单快照不受影响。"""
|
||||
from decimal import InvalidOperation
|
||||
|
||||
team = Team.objects.filter(id=team_id).first()
|
||||
if team is None:
|
||||
return Response({"detail": "not found"}, status=status.HTTP_404_NOT_FOUND)
|
||||
try:
|
||||
value = Decimal(str(request.data.get("price_multiplier")))
|
||||
except (InvalidOperation, TypeError, ValueError):
|
||||
return Response({"price_multiplier": ["数值格式不正确"]}, status=status.HTTP_400_BAD_REQUEST)
|
||||
if not value.is_finite() or value < Decimal("0.10") or value > Decimal("10"):
|
||||
return Response({"price_multiplier": ["系数需在 0.10 ~ 10.00 之间"]}, status=status.HTTP_400_BAD_REQUEST)
|
||||
before = str(team.price_multiplier)
|
||||
# 显式 HALF_UP:quantize 默认银行家舍入(0.125→0.12),与全仓取整纪律不一致(review 确认)
|
||||
team.price_multiplier = value.quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
|
||||
team.save(update_fields=["price_multiplier", "updated_at"])
|
||||
log_admin_action(
|
||||
request,
|
||||
"team.pricing_update",
|
||||
target_type="team",
|
||||
target_id=team.id,
|
||||
target_name=team.name,
|
||||
before={"price_multiplier": before},
|
||||
after={"price_multiplier": str(team.price_multiplier)},
|
||||
)
|
||||
return Response(AdminTeamSerializer(_team_qs().get(id=team.id)).data)
|
||||
|
||||
|
||||
@api_view(["GET"])
|
||||
@permission_classes([IsPlatformAdmin])
|
||||
def admin_users(request):
|
||||
@@ -496,6 +529,50 @@ def admin_ledger_adjust(request):
|
||||
return Response(AdminLedgerSerializer(ledger).data, status=status.HTTP_201_CREATED)
|
||||
|
||||
|
||||
def _billing_config_payload(cfg: BillingConfig) -> dict:
|
||||
return {
|
||||
"points_per_yuan": str(cfg.points_per_yuan),
|
||||
"video_margin_multiplier": str(cfg.video_margin_multiplier),
|
||||
"video_reserve_buffer": str(cfg.video_reserve_buffer),
|
||||
"updated_at": cfg.updated_at,
|
||||
}
|
||||
|
||||
|
||||
@api_view(["GET", "PATCH"])
|
||||
@permission_classes([IsPlatformAdmin])
|
||||
def admin_billing_config(request):
|
||||
"""平台计费配置(积分汇率/视频毛利系数/预留 buffer)。PATCH 即刻生效于下一次估价/结算。"""
|
||||
from decimal import InvalidOperation
|
||||
|
||||
cfg = get_billing_config()
|
||||
if request.method == "GET":
|
||||
return Response(_billing_config_payload(cfg))
|
||||
before = _billing_config_payload(cfg)
|
||||
updates: dict = {}
|
||||
for field, minimum in (("points_per_yuan", Decimal("0.01")), ("video_margin_multiplier", Decimal("0.01")), ("video_reserve_buffer", Decimal("1"))):
|
||||
if request.data.get(field) is None:
|
||||
continue
|
||||
try:
|
||||
value = Decimal(str(request.data[field]))
|
||||
except (InvalidOperation, TypeError, ValueError):
|
||||
return Response({field: ["数值格式不正确"]}, status=status.HTTP_400_BAD_REQUEST)
|
||||
# Decimal("NaN") 构造不抛,比较才抛 InvalidOperation → 500(review 确认),显式拦
|
||||
if not value.is_finite():
|
||||
return Response({field: ["数值格式不正确"]}, status=status.HTTP_400_BAD_REQUEST)
|
||||
if value < minimum:
|
||||
return Response({field: [f"不能小于 {minimum}"]}, status=status.HTTP_400_BAD_REQUEST)
|
||||
updates[field] = value
|
||||
if not updates:
|
||||
return Response({"detail": "没有可更新的字段"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
for field, value in updates.items():
|
||||
setattr(cfg, field, value)
|
||||
cfg.save(update_fields=[*updates.keys(), "updated_at"])
|
||||
invalidate_billing_config_cache()
|
||||
after = _billing_config_payload(cfg)
|
||||
log_admin_action(request, "billing_config.update", target_type="billing_config", target_id=cfg.id, before=before, after=after)
|
||||
return Response(after)
|
||||
|
||||
|
||||
@api_view(["GET", "POST"])
|
||||
@permission_classes([IsPlatformAdmin])
|
||||
def admin_quota_policies(request):
|
||||
|
||||
Reference in New Issue
Block a user