async def user_browse_history_search(ns={}): # 处理url_link转换成domain_name if ns.get('url_link'): domain_name = ns.get('url_link').split("//")[1].split("/")[0] else: domain_name = None # 处理userid if ns.get('userid'): userid = ns.get('userid') else: userid = await get_user() if not userid: server_error(401) # 参数处理 productid = ns.get('productid') publish_type = ns.get('publish_type') start_time = ns.get('start_time') end_time = ns.get('end_time') page_size = int(ns.get('page_size', 10000)) 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 publish_type: base_conditions.append(f"publish_type = '{publish_type}'") 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'; """ favorite_sql = f""" SELECT * FROM user_favorite WHERE productid = '{product['productid']}' AND userid = '{userid}' AND del_flg = '0'; """ favorite_status = await sor.sqlExe(favorite_sql, {}) product_info_li = await sor.sqlExe(product_sql, {}) if product_info_li: product['product_info'] = product_info_li[0] if favorite_status: product['product_info']['favorite'] = '1' else: product['product_info']['favorite'] = '0' if product['product_info'].get('phone_number'): product['product_info']['phone_number'] = '***************' if product['product_info'].get('img'): product['product_info']['img'] = 'https://' + product['product_info']['domain_name'] + '/idfile?path=' + product['product_info']['img'] else: product['product_info'] = None date_groups = {} for item in result: # 提取日期部分(如"2025-08-22 15:58:52" → "2025-08-22") browse_date = datetime.datetime.strptime(item["browse_time"], "%Y-%m-%d %H:%M:%S").strftime("%Y-%m-%d") if browse_date not in date_groups: date_groups[browse_date] = [] date_groups[browse_date].append(item) # 按日期升序排序并添加序号 sorted_dates = sorted(date_groups.keys(), reverse=True) # 按日期升序排列 result = [] for idx, date in enumerate(sorted_dates, start=1): result.append({ "id": str(idx), # 序号从1开始 "browse_date": date, "products": date_groups[date] # 该日期下的所有浏览记录 }) 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