From 9ff6b2fa38167d93de00e6d730d2b038d7731776 Mon Sep 17 00:00:00 2001
From: yaowanxin <3588231647@qq.com>
Date: 星期四, 24 七月 2025 13:21:52 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/ywx' into ywx

---
 src/main/java/com/chinaztt/mes/docx/util/TakeWords.java |  131 ++++++++++++++++++++++++++++++++++---------
 1 files changed, 103 insertions(+), 28 deletions(-)

diff --git a/src/main/java/com/chinaztt/mes/docx/util/TakeWords.java b/src/main/java/com/chinaztt/mes/docx/util/TakeWords.java
index 3a7cf0c..c3469d4 100644
--- a/src/main/java/com/chinaztt/mes/docx/util/TakeWords.java
+++ b/src/main/java/com/chinaztt/mes/docx/util/TakeWords.java
@@ -2,6 +2,9 @@
 
 import cn.hutool.core.io.FileUtil;
 import com.chinaztt.mes.docx.dto.GetFileDto;
+import com.opencsv.CSVReader;
+import com.opencsv.CSVReaderBuilder;
+import com.opencsv.exceptions.CsvValidationException;
 import com.chinaztt.mes.docx.dto.ThicknessData;
 import net.sourceforge.tess4j.Tesseract;
 import net.sourceforge.tess4j.TesseractException;
@@ -25,15 +28,20 @@
 import javax.imageio.ImageIO;
 
 import java.io.*;
-import java.nio.file.Files;
 import java.sql.*;
 import java.util.*;
