fix: _get_exchange_rates/_load_usage_log_bills env取sor.env,无则ServerEnv()兜底

This commit is contained in:
yumoqing 2026-07-21 17:52:18 +08:00
parent 5030e0640c
commit 33447fbbe5

View File

@ -7,6 +7,8 @@ from datetime import datetime, timedelta
from appPublic.uniqueID import getID
from appPublic.dictObject import DictObject
from appPublic.log import info
from ahserver.serverenv import ServerEnv
from sqlor.dbpools import get_sor_context
MODULE_NAME = "sage_datamart"
@ -113,13 +115,11 @@ async def sync_call_fact(sor, last_sync=None):
async def _load_usage_log_bills(sor, last_sync):
"""从 product_usage_log 预取与 llmusage 关联的实际计费记录。
product_usage_log 通过 source_ref_table='llmusage' + source_ref_id=<luid> 关联 llmusage
返回 {llmusage.id: {'sell_price': float, 'purchase_currency': str|None}, ...}
"""
"""从 product_usage_log 预取与 llmusage 关联的实际计费记录。"""
env = sor.env if hasattr(sor, 'env') and sor.env is not None else ServerEnv()
usage_log_map = {}
try:
async with get_sor_context(env, 'product_management') as pm_sor:
sql = """
SELECT pul.source_ref_id,
pul.sell_price,
@ -129,7 +129,7 @@ async def _load_usage_log_bills(sor, last_sync):
WHERE pul.source_ref_table = 'llmusage'
AND pul.use_time >= ${last_sync}$
"""
rows = await sor.sqlExe(sql, {'last_sync': last_sync})
rows = await pm_sor.sqlExe(sql, {'last_sync': last_sync})
for r in rows:
ref_id = r.source_ref_id if hasattr(r, 'source_ref_id') else r['source_ref_id']
sell_price = r.sell_price if hasattr(r, 'sell_price') else r['sell_price']
@ -146,12 +146,14 @@ async def _load_usage_log_bills(sor, last_sync):
async def _get_exchange_rates(sor, stat_date):
"""查询汇率表,返回 {currency: mid_rate} 字典"""
env = sor.env if hasattr(sor, 'env') and sor.env is not None else ServerEnv()
rates = {'CNY': 1.0}
try:
async with get_sor_context(env, 'accounting') as acc_sor:
sql = """SELECT from_currency, mid_rate FROM exchange_rate
WHERE to_currency = 'CNY' AND effective_date <= ${d}$
ORDER BY effective_date DESC"""
rows = await sor.sqlExe(sql, {'d': stat_date})
rows = await acc_sor.sqlExe(sql, {'d': stat_date})
for r in rows:
cur = r.from_currency if hasattr(r, 'from_currency') else r['from_currency']
rate = r.mid_rate if hasattr(r, 'mid_rate') else r['mid_rate']