fix: 去掉get_sor_context包装,sor已传入直接用

This commit is contained in:
yumoqing 2026-07-21 17:54:48 +08:00
parent 33447fbbe5
commit abc457d547

View File

@ -7,8 +7,6 @@ from datetime import datetime, timedelta
from appPublic.uniqueID import getID from appPublic.uniqueID import getID
from appPublic.dictObject import DictObject from appPublic.dictObject import DictObject
from appPublic.log import info from appPublic.log import info
from ahserver.serverenv import ServerEnv
from sqlor.dbpools import get_sor_context
MODULE_NAME = "sage_datamart" MODULE_NAME = "sage_datamart"
@ -115,50 +113,44 @@ async def sync_call_fact(sor, last_sync=None):
async def _load_usage_log_bills(sor, last_sync): async def _load_usage_log_bills(sor, last_sync):
"""从 product_usage_log 预取与 llmusage 关联的实际计费记录。""" """从 product_usage_log 预取实际计费金额"""
env = sor.env if hasattr(sor, 'env') and sor.env is not None else ServerEnv()
usage_log_map = {} usage_log_map = {}
try: try:
async with get_sor_context(env, 'product_management') as pm_sor: sql = """
sql = """ SELECT pul.source_ref_id, pul.sell_price, ps.purchase_currency
SELECT pul.source_ref_id, FROM product_usage_log pul
pul.sell_price, LEFT JOIN product_subscription ps ON ps.id = pul.subscription_id
ps.purchase_currency WHERE pul.source_ref_table = 'llmusage'
FROM product_usage_log pul AND pul.use_time >= ${last_sync}$
LEFT JOIN product_subscription ps ON ps.id = pul.subscription_id """
WHERE pul.source_ref_table = 'llmusage' rows = await sor.sqlExe(sql, {'last_sync': last_sync})
AND pul.use_time >= ${last_sync}$ for r in rows:
""" ref_id = r.source_ref_id if hasattr(r, 'source_ref_id') else r['source_ref_id']
rows = await pm_sor.sqlExe(sql, {'last_sync': last_sync}) sell_price = r.sell_price if hasattr(r, 'sell_price') else r['sell_price']
for r in rows: purchase_currency = r.purchase_currency if hasattr(r, 'purchase_currency') else r.get('purchase_currency')
ref_id = r.source_ref_id if hasattr(r, 'source_ref_id') else r['source_ref_id'] if ref_id:
sell_price = r.sell_price if hasattr(r, 'sell_price') else r['sell_price'] usage_log_map[ref_id] = {
purchase_currency = r.purchase_currency if hasattr(r, 'purchase_currency') else r.get('purchase_currency') 'sell_price': float(sell_price) if sell_price else 0.0,
if ref_id: 'purchase_currency': purchase_currency,
usage_log_map[ref_id] = { }
'sell_price': float(sell_price) if sell_price else 0.0,
'purchase_currency': purchase_currency,
}
except Exception as e: except Exception as e:
info('sage_datamart: product_usage_log lookup failed: ' + str(e)) info('sage_datamart: product_usage_log lookup failed: ' + str(e))
return usage_log_map return usage_log_map
async def _get_exchange_rates(sor, stat_date): 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} rates = {'CNY': 1.0}
try: try:
async with get_sor_context(env, 'accounting') as acc_sor: sql = """SELECT from_currency, mid_rate FROM exchange_rate
sql = """SELECT from_currency, mid_rate FROM exchange_rate WHERE to_currency = 'CNY' AND effective_date <= ${d}$
WHERE to_currency = 'CNY' AND effective_date <= ${d}$ ORDER BY effective_date DESC"""
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:
for r in rows: cur = r.from_currency if hasattr(r, 'from_currency') else r['from_currency']
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']
rate = r.mid_rate if hasattr(r, 'mid_rate') else r['mid_rate'] if cur not in rates and rate:
if cur not in rates and rate: rates[cur] = float(rate)
rates[cur] = float(rate)
except Exception as e: except Exception as e:
info('sage_datamart: exchange_rate lookup failed: ' + str(e)) info('sage_datamart: exchange_rate lookup failed: ' + str(e))
return rates return rates