main
HuangHai 4 months ago
parent 1bc1a0f699
commit 2fc8b1e7ed

@ -48,11 +48,13 @@ async def get_question_by_id(db: asyncpg.Connection, question_id: str):
_data = await db.fetch(select_sql, question_id)
return _data
# 根据 SQL 查询数据
async def get_data_by_sql(db: asyncpg.Connection, sql: str):
_data = await db.fetch(sql)
return _data
# 保存系统推荐
async def set_system_recommend_questions(db: asyncpg.Connection, question_id: str, flag: str):
sql = """
@ -82,21 +84,45 @@ async def set_user_collect_questions(db: asyncpg.Connection, question_id: str, f
# 查询系统推荐问题
async def get_system_recommend_questions(db: asyncpg.Connection):
sql = """
SELECT * FROM t_bi_question WHERE is_system = 1
async def get_system_recommend_questions(db: asyncpg.Connection, offset: int, limit: int):
query = """
SELECT *
FROM t_bi_question where is_system=1 ORDER BY id DESC LIMIT $1 OFFSET $2;
"""
_data = await db.fetch(sql)
return _data
return await db.fetch(query, limit, offset)
# 查询用户收藏问题
async def get_user_collect_questions(db: asyncpg.Connection):
sql = """
SELECT * FROM t_bi_question WHERE is_collect = 1
async def get_system_recommend_questions_count(db: asyncpg.Connection):
query = """
SELECT COUNT(*)
FROM t_bi_question where is_system=1;
"""
_data = await db.fetch(sql)
return _data
return await db.fetchval(query)
async def get_user_publish_questions(db: asyncpg.Connection, type_id: int, offset: int, limit: int):
# 基础查询
query = """
SELECT *
FROM t_bi_question
"""
# 根据 type_id 动态添加 WHERE 条件
if type_id == 1:
query += " WHERE is_collect = 1"
# 添加排序和分页
query += " ORDER BY id DESC LIMIT $1 OFFSET $2;"
# 执行查询
return await db.fetch(query, limit, offset)
async def get_user_publish_questions_count(db: asyncpg.Connection):
query = """
SELECT COUNT(*) FROM t_bi_question;
"""
return await db.fetchval(query)
# 获取数据集的字段名称

@ -59,12 +59,14 @@ async def get_db():
async with app.state.pool.acquire() as connection:
yield connection
# 初始化 OpenAI 客户端
client = AsyncOpenAI(
api_key=MODEL_API_KEY,
base_url=MODEL_API_URL,
)
@app.post("/questions/get_excel")
async def get_excel(question_id: str = Form(...), question_str: str = Form(...),
db: asyncpg.Connection = Depends(get_db)):
@ -261,19 +263,58 @@ async def set_user_collect(
@app.get("/questions/get_system_recommend")
async def get_system_recommend(db: asyncpg.Connection = Depends(get_db)): # 添加 db 参数
# 查询所有系统推荐问题
system_recommend_questions = await get_system_recommend_questions(db)
async def get_system_recommend(
db: asyncpg.Connection = Depends(get_db), # 数据库连接
page: int = Query(1, description="当前页码,默认为 1"), # 页码参数
page_size: int = Query(10, description="每页数据量,默认为 10") # 每页数据量参数
):
# 计算 OFFSET
offset = (page - 1) * page_size
# 查询分页后的系统推荐问题
system_recommend_questions = await get_system_recommend_questions(db, offset, page_size)
# 查询总数据量
total = await get_system_recommend_questions_count(db)
# 返回查询结果
return {"success": True, "data": system_recommend_questions}
return {
"success": True,
"data": system_recommend_questions,
"pagination": {
"page": page,
"page_size": page_size,
"total": total # 总数据量
}
}
@app.get("/questions/get_user_questions")
async def get_user_questions(
db: asyncpg.Connection = Depends(get_db), # 数据库连接
page: int = Query(1, description="当前页码,默认为 1"), # 页码参数
page_size: int = Query(10, description="每页数据量,默认为 10"), # 每页数据量参数
type_id: int = Query(1, description="1用户收藏的问题0全部")
):
# 计算 OFFSET
offset = (page - 1) * page_size
# 查询分页后的用户收藏问题
user_questions = await get_user_publish_questions(db, type_id, offset, page_size)
# 查询总数据量
total = await get_user_publish_questions_count(db)
@app.get("/questions/get_user_collect")
async def get_user_collect(db: asyncpg.Connection = Depends(get_db)): # 添加 db 参数
# 查询所有用户收藏问题
user_collect_questions = await get_user_collect_questions(db)
# 返回查询结果
return {"success": True, "data": user_collect_questions}
return {
"success": True,
"data": user_questions,
"pagination": {
"page": page,
"page_size": page_size,
"total": total # 总数据量
}
}
# 获取数据库字段名

Loading…
Cancel
Save