from datetime import datetime from sqlalchemy import String, Integer, Boolean, Text, ForeignKey, UniqueConstraint, DateTime from sqlalchemy.orm import Mapped, mapped_column, relationship from .base import Base, TimestampMixin, MYSQL_TABLE_ARGS class PromptTemplate(Base, TimestampMixin): __tablename__ = "biz_prompt_template" __table_args__ = MYSQL_TABLE_ARGS id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) name: Mapped[str] = mapped_column(String(100), nullable=False) category: Mapped[str] = mapped_column(String(50), nullable=False, default="summary") content: Mapped[str] = mapped_column(Text, nullable=False) description: Mapped[str | None] = mapped_column(String(255)) user_id: Mapped[int | None] = mapped_column(Integer, ForeignKey("sys_user.user_id", ondelete="SET NULL"), index=True) is_system: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False) status: Mapped[int] = mapped_column(Integer, default=1, nullable=False) sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False) user_configs = relationship("UserPromptConfig", back_populates="template", cascade="all, delete-orphan") class UserPromptConfig(Base, TimestampMixin): __tablename__ = "biz_user_prompt_config" __table_args__ = ( UniqueConstraint("user_id", "template_id", name="uk_user_template"), MYSQL_TABLE_ARGS, ) id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True) user_id: Mapped[int] = mapped_column(Integer, ForeignKey("sys_user.user_id", ondelete="CASCADE"), nullable=False, index=True) template_id: Mapped[int] = mapped_column(Integer, ForeignKey("biz_prompt_template.id", ondelete="CASCADE"), nullable=False, index=True) is_active: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False) user_sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False) template = relationship("PromptTemplate", back_populates="user_configs")