110 lines
4.0 KiB
Python
110 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
产品详情标准化接口
|
||
参数:
|
||
product_id: 产品ID (product表id)
|
||
product_code: 产品编码 (可选,与product_id二选一)
|
||
返回:
|
||
{success, data: {product_info, category_info, detail_config, actual_product_data}}
|
||
说明:
|
||
通过product.product_table_name和product.product_table_id
|
||
动态查询实际产品数据表中的详细信息
|
||
"""
|
||
import json
|
||
|
||
result = {'success': False, 'data': {}}
|
||
|
||
try:
|
||
dbname = get_module_dbname('product_management')
|
||
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)
|
||
|
||
# Step 1: Get product registry info
|
||
conditions = []
|
||
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
|
||
|
||
where_clause = " AND ".join(conditions)
|
||
|
||
sql = f"""SELECT p.*, pc.name as category_name, pc.description as category_description,
|
||
pc.product_table_name as category_table_name
|
||
FROM product p
|
||
LEFT JOIN product_category pc ON p.category_id = pc.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])
|
||
|
||
# Step 2: Get operator config for this category
|
||
user_id = await get_user()
|
||
config_sql = """SELECT * FROM product_type_config
|
||
WHERE category_id = ${category_id}$
|
||
AND enabled_flg = '1'
|
||
AND (operator_id = ${user_id}$ OR org_id = ${org_id}$)
|
||
ORDER BY created_at DESC LIMIT 1"""
|
||
|
||
from ahserver.serverenv import ServerEnv
|
||
env = ServerEnv()
|
||
org_id = getattr(env, 'orgid', None) or getattr(env, 'org_id', '0')
|
||
|
||
config_rows = await sor.sqlExe(config_sql, {
|
||
'category_id': product_info['category_id'],
|
||
'user_id': user_id,
|
||
'org_id': org_id
|
||
})
|
||
|
||
operator_config = {}
|
||
if config_rows:
|
||
operator_config = dict(config_rows[0])
|
||
if operator_config.get('config_json'):
|
||
import ast
|
||
try:
|
||
operator_config['config_parsed'] = json.loads(operator_config['config_json'])
|
||
except:
|
||
operator_config['config_parsed'] = {}
|
||
|
||
# Step 3: Try to fetch actual product data from the product_table
|
||
actual_product_data = None
|
||
table_name = product_info.get('product_table_name', '')
|
||
table_id = product_info.get('product_table_id', '')
|
||
|
||
if table_name and table_id:
|
||
try:
|
||
# Use dynamic table query
|
||
detail_sql = f"SELECT * FROM {table_name} WHERE id = ${{table_id}}$"
|
||
detail_rows = await sor.sqlExe(detail_sql, {'table_id': table_id})
|
||
if detail_rows:
|
||
actual_product_data = dict(detail_rows[0])
|
||
except Exception as table_err:
|
||
actual_product_data = {'_error': f'无法获取实际产品数据: {str(table_err)}'}
|
||
|
||
result['data'] = {
|
||
'product_info': product_info,
|
||
'category_info': {
|
||
'name': product_info.get('category_name'),
|
||
'description': product_info.get('category_description')
|
||
},
|
||
'operator_config': operator_config,
|
||
'actual_product_data': actual_product_data
|
||
}
|
||
result['success'] = True
|
||
|
||
except Exception as e:
|
||
result['error'] = str(e)
|
||
|
||
return json.dumps(result, ensure_ascii=False, default=str)
|