chemical_company.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. from fastapi import APIRouter, Request, Depends, Query, HTTPException, status
  4. from common.security import valid_access_token
  5. from sqlalchemy.orm import Session
  6. from sqlalchemy.sql import func
  7. from common.auth_user import *
  8. from sqlalchemy import text
  9. from pydantic import BaseModel
  10. from common.BigDataCenterAPI import *
  11. from database import get_db
  12. from typing import List
  13. from models import *
  14. from utils import *
  15. from utils.spatial import *
  16. from utils.rainfall_util import *
  17. import json
  18. import traceback
  19. from jobs.rainfall_conditions_job import get_stcd_data
  20. from datetime import datetime,timedelta
  21. router = APIRouter()
  22. @router.get("/list")
  23. async def get_list(
  24. area_name: str = Query(None),
  25. company_type:str = Query(None),
  26. keyword: str= Query(None),
  27. history_time:int = Query(None),
  28. future_time:int = Query(None),
  29. db: Session = Depends(get_db),
  30. page: int = Query(1, gt=0, description='页码'),
  31. pageSize: int = Query(10, gt=0, description='每页条目数量')
  32. ):
  33. try:
  34. # 计算 OFFSET 值
  35. offset = (page - 1) * pageSize
  36. # 构造基础查询
  37. base_sql = "SELECT * FROM sharedb.chemical_company"
  38. count_sql = "SELECT COUNT(*) FROM sharedb.chemical_company"
  39. # 添加 WHERE 条件
  40. conditions = []
  41. params = {}
  42. if area_name:
  43. conditions.append("area = :area_name")
  44. params['area_name'] = area_name
  45. if keyword:
  46. conditions.append("`company_name` LIKE :keyword")
  47. params['keyword'] = f"%{keyword}%"
  48. if company_type:
  49. conditions.append("`company_type` LIKE :company_type")
  50. params['company_type'] = f"%{company_type}%"
  51. if conditions:
  52. base_sql += " WHERE " + " AND ".join(conditions)
  53. count_sql += " WHERE " + " AND ".join(conditions)
  54. count_query = text(count_sql) #.bindparams(**params)
  55. # 执行统计查询并获取总数据量
  56. total = db.execute(count_query,params).scalar()
  57. # 添加 LIMIT 和 OFFSET
  58. paginated_sql = f"{base_sql} LIMIT :limit OFFSET :offset"
  59. params['limit'] = pageSize
  60. params['offset'] = offset
  61. # 构造查询对象
  62. paginated_query = text(paginated_sql) #.bindparams(**params)
  63. # 执行分页查询并获取结果
  64. result = db.execute(paginated_query,params).fetchall()
  65. # 将结果转换为rain_pits.py字典列表
  66. result_list = []
  67. for row in result:
  68. data = dict(row)
  69. data['have_video'] = False
  70. data['video_unit_indexcode'] = ''
  71. company_name = data['company_name']
  72. video_unit_info = unitName_get_video_region_info(db,company_name)
  73. if video_unit_info:
  74. data['have_video'] = True
  75. data['video_unit_indexcode'] = video_unit_info.indexCode
  76. data['weather_warning_type'],data['weather_warninglevel'] = get_weather_warning(data['area'], db)
  77. # data['weather_warning_type'] = '暴雨预警'
  78. # data['weather_warninglevel'] = '3'
  79. if history_time:
  80. real_code = get_real_code(db,data['longitude'],data['latitude'])
  81. rainfall = get_rainfall(real_code,history_time,db)
  82. data['rainfall'] = rainfall
  83. if future_time:
  84. data['rainfall'] = 0
  85. result_list.append(data)
  86. return {
  87. "code": 200,
  88. "msg": "操作成功",
  89. "data": result_list,
  90. "total": total,
  91. "page": page,
  92. "pageSize": pageSize,
  93. "totalPages": (total + pageSize - 1) // pageSize
  94. }
  95. except Exception as e:
  96. traceback.print_exc()
  97. raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
  98. @router.get('/get_video_list')
  99. async def get_video_list(
  100. video_unit_indexcode: str = Query(None, description='所属单位索引码'),
  101. db: Session = Depends(get_db),
  102. page: int = Query(1, gt=0, description='页码'),
  103. pageSize: int = Query(10, gt=0, description='每页条目数量')
  104. ):
  105. # 大屏左下角视频及更多视频
  106. try:
  107. query = db.query(TPVideoInfo)
  108. if video_unit_indexcode:
  109. query = query.filter(TPVideoInfo.regionPath.like(f'%{video_unit_indexcode}%'))
  110. else:
  111. return {
  112. "code": 200,
  113. "msg": "操作成功",
  114. "data": None}
  115. total_items = query.count()
  116. # query = query.order_by()
  117. videos = query.offset(pageSize * (page - 1)).limit(pageSize).all()
  118. data = []
  119. for row in videos:
  120. # row = row[0]
  121. # print(type(row),row[0])
  122. data.append({
  123. "name":row.name,
  124. "video_code": row.gbIndexCode,
  125. "status":row.status,
  126. "statusName":row.statusName,
  127. "regionPath":row.regionPath,
  128. "installPlace":row.installPlace,
  129. "cameraTypeName":row.cameraTypeName,
  130. "cameraType":row.cameraType
  131. })
  132. return {
  133. "code": 200,
  134. "msg": "操作成功",
  135. "data": data,
  136. "total": total_items,
  137. "page": page,
  138. "pageSize": pageSize,
  139. "totalPages": (total_items + pageSize - 1) // pageSize
  140. }
  141. except Exception as e:
  142. # 处理异常
  143. traceback.print_exc()
  144. raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e))