organization.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """组织机构模型。"""
  2. from __future__ import annotations
  3. from sqlalchemy import (
  4. BigInteger,
  5. CheckConstraint,
  6. Computed,
  7. ForeignKey,
  8. Index,
  9. Integer,
  10. String,
  11. UniqueConstraint,
  12. text,
  13. )
  14. from sqlalchemy.orm import Mapped, mapped_column
  15. from dms.database.base import BusinessTableMixin
  16. from dms.extensions import db
  17. class Organization(BusinessTableMixin, db.Model):
  18. __tablename__ = "sys_organization"
  19. __table_args__ = (
  20. UniqueConstraint(
  21. "org_code",
  22. "active_marker",
  23. name="uq_sys_organization_org_code_active",
  24. ),
  25. CheckConstraint(
  26. "status IN ('ENABLED', 'DISABLED')",
  27. name="organization_status",
  28. ),
  29. Index(
  30. "ix_sys_organization_parent_active_sort",
  31. "parent_id",
  32. "is_deleted",
  33. "sort_no",
  34. ),
  35. Index(
  36. "ix_sys_organization_status_active",
  37. "status",
  38. "is_deleted",
  39. ),
  40. )
  41. parent_id: Mapped[int | None] = mapped_column(
  42. BigInteger,
  43. ForeignKey("sys_organization.id", ondelete="RESTRICT"),
  44. nullable=True,
  45. )
  46. org_code: Mapped[str] = mapped_column(String(64), nullable=False)
  47. org_name: Mapped[str] = mapped_column(String(128), nullable=False)
  48. org_path: Mapped[str] = mapped_column(String(1000), nullable=False)
  49. sort_no: Mapped[int] = mapped_column(nullable=False, server_default=text("0"))
  50. status: Mapped[str] = mapped_column(
  51. String(20),
  52. nullable=False,
  53. server_default=text("'ENABLED'"),
  54. )
  55. active_marker: Mapped[int | None] = mapped_column(
  56. Integer,
  57. Computed("IF(is_deleted = 0, 1, NULL)", persisted=False),
  58. nullable=True,
  59. )