refactor: 移除多租户和客户相关代码及功能

refactor: 统一状态字段为字符串类型并更新相关组件
refactor: 更新模型基类移除租户和客户相关字段
refactor: 简化数据权限控制逻辑
refactor: 优化类型注解使用Python 3.10+语法
refactor: 清理无用导入和注释
docs: 更新文档移除多租户相关内容
This commit is contained in:
zhangtao
2025-12-01 00:36:05 +08:00
parent 463e2fca23
commit 3c12fa56eb
173 changed files with 3143 additions and 7672 deletions
@@ -5,13 +5,12 @@ from fastapi.responses import JSONResponse, StreamingResponse
import urllib.parse
from app.common.response import StreamResponse, SuccessResponse
from app.core.router_class import OperationLogRoute
from app.utils.common_util import bytes2file_response
from app.core.base_params import PaginationQueryParam
from app.core.dependencies import AuthPermission
from app.core.router_class import OperationLogRoute
from app.core.base_schema import BatchSetAvailable
from app.core.logger import log
from app.api.v1.module_system.auth.schema import AuthSchema
from .service import DemoService
from .schema import (
+21 -22
View File
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
from typing import Dict, List, Optional, Sequence, Union, Any
from collections.abc import Sequence
from app.core.base_crud import CRUDBase
from app.api.v1.module_system.auth.schema import AuthSchema
@@ -21,34 +20,34 @@ class DemoCRUD(CRUDBase[DemoModel, DemoCreateSchema, DemoUpdateSchema]):
"""
super().__init__(model=DemoModel, auth=auth)
async def get_by_id_crud(self, id: int, preload: Optional[List[Union[str, Any]]] = None) -> Optional[DemoModel]:
async def get_by_id_crud(self, id: int, preload: list[str] | None = None) -> DemoModel | None:
"""
详情
参数:
- id (int): 示例ID
- preload (Optional[List[Union[str, Any]]]): 预加载关系,未提供时使用模型默认项
- preload (list[str] | None): 预加载关系,未提供时使用模型默认项
返回:
- Optional[DemoModel]: 示例模型实例或None
- DemoModel | None: 示例模型实例或None
"""
return await self.get(id=id, preload=preload)
async def list_crud(self, search: Optional[Dict] = None, order_by: Optional[List[Dict[str, str]]] = None, preload: Optional[List[Union[str, Any]]] = None) -> Sequence[DemoModel]:
async def list_crud(self, search: dict | None = None, order_by: list[dict] | None = None, preload: list[str] | None = None) -> Sequence[DemoModel]:
"""
列表查询
参数:
- search (Optional[Dict]): 查询参数
- order_by (Optional[List[Dict[str, str]]]): 排序参数
- preload (Optional[List[Union[str, Any]]]): 预加载关系,未提供时使用模型默认项
- search (dict | None): 查询参数
- order_by (list[dict] | None): 排序参数
- preload (list[str] | None): 预加载关系,未提供时使用模型默认项
返回:
- Sequence[DemoModel]: 示例模型实例序列
"""
return await self.list(search=search, order_by=order_by, preload=preload)
async def create_crud(self, data: DemoCreateSchema) -> Optional[DemoModel]:
async def create_crud(self, data: DemoCreateSchema) -> DemoModel | None:
"""
创建
@@ -56,11 +55,11 @@ class DemoCRUD(CRUDBase[DemoModel, DemoCreateSchema, DemoUpdateSchema]):
- data (DemoCreateSchema): 示例创建模型
返回:
- Optional[DemoModel]: 示例模型实例或None
- DemoModel | None: 示例模型实例或None
"""
return await self.create(data=data)
async def update_crud(self, id: int, data: DemoUpdateSchema) -> Optional[DemoModel]:
async def update_crud(self, id: int, data: DemoUpdateSchema) -> DemoModel | None:
"""
更新
@@ -69,11 +68,11 @@ class DemoCRUD(CRUDBase[DemoModel, DemoCreateSchema, DemoUpdateSchema]):
- data (DemoUpdateSchema): 示例更新模型
返回:
- Optional[DemoModel]: 示例模型实例或None
- DemoModel | None: 示例模型实例或None
"""
return await self.update(id=id, data=data)
async def delete_crud(self, ids: List[int]) -> None:
async def delete_crud(self, ids: list[int]) -> None:
"""
批量删除
@@ -85,32 +84,32 @@ class DemoCRUD(CRUDBase[DemoModel, DemoCreateSchema, DemoUpdateSchema]):
"""
return await self.delete(ids=ids)
async def set_available_crud(self, ids: List[int], status: str) -> None:
async def set_available_crud(self, ids: list[int], status: str) -> None:
"""
批量设置可用状态
参数:
- ids (List[int]): 示例ID列表
- status (bool): 可用状态
- ids (list[int]): 示例ID列表
- status (str): 可用状态
返回:
- None
"""
return await self.set(ids=ids, status=status)
async def page_crud(self, offset: int, limit: int, order_by: Optional[List[Dict[str, str]]] = None, search: Optional[Dict] = None, preload: Optional[List[Union[str, Any]]] = None) -> Dict:
async def page_crud(self, offset: int, limit: int, order_by: list[dict] | None = None, search: dict | None = None, preload: list | None = None) -> dict:
"""
分页查询
参数:
- offset (int): 偏移量
- limit (int): 每页数量
- order_by (Optional[List[Dict[str, str]]]): 排序参数
- search (Optional[Dict]): 查询参数
- preload (Optional[List[Union[str, Any]]]): 预加载关系,未提供时使用模型默认项
- order_by (list[dict] | None): 排序参数
- search (dict | None): 查询参数
- preload (list | None): 预加载关系,未提供时使用模型默认项
返回:
- Dict: 分页数据
- dict: 分页数据
"""
order_by_list = order_by or [{'id': 'asc'}]
search_dict = search or {}
@@ -3,21 +3,15 @@
from sqlalchemy import String
from sqlalchemy.orm import Mapped, mapped_column
from app.core.base_model import ModelMixin, UserMixin, TenantMixin, CustomerMixin
from app.core.base_model import ModelMixin, UserMixin
class DemoModel(ModelMixin, UserMixin, TenantMixin, CustomerMixin):
class DemoModel(ModelMixin, UserMixin):
"""
示例表
数据隔离策略:
- 租户级示例: tenant_id必填, customer_id=NULL
- 客户级示例: tenant_id必填, customer_id>0
根据业务需求,此示例表支持客户级数据隔离
"""
__tablename__: str = 'gen_demo'
__table_args__: dict[str, str] = ({'comment': '示例表'})
__loader_options__: list[str] = ["created_by", "updated_by", "tenant", "customer"]
__loader_options__: list[str] = ["created_by", "updated_by"]
name: Mapped[str | None] = mapped_column(String(64), nullable=True, default='', comment='名称')
@@ -1,18 +1,17 @@
# -*- coding: utf-8 -*-
from typing import Optional
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
from fastapi import Query
from app.core.validator import DateTimeStr
from app.core.base_schema import BaseSchema, UserBySchema, TenantSchema, CustomerSchema
from app.core.base_schema import BaseSchema, UserBySchema
class DemoCreateSchema(BaseModel):
"""新增模型"""
name: str = Field(..., min_length=2, max_length=50, description='名称')
status: str = Field(default="0", description="是否启用(0:启用 1:禁用)")
description: Optional[str] = Field(default=None, max_length=255, description="描述")
description: str | None = Field(default=None, max_length=255, description="描述")
@field_validator('name')
@classmethod
@@ -37,7 +36,6 @@ class DemoCreateSchema(BaseModel):
raise ValueError('名称只能包含字母、数字、下划线和中划线')
return self
class DemoUpdateSchema(DemoCreateSchema):
@@ -45,7 +43,7 @@ class DemoUpdateSchema(DemoCreateSchema):
...
class DemoOutSchema(DemoCreateSchema, BaseSchema, UserBySchema, TenantSchema, CustomerSchema):
class DemoOutSchema(DemoCreateSchema, BaseSchema, UserBySchema):
"""响应模型"""
model_config = ConfigDict(from_attributes=True)
@@ -55,10 +53,12 @@ class DemoQueryParam:
def __init__(
self,
name: Optional[str] = Query(None, description="名称"),
status: Optional[str] = Query(None, description="是否启用"),
created_id: Optional[int] = Query(None, description="创建人"),
created_time: Optional[list[DateTimeStr]] = Query(None, description="创建时间范围", example=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
name: str | None = Query(None, description="名称"),
status: str | None = Query(None, description="是否启用"),
created_time: list[DateTimeStr] | None = Query(None, description="创建时间范围", example=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
updated_time: list[DateTimeStr] | None = Query(None, description="更新时间范围", example=["2025-01-01 00:00:00", "2025-12-31 23:59:59"]),
created_id: int | None = Query(None, description="创建人"),
updated_id: int | None = Query(None, description="更新人"),
) -> None:
# 模糊查询字段
@@ -66,10 +66,11 @@ class DemoQueryParam:
# 精确查询字段
self.created_id = created_id
self.updated_id = updated_id
self.status = status
# 时间范围查询
if created_time and len(created_time) == 2:
self.created_time = ("between", (created_time[0], created_time[1]))
if updated_time and len(updated_time) == 2:
self.updated_time = ("between", (updated_time[0], updated_time[1]))
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import io
from typing import Any, List, Dict, Optional
from typing import Any
from fastapi import UploadFile
import pandas as pd
@@ -21,7 +21,7 @@ class DemoService:
"""
@classmethod
async def detail_service(cls, auth: AuthSchema, id: int) -> Dict:
async def detail_service(cls, auth: AuthSchema, id: int) -> dict:
"""
详情
@@ -30,7 +30,7 @@ class DemoService:
- id (int): 示例ID
返回:
- Dict: 示例模型实例字典
- dict: 示例模型实例字典
"""
obj = await DemoCRUD(auth).get_by_id_crud(id=id)
if not obj:
@@ -38,24 +38,24 @@ class DemoService:
return DemoOutSchema.model_validate(obj).model_dump()
@classmethod
async def list_service(cls, auth: AuthSchema, search: Optional[DemoQueryParam] = None, order_by: Optional[List[Dict[str, str]]] = None) -> List[Dict]:
async def list_service(cls, auth: AuthSchema, search: DemoQueryParam | None = None, order_by: list[dict[str, str]] | None = None) -> list[dict]:
"""
列表查询
参数:
- auth (AuthSchema): 认证信息模型
- search (Optional[DemoQueryParam]): 查询参数
- order_by (Optional[List[Dict[str, str]]]): 排序参数
- search (DemoQueryParam | None): 查询参数
- order_by (list[dict[str, str]] | None): 排序参数
返回:
- List[Dict]: 示例模型实例字典列表
- list[dict]: 示例模型实例字典列表
"""
search_dict = search.__dict__ if search else None
obj_list = await DemoCRUD(auth).list_crud(search=search_dict, order_by=order_by)
return [DemoOutSchema.model_validate(obj).model_dump() for obj in obj_list]
@classmethod
async def page_service(cls, auth: AuthSchema, page_no: int, page_size: int, search: Optional[DemoQueryParam] = None, order_by: Optional[List[Dict[str, str]]] = None) -> Dict:
async def page_service(cls, auth: AuthSchema, page_no: int, page_size: int, search: DemoQueryParam | None = None, order_by: list[dict[str, str]] | None = None) -> dict:
"""
分页查询
@@ -63,11 +63,11 @@ class DemoService:
- auth (AuthSchema): 认证信息模型
- page_no (int): 页码
- page_size (int): 每页数量
- search (Optional[DemoQueryParam]): 查询参数
- order_by (Optional[List[Dict[str, str]]]): 排序参数
- search (DemoQueryParam | None): 查询参数
- order_by (list[dict[str, str]] | None): 排序参数
返回:
- Dict: 分页数据
- dict: 分页数据
"""
search_dict = search.__dict__ if search else {}
order_by_list = order_by or [{'id': 'asc'}]
@@ -82,7 +82,7 @@ class DemoService:
return result
@classmethod
async def create_service(cls, auth: AuthSchema, data: DemoCreateSchema) -> Dict:
async def create_service(cls, auth: AuthSchema, data: DemoCreateSchema) -> dict:
"""
创建
@@ -91,7 +91,7 @@ class DemoService:
- data (DemoCreateSchema): 示例创建模型
返回:
- Dict: 示例模型实例字典
- dict: 示例模型实例字典
"""
obj = await DemoCRUD(auth).get(name=data.name)
if obj:
@@ -100,7 +100,7 @@ class DemoService:
return DemoOutSchema.model_validate(obj).model_dump()
@classmethod
async def update_service(cls, auth: AuthSchema, id: int, data: DemoUpdateSchema) -> Dict:
async def update_service(cls, auth: AuthSchema, id: int, data: DemoUpdateSchema) -> dict:
"""
更新
@@ -110,7 +110,7 @@ class DemoService:
- data (DemoUpdateSchema): 示例更新模型
返回:
- Dict: 示例模型实例字典
- dict: 示例模型实例字典
"""
# 检查数据是否存在
obj = await DemoCRUD(auth).get_by_id_crud(id=id)
@@ -126,13 +126,13 @@ class DemoService:
return DemoOutSchema.model_validate(obj).model_dump()
@classmethod
async def delete_service(cls, auth: AuthSchema, ids: List[int]) -> None:
async def delete_service(cls, auth: AuthSchema, ids: list[int]) -> None:
"""
删除
参数:
- auth (AuthSchema): 认证信息模型
- ids (List[int]): 示例ID列表
- ids (list[int]): 示例ID列表
返回:
- None
@@ -163,12 +163,12 @@ class DemoService:
await DemoCRUD(auth).set_available_crud(ids=data.ids, status=data.status)
@classmethod
async def batch_export_service(cls, obj_list: List[Dict[str, Any]]) -> bytes:
async def batch_export_service(cls, obj_list: list[dict[str, Any]]) -> bytes:
"""
批量导出
参数:
- obj_list (List[Dict[str, Any]]): 示例模型实例字典列表
- obj_list (list[dict[str, Any]]): 示例模型实例字典列表
返回:
- bytes: Excel文件字节流