history_models.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. """
  2. 分析历史记录 ORM 模型(实现方案约定文件名)
  3. """
  4. from sqlalchemy import Column, Integer, String, Text, DateTime, func
  5. from app.models.database import Base
  6. class AnalysisHistory(Base):
  7. """分析历史记录表 — 按天存储各类分析报告"""
  8. __tablename__ = "analysis_history"
  9. id = Column(Integer, primary_key=True, autoincrement=True, comment="记录ID")
  10. user_id = Column(String(64), nullable=False, default="default", comment="用户标识")
  11. date = Column(String(16), nullable=False, comment="日期 yyyy-mm-dd")
  12. type = Column(String(32), nullable=False, comment="类型: sentiment/data_analysis/buffett/chat")
  13. stock_code = Column(String(16), nullable=True, comment="股票代码")
  14. stock_name = Column(String(64), nullable=True, comment="股票名称")
  15. title = Column(String(256), nullable=True, comment="标题")
  16. content = Column(Text, nullable=False, comment="内容(Markdown格式)")
  17. created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
  18. def to_dict(self) -> dict:
  19. return {
  20. "id": self.id,
  21. "user_id": self.user_id,
  22. "date": self.date,
  23. "type": self.type,
  24. "stock_code": self.stock_code,
  25. "stock_name": self.stock_name,
  26. "title": self.title,
  27. "content": self.content,
  28. "created_at": self.created_at.isoformat() if self.created_at else None,
  29. }