From 6d6338eb06388aedddbf9951f0c2dc40dd8f0596 Mon Sep 17 00:00:00 2001 From: lanceyq <1982376970@qq.com> Date: Wed, 1 Apr 2026 10:36:29 +0800 Subject: [PATCH] [changes] Modify the data format and improve the query logic. --- .../memory_dashboard_controller.py | 3 ++- api/app/services/memory_dashboard_service.py | 19 ++++++------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/api/app/controllers/memory_dashboard_controller.py b/api/app/controllers/memory_dashboard_controller.py index 260ea670..bedee987 100644 --- a/api/app/controllers/memory_dashboard_controller.py +++ b/api/app/controllers/memory_dashboard_controller.py @@ -1,4 +1,5 @@ import asyncio +import uuid from fastapi import APIRouter, Depends, HTTPException, status, Query from pydantic import BaseModel, Field from sqlalchemy.orm import Session @@ -48,7 +49,7 @@ def get_workspace_total_end_users( @router.get("/end_users", response_model=ApiResponse) async def get_workspace_end_users( - workspace_id: Optional[str] = Query(None, description="工作空间ID(可选,默认当前用户工作空间)"), + workspace_id: Optional[uuid.UUID] = Query(None, description="工作空间ID(可选,默认当前用户工作空间)"), keyword: Optional[str] = Query(None, description="搜索关键词(同时模糊匹配 other_name 和 id)"), page: int = Query(1, ge=1, description="页码,从1开始"), pagesize: int = Query(10, ge=1, description="每页数量"), diff --git a/api/app/services/memory_dashboard_service.py b/api/app/services/memory_dashboard_service.py index 3ab54561..9fad5dfe 100644 --- a/api/app/services/memory_dashboard_service.py +++ b/api/app/services/memory_dashboard_service.py @@ -128,27 +128,20 @@ def get_workspace_end_users_paginated( if keyword: keyword_pattern = f"%{keyword}%" - # 优先匹配 other_name,如果 other_name 为空则匹配 id - # 使用 OR 条件:匹配 other_name 不为空的数据,或者 other_name 为空但 id 匹配的数据 + # other_name 匹配始终生效;id 匹配仅对 other_name 为空的记录生效 base_query = base_query.filter( or_( - # 情况1:other_name 不为空且匹配 keyword - and_( - EndUserModel.other_name != "", - EndUserModel.other_name.isnot(None), - EndUserModel.other_name.ilike(keyword_pattern) - ), - # 情况2:other_name 为空或 None,但 id 匹配 keyword + EndUserModel.other_name.ilike(keyword_pattern), and_( or_( + EndUserModel.other_name.is_(None), EndUserModel.other_name == "", - EndUserModel.other_name.is_(None) ), - cast(EndUserModel.id, String).ilike(keyword_pattern) - ) + cast(EndUserModel.id, String).ilike(keyword_pattern), + ), ) ) - business_logger.info(f"应用模糊搜索: keyword={keyword}(优先匹配 other_name,无 other_name 时匹配 id)") + business_logger.info(f"应用模糊搜索: keyword={keyword}(匹配 other_name;other_name 为空时匹配 id)") # 获取总记录数 total = base_query.count()