""" ORM 模型定义。 所有表使用 mac_ 前缀,与现有表隔离。 """ from sqlalchemy import Column, String, Date, DateTime, Float, BigInteger, Index, PrimaryKeyConstraint, Text from sqlalchemy.orm import DeclarativeBase from config.settings import TABLE_STOCK_BASIC, TABLE_STOCK_DAILY, TABLE_STOCK_FINANCIAL, TABLE_REPORT class Base(DeclarativeBase): pass class StockBasic(Base): """股票基本信息表。""" __tablename__ = TABLE_STOCK_BASIC ts_code = Column(String(16), primary_key=True, comment="股票代码(如 000001.SZ)") name = Column(String(32), comment="股票名称") area = Column(String(16), comment="地区") industry = Column(String(32), comment="行业") market = Column(String(8), comment="市场(主板/创业板/科创板)") list_date = Column(String(8), comment="上市日期") is_hs = Column(String(1), comment="是否沪深港通") class StockDaily(Base): """日线行情表。""" __tablename__ = TABLE_STOCK_DAILY __table_args__ = ( PrimaryKeyConstraint("ts_code", "trade_date"), Index("idx_mac_daily_ts_code", "ts_code"), Index("idx_mac_daily_trade_date", "trade_date"), ) ts_code = Column(String(16), comment="股票代码") trade_date = Column(String(8), comment="交易日期") open = Column(Float, comment="开盘价") high = Column(Float, comment="最高价") low = Column(Float, comment="最低价") close = Column(Float, comment="收盘价") pre_close = Column(Float, comment="昨收价") change = Column(Float, comment="涨跌额") pct_chg = Column(Float, comment="涨跌幅(%)") vol = Column(Float, comment="成交量(手)") amount = Column(Float, comment="成交额(千元)") turnover_rate = Column(Float, comment="换手率(%)") class StockFinancial(Base): """财务数据表(同花顺核心指标)。""" __tablename__ = TABLE_STOCK_FINANCIAL __table_args__ = ( PrimaryKeyConstraint("ts_code", "end_date"), Index("idx_mac_fina_ts_code", "ts_code"), ) ts_code = Column(String(16), comment="股票代码") end_date = Column(String(8), comment="报告期 YYYYMMDD") eps = Column(Float, comment="基本每股收益") bvps = Column(Float, comment="每股净资产") roe = Column(Float, comment="净资产收益率(%)") roe_diluted = Column(Float, comment="净资产收益率-摊薄(%)") net_profit_margin = Column(Float, comment="销售净利率(%)") debt_to_assets = Column(Float, comment="资产负债率(%)") current_ratio = Column(Float, comment="流动比率") quick_ratio = Column(Float, comment="速动比率") total_revenue = Column(Float, comment="营业总收入") total_revenue_yoy = Column(Float, comment="营业总收入同比增长率(%)") net_profit = Column(Float, comment="净利润") net_profit_yoy = Column(Float, comment="净利润同比增长率(%)") class Report(Base): """报告存储表。""" __tablename__ = TABLE_REPORT __table_args__ = ( Index("idx_mac_report_date", "report_date"), Index("idx_mac_report_subject", "subject_type", "subject_code"), Index("idx_mac_report_active", "is_active"), ) id = Column(BigInteger, primary_key=True, autoincrement=True, comment="主键") report_date = Column(Date, nullable=False, comment="报告日期") title = Column(String(256), nullable=False, comment="报告标题") subject_type = Column(String(32), comment="研究对象类型: stock/index/sector/portfolio/daily") subject_code = Column(String(64), comment="研究对象代码") content = Column(Text, comment="报告内容 (markdown)") created_at = Column(DateTime, comment="报告生成时间") is_active = Column(Float, default=1.0, comment="1=有效, 0=已失效") def create_all_tables(): """创建所有 mac_ 开头的表。""" engine = __import__("database.connection", fromlist=["get_engine"]).get_engine() Base.metadata.create_all(engine) print("[OK] 所有表创建完成")