+import java.util.regex.Pattern;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 public class TakeWords {
 
     private static final String splitIdentifier = "@-@"; // 鑷畾涔夊敮涓�鏍囪瘑绗�
+
+    // 绉戝璁℃暟娉曟鍒欐ā寮�
+    private static final Pattern SCIENTIFIC_PATTERN = Pattern.compile(
+            "^[+-]?\\d+(\\.\\d+)?[eE][+-]?\\d+$"
+    );
 
     public static Object readWordFile(File file) {
         String result = "";
@@ -220,21 +228,52 @@
 
         StringBuilder stringBuilder = new StringBuilder();
         // 鍒涘缓 reader
-        try (BufferedReader br = Files.newBufferedReader(file.toPath())) {
-            // CSV鏂囦欢鐨勫垎闅旂
-            String DELIMITER = ",";
-            // 鎸夎璇诲彇
-            String line;
-            while ((line = br.readLine()) != null) {
-                // 鍒嗗壊
-                String[] columns = line.split(DELIMITER);
-                // 鎵撳嵃琛�
-                stringBuilder.append(String.join(splitIdentifier, columns)).append("\n");
+//        try (BufferedReader br = Files.newBufferedReader(file.toPath())) {
+//            // CSV鏂囦欢鐨勫垎闅旂
+//            String DELIMITER = ",";
+//            // 鎸夎璇诲彇
+//            String line;
+//            System.out.println(br.readLine());
+//            while ((line = br.readLine()) != null) {
+//                // 鍒嗗壊
+//                String[] columns = line.split(DELIMITER);
+//                // 鎵撳嵃琛�
+//                stringBuilder.append(String.join(splitIdentifier, columns)).append("\n");
+//            }
+//        } catch (IOException ex) {
+//            ex.printStackTrace();
+//        }
+        try (FileReader fileReader = new FileReader(file);
+             CSVReader csvReader = new CSVReaderBuilder(fileReader).build()) {
+
+            String[] nextLine;
+            while ((nextLine = csvReader.readNext()) != null) {
+                // 澶勭悊姣忎竴琛屾暟鎹�
+                for (String cell : nextLine) {
+                    if(StringUtils.isNotBlank(cell)){
+                        stringBuilder.append(scientificToNumber(cell)).append(splitIdentifier);
+                    }
+                }
+                stringBuilder.append("\n");
             }
-        } catch (IOException ex) {
-            ex.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } catch (CsvValidationException e) {
+            throw new RuntimeException(e);
         }
         return stringBuilder.toString();
+    }
+
+    /**
+     * 灏嗙瀛﹁鏁版硶杞崲涓烘暟瀛�
+     * @param cell
+     * @return
+     */
+    public static String scientificToNumber(String cell){
+        if(SCIENTIFIC_PATTERN.matcher(cell).matches()){
+            return String.valueOf(Double.parseDouble(cell));
+        }
+        return cell;
     }
 
     public static Object readMdbFile(File file, GetFileDto getFileDto) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
@@ -300,33 +339,69 @@
         } catch (Exception ignore) {
         }
     }
-    public static Object getmysqlFile(GetFileDto getFileDto) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
+    public static Object getMysqlFile(GetFileDto getFileDto){
         Map<String, Object> tableMap = new HashMap<>(16);
-        // 鏁版嵁搴撹繛鎺ヤ俊鎭�
-        String url = "jdbc:mysql://localhost:3306/"+getFileDto.getDbFileName()+"?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
+        // 浠� GetFileDto 鑾峰彇鏁版嵁搴撳悕锛屽搴斻�愭枃浠跺悕绉般�戝瓧娈�
+        String dbName = getFileDto.getDbFileName();
         String user = getFileDto.getDbUserName();
         String password = getFileDto.getDbPassword();
-        List<ThicknessData> dataList = new ArrayList<>();
+        // 浠� GetFileDto 鑾峰彇鏁版嵁琛ㄥ悕锛屽搴斻�愭暟鎹簱琛ㄥ悕銆戝瓧娈�
+        String table = getFileDto.getDbTable();
+        // 妫�鏌ユ暟鎹簱鍚嶅拰琛ㄥ悕鏄惁涓虹┖
+        if (dbName == null || dbName.isEmpty() || table == null || table.isEmpty()) {
+            return R.failed("鏁版嵁搴撳悕鎴栬〃鍚嶄笉鑳戒负绌�");
+        }
+        // 鏁版嵁搴撹繛鎺ヤ俊鎭�
+        String url = "jdbc:mysql://localhost:3306/"+dbName+"?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
+        Connection connection = null;
+        PreparedStatement preparedStatement = null;
+        ResultSet resultSet = null;
+        List<Map<String, Object>> dataList = new ArrayList<>();
 
-        try (
-                // 寤虹珛杩炴帴
-                Connection connection = DriverManager.getConnection(url, user, password);
-                // 鍒涘缓 Statement 瀵硅薄鎵ц SQL
-                Statement statement = connection.createStatement()
-        ) {
-            String sql = "SELECT ThinnestPoint, AverageThickness FROM model1records";
-            ResultSet resultSet = statement.executeQuery(sql);
+        try {
+            // 寤虹珛杩炴帴
+            connection = DriverManager.getConnection(url, user, password);
+            // 鏋勫缓鍩虹 SQL
 
+            StringBuilder sql = new StringBuilder("SELECT * FROM ").append(table);
+            // 鍒涘缓 PreparedStatement 瀵硅薄鎵ц SQL
+            preparedStatement = connection.prepareStatement(sql.toString());
+            resultSet = preparedStatement.executeQuery();
+            ResultSetMetaData metaData = resultSet.getMetaData();
+            int columnCount = metaData.getColumnCount();
             // 閬嶅巻缁撴灉闆嗚幏鍙栨暟鎹�
             while (resultSet.next()) {
-                double thinnestPoint = resultSet.getDouble("ThinnestPoint");
-                double averageThickness = resultSet.getDouble("AverageThickness");
-                dataList.add(new ThicknessData(thinnestPoint, averageThickness));
+                Map<String, Object> rowData = new HashMap<>();
+                for (int i = 1; i <= columnCount; i++) {
+                    String columnName = metaData.getColumnName(i);
+                    rowData.put(columnName, resultSet.getObject(i));
+                }
+                dataList.add(rowData);
             }
+//            while (resultSet.next()) {
+//                double thinnestPoint = resultSet.getDouble("ThinnestPoint");
+//                double averageThickness = resultSet.getDouble("AverageThickness");
+//                dataList.add(new ThicknessData(thinnestPoint, averageThickness));
+//            }
             tableMap.put("data", dataList);
         } catch (Exception e) {
             e.printStackTrace();
+            // 鍋囪 R 绫绘湁 failed 鏂规硶锛岃嫢娌℃湁闇�琛ュ厖瀹炵幇
             return R.failed("鏁版嵁搴撴煡璇㈠嚭閿�: " + e.getMessage());
+        } finally {
+            try {
+                if (resultSet != null) {
+                    resultSet.close();
+                }
+                if (preparedStatement != null) {
+                    preparedStatement.close();
+                }
+                if (connection != null) {
+                    connection.close();
+                }
+            } catch (SQLException e) {
+                e.printStackTrace();
+            }
         }
         return tableMap;
     }

--
Gitblit v1.9.3