[changes] Modify the data format and improve the query logic.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
import uuid
|
||||||
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
from fastapi import APIRouter, Depends, HTTPException, status, Query
|
||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
@@ -48,7 +49,7 @@ def get_workspace_total_end_users(
|
|||||||
|
|
||||||
@router.get("/end_users", response_model=ApiResponse)
|
@router.get("/end_users", response_model=ApiResponse)
|
||||||
async def get_workspace_end_users(
|
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)"),
|
keyword: Optional[str] = Query(None, description="搜索关键词(同时模糊匹配 other_name 和 id)"),
|
||||||
page: int = Query(1, ge=1, description="页码,从1开始"),
|
page: int = Query(1, ge=1, description="页码,从1开始"),
|
||||||
pagesize: int = Query(10, ge=1, description="每页数量"),
|
pagesize: int = Query(10, ge=1, description="每页数量"),
|
||||||
|
|||||||
@@ -128,27 +128,20 @@ def get_workspace_end_users_paginated(
|
|||||||
|
|
||||||
if keyword:
|
if keyword:
|
||||||
keyword_pattern = f"%{keyword}%"
|
keyword_pattern = f"%{keyword}%"
|
||||||
# 优先匹配 other_name,如果 other_name 为空则匹配 id
|
# other_name 匹配始终生效;id 匹配仅对 other_name 为空的记录生效
|
||||||
# 使用 OR 条件:匹配 other_name 不为空的数据,或者 other_name 为空但 id 匹配的数据
|
|
||||||
base_query = base_query.filter(
|
base_query = base_query.filter(
|
||||||
or_(
|
or_(
|
||||||
# 情况1:other_name 不为空且匹配 keyword
|
EndUserModel.other_name.ilike(keyword_pattern),
|
||||||
and_(
|
|
||||||
EndUserModel.other_name != "",
|
|
||||||
EndUserModel.other_name.isnot(None),
|
|
||||||
EndUserModel.other_name.ilike(keyword_pattern)
|
|
||||||
),
|
|
||||||
# 情况2:other_name 为空或 None,但 id 匹配 keyword
|
|
||||||
and_(
|
and_(
|
||||||
or_(
|
or_(
|
||||||
|
EndUserModel.other_name.is_(None),
|
||||||
EndUserModel.other_name == "",
|
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()
|
total = base_query.count()
|
||||||
|
|||||||
Reference in New Issue
Block a user