diff --git a/backend/apps/system/api/user.py b/backend/apps/system/api/user.py index 53408c1b..6544a30a 100644 --- a/backend/apps/system/api/user.py +++ b/backend/apps/system/api/user.py @@ -1,5 +1,8 @@ -from fastapi import APIRouter +from typing import Optional +from fastapi import APIRouter, Query +from sqlmodel import func, or_, select from apps.system.crud.user import get_db_user, user_ws_options +from apps.system.models.system_model import UserWsModel, WorkspaceModel from apps.system.models.user import UserModel from apps.system.schemas.auth import CacheName, CacheNamespace from apps.system.schemas.system_schema import PwdEditor, UserCreator, UserEditor, UserGrid, UserLanguage, UserWs @@ -18,13 +21,54 @@ async def user_info(current_user: CurrentUser): async def pager( session: SessionDep, pageNum: int, - pageSize: int + pageSize: int, + keyword: Optional[str] = Query(None, description="搜索关键字(可选)"), + status: Optional[int] = Query(None, description="状态"), + origins: Optional[list[int]] = Query(None, description="来源"), + oidlist: Optional[list[int]] = Query(None, description="空间ID集合(可选)"), ): pagination = PaginationParams(page=pageNum, size=pageSize) paginator = Paginator(session) filters = {} + + stmt = ( + select( + UserModel, + func.coalesce(func.string_agg(WorkspaceModel.name, ','), '').label("space_name") + ) + .join(UserWsModel, UserModel.id == UserWsModel.uid, isouter=True) + .join(WorkspaceModel, UserWsModel.oid == WorkspaceModel.id, isouter=True) + .group_by(UserModel.id) + .order_by(UserModel.create_time) + ) + + if status is not None: + stmt = stmt.where(UserModel.status == status) + + if oidlist: + user_filter = ( + select(UserModel.id) + .join(UserWsModel, UserModel.id == UserWsModel.uid) + .where(UserWsModel.oid.in_(oidlist)) + .distinct() + ) + stmt = stmt.where(UserModel.id.in_(user_filter)) + + """ if origins is not None: + stmt = stmt.where(UserModel.origin == origins) """ + + if keyword: + keyword_pattern = f"%{keyword}%" + stmt = stmt.where( + or_( + UserModel.account.ilike(keyword_pattern), + UserModel.name.ilike(keyword_pattern), + UserModel.email.ilike(keyword_pattern) + ) + ) + return await paginator.get_paginated_response( - stmt=UserModel, + stmt=stmt, pagination=pagination, **filters) diff --git a/backend/apps/system/crud/user.py b/backend/apps/system/crud/user.py index 1af1c9a8..2b237ee9 100644 --- a/backend/apps/system/crud/user.py +++ b/backend/apps/system/crud/user.py @@ -44,8 +44,8 @@ async def user_ws_options(session: Session, uid: int, trans: Optional[I18n] = No if uid == 1: stmt = select(WorkspaceModel.id, WorkspaceModel.name).order_by(WorkspaceModel.create_time) else: - stmt = select(UserWsModel.oid, WorkspaceModel.name).join( - WorkspaceModel, UserWsModel.oid == WorkspaceModel.id + stmt = select(WorkspaceModel.id, WorkspaceModel.name).join( + UserWsModel, UserWsModel.oid == WorkspaceModel.id ).where( UserWsModel.uid == uid, ).order_by(WorkspaceModel.create_time) diff --git a/backend/apps/system/schemas/system_schema.py b/backend/apps/system/schemas/system_schema.py index 4511b1d2..dcac66eb 100644 --- a/backend/apps/system/schemas/system_schema.py +++ b/backend/apps/system/schemas/system_schema.py @@ -37,6 +37,9 @@ class UserEditor(UserCreator, BaseCreatorDTO): class UserGrid(UserEditor): create_time: int language: str = "zh-CN" + space_name: Optional[str] = None + origin: str = '' + class PwdEditor(BaseModel): pwd: str diff --git a/backend/common/core/sqlbot_cache.py b/backend/common/core/sqlbot_cache.py index 9a00a9d1..82c8812a 100644 --- a/backend/common/core/sqlbot_cache.py +++ b/backend/common/core/sqlbot_cache.py @@ -168,9 +168,9 @@ def clear_cache( ) logger.debug(f"Clearing cache for key: {cache_key}") - # 2. 清除缓存 - await FastAPICache.clear(key=cache_key) + if await FastAPICache.get_backend().get(cache_key): + await FastAPICache.clear(key=cache_key) # 3. 执行原函数 return await func(*args, **kwargs)