添加数据库模块
This commit is contained in:
98
api/database/models.py
Normal file
98
api/database/models.py
Normal file
@@ -0,0 +1,98 @@
|
||||
"""
|
||||
数据库模型定义
|
||||
"""
|
||||
from datetime import datetime
|
||||
from typing import Optional, List
|
||||
from sqlalchemy import Column, String, Integer, DateTime, Boolean, Text, ForeignKey, JSON
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
class User(Base):
|
||||
"""用户表"""
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(String, primary_key=True)
|
||||
openid = Column(String, unique=True, nullable=True) # 微信 OpenID
|
||||
nickname = Column(String, nullable=False)
|
||||
avatar_url = Column(String, nullable=True)
|
||||
subscription_type = Column(String, default="free") # free, premium
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
conversations = relationship("Conversation", back_populates="user")
|
||||
chapters = relationship("Chapter", back_populates="user")
|
||||
books = relationship("Book", back_populates="user")
|
||||
|
||||
|
||||
class Conversation(Base):
|
||||
"""对话会话表"""
|
||||
__tablename__ = "conversations"
|
||||
|
||||
id = Column(String, primary_key=True)
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
started_at = Column(DateTime, default=datetime.utcnow)
|
||||
ended_at = Column(DateTime, nullable=True)
|
||||
duration_seconds = Column(Integer, default=0)
|
||||
summary = Column(Text, nullable=True)
|
||||
status = Column(String, default="active") # active, ended, processing
|
||||
current_topic = Column(String, nullable=True)
|
||||
conversation_stage = Column(String, nullable=True) # childhood, education, career, family, beliefs, summary
|
||||
|
||||
# Relationships
|
||||
user = relationship("User", back_populates="conversations")
|
||||
segments = relationship("Segment", back_populates="conversation", cascade="all, delete-orphan")
|
||||
|
||||
|
||||
class Segment(Base):
|
||||
"""对话段落表"""
|
||||
__tablename__ = "segments"
|
||||
|
||||
id = Column(String, primary_key=True)
|
||||
conversation_id = Column(String, ForeignKey("conversations.id"), nullable=False)
|
||||
audio_url = Column(String, nullable=True)
|
||||
transcript_text = Column(Text, nullable=False)
|
||||
created_at = Column(DateTime, default=datetime.utcnow)
|
||||
processed = Column(Boolean, default=False)
|
||||
topic_category = Column(String, nullable=True)
|
||||
agent_response = Column(Text, nullable=True)
|
||||
|
||||
# Relationships
|
||||
conversation = relationship("Conversation", back_populates="segments")
|
||||
|
||||
|
||||
class Chapter(Base):
|
||||
"""章节表"""
|
||||
__tablename__ = "chapters"
|
||||
|
||||
id = Column(String, primary_key=True)
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
title = Column(String, nullable=False)
|
||||
content = Column(Text, nullable=False)
|
||||
order_index = Column(Integer, nullable=False)
|
||||
status = Column(String, default="draft") # draft, completed
|
||||
images = Column(JSON, nullable=True) # 图片 URL 列表
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
category = Column(String, nullable=True) # 章节分类
|
||||
|
||||
# Relationships
|
||||
user = relationship("User", back_populates="chapters")
|
||||
|
||||
|
||||
class Book(Base):
|
||||
"""回忆录表"""
|
||||
__tablename__ = "books"
|
||||
|
||||
id = Column(String, primary_key=True)
|
||||
user_id = Column(String, ForeignKey("users.id"), nullable=False)
|
||||
title = Column(String, nullable=False)
|
||||
total_pages = Column(Integer, default=0)
|
||||
total_words = Column(Integer, default=0)
|
||||
cover_image_url = Column(String, nullable=True)
|
||||
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
user = relationship("User", back_populates="books")
|
||||
|
||||
Reference in New Issue
Block a user