- 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
105 lines
3.6 KiB
Python
105 lines
3.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
产品详情标准化接口 (按机构隔离)
|
|
参数:
|
|
product_id: 产品ID
|
|
product_code: 产品编码 (可选)
|
|
org_id: 机构ID (可选,不传则用当前用户机构)
|
|
返回:
|
|
{success, data: {product_info, category_info, operator_config, extra_parsed}}
|
|
说明:
|
|
product_info 包含产品全部信息
|
|
extra_parsed 是 extra_json 解析后的结构化数据
|
|
operator_config 是当前运营商对该产品类型的配置
|
|
"""
|
|
import json
|
|
|
|
result = {'success': False, 'data': {}}
|
|
|
|
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', '')
|
|
|
|
if not product_id and not product_code:
|
|
result['error'] = '缺少product_id或product_code参数'
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
dbname = get_module_dbname('product_management')
|
|
conditions = ["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
|
|
|
|
where_clause = " AND ".join(conditions)
|
|
|
|
sql = f"""SELECT p.*, pc.name as category_name, pc.description as category_description
|
|
FROM product p
|
|
LEFT JOIN product_category pc ON p.category_id = pc.id AND p.org_id = pc.org_id
|
|
WHERE {where_clause}"""
|
|
|
|
async with DBPools().sqlorContext(dbname) as sor:
|
|
rows = await sor.sqlExe(sql, params)
|
|
if not rows:
|
|
result['error'] = '产品不存在或无权访问'
|
|
return json.dumps(result, ensure_ascii=False)
|
|
|
|
product_info = dict(rows[0])
|
|
|
|
# Parse extra_json
|
|
extra_parsed = {}
|
|
extra_str = product_info.get('extra_json', '')
|
|
if extra_str:
|
|
try:
|
|
extra_parsed = json.loads(extra_str)
|
|
except:
|
|
extra_parsed = {'_raw': extra_str}
|
|
product_info['extra_parsed'] = extra_parsed
|
|
|
|
# Get operator config for this product_type
|
|
config_sql = """SELECT * FROM product_type_config
|
|
WHERE category_id = ${category_id}$
|
|
AND enabled_flg = '1'
|
|
AND org_id = ${org_id}$
|
|
AND (operator_id = ${user_id}$ OR operator_id = '0')
|
|
ORDER BY created_at DESC LIMIT 1"""
|
|
config_rows = await sor.sqlExe(config_sql, {
|
|
'category_id': product_info['category_id'],
|
|
'org_id': org_id,
|
|
'user_id': user_id
|
|
})
|
|
|
|
operator_config = {}
|
|
if config_rows:
|
|
operator_config = dict(config_rows[0])
|
|
config_json = operator_config.get('config_json', '')
|
|
if config_json:
|
|
try:
|
|
operator_config['config_parsed'] = json.loads(config_json)
|
|
except:
|
|
operator_config['config_parsed'] = {}
|
|
|
|
result['data'] = {
|
|
'product_info': product_info,
|
|
'category_info': {
|
|
'name': product_info.get('category_name'),
|
|
'description': product_info.get('category_description')
|
|
},
|
|
'operator_config': operator_config
|
|
}
|
|
result['success'] = True
|
|
|
|
except Exception as e:
|
|
result['error'] = str(e)
|
|
|
|
return json.dumps(result, ensure_ascii=False, default=str)
|