Files
video-gen/video-gen-api/app/services/llm_billing/config.py
T
2026-08-12 11:49:53 +08:00

53 lines
1.7 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(
charge_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(
charge_credits=0.0,
valid=False,
error=f"未配置LLM场景消费积分:{resolved_scene}",
)
# 数据库物理字段 pre_deduct_credits 为历史命名,本轮不迁移字段;业务语义统一为 charge_credits。
amount = float(model.pre_deduct_credits)
if not model.is_active or amount <= 0:
return LlmBillingPolicy(
charge_credits=amount,
valid=False,
error=f"LLM场景积分配置未启用或金额无效:{resolved_scene}",
policy_id=model.id,
version=model.version,
scene_name=model.scene_name,
)
return LlmBillingPolicy(
charge_credits=round(amount, 2),
valid=True,
policy_id=model.id,
version=model.version,
scene_name=model.scene_name,
)