model.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from datetime import datetime
  2. from sqlalchemy import DateTime, String
  3. from sqlalchemy.orm import Mapped, mapped_column, validates
  4. from app.common.enums import PermissionFilterStrategy
  5. from app.core.base_model import ModelMixin
  6. class TenantModel(ModelMixin):
  7. """
  8. 租户模型
  9. - 系统租户(id=1):平台管理,由超级管理员维护
  10. - 普通租户(id>1):独立组织数据,通过业务表的 tenant_id 隔离
  11. """
  12. __tablename__: str = "sys_tenant"
  13. __table_args__: dict[str, str] = {"comment": "租户表"}
  14. __permission_strategy__: PermissionFilterStrategy = PermissionFilterStrategy.DATA_SCOPE
  15. name: Mapped[str] = mapped_column(String(100), nullable=False, unique=True, comment="租户名称")
  16. code: Mapped[str] = mapped_column(String(100), nullable=False, unique=True, comment="租户编码")
  17. start_time: Mapped[datetime | None] = mapped_column(
  18. DateTime, nullable=True, default=None, comment="开始时间"
  19. )
  20. end_time: Mapped[datetime | None] = mapped_column(
  21. DateTime, nullable=True, default=None, comment="结束时间"
  22. )
  23. @validates("name")
  24. def validate_name(self, key: str, name: str) -> str:
  25. if not name or not name.strip():
  26. raise ValueError("名称不能为空")
  27. return name
  28. @validates("code")
  29. def validate_code(self, key: str, code: str) -> str:
  30. if not code or not code.strip():
  31. raise ValueError("编码不能为空")
  32. if not code.isalnum():
  33. raise ValueError("编码只能包含字母和数字")
  34. return code