- product_category: org_id scoped tree, product_table_name -> product_type - product: org_id scoped, added extra_json for custom attributes, product_type field - product_type_config: org_id + operator_id dual isolation, unique key on (org_id, operator_id, category_id, config_name) - All 18 API endpoints enforce org_id filtering via ServerEnv - core.py: all methods accept optional org_id, default to current user's org - CRUD definitions: logined_userorgid set to org_id on all lists - init/data.json: removed hardcoded global categories (managed per reseller) - Rebuilt mysql.ddl.sql and all CRUD UI files
79 lines
2.7 KiB
Python
79 lines
2.7 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()
|
|
from ahserver.serverenv import ServerEnv
|
|
env = ServerEnv()
|
|
org_id = params_kw.get('org_id', None) or getattr(env, 'orgid', None) or getattr(env, 'org_id', '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)
|