This commit is contained in:
77
database/thotcontent/crud.py
Normal file
77
database/thotcontent/crud.py
Normal file
@ -0,0 +1,77 @@
|
||||
from database.thotcontent.model import THotContent
|
||||
from log.log_manager import logger
|
||||
|
||||
|
||||
def create_hot_content(db, hot_content: THotContent):
|
||||
db.add(hot_content)
|
||||
db.commit()
|
||||
db.refresh(hot_content)
|
||||
return hot_content
|
||||
|
||||
# 插入数据库之前判断数据库中是否已经存在,根据news.url 判断
|
||||
def create_content_if_url_not_exists(db, hot_content: THotContent):
|
||||
# 检查是否已经存在具有相同 URL 的记录
|
||||
existing_content = db.query(THotContent).filter(THotContent.url == hot_content.url).first()
|
||||
|
||||
if existing_content:
|
||||
# 如果记录已存在,直接返回已有的记录
|
||||
return existing_content
|
||||
|
||||
# 如果记录不存在,插入新的记录
|
||||
db.add(hot_content)
|
||||
db.commit()
|
||||
db.refresh(hot_content)
|
||||
return hot_content
|
||||
|
||||
|
||||
def create_contents_top3_if_url_not_exists(db, contents: list[THotContent]):
|
||||
logger.info(f"采集到内容数量:{len(contents)},存入数据库前三")
|
||||
# 按照 THotContent.content_upvote_count 对contents进行排序
|
||||
contents.sort(key=lambda x: x.content_upvote_count, reverse=True)
|
||||
|
||||
# 保留 contents 的前3条
|
||||
contents = contents[:3]
|
||||
|
||||
inserted_contents = [] # 用于保存实际插入的新闻记录
|
||||
|
||||
for content in contents:
|
||||
# 检查是否已经存在具有相同 URL 的记录
|
||||
existing_content = db.query(THotContent).filter(THotContent.url == content.url).first()
|
||||
|
||||
if not existing_content:
|
||||
# 如果记录不存在,插入新的记录
|
||||
db.add(content)
|
||||
inserted_contents.append(content)
|
||||
|
||||
# 批量提交所有插入的记录
|
||||
db.commit()
|
||||
|
||||
# 刷新所有新插入的记录
|
||||
for content in inserted_contents:
|
||||
db.refresh(content)
|
||||
|
||||
return inserted_contents
|
||||
|
||||
def get_hot_content_by_id(db, hot_content_id: int):
|
||||
return db.query(THotContent).filter(THotContent.id == hot_content_id).first()
|
||||
|
||||
def get_hot_content_by_topic_id(db, topic_id: int):
|
||||
return db.query(THotContent).filter(THotContent.topic_id == topic_id).all()
|
||||
|
||||
def get_hot_contents(db, skip: int = 0, limit: int = 100):
|
||||
return db.query(THotContent).offset(skip).limit(limit).all()
|
||||
|
||||
def update_hot_content(db, hot_content_id: int, updates: dict):
|
||||
hot_content = db.query(THotContent).filter(THotContent.id == hot_content_id).first()
|
||||
if hot_content:
|
||||
for key, value in updates.items():
|
||||
setattr(hot_content, key, value)
|
||||
db.commit()
|
||||
db.refresh(hot_content)
|
||||
return hot_content
|
||||
|
||||
def delete_hot_content(db, hot_content_id: int):
|
||||
hot_content = db.query(THotContent).filter(THotContent.id == hot_content_id).first()
|
||||
if hot_content:
|
||||
db.delete(hot_content)
|
||||
db.commit()
|
||||
23
database/thotcontent/model.py
Normal file
23
database/thotcontent/model.py
Normal file
@ -0,0 +1,23 @@
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import Column, Integer, String, BIGINT, TIMESTAMP, func
|
||||
from sqlalchemy.dialects.postgresql import BIGINT
|
||||
|
||||
from database.database import Base
|
||||
|
||||
@dataclass
|
||||
class THotContent(Base):
|
||||
__tablename__ = 't_hot_content'
|
||||
|
||||
id: int = Column(BIGINT, primary_key=True, autoincrement=True, comment='序号')
|
||||
topic_id: int = Column(BIGINT, nullable=False, comment='关联话题ID')
|
||||
url: Optional[str] = Column(String, nullable=True, comment='内容链接')
|
||||
content: Optional[str] = Column(String, nullable=True, comment='内容详情')
|
||||
content_upvote_count: Optional[int] = Column(BIGINT, nullable=True, comment='内容点赞数量')
|
||||
content_comment_count: Optional[int] = Column(Integer, nullable=True, comment='内容评论数量')
|
||||
create_time: datetime = Column(TIMESTAMP(timezone=True), server_default=func.now(), nullable=False, comment='创建时间')
|
||||
|
||||
def __repr__(self):
|
||||
return f"<THotContent(id={self.id}, topic_id={self.topic_id}, url={self.url}, content_upvote_count={self.content_upvote_count})>"
|
||||
Reference in New Issue
Block a user