This file is a merged representation of a subset of the codebase, containing specifically included files, combined into a single document by Repomix.

================================================================
File Summary
================================================================

Purpose:
--------
This file contains a packed representation of the entire repository's contents.
It is designed to be easily consumable by AI systems for analysis, code review,
or other automated processes.

File Format:
------------
The content is organized as follows:
1. This summary section
2. Repository information
3. Directory structure
4. Multiple file entries, each consisting of:
  a. A separator line (================)
  b. The file path (File: path/to/file)
  c. Another separator line
  d. The full contents of the file
  e. A blank line

Usage Guidelines:
-----------------
- This file should be treated as read-only. Any changes should be made to the
  original repository files, not this packed version.
- When processing this file, use the file path to distinguish
  between different files in the repository.
- Be aware that this file may contain sensitive information. Handle it with
  the same level of security as you would the original repository.

Notes:
------
- Some files may have been excluded based on .gitignore rules and Repomix's configuration
- Binary files are not included in this packed representation. Please refer to the Repository Structure section for a complete list of file paths, including binary files
- Only files matching these patterns are included: models.py
- Files matching patterns in .gitignore are excluded
- Files matching default ignore patterns are excluded

Additional Info:
----------------

================================================================
Directory Structure
================================================================
models.py

================================================================
Files
================================================================

================
File: models.py
================
from datetime import datetime
from enum import Enum
from typing import List, Optional

from pydantic import BaseModel
from sqlalchemy import (
    Boolean,
    Column,
    Float,
    ForeignKey,
    String,
    Integer,
    DateTime,
    Enum as SQLAlchemyEnum,
    Text,
    UniqueConstraint,
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlmodel import Field, Relationship, SQLModel


class User(SQLModel, table=True):
    """用户信息表"""

    id: Optional[int] = Field(
        default=None,
        sa_column=Column(Integer, primary_key=True, autoincrement=True),
        description="用户ID",
    )
    username: str = Field(
        sa_column=Column(String(50), comment="用户名"), description="用户名"
    )
    nickname: str = Field(
        default="",
        sa_column=Column(String(50), comment="昵称"), description="昵称"
    )
    comment: str = Field(
        default="",
        sa_column=Column(String(255), comment="备注"), description="备注"
    )
    phone: str = Field(
        sa_column=Column(String(11), comment="手机号"), description="手机号"
    )
    password: str = Field(
        sa_column=Column(String(100), comment="密码(加密存储)"), description="密码"
    )
    created_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="创建时间"),
        description="创建时间",
    )
    updated_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="更新时间", onupdate=datetime.now()),
        description="更新时间",
    )
    tags: List[str] = Field(
        default=[], sa_column=Column(JSONB, comment="标签列表"), description="标签列表"
    )
    orders: List["Order"] = Relationship(
        back_populates="user", sa_relationship_kwargs={"cascade": "all, delete-orphan"}
    )
    assets: List["UserAsset"] = Relationship(
        back_populates="user", sa_relationship_kwargs={"cascade": "all, delete-orphan"}
    )
    agents: List["Agent"] = Relationship(
        back_populates="owner", sa_relationship_kwargs={"cascade": "all, delete-orphan"}
    )
    privileges: List["UserPrivilege"] = Relationship(
        back_populates="user", sa_relationship_kwargs={"cascade": "all, delete-orphan"}
    )
    __table_args__ = (
        UniqueConstraint("phone", name="uq_user_phone"),
        {"comment": "用户表"},
    )

    __tablename__ = "qu_users"


class Agent(SQLModel, table=True):
    """智能体"""

    class Type(str, Enum):
        CHAT = "chat"
        AGENT_CHAT = "agent-chat"
        WORKFLOW = "workflow"
        COMPLETION = "completion"

    id: str = Field(
        default=None,
        sa_column=Column(String, primary_key=True, comment="应用ID"),
        description="应用ID",
    )
    name: str = Field(
        sa_column=Column(String, index=True, comment="名称"), description="名称"
    )
    monthly_price: float = Field(
        default=0.0,
        sa_column=Column(Float, comment="月付价格"),
        description="月付价格",
    )
    original_monthly_price: float = Field(
        default=0.0,
        sa_column=Column(Float, comment="月付原价"),
        description="月付原价",
    )
    yearly_price: float = Field(
        default=0.0,
        sa_column=Column(Float, comment="年付价格"),
        description="年付价格",
    )
    original_yearly_price: float = Field(
        default=0.0,
        sa_column=Column(Float, comment="年付原价"),
        description="年付原价",
    )
    icon_url: Optional[str] = Field(
        default=None, sa_column=Column(String, comment="图标URL"), description="图标URL"
    )
    description: str = Field(
        sa_column=Column(String, comment="功能描述"), description="功能描述"
    )
    type: Type = Field(
        sa_column=Column(
            SQLAlchemyEnum(Type, name="agent_type_enum"),
            comment="应用模式",
        ),
        description="应用模式",
    )
    api_key: Optional[str] = Field(
        default=None,
        sa_column=Column(String, index=True, comment="API密钥"),
        description="最后同步的可用API密钥",
    )
    owner_id: Optional[int] = Field(
        default=None,
        sa_column=Column(Integer, ForeignKey("qu_users.id"), comment="创建者ID"),
        description="创建者ID",
    )
    tags: List[str] = Field(
        default=[], sa_column=Column(JSONB, comment="标签列表"), description="标签列表"
    )

    created_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="创建时间"),
        description="创建时间",
    )
    updated_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="更新时间", onupdate=datetime.now()),
        description="更新时间",
    )
    owner: Optional["User"] = Relationship(back_populates="agents")

    __table_args__ = {"comment": "Dify应用表"}
    __tablename__ = "qu_agents"


