""" 验证文档生成接口可以正确生成文档 测试模板和字段关联是否正确 """ import sys import os import json import pymysql sys.path.insert(0, os.path.dirname(__file__)) from services.document_service import DocumentService # 数据库连接配置 DB_CONFIG = { 'host': '152.136.177.240', 'port': 5012, 'user': 'finyx', 'password': '6QsGK6MpePZDE57Z', 'database': 'finyx', 'charset': 'utf8mb4' } TENANT_ID = 615873064429507639 def get_template_by_name(conn, template_name: str): """根据模板名称获取模板信息""" cursor = conn.cursor(pymysql.cursors.DictCursor) try: sql = """ SELECT id, name, file_path, state FROM f_polic_file_config WHERE tenant_id = %s AND name = %s AND state = 1 """ cursor.execute(sql, (TENANT_ID, template_name)) return cursor.fetchone() finally: cursor.close() def get_template_fields(conn, file_id: int): """获取模板关联的字段""" cursor = conn.cursor(pymysql.cursors.DictCursor) try: sql = """ SELECT f.id, f.name, f.filed_code, f.field_type FROM f_polic_field f INNER JOIN f_polic_file_field fff ON f.id = fff.filed_id WHERE fff.file_id = %s AND fff.tenant_id = %s AND fff.state = 1 ORDER BY f.field_type, f.filed_code """ cursor.execute(sql, (file_id, TENANT_ID)) return cursor.fetchall() finally: cursor.close() def test_document_generation(template_name: str, test_data: list): """测试文档生成""" print("=" * 80) print(f"测试文档生成: {template_name}") print("=" * 80) # 连接数据库 conn = pymysql.connect(**DB_CONFIG) try: # 获取模板信息 template = get_template_by_name(conn, template_name) if not template: print(f"[ERROR] 未找到模板: {template_name}") return False print(f"\n模板信息:") print(f" ID: {template['id']}") print(f" 名称: {template['name']}") print(f" 文件路径: {template['file_path']}") print(f" 状态: {template['state']}") # 获取模板关联的字段 fields = get_template_fields(conn, template['id']) print(f"\n关联的字段数量: {len(fields)}") if fields: print(" 字段列表:") for field in fields[:10]: # 只显示前10个 field_type = "输出字段" if field['field_type'] == 2 else "输入字段" print(f" - {field['name']} ({field['filed_code']}) [{field_type}]") if len(fields) > 10: print(f" ... 还有 {len(fields) - 10} 个字段") # 准备测试数据 print(f"\n测试数据字段数量: {len(test_data)}") # 创建文档服务 doc_service = DocumentService() # 准备文件信息 file_info = { 'fileId': template['id'], 'fileName': f"{template_name}.doc" } print(f"\n开始生成文档...") # 生成文档 try: result = doc_service.generate_document( file_id=template['id'], input_data=test_data, file_info=file_info ) print(f"[OK] 文档生成成功!") print(f"\n生成结果:") print(f" 文件路径: {result.get('filePath')}") print(f" 文件名称: {result.get('fileName')}") if result.get('downloadUrl'): print(f" 下载URL: {result.get('downloadUrl')[:80]}...") return True except Exception as e: print(f"[ERROR] 文档生成失败: {str(e)}") import traceback traceback.print_exc() return False finally: conn.close() def main(): """主函数""" print("=" * 80) print("验证文档生成功能") print("=" * 80) print() # 测试数据 test_data = [ {"fieldCode": "target_name", "fieldValue": "张三"}, {"fieldCode": "target_gender", "fieldValue": "男"}, {"fieldCode": "target_age", "fieldValue": "44"}, {"fieldCode": "target_date_of_birth", "fieldValue": "198005"}, {"fieldCode": "target_organization_and_position", "fieldValue": "某公司总经理"}, {"fieldCode": "target_organization", "fieldValue": "某公司"}, {"fieldCode": "target_position", "fieldValue": "总经理"}, {"fieldCode": "target_education_level", "fieldValue": "本科"}, {"fieldCode": "target_political_status", "fieldValue": "中共党员"}, {"fieldCode": "target_professional_rank", "fieldValue": "正处级"}, {"fieldCode": "clue_source", "fieldValue": "群众举报"}, {"fieldCode": "target_issue_description", "fieldValue": "违反国家计划生育有关政策规定,于2010年10月生育二胎。"}, {"fieldCode": "department_opinion", "fieldValue": "建议进行初步核实"}, {"fieldCode": "filler_name", "fieldValue": "李四"}, {"fieldCode": "target_id_number", "fieldValue": "110101198005011234"}, {"fieldCode": "target_contact", "fieldValue": "13800138000"}, {"fieldCode": "target_work_basic_info", "fieldValue": "在某公司工作10年,担任总经理职务"}, {"fieldCode": "target_family_situation", "fieldValue": "已婚,有一子一女"}, {"fieldCode": "target_social_relations", "fieldValue": "社会关系简单"}, {"fieldCode": "investigation_unit_name", "fieldValue": "某市纪委监委"}, {"fieldCode": "investigation_team_leader_name", "fieldValue": "王五"}, {"fieldCode": "investigation_team_member_names", "fieldValue": "赵六、钱七"}, {"fieldCode": "investigation_team_code", "fieldValue": "DC2024001"}, {"fieldCode": "investigation_location", "fieldValue": "某市纪委监委谈话室"}, {"fieldCode": "appointment_time", "fieldValue": "2024年12月10日上午9:00"}, {"fieldCode": "appointment_location", "fieldValue": "某市纪委监委谈话室"}, {"fieldCode": "approval_time", "fieldValue": "2024年12月9日"}, {"fieldCode": "handling_department", "fieldValue": "某市纪委监委第一监督检查室"}, {"fieldCode": "handler_name", "fieldValue": "王五"}, ] # 测试几个关键模板 test_templates = [ "初步核实审批表", "请示报告卡", "谈话通知书第一联", "谈话前安全风险评估表" ] success_count = 0 failed_count = 0 for template_name in test_templates: print() success = test_document_generation(template_name, test_data) if success: success_count += 1 else: failed_count += 1 print() # 打印汇总 print("=" * 80) print("测试汇总") print("=" * 80) print(f"总测试数: {len(test_templates)}") print(f"成功: {success_count}") print(f"失败: {failed_count}") print("=" * 80) if __name__ == '__main__': main()