73 lines
2.5 KiB
Python
73 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
产品简介标准化接口
|
||
参数:
|
||
product_id: 产品ID (product表id)
|
||
product_code: 产品编码 (可选,与product_id二选一)
|
||
category_id: 类别ID (可选,返回该类别下所有产品的简介)
|
||
返回:
|
||
{success, data: [{id, product_code, product_name, category_name, brief_intro, price, currency, enabled_date, expired_date, status}]}
|
||
"""
|
||
import json
|
||
|
||
result = {'success': False, 'data': [], 'total': 0}
|
||
|
||
try:
|
||
dbname = get_module_dbname('product_management')
|
||
product_id = params_kw.get('product_id', '')
|
||
product_code = params_kw.get('product_code', '')
|
||
category_id = params_kw.get('category_id', '')
|
||
|
||
conditions = ["p.status = '1'"]
|
||
params = {}
|
||
|
||
if product_id:
|
||
conditions.append("p.id = ${product_id}$")
|
||
params['product_id'] = product_id
|
||
elif product_code:
|
||
conditions.append("p.product_code = ${product_code}$")
|
||
params['product_code'] = product_code
|
||
|
||
if category_id:
|
||
conditions.append("p.category_id = ${category_id}$")
|
||
params['category_id'] = category_id
|
||
|
||
where_clause = " AND ".join(conditions)
|
||
|
||
sql = f"""SELECT p.id, p.product_code, p.product_name, p.category_id,
|
||
pc.name as category_name, p.brief_intro,
|
||
p.price, p.currency, p.enabled_date, p.expired_date,
|
||
p.status, p.product_table_name, p.product_table_id
|
||
FROM product p
|
||
LEFT JOIN product_category pc ON p.category_id = pc.id
|
||
WHERE {where_clause}
|
||
ORDER BY p.sort_order ASC, p.created_at DESC"""
|
||
|
||
async with DBPools().sqlorContext(dbname) as sor:
|
||
rows = await sor.sqlExe(sql, params)
|
||
rows = rows or []
|
||
|
||
# Check enabled/expired date
|
||
import datetime
|
||
today = datetime.date.today().isoformat()
|
||
active_products = []
|
||
for r in rows:
|
||
enabled = str(r.get('enabled_date', '') or '')
|
||
expired = str(r.get('expired_date', '') or '')
|
||
is_active = True
|
||
if enabled and enabled > today:
|
||
is_active = False
|
||
if expired and expired < today:
|
||
is_active = False
|
||
r['is_active'] = is_active
|
||
active_products.append(r)
|
||
|
||
result['data'] = [dict(r) for r in active_products]
|
||
result['total'] = len(result['data'])
|
||
result['success'] = True
|
||
|
||
except Exception as e:
|
||
result['error'] = str(e)
|
||
|
||
return json.dumps(result, ensure_ascii=False, default=str)
|