diff --git a/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc b/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc index 4c4601c5..89f959b3 100644 Binary files a/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc and b/AI/Text2Sql/Model/__pycache__/biModel.cpython-310.pyc differ diff --git a/AI/Text2Sql/Model/biModel.py b/AI/Text2Sql/Model/biModel.py index 2fc3e79b..e9d615b5 100644 --- a/AI/Text2Sql/Model/biModel.py +++ b/AI/Text2Sql/Model/biModel.py @@ -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) # 获取数据集的字段名称 diff --git a/AI/Text2Sql/__pycache__/app.cpython-310.pyc b/AI/Text2Sql/__pycache__/app.cpython-310.pyc index 8e51a993..7ea2669c 100644 Binary files a/AI/Text2Sql/__pycache__/app.cpython-310.pyc and b/AI/Text2Sql/__pycache__/app.cpython-310.pyc differ diff --git a/AI/Text2Sql/app.py b/AI/Text2Sql/app.py index 4bad420a..ec60ef91 100644 --- a/AI/Text2Sql/app.py +++ b/AI/Text2Sql/app.py @@ -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 # 总数据量 + } + } # 获取数据库字段名 diff --git a/AI/Text2Sql/static/xlsx/655fa01e-58a7-45c9-8602-56e5b5d1cdcf.xlsx b/AI/Text2Sql/static/xlsx/655fa01e-58a7-45c9-8602-56e5b5d1cdcf.xlsx new file mode 100644 index 00000000..ca577c51 Binary files /dev/null and b/AI/Text2Sql/static/xlsx/655fa01e-58a7-45c9-8602-56e5b5d1cdcf.xlsx differ diff --git a/AI/Text2Sql/static/xlsx/a288b53d-f4af-4ba3-bc51-eb15ae4ec62d.xlsx b/AI/Text2Sql/static/xlsx/a288b53d-f4af-4ba3-bc51-eb15ae4ec62d.xlsx new file mode 100644 index 00000000..750be65c Binary files /dev/null and b/AI/Text2Sql/static/xlsx/a288b53d-f4af-4ba3-bc51-eb15ae4ec62d.xlsx differ