1- from fastapi import APIRouter
1+ from typing import Optional
2+ from fastapi import APIRouter , Query
3+ from sqlmodel import func , or_ , select
24from apps .system .crud .user import get_db_user , user_ws_options
5+ from apps .system .models .system_model import UserWsModel , WorkspaceModel
36from apps .system .models .user import UserModel
47from apps .system .schemas .auth import CacheName , CacheNamespace
58from apps .system .schemas .system_schema import PwdEditor , UserCreator , UserEditor , UserGrid , UserLanguage , UserWs
@@ -18,13 +21,54 @@ async def user_info(current_user: CurrentUser):
1821async def pager (
1922 session : SessionDep ,
2023 pageNum : int ,
21- pageSize : int
24+ pageSize : int ,
25+ keyword : Optional [str ] = Query (None , description = "搜索关键字(可选)" ),
26+ status : Optional [int ] = Query (None , description = "状态" ),
27+ origins : Optional [list [int ]] = Query (None , description = "来源" ),
28+ oidlist : Optional [list [int ]] = Query (None , description = "空间ID集合(可选)" ),
2229):
2330 pagination = PaginationParams (page = pageNum , size = pageSize )
2431 paginator = Paginator (session )
2532 filters = {}
33+
34+ stmt = (
35+ select (
36+ UserModel ,
37+ func .coalesce (func .string_agg (WorkspaceModel .name , ',' ), '' ).label ("space_name" )
38+ )
39+ .join (UserWsModel , UserModel .id == UserWsModel .uid , isouter = True )
40+ .join (WorkspaceModel , UserWsModel .oid == WorkspaceModel .id , isouter = True )
41+ .group_by (UserModel .id )
42+ .order_by (UserModel .create_time )
43+ )
44+
45+ if status is not None :
46+ stmt = stmt .where (UserModel .status == status )
47+
48+ if oidlist :
49+ user_filter = (
50+ select (UserModel .id )
51+ .join (UserWsModel , UserModel .id == UserWsModel .uid )
52+ .where (UserWsModel .oid .in_ (oidlist ))
53+ .distinct ()
54+ )
55+ stmt = stmt .where (UserModel .id .in_ (user_filter ))
56+
57+ """ if origins is not None:
58+ stmt = stmt.where(UserModel.origin == origins) """
59+
60+ if keyword :
61+ keyword_pattern = f"%{ keyword } %"
62+ stmt = stmt .where (
63+ or_ (
64+ UserModel .account .ilike (keyword_pattern ),
65+ UserModel .name .ilike (keyword_pattern ),
66+ UserModel .email .ilike (keyword_pattern )
67+ )
68+ )
69+
2670 return await paginator .get_paginated_response (
27- stmt = UserModel ,
71+ stmt = stmt ,
2872 pagination = pagination ,
2973 ** filters )
3074
0 commit comments