You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
6.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# routes/DocumentController.py
import os
from fastapi import APIRouter, Request, Response, Depends, UploadFile, File
from auth.dependencies import get_current_user
from utils.PageUtil import *
from utils.ParseRequest import *
# 创建一个路由实例,需要依赖get_current_user,登录后才能访问
router = APIRouter(dependencies=[Depends(get_current_user)])
# 创建上传文件的目录
UPLOAD_DIR = "upload_file"
if not os.path.exists(UPLOAD_DIR):
os.makedirs(UPLOAD_DIR)
# 合法文件扩展名
supported_suffix_types = ['doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx']
# 【Document-1】文档管理列表
@router.get("/list")
async def list(request: Request):
# 获取参数
person_id = await get_request_str_param(request, "person_id", True, True)
stage_id = await get_request_num_param(request, "stage_id", False, True, -1)
subject_id = await get_request_num_param(request, "subject_id", False, True, -1)
document_suffix = await get_request_str_param(request, "document_suffix", False, True)
document_name = await get_request_str_param(request, "document_name", False, True)
page_number = await get_request_num_param(request, "page_number", False, True, 1)
page_size = await get_request_num_param(request, "page_size", False, True, 10)
print(person_id, stage_id, subject_id, document_suffix, document_name, page_number, page_size)
# 拼接查询SQL语句
select_document_sql: str = " SELECT * FROM t_ai_teaching_model_document WHERE is_deleted = 0 and person_id = '" + person_id + "'"
if stage_id != -1:
select_document_sql += " AND stage_id = " + str(stage_id)
if subject_id != -1:
select_document_sql += " AND subject_id = " + str(subject_id)
if document_suffix != "":
select_document_sql += " AND document_suffix = '" + document_suffix + "'"
if document_name != "":
select_document_sql += " AND document_name = '" + document_name + "'"
select_document_sql += " ORDER BY create_time DESC "
# 查询文档列表
page = await get_page_data_by_sql(select_document_sql, page_number, page_size)
for item in page["list"]:
theme_info = await find_by_id("t_ai_teaching_model_theme", "id", item["theme_id"])
item["theme_info"] = theme_info
return {"success": True, "message": "查询成功!", "data": page}
# 【Document-2】保存文档管理
@router.post("/save")
async def save(request: Request, file: UploadFile = File(...)):
# 获取参数
id = await get_request_num_param(request, "id", False, True, 0)
stage_id = await get_request_num_param(request, "stage_id", False, True, -1)
subject_id = await get_request_num_param(request, "subject_id", False, True, -1)
theme_id = await get_request_num_param(request, "theme_id", True, True, None)
person_id = await get_request_str_param(request, "person_id", True, True)
bureau_id = await get_request_str_param(request, "bureau_id", True, True)
# 先获取theme主题信息
theme_object = await find_by_id("t_ai_teaching_model_theme", "id", theme_id)
if theme_object is None:
return {"success": False, "message": "主题不存在!"}
# 获取文件名
document_name = file.filename.split(".")[0]
# 检查文件名在该主题下是否重复
select_theme_document_sql: str = "SELECT * FROM t_ai_teaching_model_document WHERE is_deleted = 0 and document_name = '" + document_name + "' and theme_id = " + str(theme_id)
if id != 0:
select_theme_document_sql += " AND id <> " + id
theme_document = await find_by_sql(select_theme_document_sql, ())
if theme_document is not None:
return {"success": False, "message": "该主题下文档名称重复!"}
# 获取文件扩展名
document_suffix = file.filename.split(".")[-1]
# 检查文件扩展名
if document_suffix not in supported_suffix_types:
return {"success": False, "message": "不支持的文件类型!"}
# 构造文件保存路径
document_dir = UPLOAD_DIR + "/" + str(theme_object["short_name"]) + "_" + str(theme_object["id"]) + "/"
if not os.path.exists(document_dir):
os.makedirs(document_dir)
document_path = os.path.join(document_dir, file.filename)
# 保存文件
try:
with open(document_path, "wb") as buffer:
buffer.write(await file.read())
except Exception as e:
return {"success": False, "message": f"文件保存失败!{e}"}
# 构造保存文档SQL语句
param = {"stage_id": stage_id, "subject_id": subject_id, "document_name": document_name, "theme_id": theme_id, "document_path": document_path, "document_suffix": document_suffix, "person_id": person_id, "bureau_id": bureau_id}
# 保存数据
if id == 0:
param["train_flag"] = 0
# 插入数据
id = await insert("t_ai_teaching_model_document", param, False)
return {"success": True, "message": "保存成功!", "data": {"insert_id" : id}}
else:
# 更新数据
await update("t_ai_teaching_model_document", param, "id", id)
return {"success": True, "message": "更新成功!", "data": {"update_id" : id}}
# 【Document-3】获取文档信息
@router.get("/get")
async def get(request: Request):
# 获取参数
id = await get_request_num_param(request, "id", True, True, None)
# 查询数据
document_object = await find_by_id("t_ai_teaching_model_document", "id", id)
if document_object is None:
return {"success": False, "message": "未查询到该文档信息!"}
theme_info = await find_by_id("t_ai_teaching_model_theme", "id", document_object["theme_id"])
document_object["theme_info"] = theme_info
return {"success": True, "message": "查询成功!", "data": {"document": document_object}}
# 功能【Document-4】删除文档信息
# 作者Kalman.CHENG ☆
# 时间2025-07-23
# 备注:
@router.post("/delete")
async def delete(request: Request):
# 获取参数
id = await get_request_num_param(request, "id", True, True, None)
document_object = await find_by_id("t_ai_teaching_model_document", "id", id)
if document_object is None:
return {"success": False, "message": "未查询到该文档信息!"}
# 删除文件
train_flag = document_object["train_flag"]
if train_flag == 0:
# 未训练的文档,直接删除文件
await update_batch_property("t_ai_teaching_model_document",
{"is_deleted": 1, "train_flag": 6}, {"is_deleted": 0, "id": id}, False)
elif train_flag == 1:
# 正在训练的文档,不能删除
return {"success": False, "message": "该文档正在训练中,不能删除!"}
elif train_flag == 2:
# 训练完成的文档,删除文件
await update_batch_property("t_ai_teaching_model_document",
{"is_deleted": 1, "train_flag": 3}, {"is_deleted": 0, "id": id}, False)
return {"success": True, "message": "删除成功!"}