feat: add entitlement plans and topic sessions

This commit is contained in:
2026-07-31 15:22:11 +08:00
parent ad2161497f
commit 884dc765ad
27 changed files with 2389 additions and 15 deletions

View File

@@ -1,7 +1,8 @@
from app.models.admin import Admin, Role
from app.models.ai_config import ModelConfig, Prompt, SystemConfig
from app.models.base import Base
from app.models.chat import ChatMessage, ChatSession
from app.models.chat import ChatMessage, ChatSession, TopicSession
from app.models.entitlement import EntitlementPlan, UserEntitlement, UserEntitlementLog
from app.models.knowledge import (
HumanAttentionHistory,
HumanAttentionRecord,
@@ -28,6 +29,7 @@ __all__ = [
"Base",
"ChatMessage",
"ChatSession",
"EntitlementPlan",
"Knowledge",
"KnowledgeCard",
"KnowledgeChunk",
@@ -46,9 +48,12 @@ __all__ = [
"OperationLog",
"LogRetentionPolicy",
"StorageSnapshot",
"TopicSession",
"Prompt",
"Role",
"SystemConfig",
"User",
"UserEntitlement",
"UserEntitlementLog",
"UserKnowledgePermission",
]

View File

@@ -29,6 +29,7 @@ class ChatMessage(Base):
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
session_id: Mapped[int] = mapped_column(ForeignKey("sys_chat_session.id"), index=True, nullable=False)
topic_session_id: Mapped[int | None] = mapped_column(BigInteger, index=True, nullable=True)
user_id: Mapped[int] = mapped_column(ForeignKey("sys_user.id"), index=True, nullable=False)
role: Mapped[str] = mapped_column(String(20), nullable=False)
content: Mapped[str] = mapped_column(Text, nullable=False)
@@ -40,3 +41,25 @@ class ChatMessage(Base):
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), nullable=False)
session: Mapped[ChatSession] = relationship("ChatSession", back_populates="messages")
class TopicSession(Base):
__tablename__ = "sys_topic_session"
id: Mapped[int] = mapped_column(BigInteger().with_variant(Integer, "sqlite"), primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped_column(ForeignKey("sys_user.id"), index=True, nullable=False)
chat_session_id: Mapped[int] = mapped_column(ForeignKey("sys_chat_session.id"), index=True, nullable=False)
title: Mapped[str] = mapped_column(String(120), nullable=False)
core_question: Mapped[str] = mapped_column(Text, nullable=False)
status: Mapped[str] = mapped_column(String(20), default="active", index=True, nullable=False)
message_count: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
token_input: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
token_output: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
quota_deducted: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
recommended_homework: Mapped[str | None] = mapped_column(Text, nullable=True)
help_card_generated: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
share_draft_generated: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
started_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), nullable=False)
ended_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), nullable=False)
updated_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now(), onupdate=func.now(), nullable=False)

View File

@@ -0,0 +1,55 @@
from __future__ import annotations
from datetime import datetime
from sqlalchemy import BigInteger, DateTime, ForeignKey, Integer, String, Text
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base, TimestampMixin
PRIMARY_KEY_TYPE = BigInteger().with_variant(Integer, "sqlite")
class EntitlementPlan(Base, TimestampMixin):
__tablename__ = "sys_entitlement_plan"
id: Mapped[int] = mapped_column(PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(80), nullable=False)
plan_type: Mapped[str] = mapped_column(String(30), index=True, nullable=False)
description: Mapped[str | None] = mapped_column(String(255), nullable=True)
validity_days: Mapped[int | None] = mapped_column(Integer, nullable=True)
monthly_topic_limit: Mapped[int | None] = mapped_column(Integer, nullable=True)
enable_growth_profile: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
enable_periodic_reports: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
allow_help_card: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
allow_share_draft: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
deduct_quota: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
status: Mapped[int] = mapped_column(Integer, default=1, index=True, nullable=False)
sort_order: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
class UserEntitlement(Base, TimestampMixin):
__tablename__ = "sys_user_entitlement"
id: Mapped[int] = mapped_column(PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped_column(ForeignKey("sys_user.id"), index=True, nullable=False)
plan_id: Mapped[int] = mapped_column(ForeignKey("sys_entitlement_plan.id"), index=True, nullable=False)
status: Mapped[str] = mapped_column(String(20), default="active", index=True, nullable=False)
effective_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
expired_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
assigned_by: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
remark: Mapped[str | None] = mapped_column(String(255), nullable=True)
class UserEntitlementLog(Base):
__tablename__ = "sys_user_entitlement_log"
id: Mapped[int] = mapped_column(PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True)
user_id: Mapped[int] = mapped_column(BigInteger, index=True, nullable=False)
entitlement_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
from_plan_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
to_plan_id: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
action: Mapped[str] = mapped_column(String(30), nullable=False)
detail_json: Mapped[str | None] = mapped_column(Text, nullable=True)
operated_by: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, nullable=False)