From 0dbf74db9dd58018f1c93d5bd3ae771f7ca8e9b3 Mon Sep 17 00:00:00 2001 From: KiriAky 107 Date: Fri, 10 Apr 2026 01:27:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=BB=E5=8A=A1ID=E8=B7=9F?= =?UTF-8?q?=E8=B8=AA=E5=8A=9F=E8=83=BD=E5=88=B0=E6=A8=A1=E6=9D=BF=E5=A1=AB?= =?UTF-8?q?=E5=85=85=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在FillRequest中添加可选的task_id字段,用于任务历史跟踪 - 实现任务状态管理,包括创建、更新和错误处理 - 集成MongoDB任务记录功能,在处理过程中更新进度 - 添加任务进度更新逻辑,支持开始、处理中、成功和失败状态 - 修改模板填充服务以接收并传递task_id参数 --- backend/app/api/endpoints/templates.py | 48 ++++++++++++++++++- backend/app/services/template_fill_service.py | 4 +- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/backend/app/api/endpoints/templates.py b/backend/app/api/endpoints/templates.py index 0ef92d3..625b274 100644 --- a/backend/app/api/endpoints/templates.py +++ b/backend/app/api/endpoints/templates.py @@ -79,6 +79,7 @@ class FillRequest(BaseModel): source_doc_ids: Optional[List[str]] = None # MongoDB 文档 ID 列表 source_file_paths: Optional[List[str]] = None # 源文档文件路径列表 user_hint: Optional[str] = None + task_id: Optional[str] = None # 可选的任务ID,用于任务历史跟踪 class ExportRequest(BaseModel): @@ -439,7 +440,27 @@ async def fill_template( Returns: 填写结果 """ + # 生成或使用传入的 task_id + task_id = request.task_id or str(uuid.uuid4()) + try: + # 创建任务记录到 MongoDB + try: + await mongodb.insert_task( + task_id=task_id, + task_type="template_fill", + status="processing", + message=f"开始填表任务: {len(request.template_fields)} 个字段" + ) + except Exception as mongo_err: + logger.warning(f"MongoDB 创建任务记录失败: {mongo_err}") + + # 更新进度 - 开始 + await update_task_status( + task_id, "processing", + progress=0, message="开始处理..." + ) + # 转换字段 fields = [ TemplateField( @@ -461,6 +482,12 @@ async def fill_template( elif ext == "docx": template_file_type = "docx" + # 更新进度 - 准备开始填写 + await update_task_status( + task_id, "processing", + progress=10, message=f"准备填写 {len(fields)} 个字段..." + ) + # 执行填写 result = await template_fill_service.fill_template( template_fields=fields, @@ -468,12 +495,29 @@ async def fill_template( source_file_paths=request.source_file_paths, user_hint=request.user_hint, template_id=request.template_id, - template_file_type=template_file_type + template_file_type=template_file_type, + task_id=task_id ) - return result + # 更新为成功 + await update_task_status( + task_id, "success", + progress=100, message="填表完成", + result={ + "field_count": len(fields), + "max_rows": result.get("max_rows", 0) + } + ) + + return {**result, "task_id": task_id} except Exception as e: + # 更新为失败 + await update_task_status( + task_id, "failure", + progress=0, message="填表失败", + error=str(e) + ) logger.error(f"填写表格失败: {str(e)}") raise HTTPException(status_code=500, detail=f"填写失败: {str(e)}") diff --git a/backend/app/services/template_fill_service.py b/backend/app/services/template_fill_service.py index 9d18529..dfa5b20 100644 --- a/backend/app/services/template_fill_service.py +++ b/backend/app/services/template_fill_service.py @@ -62,7 +62,8 @@ class TemplateFillService: source_file_paths: Optional[List[str]] = None, user_hint: Optional[str] = None, template_id: Optional[str] = None, - template_file_type: Optional[str] = "xlsx" + template_file_type: Optional[str] = "xlsx", + task_id: Optional[str] = None ) -> Dict[str, Any]: """ 填写表格模板 @@ -74,6 +75,7 @@ class TemplateFillService: user_hint: 用户提示(如"请从合同文档中提取") template_id: 模板文件路径(用于重新生成表头) template_file_type: 模板文件类型 + task_id: 可选的任务ID,用于任务进度跟踪 Returns: 填写结果