wartheking преди 9 месеца
родител
ревизия
c0af06cb4c
променени са 1 файла, в които са добавени 22 реда и са изтрити 6 реда
  1. 22 6
      AccessServer.py

+ 22 - 6
AccessServer.py

@@ -789,7 +789,8 @@ def get_six_hour_sw_data():
     requestHead = {
         "token": "6C60F37D40B48DECF9A3F2CC1A5A50142C4BE52F26D15B171153F20DB63960696A439E435DDCA9AF925F13338470FFBDDC98CEE65AFBC92C2EB4E44C0A757DB5"
     }
-
+    # 连接数据库
+    conn = pymysql.connect(host='localhost', user='root', password='HelloWorld123', db='water', charset='utf8mb4')
     try:
         # 发送第一个请求以获取基本数据
         response = requests.post("{0}/shareddata/api/v1/baseinfo/stbprp/sw".format(SQServerHost), data=requestData, headers=requestHead)
@@ -799,11 +800,26 @@ def get_six_hour_sw_data():
         # 初始化一个字典以将 STCD 映射到其在 swData 中的索引
         swDataDic = {}
         index = 0
-        for jsData in jsonData["data"]["list"]:
-            if (114.115926000756 > jsData["LGTD"] and jsData["LGTD"] > 113.410714576698 and 30.2676753765206 > jsData["LTTD"] and jsData["LTTD"] > 29.8114499954776):
-                swData.append(jsData)
-                swDataDic[jsData["STCD"]] = index
-                index += 1
+        # 连接数据库并开始操作
+        with conn.cursor() as cursor:
+            for jsData in jsonData["data"]["list"]:
+                if (114.115926000756 > jsData["LGTD"] and jsData["LGTD"] > 113.410714576698 and 30.2676753765206 > jsData["LTTD"] and jsData["LTTD"] > 29.8114499954776):
+                    swData.append(jsData)
+                    swDataDic[jsData["STCD"]] = index
+                    index += 1
+
+                    # 检查数据库中是否已有该记录
+                    cursor.execute("SELECT id FROM WaterHotPoints WHERE longitude=%s AND latitude=%s", 
+                                    (jsData["LGTD"], jsData["LTTD"]))
+                    result = cursor.fetchone()
+                    
+                    if result:
+                        pass
+                    else:
+                        # 如果记录不存在,执行插入操作
+                        cursor.execute("INSERT INTO WaterHotPoints (type, longitude, latitude, name, name_pri) VALUES (%s, %s, %s, %s, %s)",
+                                        (2, jsData['LGTD'], jsData['LTTD'], jsData['STNM'], ""))
+            conn.commit()
 
         # 准备第二个 API 请求的数据
         requestData = {}