From b1ff3475c44738107398f4e502d9b54aac97fc48 Mon Sep 17 00:00:00 2001
From: zouyu <2723363702@qq.com>
Date: 星期二, 21 十月 2025 17:14:47 +0800
Subject: [PATCH] 数采调整2

---
 src/main/java/com/chinaztt/mes/docx/util/TakeWords.java |  202 ++++++++++++++++++++++++++++++--------------------
 1 files changed, 121 insertions(+), 81 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 bbaa58d..70f5c15 100644
--- a/src/main/java/com/chinaztt/mes/docx/util/TakeWords.java
+++ b/src/main/java/com/chinaztt/mes/docx/util/TakeWords.java
@@ -2,38 +2,39 @@
 
 import cn.hutool.core.io.FileUtil;
 import com.chinaztt.mes.docx.dto.GetFileDto;
-import com.chinaztt.mes.docx.dto.ThicknessData;
+import com.opencsv.CSVReader;
+import com.opencsv.CSVReaderBuilder;
+import com.opencsv.exceptions.CsvValidationException;
 import net.sourceforge.tess4j.Tesseract;
 import net.sourceforge.tess4j.TesseractException;
 import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.poi.POIXMLDocument;
 import org.apache.poi.POIXMLTextExtractor;
-import org.apache.poi.hssf.usermodel.HSSFPicture;
 import org.apache.poi.hssf.usermodel.HSSFPictureData;
-import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.hwpf.extractor.WordExtractor;
-import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.opc.OPCPackage;
-import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
-import java.awt.Color;
-import java.awt.image.BufferedImage;
-import javax.imageio.ImageIO;
 
 import java.io.*;
 import java.nio.file.Files;
 import java.sql.*;
 import java.util.*;
-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 = "";
@@ -56,32 +57,30 @@
         return result;
     }
 
-    public static Object readExcelFile(File file) throws FileNotFoundException, IOException {
+    public static Object readExcelFile(File file) throws IOException {
         StringBuilder result = new StringBuilder();
         //鍒涘缓宸ヤ綔绨垮璞�
-        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));
+        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(Files.newInputStream(file.toPath()));
         //鑾峰彇宸ヤ綔绨夸笅sheet鐨勪釜鏁� 鍙鍙栫涓�涓猻heet
 //            int sheetNum = xssfWorkbook.getNumberOfSheets();
         //閬嶅巻宸ヤ綔绨夸腑鐨勬墍鏈夋暟鎹�
