kboss/b/user/user_browse_history_search.dspy
2025-08-22 11:27:17 +08:00

83 lines
3.0 KiB
Plaintext

async def user_browse_history_search(ns={}):
# 处理userid
if ns.get('userid'):
userid = ns.get('userid')
else:
userid = None
if not userid:
server_error(401)
# 参数处理
productid = ns.get('productid')
start_time = ns.get('start_time')
end_time = ns.get('end_time')
page_size = int(ns.get('page_size', 1000))
current_page = int(ns.get('current_page', 1))
offset = (current_page - 1) * page_size
db = DBPools()
async with db.sqlorContext('kboss') as sor:
try:
# 基础查询条件
base_conditions = ["del_flg = '0'"]
if userid:
base_conditions.append(f"userid = '{userid}'")
if productid:
base_conditions.append(f"productid = '{productid}'")
if start_time and end_time:
end_time += ' 23:59:59'
base_conditions.append(f"browse_time BETWEEN '{start_time}' AND '{end_time}'")
# 构建查询SQL
where_clause = " AND ".join(base_conditions)
find_sql = f"""
SELECT * FROM user_browse_history
WHERE {where_clause}
ORDER BY browse_time DESC
LIMIT {page_size} OFFSET {offset};
"""
# 总数查询SQL
count_sql = f"""
SELECT COUNT(*) AS total_count FROM user_browse_history
WHERE {where_clause};
"""
# 执行查询
result = await sor.sqlExe(find_sql, {})
total_count = (await sor.sqlExe(count_sql, {}))[0]['total_count']
# 根据result中productid查询product_info
for product in result:
# 查询product_info
product_sql = f"""
SELECT * FROM user_publish_product
WHERE id = '{product['productid']}' AND del_flg = '0' AND listing_status = 'listing';
"""
product_info_li = await sor.sqlExe(product_sql, {})
if product_info_li:
product['product_info'] = product_info_li[0]
if product['product_info'].get('phone_number'):
product['product_info']['phone_number'] = '***************'
else:
product['product_info'] = None
return {
'status': True,
'msg': 'Browse history retrieved successfully',
'data': {
'total_count': total_count,
'page_size': page_size,
'current_page': current_page,
'history_list': result
}
}
except Exception as e:
return {
'status': False,
'msg': 'Failed to retrieve browse history, %s' % str(e)
}
ret = await user_browse_history_search(params_kw)
return ret