|
@@ -1109,3 +1109,87 @@ async def get_inspection_task_list(
|
|
|
traceback.print_exc()
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
+
|
|
|
+@router.get('/children/task/result/{children_task_id}/export')
|
|
|
+async def get_inspection_task_list(
|
|
|
+ request: Request,
|
|
|
+ children_task_id: str ,
|
|
|
+ area_code: str = Query(None, description='区划编码'),
|
|
|
+ inspection_result: str = Query(None, description='巡查结果'),
|
|
|
+ nick_name: str = Query(None, description='姓名'),
|
|
|
+ db: Session = Depends(get_db),
|
|
|
+ auth_user: AuthUser = Depends(find_auth_user),
|
|
|
+ user_id = Depends(valid_access_token)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ # 构建查询
|
|
|
+ query = db.query(RiskManagementInspectionTaskChildrenTaskResult)
|
|
|
+ query = query.filter(RiskManagementInspectionTaskChildrenTaskResult.del_flag != '2')
|
|
|
+ query = query.filter(RiskManagementInspectionTaskChildrenTaskResult.children_task_id == children_task_id)
|
|
|
+ # 应用查询条件
|
|
|
+ if area_code:
|
|
|
+ query = query.filter(RiskManagementInspectionTaskChildrenTaskResult.area_code == area_code)
|
|
|
+ if inspection_result:
|
|
|
+ query = query.filter(RiskManagementInspectionTaskChildrenTaskResult.inspection_result == inspection_result)
|
|
|
+ if nick_name:
|
|
|
+ query = query.filter(RiskManagementInspectionTaskChildrenTaskResult.nick_name.like(f'%{nick_name}%') )
|
|
|
+
|
|
|
+ # 计算总条目数
|
|
|
+ total_items = query.count()
|
|
|
+
|
|
|
+ # 排序
|
|
|
+
|
|
|
+ query = query.order_by(RiskManagementInspectionTaskChildrenTaskResult.create_time.desc())
|
|
|
+ # 执行分页查询
|
|
|
+ InspectionTasks = query.all()
|
|
|
+
|
|
|
+ # 将查询结果转换为列表形式的字典
|
|
|
+ InspectionTasks_list = []
|
|
|
+ for task in InspectionTasks:
|
|
|
+ area_code = task.area_code
|
|
|
+ area = area_code_get_area_info(db, area_code)
|
|
|
+ area = area_code_get_ancestors_names(db, area)
|
|
|
+ task_info = {
|
|
|
+ "id": task.id,
|
|
|
+ "子任务表id": task.children_task_id,
|
|
|
+ "经度":task.longitude,
|
|
|
+ "纬度":task.latitude,
|
|
|
+ "巡查点":task.inspection_point_name,
|
|
|
+ "区划": area,
|
|
|
+ "创建时间": task.create_time.strftime('%Y-%m-%d'),
|
|
|
+ "采集人员": task.nick_name,
|
|
|
+ "采集结果": task.inspection_result,
|
|
|
+ # "fileList": get_file_query_fun(db=db,from_scenario='RiskManagementInspectionTaskChildrenTaskResult', foreign_key=task.id),
|
|
|
+ "备注":task.remark
|
|
|
+ }
|
|
|
+ InspectionTasks_list.append(task_info)
|
|
|
+ # 返回结果
|
|
|
+ import pandas as pd
|
|
|
+ from io import BytesIO
|
|
|
+ # 将查询结果转换为 DataFrame
|
|
|
+ df = pd.DataFrame(InspectionTasks_list)
|
|
|
+
|
|
|
+ # 将 DataFrame 导出为 Excel 文件
|
|
|
+ output = BytesIO()
|
|
|
+ with pd.ExcelWriter(output, engine='openpyxl') as writer:
|
|
|
+ df.to_excel(writer, index=False)
|
|
|
+
|
|
|
+ # 设置响应头
|
|
|
+ output.seek(0)
|
|
|
+ from urllib.parse import quote
|
|
|
+ encoded_filename = f'巡查任务巡查结果{datetime.now().strftime("%Y%m%d%H%mi%s")}.xlsx'
|
|
|
+ encoded_filename = quote(encoded_filename, encoding='utf-8')
|
|
|
+ headers = {
|
|
|
+ 'Content-Disposition': f'attachment; filename*=UTF-8\'\'{encoded_filename}',
|
|
|
+ 'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
|
+ }
|
|
|
+
|
|
|
+ db_czrz.log(db, auth_user, "巡查任务管理", f"巡查任务巡查结果导出数据成功", request.client.host)
|
|
|
+
|
|
|
+ # 返回文件流
|
|
|
+ return StreamingResponse(output, headers=headers)
|
|
|
+ except Exception as e:
|
|
|
+ # 处理异常
|
|
|
+ traceback.print_exc()
|
|
|
+ raise HTTPException(status_code=500, detail=str(e))
|
|
|
+
|