diff --git a/scripts/load_path.py b/scripts/load_path.py index 100b45b..8563bcb 100644 --- a/scripts/load_path.py +++ b/scripts/load_path.py @@ -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", diff --git a/wwwroot/api/product_query_price.dspy b/wwwroot/api/product_query_price.dspy new file mode 100644 index 0000000..f516619 --- /dev/null +++ b/wwwroot/api/product_query_price.dspy @@ -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)