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 -*- debug(f'{params_kw=}')
"""查询产品价格(适配 platformbiz 调用方) product_type = params_kw.product_type
返回: {"price": 0.0, "currency": "CNY", "product_name": ""} product_id = params_kw.product_id
""" config_data = params_kw.config_data
import json 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', '') return {
userorgid = params_kw.get('userorgid', '') "status": "ok",
"data": {
if not productid: "amount": amount,
return json.dumps({'error': 'missing productid'}, ensure_ascii=False) "original_amoutn": None if original_amount == 0 else original_amount
}
env = request._run_ns }
except Exception as e:
# Try product_management pricing first return {
try: "status": "error",
async with get_sor_context(env, 'product_management') as sor: "data": {
sql = """SELECT p.name, p.unit_price, p.currency, pp.program "message": str(e)
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}$""" return {
recs = await sor.sqlExe(sql, {'productid': productid, 'userorgid': userorgid}) "status": "error",
if recs: "data": {
r = recs[0] "message": f"{product_type=} 未知产品类型"
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)