-        for (int i = 0; i < 1; i++) {
-            XSSFSheet sheet = xssfWorkbook.getSheetAt(i);
-            //鑾峰彇鏈�鍚庝竴琛岀殑num锛屽嵆鎬昏鏁般�傛澶勪粠0寮�濮�
-            int maxRow = sheet.getLastRowNum();
-            for (int row = 0; row <= maxRow; row++) {
-                //鑾峰彇鏈�鍚庡崟鍏冩牸num锛屽嵆鎬诲崟鍏冩牸鏁� ***娉ㄦ剰锛氭澶勪粠1寮�濮嬭鏁�***
-                int maxRol = sheet.getRow(row).getLastCellNum();
-                StringBuilder aLine = new StringBuilder();
-                for (int rol = 0; rol < maxRol; rol++) {
-                    aLine.append(sheet.getRow(row).getCell(rol)).append(splitIdentifier);
-                }
-                result.append(aLine).append("\n");
+        XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
+        //鑾峰彇鏈�鍚庝竴琛岀殑num锛屽嵆鎬昏鏁般�傛澶勪粠0寮�濮�
+        int maxRow = sheet.getLastRowNum();
+        for (int row = 1; row <= maxRow; row++) {
+            //鑾峰彇鏈�鍚庡崟鍏冩牸num锛屽嵆鎬诲崟鍏冩牸鏁� ***娉ㄦ剰锛氭澶勪粠1寮�濮嬭鏁�***
+            int maxRol = sheet.getRow(row).getLastCellNum();
+            StringBuilder aLine = new StringBuilder();
+            for (int rol = 0; rol < maxRol; rol++) {
+                aLine.append(sheet.getRow(row).getCell(rol)).append(splitIdentifier);
             }
+            result.append(aLine).append("\n");
         }
         return result.toString();
     }
 
     public static Object readExcelxlsFile(File file) throws IOException {
-        StringBuilder result = new StringBuilder();
+        String result = "";
         try (FileInputStream fis = new FileInputStream(file);
              Workbook workbook = new HSSFWorkbook(fis)) {
             // 鑾峰彇绗竴涓伐浣滆〃
@@ -90,41 +89,66 @@
             if (workbook instanceof HSSFWorkbook) {
                 HSSFWorkbook hssfWorkbook = (HSSFWorkbook) workbook;
                 List<HSSFPictureData> pictures = hssfWorkbook.getAllPictures();
-                for (HSSFPictureData picture : pictures) {
-                    // 鑾峰彇鍥剧墖绫诲瀷
-                    String pictureType = picture.suggestFileExtension();
-                    // 鑾峰彇鍥剧墖鏁版嵁
-                    byte[] pictureData = picture.getData();
-                    // 鍒涘缓涓存椂鏂囦欢
-                    File tempFile = File.createTempFile(UUID.randomUUID().toString(), "." + pictureType);
-                    try (FileOutputStream fos = new FileOutputStream(tempFile)) {
-                        fos.write(pictureData);
-                    }
-                    // 鍥剧墖棰勫鐞�
-//                    File processedFile = preprocessImage(tempFile, pictureType);
-                    // 璋冪敤 readPngFile1 鏂规硶璇诲彇鍥剧墖鏂囧瓧淇℃伅
-                    String ocrResult = "";
-                    try {
-                        ocrResult = (String) readPngFile1(tempFile);
-//                        ocrResult = (String) readPngFile(tempFile);
-//                        ocrResult = (String) readPngFile(processedFile);
-                    } catch (TesseractException e) {
-                        ocrResult = "OCR璇嗗埆澶辫触: " + e.getMessage();
-                    } finally {
-                        // 鍒犻櫎涓存椂鏂囦欢
-                        tempFile.delete();
-//                        processedFile.delete();
-                    }
-
-                    // 灏嗗浘鐗囦俊鎭坊鍔犲埌缁撴灉涓�
-//                    result.append("Picture Type: ").append(pictureType)
-//                            .append(", Picture Size: ").append(pictureData.length)
-//                            .append(" bytes")
-//                            .append(", OCR Result: ").append(ocrResult)
-//                            .append(",");
-                    String ocrText = fixOcrText(ocrResult);
-                    result.append("OCR Result:").append(ocrText).append(",");
+                //澶勭悊鏈�鍚庝竴寮犲浘鐗囨暟鎹�
+                HSSFPictureData lastPicture = pictures.get(pictures.size()-1);
+                // 鑾峰彇鍥剧墖绫诲瀷
+                String pictureType = lastPicture.suggestFileExtension();
+                // 鑾峰彇鍥剧墖鏁版嵁
+                byte[] pictureData = lastPicture.getData();
+                // 鍒涘缓涓存椂鏂囦欢
+                File tempFile = File.createTempFile(UUID.randomUUID().toString(), "." + pictureType);
+                try (FileOutputStream fos = new FileOutputStream(tempFile)) {
+                    fos.write(pictureData);
                 }
+                String ocrResult = "";
+                try {
+                    ocrResult = (String) readPngFile(tempFile);
+                } catch (TesseractException e) {
+                    ocrResult = "OCR璇嗗埆澶辫触: " + e.getMessage();
+                } finally {
+                    // 鍒犻櫎涓存椂鏂囦欢
+                    tempFile.delete();
+                }
+                result = ocrResult;
+//                String ocrText = fixOcrText(ocrResult);
+//                result.append("OCR Result:").append(ocrText).append(",");
+
+
+//                for (HSSFPictureData picture : pictures) {
+//                    // 鑾峰彇鍥剧墖绫诲瀷
+//                    String pictureType = picture.suggestFileExtension();
+//                    // 鑾峰彇鍥剧墖鏁版嵁
+//                    byte[] pictureData = picture.getData();
+//                    // 鍒涘缓涓存椂鏂囦欢
+//                    File tempFile = File.createTempFile(UUID.randomUUID().toString(), "." + pictureType);
+//                    try (FileOutputStream fos = new FileOutputStream(tempFile)) {
+//                        fos.write(pictureData);
+//                    }
+//                    // 鍥剧墖棰勫鐞�
+////                    File processedFile = preprocessImage(tempFile, pictureType);
+//                    // 璋冪敤 readPngFile1 鏂规硶璇诲彇鍥剧墖鏂囧瓧淇℃伅
+//                    String ocrResult = "";
+//                    try {
+//                        ocrResult = (String) readPngFile(tempFile);
+////                        ocrResult = (String) readPngFile(tempFile);
+////                        ocrResult = (String) readPngFile(processedFile);
+//                    } catch (TesseractException e) {
+//                        ocrResult = "OCR璇嗗埆澶辫触: " + e.getMessage();
+//                    } finally {
+//                        // 鍒犻櫎涓存椂鏂囦欢
+////                        tempFile.delete();
+////                        processedFile.delete();
+//                    }
+//
+//                    // 灏嗗浘鐗囦俊鎭坊鍔犲埌缁撴灉涓�
+////                    result.append("Picture Type: ").append(pictureType)
+////                            .append(", Picture Size: ").append(pictureData.length)
+////                            .append(" bytes")
+////                            .append(", OCR Result: ").append(ocrResult)
+////                            .append(",");
+//                    String ocrText = fixOcrText(ocrResult);
+//                    result.append("OCR Result:").append(ocrText).append(",");
+//                }
             }
 //
 //            // 閬嶅巻姣忎竴琛�
@@ -200,9 +224,6 @@
         return tesseract.doOCR(file);
     }
 
-
-
-
     public static Object readTxtFile(File file) throws IOException {
         FileInputStream fin = new FileInputStream(file);
         InputStreamReader reader = new InputStreamReader(fin);
@@ -220,21 +241,37 @@
 
         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 (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 {
@@ -280,6 +317,7 @@
             }
             tableMap.put("data", list);
         } catch (Exception e) {
+            e.printStackTrace();
         } finally {
             closeA1l(conn, preparedStatement, rs);
         }
@@ -300,6 +338,7 @@
         } catch (Exception ignore) {
         }
     }
+
     public static Object getMysqlFile(GetFileDto getFileDto){
         Map<String, Object> tableMap = new HashMap<>(16);
         // 浠� GetFileDto 鑾峰彇鏁版嵁搴撳悕锛屽搴斻�愭枃浠跺悕绉般�戝瓧娈�
@@ -313,7 +352,7 @@
             return R.failed("鏁版嵁搴撳悕鎴栬〃鍚嶄笉鑳戒负绌�");
         }
         // 鏁版嵁搴撹繛鎺ヤ俊鎭�
-        String url = "jdbc:mysql://localhost:3306/"+dbName+"?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
+        String url = "jdbc:mysql://localhost:3306/"+dbName+"?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&characterEncoding=utf8";
         Connection connection = null;
         PreparedStatement preparedStatement = null;
         ResultSet resultSet = null;
@@ -324,9 +363,15 @@
             connection = DriverManager.getConnection(url, user, password);
             // 鏋勫缓鍩虹 SQL
 
-            StringBuilder sql = new StringBuilder("SELECT * FROM ").append(table);
+            String sql = "SELECT * FROM "+table+" WHERE 1=1";
+            if(StringUtils.isNotBlank(getFileDto.getMdbEntrustCode())){
+                sql+=" AND " + getFileDto.getMdbEntrustCode() + " = '" + getFileDto.getEntrustCode()+ "'";
+            }
+            if(StringUtils.isNotBlank(getFileDto.getMdbSampleCode())){
+                sql+=" AND " + getFileDto.getMdbSampleCode() + " = '" + getFileDto.getSampleCode() + "'";
+            }
             // 鍒涘缓 PreparedStatement 瀵硅薄鎵ц SQL
-            preparedStatement = connection.prepareStatement(sql.toString());
+            preparedStatement = connection.prepareStatement(sql);
             resultSet = preparedStatement.executeQuery();
             ResultSetMetaData metaData = resultSet.getMetaData();
             int columnCount = metaData.getColumnCount();
@@ -339,11 +384,6 @@
                 }
                 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();

--
Gitblit v1.9.3