130 lines
4.5 KiB
Python
130 lines
4.5 KiB
Python
from database.tmaterial.model import TMaterial
|
||
from database.tnews.model import TNews
|
||
|
||
|
||
def create_material(db, material: TMaterial):
|
||
db.add(material)
|
||
db.commit()
|
||
db.refresh(material)
|
||
return material
|
||
|
||
def receive_news(db, news_list: list[TNews]):
|
||
material_list = []
|
||
for news in news_list:
|
||
material = TMaterial()
|
||
material.title = news.title
|
||
material.summary = news.summary
|
||
material.url = news.url
|
||
material.content = news.content
|
||
material.occurrence_date = news.occurrence_date
|
||
material.source = news.source
|
||
material.primary_category = news.primary_category
|
||
material.secondary_category = news.secondary_category
|
||
material.tertiary_category = news.tertiary_category
|
||
material.label = news.label
|
||
material_list.append(material)
|
||
create_materials_if_url_not_exists(db, material_list)
|
||
|
||
|
||
# 插入数据库之前判断数据库中是否已经存在,根据TMaterial.url 判断
|
||
def create_material_if_url_not_exists(db, material: TMaterial):
|
||
# 检查是否已经存在具有相同 URL 的记录
|
||
existing_material = db.query(TMaterial).filter(TMaterial.url == material.url).first()
|
||
|
||
if existing_material:
|
||
# 如果记录已存在,直接返回已有的记录
|
||
return existing_material
|
||
|
||
# 如果记录不存在,插入新的记录
|
||
db.add(material)
|
||
db.commit()
|
||
db.refresh(material)
|
||
return material
|
||
|
||
|
||
def create_materials_if_url_not_exists(db, material_list: list[TMaterial]):
|
||
inserted_materials = [] # 用于保存实际插入的新闻记录
|
||
|
||
for material in material_list:
|
||
# 检查是否已经存在具有相同 URL 的记录
|
||
existing_materials = db.query(TMaterial).filter(TMaterial.url == material.url).first()
|
||
|
||
if not existing_materials:
|
||
# 如果记录不存在,插入新的记录
|
||
db.add(material)
|
||
inserted_materials.append(material)
|
||
|
||
# 批量提交所有插入的记录
|
||
db.commit()
|
||
|
||
# 刷新所有新插入的记录
|
||
for material in inserted_materials:
|
||
db.refresh(material)
|
||
|
||
return inserted_materials
|
||
|
||
|
||
def get_material_by_id(db, material_id: int):
|
||
return db.query(TMaterial).filter(TMaterial.id == material_id).first()
|
||
|
||
|
||
def get_material_need_summary(db):
|
||
return db.query(TMaterial).filter(TMaterial.ai_summary == None).all()
|
||
|
||
|
||
def get_materials_for_generate_reference_message(db, news_type: str) -> list[TMaterial]:
|
||
return db.query(TMaterial).filter(
|
||
TMaterial.type == news_type,
|
||
TMaterial.ai_summary != None,
|
||
TMaterial.is_usage == False
|
||
).order_by(TMaterial.occurrence_date.desc()).all()
|
||
|
||
def get_materials_for_generate_real_estate_reference_message(db) -> list[TMaterial]:
|
||
return db.query(TMaterial).filter(
|
||
TMaterial.primary_category == '新闻类',
|
||
TMaterial.secondary_category == '经济类',
|
||
TMaterial.tertiary_category == '房地产',
|
||
TMaterial.ai_summary != None,
|
||
TMaterial.ai_summary != 'summary formate error',
|
||
TMaterial.is_usage == False
|
||
).order_by(TMaterial.occurrence_date.desc()).all()
|
||
|
||
def get_materials_for_generate_news(db) -> list[TMaterial]:
|
||
return db.query(TMaterial).filter(
|
||
TMaterial.label == '资讯',
|
||
TMaterial.ai_summary != None,
|
||
TMaterial.ai_summary != 'summary formate error',
|
||
TMaterial.is_usage == False
|
||
).order_by(TMaterial.occurrence_date.desc()).all()
|
||
|
||
def get_materials_for_generate_tech_reference_message(db) -> list[TMaterial]:
|
||
return db.query(TMaterial).filter(
|
||
TMaterial.primary_category == '新闻类',
|
||
TMaterial.secondary_category == '科技类',
|
||
TMaterial.ai_summary != None,
|
||
TMaterial.ai_summary != 'summary formate error',
|
||
TMaterial.is_usage == False
|
||
).order_by(TMaterial.occurrence_date.desc()).all()
|
||
|
||
def update_material_by_id(db, news: TMaterial):
|
||
db.merge(news)
|
||
db.commit()
|
||
|
||
|
||
def update_material(db, material_id: int, updates: dict):
|
||
material = db.query(TMaterial).filter(TMaterial.id == material_id).first()
|
||
if material:
|
||
for key, value in updates.items():
|
||
setattr(material, key, value)
|
||
db.commit()
|
||
db.refresh(material)
|
||
return material
|
||
|
||
|
||
def delete_material(db, material_id: int):
|
||
material = db.query(TMaterial).filter(TMaterial.id == material_id).first()
|
||
if material:
|
||
db.delete(material)
|
||
db.commit()
|
||
return material
|