Explorar o código

250409-1代码。

baoyubo hai 1 mes
pai
achega
6cdef5d5a1
Modificáronse 1 ficheiros con 30 adicións e 5 borrados
  1. 30 5
      utils/rainfall_util.py

+ 30 - 5
utils/rainfall_util.py

@@ -52,13 +52,38 @@ def get_real_code(db,longitude,latitude):
     else:
         return None
 
+# def get_rainfall(
+#     code: str,num:int,
+#     db: Session = Depends(get_db)
+# ):
+#     value=0
+#     rainfulldata = get_stcd_data(code,num+1)
+#     for i in rainfulldata:
+#         value += i['F3070220000034_000018005']
+#
+#     return value
+
+
 def get_rainfall(
-    code: str,num:int,
+    code: str,num:int=24,
     db: Session = Depends(get_db)
 ):
     value=0
-    rainfulldata = get_stcd_data(code,num+1)
-    for i in rainfulldata:
-        value += i['F3070220000034_000018005']
+    now = datetime.now()
+    twenty_four_hours_ago = now - timedelta(hours=num+1)
+    formatted_now = f"{now.strftime('%Y-%m-%d %H')}:00:00"
+    formatted_twenty_four_hours_ago = f"{twenty_four_hours_ago.strftime('%Y-%m-%d %H')}:00:00"
+    sql = text("""SELECT SUM(rainfall) as rainfall FROM `govdata_rain_data_info` where `code`=:code and create_time BETWEEN :formatted_twenty_four_hours_ago and :formatted_now""").bindparams(code=code,formatted_twenty_four_hours_ago=formatted_twenty_four_hours_ago,formatted_now=formatted_now)
+    # 执行查询
+    result = db.execute(sql).fetchone()
 
-    return value
+    # 处理结果
+    if result:
+        return dict(result)['rainfall']
+    else:
+        return 0
+    # rainfulldata = get_stcd_data(code,num+1)
+    # for i in rainfulldata:
+    #     value += i['F3070220000034_000018005']
+    #
+    # return value