浏览代码

250402-2代码。

baoyubo 3 月之前
父节点
当前提交
00da116475
共有 2 个文件被更改,包括 96 次插入22 次删除
  1. 69 11
      routers/api/rainfall/dzzh.py
  2. 27 11
      routers/api/rainfall/rain_pits.py

+ 69 - 11
routers/api/rainfall/dzzh.py

@@ -27,6 +27,7 @@ router = APIRouter()
 @router.get("/list")
 async def get_list(
     area_name: str= Query(None),
+    keyword: str= Query(None),
     history_time:int = Query(None),
     future_time:int = Query(None),
     db: Session = Depends(get_db),
@@ -37,29 +38,45 @@ async def get_list(
         # 计算 OFFSET 值
         offset = (page - 1) * pageSize
         # 构造基础查询
-        base_query = text("SELECT * FROM sharedb.midmap_dzzh")
+        base_sql = "SELECT * FROM sharedb.midmap_dzzh"
+        count_sql = "SELECT COUNT(*) FROM sharedb.midmap_dzzh"
+
+        # 添加 WHERE 条件
+        conditions = []
+        params = {}
         if area_name:
-            base_query = base_query.bindparams(area_name=area_name).where(text("area = :area_name"))
+            conditions.append("area = :area_name")
+            params['area_name'] = area_name
+        if keyword:
+            conditions.append("`address` LIKE :keyword")
+            params['keyword'] = f"%{keyword}%"
 
-        # 构造分页查询
-        paginated_query = f"{base_query} LIMIT :limit OFFSET :offset"
-        paginated_query = text(paginated_query).bindparams(limit=pageSize, offset=offset)
+        if conditions:
+            base_sql += " WHERE " + " AND ".join(conditions)
+            count_sql += " WHERE " + " AND ".join(conditions)
 
-        # 构造统计总数据量的查询
-        count_query = select(func.count()).select_from(text("sharedb.midmap_dzzh"))
-        if area_name:
-            count_query = count_query.where(text("area = :area_name")).bindparams(area_name=area_name)
+
+        # 构造查询对象
+        count_query = text(count_sql)#.bindparams(**params)
+        # 添加 LIMIT 和 OFFSET
+        # paginated_sql = base_sql
+        paginated_sql = f"{base_sql} LIMIT :limit OFFSET :offset"
+        params['limit'] = pageSize
+        params['offset'] = offset
+        paginated_query = text(paginated_sql)#.bindparams(**params)#.limit(pageSize).offset(offset)
 
         # 执行统计查询并获取总数据量
-        total = db.execute(count_query).scalar()
+        total = db.execute(count_query,params).scalar()
 
         # 执行分页查询并获取结果
-        result = db.execute(paginated_query).fetchall()
+        result = db.execute(paginated_query,params).fetchall()
 
         # 将结果转换为rain_pits.py字典列表
         result_list = []
         for row in result:
             data = dict(row)
+            data['weather_warning_type'] = '暴雨预警'
+            data['weather_warninglevel'] = '3'
             if history_time:
                 real_code = get_real_code(db, data['longitude'], data['latitude'])
                 rainfall = get_rainfall(real_code, history_time, db)
@@ -76,6 +93,47 @@ async def get_list(
             "pageSize": pageSize,
             "totalPages": (total + pageSize - 1) // pageSize
         }
+    except Exception as e:
+        traceback.print_exc()
+        raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
+
+
+@router.get("/info")
+async def get_info(
+    id: str= Query(None),
+    db: Session = Depends(get_db)
+):
+    try:
+        # 构造基础查询
+        base_sql = "SELECT * FROM sharedb.midmap_dzzh"
+        # 添加 WHERE 条件
+        conditions = []
+        params = {}
+        if id:
+            conditions.append("id = :id")
+            params['id'] = id
+        else:
+            return {
+                "code": 200,
+                "msg": "操作成功",
+                "data": None
+            }
+        if conditions:
+            base_sql += " WHERE " + " AND ".join(conditions)
+
+        # 构造查询对象
+        paginated_query = text(base_sql)#.bindparams(**params)#.limit(pageSize).offset(offset)
+
+        # 执行分页查询并获取结果
+        result = db.execute(paginated_query,params).fetchone()
+
+        data = dict(result)
+
+        return {
+            "code": 200,
+            "msg": "操作成功",
+            "data": data
+        }
     except Exception as e:
         traceback.print_exc()
         raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")

+ 27 - 11
routers/api/rainfall/rain_pits.py

@@ -27,6 +27,7 @@ router = APIRouter()
 @router.get("/list")
 async def get_list(
     area_name: str = Query(None),
+    keyword: str= Query(None),
     history_time:int = Query(None),
     future_time:int = Query(None),
     db: Session = Depends(get_db),
@@ -37,28 +38,43 @@ async def get_list(
         # 计算 OFFSET 值
         offset = (page - 1) * pageSize
         # 构造基础查询
-        base_query = text("SELECT * FROM sharedb.govdata_rain_pits")
+        base_sql = "SELECT * FROM sharedb.govdata_rain_pits"
+        count_sql = "SELECT COUNT(*) FROM sharedb.govdata_rain_pits"
+
+        # 添加 WHERE 条件
+        conditions = []
+        params = {}
         if area_name:
-            base_query = base_query.bindparams(area_name=area_name).where(text("district = :area_name"))
+            conditions.append("district = :area_name")
+            params['area_name'] = area_name
+        if keyword:
+            conditions.append("`name` LIKE :keyword")
+            params['keyword'] = f"%{keyword}%"
 
-        # 构造分页查询
-        paginated_query = f"{base_query} LIMIT :limit OFFSET :offset"
-        paginated_query = text(paginated_query).bindparams(limit=pageSize, offset=offset)
+        if conditions:
+            base_sql += " WHERE " + " AND ".join(conditions)
+            count_sql += " WHERE " + " AND ".join(conditions)
 
-        # 构造统计总数据量的查询
-        count_query = select(func.count()).select_from(text("sharedb.govdata_rain_pits"))
-        if area_name:
-            count_query = count_query.where(text("district = :area_name")).bindparams(area_name=area_name)
+        count_query = text(count_sql) #.bindparams(**params)
 
         # 执行统计查询并获取总数据量
-        total = db.execute(count_query).scalar()
+        total = db.execute(count_query,params).scalar()
+
+        # 添加 LIMIT 和 OFFSET
+        paginated_sql = f"{base_sql} LIMIT :limit OFFSET :offset"
+        params['limit'] = pageSize
+        params['offset'] = offset
 
+        # 构造查询对象
+        paginated_query = text(paginated_sql) #.bindparams(**params)
         # 执行分页查询并获取结果
-        result = db.execute(paginated_query).fetchall()
+        result = db.execute(paginated_query,params).fetchall()
         # 将结果转换为rain_pits.py字典列表
         result_list = []
         for row in result:
             data = dict(row)
+            data['weather_warning_type'] = '暴雨预警'
+            data['weather_warninglevel'] = '3'
             if history_time:
                 real_code = get_real_code(db,data['longitude'],data['latitude'])
                 rainfall = get_rainfall(real_code,history_time,db)