From 0272ac944a277bc7ed683bab2d1fb13fcfb58869 Mon Sep 17 00:00:00 2001
From: xingjinshuang <xingjs@qq.com>
Date: 星期四, 26 十二月 2024 16:29:44 +0800
Subject: [PATCH] @xingjs@20241226@添加查询管线工具类,修改解决查询站点接口token存在无法获取的情况,解决后可正常获取token,并查询出需要的站点;解析shp文件获取站点范围;新增把降水CSV文件内容解析保存到数据库中

---
 src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java |   88 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java b/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java
index a4c9c6e..1d35646 100644
--- a/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java
+++ b/src/main/java/com/se/simu/utils/CsvToSQLiteUtils.java
@@ -10,6 +10,12 @@
 import java.nio.file.Paths;
 import java.sql.*;
 
+/**
+ * CSV 鍒� SQLite 瀹炵敤绋嬪簭
+ *
+ * @author xingjinshuang@smartearth.cn
+ * @date 2024/12/26
+ */
 public class CsvToSQLiteUtils {
 
     private static final String DATABASE_URL = "jdbc:sqlite:D:\\0a_project\\simulation\\simuserver\\rainfall.db"; // SQLite鏁版嵁搴撹矾寰�
@@ -142,4 +148,86 @@
             System.err.println("璇诲彇CSV鎴栨彃鍏ユ暟鎹椂鍑洪敊: " + e.getMessage());
         }
     }
+
+
+    /**
+     * 璇诲彇 CSV 淇濆瓨鏈湴
+     *
+     * @param stationRainFile Station Rain 鏂囦欢
+     * @param tableName       琛ㄥ悕绉�
+     */
+    public static void readCsvSaveLocal(String stationRainFile, String tableName) {
+        // 1. 鍒涘缓SQLite鏁版嵁搴撹繛鎺�
+        try (Connection conn = DriverManager.getConnection("jdbc:sqlite:rainfall.db")) {
+            if (conn != null) {
+                // 2. 鍒涘缓琛紙濡傛灉涓嶅瓨鍦級
+                createTableIfNotExists(conn, tableName);
+                // 3. 璁剧疆SQLite杩炴帴鐨勫瓧绗︾紪鐮佷负UTF-8
+                try (Statement stmt = conn.createStatement()) {
+                    // 璁剧疆SQLite缂栫爜涓篣TF-8
+                    stmt.execute("PRAGMA encoding = 'UTF-8';");
+                }
+                // 4. 寮�濮嬩簨鍔�
+                conn.setAutoCommit(false);
+                // 5. 璇诲彇CSV鏂囦欢骞舵彃鍏ユ暟鎹�
+                readCsvAndInsertDatas(stationRainFile, conn, tableName);
+                // 6. 鎻愪氦浜嬪姟
+                conn.commit();
+                System.out.println("鏁版嵁鎴愬姛鎻掑叆鍒癝QLite鏁版嵁搴擄紒");
+            }
+        } catch (SQLException e) {
+            System.err.println("SQLite杩炴帴澶辫触: " + e.getMessage());
+        }
+    }
+
+
+    private static void readCsvAndInsertDatas(String csvFilePath, Connection conn, String tableName) {
+        // 浣跨敤 Apache Commons CSV 璇诲彇CSV鏂囦欢
+        try (Reader reader = new InputStreamReader(Files.newInputStream(Paths.get(csvFilePath)), "GBK")) {
+            Iterable<CSVRecord> records = CSVFormat.DEFAULT
+                    .withHeader("闆ㄩ噺绔�", "闄嶉洦閲�", "缁忓害", "绾害", "datatime")
+                    .withSkipHeaderRecord() // 璺宠繃琛ㄥご
+                    .parse(reader);
+
+            String insertDataSql = "INSERT INTO " + tableName + " (station_name, rainfall, longitude, latitude, datetime) VALUES (?, ?, ?, ?, ?);";
+
+            try (PreparedStatement pstmt = conn.prepareStatement(insertDataSql)) {
+                // 鎵归噺澶у皬
+                int batchSize = 1000;
+                int count = 0;
+
+                for (CSVRecord record : records) {
+                    // 鑾峰彇姣忎竴鍒楃殑鏁版嵁
+                    String stationName = record.get("闆ㄩ噺绔�");
+                    double rainfall = Double.parseDouble(record.get("闄嶉洦閲�"));
+                    double longitude = Double.parseDouble(record.get("缁忓害"));
+                    double latitude = Double.parseDouble(record.get("绾害"));
+                    String datetime = record.get("datatime");
+
+                    // 璁剧疆鎻掑叆鏁版嵁鐨勫弬鏁�
+                    pstmt.setString(1, stationName);
+                    pstmt.setDouble(2, rainfall);
+                    pstmt.setDouble(3, longitude);
+                    pstmt.setDouble(4, latitude);
+                    pstmt.setString(5, datetime);
+
+                    // 娣诲姞鍒版壒澶勭悊
+                    pstmt.addBatch();
+                    count++;
+
+                    // 姣廱atchSize鏉℃暟鎹墽琛屼竴娆℃壒閲忔彃鍏�
+                    if (count % batchSize == 0) {
+                        pstmt.executeBatch();
+                    }
+                }
+                // 鎵ц鍓╀綑鐨勬壒閲忔彃鍏�
+                pstmt.executeBatch();
+            } catch (SQLException e) {
+                System.err.println("鎵归噺鎻掑叆鏁版嵁鏃跺嚭閿�: " + e.getMessage());
+            }
+        } catch (IOException e) {
+            System.err.println("璇诲彇CSV鎴栨彃鍏ユ暟鎹椂鍑洪敊: " + e.getMessage());
+        }
+    }
+
 }

--
Gitblit v1.9.3