59 lines
2.1 KiB
Plaintext
59 lines
2.1 KiB
Plaintext
# -*- coding: utf-8 -*-
|
|
"""查询产品价格(适配 platformbiz 调用方)
|
|
返回: {"price": 0.0, "currency": "CNY", "product_name": ""}
|
|
"""
|
|
import json
|
|
|
|
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)
|