86 lines
3.2 KiB
Plaintext
86 lines
3.2 KiB
Plaintext
async def home_page_product_search(ns={}):
|
||
"""
|
||
查找产品信息,支持按菜单ID、产品名称等筛选
|
||
:param ns:
|
||
:return:
|
||
"""
|
||
# 分页参数
|
||
current_page = int(ns.get('current_page', 1))
|
||
page_size = int(ns.get('page_size', 10))
|
||
offset = (current_page - 1) * page_size
|
||
|
||
# 构建查询条件
|
||
conditions = ["hp.del_flg = '0'"]
|
||
|
||
if ns.get('menu_id'):
|
||
conditions.append(f"hp.menu_id = '{ns.get('menu_id')}'")
|
||
if ns.get('name'):
|
||
conditions.append(f"hp.name LIKE '%{ns.get('name')}%'")
|
||
if ns.get('product_group'):
|
||
conditions.append(f"hp.product_group = '{ns.get('product_group')}'")
|
||
if ns.get('source'):
|
||
conditions.append(f"hp.source = '{ns.get('source')}'")
|
||
|
||
where_clause = " AND ".join(conditions)
|
||
|
||
db = DBPools()
|
||
async with db.sqlorContext('kboss') as sor:
|
||
try:
|
||
# 查询总数
|
||
count_sql = f"""
|
||
SELECT COUNT(*) AS total_count
|
||
FROM home_page_product_info hp
|
||
WHERE {where_clause}
|
||
"""
|
||
total_count = (await sor.sqlExe(count_sql, {}))[0]['total_count']
|
||
|
||
# 查询产品列表
|
||
find_sql = f"""
|
||
SELECT hp.*, hm.title as menu_title, hm.menu_level
|
||
FROM home_page_product_info hp
|
||
LEFT JOIN home_page_product_menu hm ON hp.menu_id = hm.id
|
||
WHERE {where_clause}
|
||
ORDER BY hp.sort_order ASC, hp.create_at DESC
|
||
LIMIT {page_size} OFFSET {offset}
|
||
"""
|
||
result = await sor.sqlExe(find_sql, {})
|
||
|
||
# 格式化返回数据,匹配home_page_menu.json的数据格式
|
||
product_list = []
|
||
for product in result:
|
||
product_data = {
|
||
'id': product['id'],
|
||
'name': product['name'],
|
||
'description': product['description'],
|
||
'label': product['label'],
|
||
'product_group': product['product_group'], # 注意字段名映射
|
||
'url': product['url'],
|
||
'list_url': product['list_url'], # 注意字段名映射
|
||
'iconUrl': product['icon_url'], # 注意字段名映射
|
||
'icon_url': product['source'],
|
||
'menu_id': product['menu_id'],
|
||
'menu_title': product['menu_title'],
|
||
'menu_level': product['menu_level']
|
||
}
|
||
product_list.append(product_data)
|
||
|
||
return {
|
||
'status': True,
|
||
'msg': 'search product success',
|
||
'data': {
|
||
'product_list': product_list
|
||
},
|
||
'pagination': {
|
||
'total': total_count,
|
||
'page_size': page_size,
|
||
'current_page': current_page,
|
||
}
|
||
}
|
||
except Exception as e:
|
||
return {
|
||
'status': False,
|
||
'msg': 'search product failed, %s' % str(e)
|
||
}
|
||
|
||
ret = await home_page_product_search(params_kw)
|
||
return ret |