async def global_search(ns={}): if not ns.get('url_link') or not ns.get('publish_type'): return { 'status': False, 'msg': '请传递路由链接(url_link)和发布类型(publish_type)' } if not ns.get('keyword'): return { 'status': False, 'msg': '请传递搜索关键词' } if not ns.get('display_page'): return { 'status': False, 'msg': '请传递回显页面(display_page)' } # 增加分页功能 current_page = int(ns.get('current_page')) if ns.get('current_page') else 1 page_size = int(ns.get('page_size')) if ns.get('page_size') else 8 # 分页查询 offset = (current_page - 1) * page_size domain_name = ns.get('url_link').split("//")[1].split("/")[0] if 'localhost' in domain_name: domain_name = 'dev.opencomputing.cn' db = DBPools() async with db.sqlorContext('kboss') as sor: try: # 增加分页条件 # 分页条件 find_sql = """SELECT * FROM user_publish_product WHERE publish_type = '%s' AND domain_name = '%s' AND audit_status = 'approved' AND listing_status = 'listing' AND del_flg = '0' ORDER BY priority DESC LIMIT %s OFFSET %s;""" find_sql = find_sql % (ns.get('publish_type'), domain_name, page_size, offset) # 添加keyword模糊查询 筛选product_name, requirement_summary find_sql = find_sql.split('WHERE')[0] + ' WHERE ' + "(product_name LIKE '%%%%%s%%%%' OR requirement_summary LIKE '%%%%%s%%%%') AND " % (ns.get('keyword'), ns.get('keyword')) + find_sql.split('WHERE')[1] # 如果是overview sql不筛选requirement_summary if ns.get('display_page') == 'overview': find_sql = find_sql.split('WHERE')[0] + ' WHERE ' + "(product_name LIKE '%%%%%s%%%%') AND " % (ns.get('keyword')) + find_sql.split('WHERE')[1] # 计算筛选后 返回总数量 count(*) count_sql = """SELECT COUNT(*) AS total_count FROM user_publish_product WHERE publish_type = '%s' AND domain_name = '%s' AND audit_status = 'approved' AND listing_status = 'listing' AND del_flg = '0' AND (product_name LIKE '%%%%%s%%%%' OR requirement_summary LIKE '%%%%%s%%%%');""" count_sql = count_sql % (ns.get('publish_type'), domain_name, ns.get('keyword'), ns.get('keyword')) count_result = await sor.sqlExe(count_sql, {}) total_count = count_result[0]['total_count'] result = await sor.sqlExe(find_sql, {}) for res in result: if res.get('img') and res['img'] != 'null': res['img'] = 'https://' + domain_name + '/idfile?path=' + res['img'] else: res['img'] = None # 根据display_page区分返回字段 if ns.get('display_page') == 'overview': # result 产品名称去重后的 产品名称列表 根据名称去重 product_names = list(set([item['product_name'] for item in result])) # 产品名称添加 uuid形成的八位id 形成列表嵌套字典的格式 result = [{ 'id': str(uuid())[:8], 'product_name': product_name} for product_name in product_names] return { 'status': True, 'msg': 'search success', 'data': { 'current_page': current_page, 'page_size': page_size, 'total_count': total_count, 'result': result } } except Exception as e: return { 'status': False, 'msg': 'Failed to search publish product, %s' % str(e) } ret = await global_search(params_kw) return ret