class Course(SQLModel, table=True):
    """
    课程表
    """

    id: Optional[int] = Field(
        default=None,
        sa_column=Column(Integer, primary_key=True, comment="课程ID"),
        description="ID",
    )

    title: str = Field(
        sa_column=Column(String, index=True, comment="课程标题"), description="课程标题"
    )

    description: str = Field(
        sa_column=Column(String, comment="课程描述"), description="课程描述"
    )

    price: float = Field(sa_column=Column(Float, comment="价格"), description="价格")
    tags: List[str] = Field(
        default=[], sa_column=Column(JSONB, comment="标签列表"), description="标签列表"
    )
    cover_image: Optional[str] = Field(
        default=None,
        sa_column=Column(String, comment="课程封面图片URL"),
        description="课程封面",
    )
    poster_url: Optional[str] = Field(
        default=None,
        sa_column=Column(String, comment="海报图片URL"),
        description="海报图片",
    )

    created_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="创建时间"),
        description="创建时间",
    )

    updated_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="更新时间", onupdate=datetime.now()),
        description="更新时间",
    )
    instructor: Optional[str] = Field(
        default=None, sa_column=Column(String, comment="导师"), description="导师"
    )

    # 添加这一行，建立与章节的关系
    sections: List["CourseSection"] = Relationship(back_populates="course")
    __table_args__ = {"comment": "课程信息表"}
    __tablename__ = "qu_courses"


class CourseSection(SQLModel, table=True):
    """
    课程章节表
    """

    id: Optional[int] = Field(
        default=None,
        sa_column=Column(Integer, primary_key=True, comment="章节ID"),
        description="ID",
    )

    title: str = Field(
        sa_column=Column(String, index=True, comment="章节标题"), description="章节标题"
    )

    duration: int = Field(
        default=0, sa_column=Column(Integer, comment="时长(秒)"), description="时长"
    )

    sort_order: int = Field(
        default=0, sa_column=Column(Integer, comment="排序"), description="排序"
    )

    is_free: bool = Field(
        default=False,
        sa_column=Column(Boolean, comment="是否免费"),
        description="是否免费",
    )

    video_url: Optional[str] = Field(
        default=None, sa_column=Column(String, comment="视频URL"), description="视频URL"
    )

    created_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="创建时间"),
        description="创建时间",
    )

    updated_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="更新时间", onupdate=datetime.now()),
        description="更新时间",
    )

    is_published: bool = Field(
        default=False,
        sa_column=Column(Boolean, comment="是否发布"),
        description="是否发布",
    )

    course_id: int = Field(
        sa_column=Column(Integer, ForeignKey("qu_courses.id"), comment="关联课程ID"),
        description="课程ID",
    )

    course: "Course" = Relationship(back_populates="sections")

    __table_args__ = {"comment": "课程章节表"}
    __tablename__ = "qu_course_sections"


class UserAsset(SQLModel, table=True):
    """用户资产表"""

    class Type(str, Enum):
        APP = "app"
        COURSE = "course"

    id: Optional[int] = Field(
        default=None,
        sa_column=Column(Integer, primary_key=True, autoincrement=True),
        description="资产ID",
    )
    user_id: int = Field(
        sa_column=Column(Integer, ForeignKey("qu_users.id"), comment="用户ID"),
        description="用户ID",
    )
    asset_type: Type = Field(
        default=Type.APP,
        sa_column=Column(
            SQLAlchemyEnum(Type, name="asset_type_enum"),
            comment="资产类型",
        ),
        description="资产类型",
    )
    app_mode: Optional[Agent.Type] = Field(
        default=None,
        sa_column=Column(
            SQLAlchemyEnum(Agent.Type, name="app_mode_enum"),
            comment="应用模式,当资产类型为APP时有效",
        ),
        description="应用模式",
    )
    asset_id: str = Field(
        sa_column=Column(String, comment="资产ID"), description="资产ID"
    )
    asset_name: str = Field(
        sa_column=Column(String, comment="资产名称"), description="资产名称"
    )
    quantity: int = Field(
        default=1,
        sa_column=Column(Integer, comment="数量"),
        description="数量",
    )

    expire_at: Optional[datetime] = Field(
        default=None,
        sa_column=Column(DateTime(), comment="有效期至"),
        description="有效期至",
    )
    created_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="创建时间"),
        description="创建时间",
    )
    updated_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="更新时间", onupdate=datetime.now()),
        description="更新时间",
    )
    user: "User" = Relationship(back_populates="assets")
    __table_args__ = {"comment": "用户资产表"}
    __tablename__ = "qu_user_assets"


