- 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
103 lines
3.5 KiB
Python
103 lines
3.5 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()
|
|
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', '')
|
|
|
|
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)
|