feat: product_query_price.dspy替代platformbiz版本
This commit is contained in:
parent
93e12c2bad
commit
2c5320bb4a
@ -66,6 +66,7 @@ PATHS_LOGINED = [
|
||||
|
||||
# API endpoints (.dspy)
|
||||
f"/{MOD}/api/category_options.dspy",
|
||||
f"/{MOD}/api/product_query_price.dspy",
|
||||
f"/{MOD}/api/product_brief.dspy",
|
||||
f"/{MOD}/api/product_category_create.dspy",
|
||||
f"/{MOD}/api/product_category_update.dspy",
|
||||
|
||||
58
wwwroot/api/product_query_price.dspy
Normal file
58
wwwroot/api/product_query_price.dspy
Normal file
@ -0,0 +1,58 @@
|
||||
# -*- 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)
|
||||
Loading…
x
Reference in New Issue
Block a user