feat: credit-based billing system

- New DB models: credit_packages, subscription_plans, user_credits, credit_consumptions, credit_purchases
- CreditService: balance, deduct, add_credits, grant_free_trial, history
- User API: /api/v1/credits/* (balance/history/packages/purchase/subscribe)
- Admin API: /api/v1/admin/credit-* (CRUD packages/plans, user credits, consumptions)
- PaymentService.create_credit_order + handle_callback for credit purchases
- Credit deduction on: discovery, translate, marketing, ai_chat, followup
- Free trial 30 credits on registration
- Documentation: docs/CREDIT_SYSTEM.md
This commit is contained in:
TradeMate Dev
2026-06-12 10:39:45 +08:00
parent 5d895ae12c
commit 2a107a42f3
21 changed files with 1528 additions and 33 deletions
+9
View File
@@ -3,6 +3,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from typing import Optional
from app.database import get_db
from app.services.followup_engine import FollowupEngine
from app.services.credit import CreditService
from app.api.v1.deps import get_current_user_id
router = APIRouter()
@@ -84,6 +85,14 @@ async def trigger_followup_scan(
user_id: str = Depends(get_current_user_id),
db: AsyncSession = Depends(get_db),
):
credit_svc = CreditService(db)
ok, balance = await credit_svc.deduct(user_id, "followup_scan")
if not ok:
raise HTTPException(
status_code=402,
detail=f"次数不足 (剩余 {balance:.1f}, 需要 2)"
)
engine = FollowupEngine(db)
result = await engine.scan_and_followup()
return result