kboss/b/product/home_page_content_get_tree.dspy
2025-11-27 18:00:11 +08:00

118 lines
5.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

async def home_page_content_get_tree(ns={}):
"""
获取首页内容的树形结构按content_type分组
:param ns: 包含menu_product_id的字典
:return: 树形结构数据
"""
menu_product_id = ns.get('menu_product_id')
if not menu_product_id:
return {
'status': False,
'msg': 'menu_product_id is required'
}
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
# 查询所有有效的内容项
sql = """
SELECT * FROM home_page_content_items
WHERE menu_product_id = '%s' AND del_flg = '0'
ORDER BY sort_order ASC, create_at ASC
""" % menu_product_id
all_items = await sor.sqlExe(sql, {})
# 通过home_page_product_info表查找标题和描述
find_sql = """
SELECT * FROM home_page_product_info
WHERE id = '%s' AND del_flg = '0'
""" % menu_product_id
product_info = await sor.sqlExe(find_sql, {})
product_title = None
product_description = None
if product_info:
product_info = product_info[0]
product_title = product_info.get('name', '')
product_description = product_info.get('description', '')
# 按content_type分组
result = {
'title': product_title, # 可以从其他表获取
'description': product_description, # 可以从其他表获取
'advantages': [],
'features': [],
'applications': [],
'products': []
}
for item in all_items:
content_type = item.get('content_type')
item_data = {
'id': item['id'],
'title': item['title'],
'description': item['description'],
'img': item['img'],
'icon': item['icon']
}
# 根据内容类型添加到对应的列表
if content_type == 'advantage':
result['advantages'].append(item_data)
elif content_type == 'feature':
result['features'].append(item_data)
elif content_type == 'application':
# 应用场景可能包含子项
app_data = item_data.copy()
if not item.get('parent_id'): # 顶级应用
app_data['provide'] = [] # 子项将在后续处理
result['applications'].append(app_data)
elif content_type == 'product' and not item.get('parent_id'):
# 产品可能包含配置项
product_data = item_data.copy()
product_data['price'] = item.get('price')
product_data['pre_price'] = item.get('pre_price')
product_data['price_unit'] = item.get('price_unit')
product_data['discount'] = item.get('discount')
product_data['bg_img_url'] = item.get('bg_img_url')
product_data['list'] = [] # 子项将在后续处理
result['products'].append(product_data)
# 处理子级项目
for item in all_items:
if item.get('parent_id') != '0':
parent_id = item.get('parent_id')
content_type = item.get('content_type')
# 查找父级并添加子项
if content_type == 'application':
for app in result['applications']:
if app['id'] == parent_id:
app['provide'].append({
'id': item['id'],
'title': item['title'],
'description': item['description']
})
elif content_type == 'product':
for product in result['products']:
if product['id'] == parent_id:
product['list'].append({
'id': item['id'],
'name': item['name'],
'description': item['description'],
'icon': item['icon']
})
return {
'status': True,
'msg': 'get home page content tree success',
'data': result
}
except Exception as e:
return {
'status': False,
'msg': 'get home page content tree failed, %s' % str(e)
}
ret = await home_page_content_get_tree(params_kw)
return ret