diff --git a/api/app/repositories/tenant_repository.py b/api/app/repositories/tenant_repository.py index 462c75e5..2934dda3 100644 --- a/api/app/repositories/tenant_repository.py +++ b/api/app/repositories/tenant_repository.py @@ -100,6 +100,15 @@ class TenantRepository: db_tenant.is_active = False return True + def get_tenant_users(self, tenant_id: uuid.UUID, is_active: Optional[bool] = None) -> List[User]: + """获取租户下的所有用户""" + query = self.db.query(User).filter(User.tenant_id == tenant_id) + + if is_active is not None: + query = query.filter(User.is_active == is_active) + + return query.all() + def get_user_tenant(self, user_id: uuid.UUID) -> Optional[Tenants]: """获取用户所属的租户""" user = self.db.query(User).filter(User.id == user_id).first() @@ -121,6 +130,15 @@ class TenantRepository: user.tenant_id = tenant_id self.db.flush() + return True + + def count_tenant_users(self, tenant_id: uuid.UUID, is_active: Optional[bool] = None) -> int: + """统计租户下的用户数量""" + query = self.db.query(func.count(User.id)).filter(User.tenant_id == tenant_id) + + if is_active is not None: + query = query.filter(User.is_active == is_active) + return query.scalar() @@ -143,4 +161,8 @@ def get_tenants(db: Session, skip: int = 0, limit: int = 100) -> List[Tenants]: def get_user_tenant(db: Session, user_id: uuid.UUID) -> Optional[Tenants]: """获取用户所属的租户""" - return TenantRepository(db).get_user_tenant(user_id) \ No newline at end of file + return TenantRepository(db).get_user_tenant(user_id) + +def get_tenant_users(db: Session, tenant_id: uuid.UUID) -> List[User]: + """获取租户下的所有用户""" + return TenantRepository(db).get_tenant_users(tenant_id) \ No newline at end of file diff --git a/api/app/services/tenant_service.py b/api/app/services/tenant_service.py index 369327ba..36205503 100644 --- a/api/app/services/tenant_service.py +++ b/api/app/services/tenant_service.py @@ -140,6 +140,41 @@ class TenantService: business_logger.error(f"删除租户失败: {str(e)}") raise BusinessException(f"删除租户失败:{str(e)}", code=BizCode.DB_ERROR) + # 租户用户管理 + def get_tenant_users( + self, + tenant_id: uuid.UUID, + skip: int = 0, + limit: int = 100, + is_active: Optional[bool] = None, + is_superuser: Optional[bool] = None, + search: Optional[str] = None + ) -> List[UserModel]: + """获取租户下的用户列表""" + return self.user_repo.get_users_by_tenant( + tenant_id=tenant_id, + skip=skip, + limit=limit, + is_active=is_active, + is_superuser=is_superuser, + search=search + ) + + def count_tenant_users( + self, + tenant_id: uuid.UUID, + is_active: Optional[bool] = None, + is_superuser: Optional[bool] = None, + search: Optional[str] = None + ) -> int: + """统计租户下的用户数量""" + return self.user_repo.count_users_by_tenant( + tenant_id=tenant_id, + is_active=is_active, + is_superuser=is_superuser, + search=search + ) + def assign_user_to_tenant(self, user_id: uuid.UUID, tenant_id: uuid.UUID) -> bool: """将用户分配给租户""" # 检查租户是否存在