|
@@ -2,6 +2,8 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
from fastapi import APIRouter, Request, Depends, Query, HTTPException, status
|
|
|
+from fastapi.responses import JSONResponse,StreamingResponse
|
|
|
+from common.db import db_czrz
|
|
|
from common.security import valid_access_token
|
|
|
from sqlalchemy.orm import Session
|
|
|
from sqlalchemy.sql import func
|
|
@@ -98,4 +100,59 @@ async def get_pattern_info(
|
|
|
|
|
|
return {
|
|
|
"code": 200, "msg": "获取成功", "data":{"areaName":area_name,"updateTime":update_time_max,"rainfallHistory":rainfall_history,"rainfallFuture":rainfall_future}#,"cumulativeRainfall":cumulative_rainfall}
|
|
|
- }
|
|
|
+ }
|
|
|
+
|
|
|
+@router.get('/get_rainfall_range/export')
|
|
|
+async def get_inspection_task_list(
|
|
|
+ request: Request,
|
|
|
+ sort: str = Query('desc'),
|
|
|
+ timeOption: str = Query('24'),
|
|
|
+ area: str = Query(''),
|
|
|
+ township: str = Query(''),
|
|
|
+ db: Session = Depends(get_db),
|
|
|
+ auth_user: AuthUser = Depends(find_auth_user),
|
|
|
+ user_id = Depends(valid_access_token)
|
|
|
+):
|
|
|
+ try:
|
|
|
+ sql=f"""SELECT ROW_NUMBER() OVER ( ORDER BY T2.rainfall {sort}) AS `序号`,T1.area as `区县`,T1.township as `镇街`,T1.address as `站点地质`,T1.area_name as `站点名称`,T2.rainfall as `雨量`,T1.`code` as `站点编号` FROM sharedb.govdata_real_time_address T1 LEFT JOIN (select `latest_data`.`code` AS `code`,sum(`latest_data`.`rainfall`) AS `rainfall` from (select `govdata_rain_data_info`.`code` AS `code`,`govdata_rain_data_info`.`area_name` AS `area_name`,`govdata_rain_data_info`.`address` AS `address`,`govdata_rain_data_info`.`create_time` AS `create_time`,`govdata_rain_data_info`.`rainfall` AS `rainfall`,`govdata_rain_data_info`.`update_time` AS `update_time`,row_number() OVER (PARTITION BY `govdata_rain_data_info`.`code` ORDER BY `govdata_rain_data_info`.`create_time` desc ) AS `rn` from sharedb.`govdata_rain_data_info`) `latest_data` where ((`latest_data`.`rn` <= '{timeOption}') and `latest_data`.`code` in (select `govdata_real_time_address`.`code` from sharedb.`govdata_real_time_address`)) group by `latest_data`.`code` order by `rainfall` desc) T2 on T1.code=T2.code where CASE
|
|
|
+ WHEN '{area}'<>'' THEN
|
|
|
+ T1.area='{area}'
|
|
|
+ ELSE
|
|
|
+ 1=1
|
|
|
+END and CASE
|
|
|
+ WHEN '{township}'<>'' THEN
|
|
|
+ T1.township='{township}'
|
|
|
+ ELSE
|
|
|
+ 1=1
|
|
|
+END and T2.rainfall>0 ORDER BY T2.rainfall {sort}"""
|
|
|
+ da = db.execute(sql).fetchall()
|
|
|
+ outlist = [dict(row) for row in da]
|
|
|
+ # 返回结果
|
|
|
+ import pandas as pd
|
|
|
+ from io import BytesIO
|
|
|
+ # 将查询结果转换为 DataFrame
|
|
|
+ df = pd.DataFrame(outlist)
|
|
|
+
|
|
|
+ # 将 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()
|
|
|
+ return JSONResponse(status_code=500, content={"code": 500, "msg": f"Internal server error: {str(e)}"})
|