fix: product_query_price改用llm_query_price计价

This commit is contained in:
yumoqing 2026-07-24 16:08:15 +08:00
parent 2c5320bb4a
commit 91c579cff7

View File

@ -1,58 +1,42 @@
# -*- coding: utf-8 -*-
"""查询产品价格(适配 platformbiz 调用方)
返回: {"price": 0.0, "currency": "CNY", "product_name": ""}
"""
import json
debug(f'{params_kw=}')
product_type = params_kw.product_type
product_id = params_kw.product_id
config_data = params_kw.config_data
keys = [k for k in config_data.keys()]
if 'off_peak' in keys:
off_peak = config_data.off_peak
if off_peak in ['Y', '1', 1]:
config_data.off_peak = True
elif off_peak in ['N', '0', 0]:
config_data.off_peak = False
debug(f'{config_data=}')
if product_type == 'llm':
try:
recs = await llm_query_price(product_id, config_data)
amount = 0
original_amount = 0
for r in recs:
amount += r.amount
if r.original_amount:
original_amount += r.original_amount
productid = params_kw.get('productid', '')
userorgid = params_kw.get('userorgid', '')
if not productid:
return json.dumps({'error': 'missing productid'}, ensure_ascii=False)
env = request._run_ns
# Try product_management pricing first
try:
async with get_sor_context(env, 'product_management') as sor:
sql = """SELECT p.name, p.unit_price, p.currency, pp.program
FROM product p
LEFT JOIN product_pricing pp ON pp.productid = p.id AND pp.userorgid = ${userorgid}$
WHERE p.id = ${productid}$ OR p.name = ${productid}$"""
recs = await sor.sqlExe(sql, {'productid': productid, 'userorgid': userorgid})
if recs:
r = recs[0]
price = float(r.unit_price or 0)
if hasattr(r, 'program') and r.program:
try:
prog = json.loads(r.program)
price = float(prog.get('base_price', price))
except Exception:
pass
return json.dumps({
'productid': productid,
'product_name': r.name or productid,
'price': price,
'currency': r.currency or 'CNY',
'unit_price': float(r.unit_price or 0),
}, ensure_ascii=False)
except Exception:
pass
# Fallback: try platformbiz DB
try:
async with get_sor_context(env, 'platformbiz') as sor:
sql = """SELECT name, price, currency FROM product WHERE id = ${productid}$"""
recs = await sor.sqlExe(sql, {'productid': productid})
if recs:
r = recs[0]
return json.dumps({
'productid': productid,
'product_name': r.name or productid,
'price': float(r.price or 0),
'currency': 'CNY',
}, ensure_ascii=False)
except Exception:
pass
return json.dumps({'productid': productid, 'price': 0, 'currency': 'CNY'}, ensure_ascii=False)
return {
"status": "ok",
"data": {
"amount": amount,
"original_amoutn": None if original_amount == 0 else original_amount
}
}
except Exception as e:
return {
"status": "error",
"data": {
"message": str(e)
}
}
return {
"status": "error",
"data": {
"message": f"{product_type=} 未知产品类型"
}
}