- All .dspy files: replace ServerEnv() org_id access with await get_userorgid() (get_userorgid is registered as global in ahserver processorResource) - core.py: simplify _get_db to use DBPools() singleton directly (DBPools is @SingletonDecorator) remove unnecessary db.databases = config.databases assignment - core.py: add MODULE_NAME constant, use env.get_module_dbname(MODULE_NAME) pattern - Create scripts/load_path.py with all RBAC paths per module-development-spec Covers: entry pages, feature .ui files, CRUD directories, all API .dspy endpoints - .gitignore: add __pycache__/ exclusion - All models/*.json and json/*.json pass spec validation checks
77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
产品简介标准化接口 (按机构隔离)
|
|
参数:
|
|
product_id: 产品ID (product表id)
|
|
product_code: 产品编码 (可选)
|
|
category_id: 类别ID (可选,返回该类别下所有产品)
|
|
org_id: 机构ID (可选,不传则用当前用户机构)
|
|
返回:
|
|
{success, data: [{id, product_code, product_name, category_name, brief_intro, price, enabled_date, expired_date, status, extra_json}]}
|
|
"""
|
|
import json
|
|
|
|
result = {'success': False, 'data': [], 'total': 0}
|
|
|
|
try:
|
|
user_id = await get_user()
|
|
org_id = params_kw.get('org_id', None) or (await get_userorgid()) or '0'
|
|
|
|
product_id = params_kw.get('product_id', '')
|
|
product_code = params_kw.get('product_code', '')
|
|
category_id = params_kw.get('category_id', '')
|
|
|
|
dbname = get_module_dbname('product_management')
|
|
conditions = ["p.status = '1'", "p.org_id = ${org_id}$"]
|
|
params = {'org_id': org_id}
|
|
|
|
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_type, p.extra_json
|
|
FROM product p
|
|
LEFT JOIN product_category pc ON p.category_id = pc.id AND p.org_id = pc.org_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 []
|
|
|
|
import datetime
|
|
today = datetime.date.today().isoformat()
|
|
active_products = []
|
|
for r in rows:
|
|
r = dict(r)
|
|
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'] = 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)
|