From a320f55da059f452f7f53438d6ea69837acf1629 Mon Sep 17 00:00:00 2001 From: python Date: Thu, 11 Dec 2025 09:09:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E6=A1=A3=E7=94=9F?= =?UTF-8?q?=E6=88=90=E9=80=BB=E8=BE=91=EF=BC=8C=E6=94=B9=E7=94=A8=E6=96=87?= =?UTF-8?q?=E4=BB=B6ID=E6=9B=BF=E4=BB=A3=E6=A8=A1=E6=9D=BF=E7=BC=96?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E5=A2=9E=E5=BC=BA=E5=8F=82=E6=95=B0=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E5=92=8C=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=E3=80=82=E5=90=8C=E6=97=B6=EF=BC=8C=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E9=85=8D=E7=BD=AE=E6=9F=A5=E8=AF=A2=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E7=A1=AE=E4=BF=9D=E6=A0=B9=E6=8D=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6ID=E8=8E=B7=E5=8F=96=E6=96=87=E4=BB=B6=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=BF=A1=E6=81=AF=EF=BC=8C=E6=8F=90=E5=8D=87=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=8F=AF=E8=AF=BB=E6=80=A7=E5=92=8C=E7=BB=B4=E6=8A=A4?= =?UTF-8?q?=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 25 ++++---- .../document_service.cpython-312.pyc | Bin 12761 -> 12652 bytes services/document_service.py | 60 ++++++------------ test_document_generation_by_file_id.py | 41 ++++++++++++ 4 files changed, 73 insertions(+), 53 deletions(-) create mode 100644 test_document_generation_by_file_id.py diff --git a/app.py b/app.py index 66c9084..81c60e7 100644 --- a/app.py +++ b/app.py @@ -440,19 +440,17 @@ def generate_document(): description: 文件列表 items: type: object + required: + - fileId properties: fileId: type: integer - description: 文件ID - example: 1 + description: 文件配置ID(从f_polic_file_config表获取) + example: 1765273961563507 fileName: type: string - description: 文件名称 + description: 文件名称(可选,用于生成文档名称) example: 请示报告卡.doc - templateCode: - type: string - description: 模板编码 - example: REPORT_CARD responses: 200: description: 生成成功 @@ -499,7 +497,7 @@ def generate_document(): type: boolean example: true 1001: - description: 模板不存在 + description: 模板不存在或参数错误 schema: type: object properties: @@ -508,7 +506,7 @@ def generate_document(): example: 1001 errorMsg: type: string - example: 模板不存在 + example: 文件ID对应的模板不存在或未启用 isSuccess: type: boolean example: false @@ -575,15 +573,14 @@ def generate_document(): for file_info in file_list: file_id = file_info.get('fileId') file_name = file_info.get('fileName', '') - template_code = file_info.get('templateCode', '') - if not template_code: - return error_response(1001, f"文件 {file_name} 缺少templateCode参数") + if not file_id: + return error_response(1001, f"文件 {file_name} 缺少fileId参数") try: - # 生成文档 + # 生成文档(使用fileId而不是templateCode) result = document_service.generate_document( - template_code=template_code, + file_id=file_id, input_data=input_data, file_info=file_info ) diff --git a/services/__pycache__/document_service.cpython-312.pyc b/services/__pycache__/document_service.cpython-312.pyc index 8cb0c13fd0d07a22e945534da19f05d72b9b8550..15bc3b9e548887d3dc640bd89fe32795e331978f 100644 GIT binary patch delta 2840 zcmZ`)du&tJ8NW~4*Vpe?UJh}B9qc&7NubaIfrOAy9(77-l0i4pB=@Fslsfd3%k-tus3%|u1uG)E0_RQdk$ZVTBl10rzzs9o4T4_e1zzMt9&A(r zj>KM#U;wXC(1F(opK3FK%_Oj*RxpcZkyD~AA_rcsM<-apYf&O8!3MHbusKPCn=QVsU50+M37`=e25Ef(q6TYV`F=sl4)Vj8fh>q)iP>3lC$htOuj8+ z!znf&+F*q35K|W4{F4!E!h1mtX zgyc?Wp4hV{UW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s)40Fh-=;WtF-AnAuec=d$^93hGrzV6s=iU7=Nl0#`yu;ivoQEV*lp-xTiFW0k* zJ;ZZ2KK|0Tw&$bPlYTz5CQ3#m4-JTswB#ph@>DIEtGHRBJxc`HaA9pCK~cQL)kWRF z@4J4(N0BlOSj#~7Px#d**yM}+otDbZZVb`9;NMWn~q=9 zXk>JGnZ>AC;-rNqh`dE6n_AW`I_hMdtQFMPXqm?+s$Qme{Ie>bo-a-YuY_891;16* zifgOg_(m0vw^w@@UTOv1TH;;p1Nr;vaz-s}z;9I#;7Cn5qZyL$*_sx@Z>#tB^i8uoyYdtI`M2Y+*5R#FhK{1gI zCBlQE^mXDai4L?X?>5&0zUQ`dHL~|!A^LlbbXSY=-g+fqJfpPY4Zblasn_6_gsr^X{}iSa z#{c$}QTwsQznN0wuzwSEdGg2pw<&A>MLLSBfxji zBle|v*2MYe>u7HEyh_QbbEQ7cJ{8F!Xl@<3pPo)E5SZIbIk?T#1QgTf3&!c{sCYU!grHED%%f4GQmD)vEsiSp>NmSy~dM*YSs|PEtR? S?$youoh;>>M?^~N!2TZ^*67gy diff --git a/services/document_service.py b/services/document_service.py index 2f8b941..98dc499 100644 --- a/services/document_service.py +++ b/services/document_service.py @@ -50,54 +50,36 @@ class DocumentService: secure=self.minio_config['secure'] ) - def get_file_config_by_template_code(self, template_code: str) -> Optional[Dict]: + def get_file_config_by_id(self, file_id: int) -> Optional[Dict]: """ - 根据模板编码获取文件配置 + 根据文件ID获取文件配置 Args: - template_code: 模板编码,如 'PRELIMINARY_VERIFICATION_APPROVAL' + file_id: 文件配置ID Returns: - 文件配置信息,包含: id, name, file_path, template_code + 文件配置信息,包含: id, name, file_path """ - import json conn = self.get_connection() cursor = conn.cursor(pymysql.cursors.DictCursor) try: - # 查询文件配置(template_code可能存储在template_code列或input_data的JSON字段中) sql = """ - SELECT id, name, file_path, input_data, template_code + SELECT id, name, file_path FROM f_polic_file_config - WHERE tenant_id = %s + WHERE id = %s + AND tenant_id = %s AND state = 1 """ - cursor.execute(sql, (self.tenant_id,)) - configs = cursor.fetchall() + cursor.execute(sql, (file_id, self.tenant_id)) + config = cursor.fetchone() - # 查找匹配的template_code(优先检查template_code列,然后检查input_data JSON) - for config in configs: - # 方法1: 检查template_code列 - if config.get('template_code') == template_code: - return { - 'id': config['id'], - 'name': config['name'], - 'file_path': config['file_path'], - 'template_code': template_code - } - - # 方法2: 从input_data的JSON中查找匹配的template_code - try: - input_data = json.loads(config['input_data']) if config['input_data'] else {} - if input_data.get('template_code') == template_code: - return { - 'id': config['id'], - 'name': config['name'], - 'file_path': config['file_path'], - 'template_code': template_code - } - except (json.JSONDecodeError, TypeError): - continue + if config: + return { + 'id': config['id'], + 'name': config['name'], + 'file_path': config['file_path'] + } return None @@ -218,27 +200,27 @@ class DocumentService: except S3Error as e: raise Exception(f"上传文件到MinIO失败: {str(e)}") - def generate_document(self, template_code: str, input_data: List[Dict], file_info: Dict) -> Dict: + def generate_document(self, file_id: int, input_data: List[Dict], file_info: Dict) -> Dict: """ 生成文档 Args: - template_code: 模板编码 + file_id: 文件配置ID input_data: 输入数据列表,格式: [{'fieldCode': 'xxx', 'fieldValue': 'xxx'}] - file_info: 文件信息,格式: {'fileId': 1, 'fileName': 'xxx.doc', 'templateCode': 'xxx'} + file_info: 文件信息,格式: {'fileId': 1, 'fileName': 'xxx.doc'} Returns: 生成结果,包含: filePath """ # 获取文件配置 - file_config = self.get_file_config_by_template_code(template_code) + file_config = self.get_file_config_by_id(file_id) if not file_config: - raise Exception(f"模板编码 {template_code} 不存在") + raise Exception(f"文件ID {file_id} 对应的模板不存在或未启用") # 检查file_path是否存在 file_path = file_config.get('file_path') if not file_path: - raise Exception(f"模板编码 {template_code} 的文件路径(file_path)为空,请检查数据库配置") + raise Exception(f"文件ID {file_id} ({file_config.get('name', '')}) 的文件路径(file_path)为空,请检查数据库配置") # 将input_data转换为字典格式 field_data = {} diff --git a/test_document_generation_by_file_id.py b/test_document_generation_by_file_id.py new file mode 100644 index 0000000..e9d255e --- /dev/null +++ b/test_document_generation_by_file_id.py @@ -0,0 +1,41 @@ +""" +测试通过fileId生成文档(不再依赖templateCode) +""" +import sys +import os +sys.path.insert(0, os.path.dirname(__file__)) + +from services.document_service import DocumentService + +# 测试通过fileId获取文件配置 +print("="*80) +print("Test: Get file config by fileId") +print("="*80) + +service = DocumentService() + +# 测试查询一个已知的文件ID(从之前的查询结果中获取) +# "1.请示报告卡(初核谈话)" 的ID是 1765273963893166 +test_file_id = 1765273963893166 + +print(f"\nTest file ID: {test_file_id}") +print("-" * 80) + +result = service.get_file_config_by_id(test_file_id) + +if result: + print("\n[OK] Found file config:") + print(f" - ID: {result['id']}") + print(f" - Name: {result['name']}") + print(f" - File Path: {result['file_path']}") +else: + print("\n[ERROR] File config not found") + print(" Possible reasons:") + print(" 1. File ID does not exist") + print(" 2. File state is not enabled (state != 1)") + print(" 3. Tenant ID mismatch") + +print("\n" + "="*80) +print("Test completed") +print("="*80) +