class Order(SQLModel, table=True):
    """订单表"""

    class PaymentMethod(str, Enum):
        """支付方式枚举"""

        ALIPAY = "alipay"  # 支付宝
        WECHATPAY = "wechatpay"  # 微信支付

    class Item(BaseModel):
        """订单项目"""

        asset_type: Agent.Type
        asset_id: str
        quantity: int
        unit_price: str

    class Status(str, Enum):
        """订单状态枚举"""

        PENDING = "PENDING"  # 待支付
        PAID = "PAID"  # 已支付
        CANCELLED = "CANCELLED"  # 已取消
        REFUNDED = "REFUNDED"  # 已退款

    id: Optional[str] = Field(
        default=None,
        sa_column=Column(String, primary_key=True, comment="订单ID"),
        description="ID",
    )
    user_id: int = Field(
        sa_column=Column(Integer, ForeignKey("qu_users.id"), comment="用户ID"),
        description="用户ID",
    )

    amount: float = Field(
        sa_column=Column(Float, comment="订单金额"), description="订单金额"
    )

    status: str = Field(
        default=Status.PENDING,
        sa_column=Column(
            SQLAlchemyEnum(Status, name="order_status_enum" ),
            comment="订单状态",
        ),
        description="订单状态",
    )
    payment_method: PaymentMethod = Field(
        default=PaymentMethod.ALIPAY,
        sa_column=Column(
            SQLAlchemyEnum(PaymentMethod, name="payment_method_enum"),
            comment="支付方式",
        ),
        description="支付方式",
    )
    pay_time: Optional[datetime] = Field(
        default=None,
        sa_column=Column(DateTime(), comment="支付时间"),
        description="支付时间",
    )
    items: List[Item] = Field(
        default=[], sa_column=Column(JSONB, comment="订单项"), description="订单项"
    )
    created_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="创建时间"),
        description="创建时间",
    )

    updated_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="更新时间", onupdate=datetime.now()),
        description="更新时间",
    )
    user: "User" = Relationship(back_populates="orders")

    __table_args__ = {"comment": "订单信息表"}
    __tablename__ = "qu_orders"


class Admin(SQLModel, table=True):
    """管理员表"""

    id: Optional[int] = Field(
        default=None,
        sa_column=Column(
            Integer, primary_key=True, autoincrement=True, comment="管理员ID"
        ),
        description="管理员ID",
    )
    username: str = Field(
        sa_column=Column(String(50), unique=True, comment="用户名"),
        description="用户名",
    )
    password: str = Field(
        sa_column=Column(String(100), comment="密码"),
        description="密码",
    )
    avatar_url: Optional[str] = Field(
        default=None,
        sa_column=Column(Text, comment="头像URL"),
        description="头像URL",
    )
    created_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="创建时间"),
        description="创建时间",
    )
    updated_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="更新时间", onupdate=datetime.now()),
        description="更新时间",
    )

    __table_args__ = {"comment": "管理员表"}
    __tablename__ = "qu_admins"


class UserPrivilege(SQLModel, table=True):
    """用户权益表"""

    class PrivilegeType(str, Enum):
        """用户权益类型枚举"""

        CREATE_AGENT = "create_agent"  # 创建智能体
        SINGLE_CHAT = "single_chat"  # 单次对话聊天

    id: Optional[int] = Field(
        default=None,
        sa_column=Column(Integer, primary_key=True, autoincrement=True),
        description="权益ID",
    )
    user_id: int = Field(
        sa_column=Column(Integer, ForeignKey("qu_users.id"), index=True),
        description="用户ID",
    )
    privilege_type: PrivilegeType = Field(
        sa_column=Column(
            SQLAlchemyEnum(PrivilegeType, name="privilege_type_enum"),
            comment="权益类型",
        ),
        description="权益类型",
    )
    value: int = Field(
        sa_column=Column(Integer, comment="权益值"), description="权益值"
    )
    expires_at: Optional[datetime] = Field(
        default=None,
        sa_column=Column(DateTime(), comment="过期时间"),
        description="过期时间",
    )
    created_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="创建时间"),
        description="创建时间",
    )
    updated_at: datetime = Field(
        default_factory=lambda: datetime.now(),
        sa_column=Column(DateTime(), comment="更新时间", onupdate=datetime.now()),
        description="更新时间",
    )
    user: "User" = Relationship(back_populates="privileges")

    __table_args__ = (
        UniqueConstraint("user_id", "privilege_type", name="uix_user_privilege_type"),
        {"comment": "用户权益表"},
    )
    __tablename__ = "qu_user_privileges"



================================================================
End of Codebase
================================================================
