Files
video-gen/video-gen-api/app/services/llm_billing/config.py
T
2026-08-11 09:24:18 +08:00

53 lines
1.6 KiB
Python

from __future__ import annotations
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.llm_billing.policy import LlmBillingPolicyModel
from app.services.llm_billing.context import LlmBillingPolicy
async def get_llm_billing_policy(
db: AsyncSession,
*,
scene_code: str | None = None,
) -> LlmBillingPolicy:
"""按业务场景读取固定预扣;不再读取旧 SystemConfig 冻结配置。"""
resolved_scene = scene_code
if not resolved_scene:
return LlmBillingPolicy(
pre_deduct_credits=0.0,
valid=False,
error="LLM业务场景不能为空",
)
result = await db.execute(
select(LlmBillingPolicyModel)
.where(LlmBillingPolicyModel.scene_code == resolved_scene)
.limit(1)
)
model = result.scalar_one_or_none()
if model is None:
return LlmBillingPolicy(
pre_deduct_credits=0.0,
valid=False,
error=f"未配置LLM场景固定预扣积分:{resolved_scene}",
)
amount = float(model.pre_deduct_credits)
if not model.is_active or amount <= 0:
return LlmBillingPolicy(
pre_deduct_credits=amount,
valid=False,
error=f"LLM场景固定预扣配置未启用或金额无效:{resolved_scene}",
policy_id=model.id,
version=model.version,
scene_name=model.scene_name,
)
return LlmBillingPolicy(
pre_deduct_credits=round(amount, 2),
valid=True,
policy_id=model.id,
version=model.version,
scene_name=model.scene_name,
)