Browse Source

填报导出

xuguoyang 2 months ago
parent
commit
a953345306
1 changed files with 12 additions and 11 deletions
  1. 12 11
      routers/api/dataFilling/__init__.py

+ 12 - 11
routers/api/dataFilling/__init__.py

@@ -1094,17 +1094,19 @@ async def get_records_by_creator_and_report(
 
 
 
-# 批量导出填报数据为 Excel 文件
+from fastapi import status
 @router.get("/export_to_excel")
 @router.post("/export_to_excel")
 async def export_to_excel(
     report_id: str = Query(..., description="填报ID"),
     db: Session = Depends(get_db),
-    creator_id = Depends(valid_access_token)
+    creator_id: str = Depends(valid_access_token)
 ):
     # 获取对应填报ID的数据表名称
-    report = db.query(ReportManagement).filter(ReportManagement.report_id == report_id,
-                                               ReportManagement.creator_id == creator_id).first()
+    report = db.query(ReportManagement).filter(
+        ReportManagement.report_id == report_id,
+        ReportManagement.creator_id == creator_id
+    ).first()
     if not report:
         raise HTTPException(status_code=404, detail="未找到对应的填报ID")
 
@@ -1117,19 +1119,18 @@ async def export_to_excel(
     columns = inspector.get_columns(data_table_name)
 
     # 提取用户填报的字段注释
-    user_report_columns = [col for col in columns if
-                           col['name'] not in ['id', 'create_id', 'collect_status', 'add_time','user_id']]
+    user_report_columns = [col for col in columns if col['name'] not in ['id', 'create_id', 'collect_status', 'add_time', 'user_id']]
     column_comments = [col.get('comment', '') for col in user_report_columns]
 
     # 构建查询SQL,关联 sys_user 表获取 nick_name
-    query_sql = text(f"""
+    query_sql = f"""
         SELECT su.nick_name AS user_name, {', '.join([f'rd.{col["name"]}' for col in user_report_columns])}
         FROM {data_table_name} rd
         JOIN sys_user su ON rd.user_id = su.user_id
-    """)
-    print(query_sql)
-    # 查询数据
-    result = db.execute(query_sql)
+    """
+
+    # 使用 text 包装查询字符串
+    result = db.execute(text(query_sql))
     rows = result.fetchall()
 
     # 将查询结果转换为 DataFrame