from dataclasses import dataclass from datetime import datetime from typing import Optional from sqlalchemy import Column, String, TIMESTAMP, func, Boolean from sqlalchemy.dialects.postgresql import BIGINT from database.database import Base @dataclass class THealthKnowledge(Base): __tablename__ = 't_health_knowledge' id: int = Column(BIGINT, primary_key=True, autoincrement=True, comment='序号') subject: Optional[str] = Column(String, nullable=True, comment='主题') knowledge: Optional[str] = Column(String, nullable=True, comment='知识内容') keywords: Optional[str] = Column(String, nullable=True, comment='关键字') url: Optional[str] = Column(String, nullable=True, comment='链接') is_used: bool = Column(Boolean, default=False, nullable=False, comment='表示数据条目是否已被使用') create_time: datetime = Column(TIMESTAMP(timezone=True), server_default=func.now(), nullable=False, comment='创建时间') def __repr__(self): return (f"") # 把未使用的数据按照create_time排序,获取最老的1条数据 def get_oldest_unused_data(db): unused_data = db.query(THealthKnowledge).filter(THealthKnowledge.is_used == False).order_by( THealthKnowledge.create_time).first() return unused_data # 把数据标记为已使用 def mark_data_as_used(db, data): data.is_used = True db.commit()