model.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. from sqlalchemy import String, Integer, ForeignKey, Boolean
  3. from sqlalchemy.orm import Mapped, mapped_column, relationship, validates
  4. from sqlalchemy.sql import expression
  5. from app.config.setting import settings
  6. from app.core.base_model import ModelMixin, UserMixin
  7. from app.utils.common_util import SqlalchemyUtil
  8. class GenTableModel(ModelMixin, UserMixin):
  9. """
  10. 代码生成表
  11. """
  12. __tablename__: str = 'gen_table'
  13. __table_args__: dict[str, str] = ({'comment': '代码生成表'})
  14. __loader_options__: list[str] = ["columns", "created_by", "updated_by"]
  15. table_name: Mapped[str] = mapped_column(String(200), nullable=False, default='', comment='表名称')
  16. table_comment: Mapped[str | None] = mapped_column(String(500), nullable=True, comment='表描述')
  17. class_name: Mapped[str] = mapped_column(String(100), nullable=False, default='', comment='实体类名称')
  18. package_name: Mapped[str | None] = mapped_column(String(100), nullable=True, comment='生成包路径')
  19. module_name: Mapped[str | None] = mapped_column(String(30), nullable=True, comment='生成模块名')
  20. business_name: Mapped[str | None] = mapped_column(String(30), nullable=True, comment='生成业务名')
  21. function_name: Mapped[str | None] = mapped_column(String(100), nullable=True, comment='生成功能名')
  22. sub_table_name: Mapped[str | None] = mapped_column(
  23. String(64),
  24. nullable=True,
  25. server_default=SqlalchemyUtil.get_server_default_null(settings.DATABASE_TYPE),
  26. comment='关联子表的表名'
  27. )
  28. sub_table_fk_name: Mapped[str | None] = mapped_column(
  29. String(64),
  30. nullable=True,
  31. server_default=SqlalchemyUtil.get_server_default_null(settings.DATABASE_TYPE),
  32. comment='子表关联的外键名'
  33. )
  34. parent_menu_id: Mapped[int | None] = mapped_column(Integer, nullable=True, comment='父菜单ID')
  35. # 关联关系
  36. columns: Mapped[list['GenTableColumnModel']] = relationship(
  37. order_by='GenTableColumnModel.sort',
  38. back_populates='table',
  39. cascade='all, delete-orphan'
  40. )
  41. @validates('table_name')
  42. def validate_table_name(self, key: str, table_name: str) -> str:
  43. """验证表名不为空"""
  44. if not table_name or not table_name.strip():
  45. raise ValueError('表名称不能为空')
  46. return table_name.strip()
  47. @validates('class_name')
  48. def validate_class_name(self, key: str, class_name: str) -> str:
  49. """验证类名不为空"""
  50. if not class_name or not class_name.strip():
  51. raise ValueError('实体类名称不能为空')
  52. return class_name.strip()
  53. class GenTableColumnModel(ModelMixin, UserMixin):
  54. """
  55. 代码生成表字段
  56. 数据隔离策略:
  57. - 继承自GenTableModel的隔离级别
  58. - 不需要customer_id
  59. 用于存储代码生成器的字段配置
  60. """
  61. __tablename__: str = 'gen_table_column'
  62. __table_args__: dict[str, str] = ({'comment': '代码生成表字段'})
  63. __loader_options__: list[str] = ["created_by", "updated_by"]
  64. # 数据库设计表字段
  65. column_name: Mapped[str] = mapped_column(String(200), nullable=False, comment='列名称')
  66. column_comment: Mapped[str | None] = mapped_column(String(500), nullable=True, comment='列描述')
  67. column_type: Mapped[str] = mapped_column(String(100), nullable=False, comment='列类型')
  68. column_length: Mapped[str | None] = mapped_column(String(50), nullable=True, comment='列长度')
  69. column_default: Mapped[str | None] = mapped_column(String(200), nullable=True, comment='列默认值')
  70. is_pk: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=expression.false(), comment='是否主键')
  71. is_increment: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=expression.false(), comment='是否自增')
  72. is_nullable: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=expression.true(), comment='是否允许为空')
  73. is_unique: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=expression.false(), comment='是否唯一')
  74. # Python字段映射
  75. python_type: Mapped[str | None] = mapped_column(String(100), nullable=True, comment='Python类型')
  76. python_field: Mapped[str | None] = mapped_column(String(200), nullable=True, comment='Python字段名')
  77. # 序列化配置
  78. is_insert: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=expression.true(), comment='是否为新增字段')
  79. is_edit: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=expression.true(), comment='是否编辑字段')
  80. is_list: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default=expression.true(), comment='是否列表字段')
  81. is_query: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default=expression.false(), comment='是否查询字段')
  82. query_type: Mapped[str | None] = mapped_column(String(50), nullable=True, default=None, comment='查询方式')
  83. # 前端展示配置
  84. html_type: Mapped[str | None] = mapped_column(String(100), nullable=True, default='input', comment='显示类型')
  85. dict_type: Mapped[str | None] = mapped_column(String(200), nullable=True, default='', comment='字典类型')
  86. # 排序和扩展配置
  87. sort: Mapped[int] = mapped_column(Integer, nullable=False, default=0, comment='排序')
  88. # 归属关系
  89. table_id: Mapped[int] = mapped_column(
  90. Integer,
  91. ForeignKey('gen_table.id', ondelete='CASCADE'),
  92. nullable=False,
  93. index=True,
  94. comment='归属表编号'
  95. )
  96. # 关联关系
  97. table: Mapped['GenTableModel'] = relationship(back_populates='columns')
  98. @validates('column_name')
  99. def validate_column_name(self, key: str, column_name: str) -> str:
  100. """验证列名不为空"""
  101. if not column_name or not column_name.strip():
  102. raise ValueError('列名称不能为空')
  103. return column_name.strip()
  104. @validates('column_type')
  105. def validate_column_type(self, key: str, column_type: str) -> str:
  106. """验证列类型不为空"""
  107. if not column_type or not column_type.strip():
  108. raise ValueError('列类型不能为空')
  109. return column_type.strip()