Browse Source

feat:添加错误的输出到excel中,输出两个sheet;给开始按钮添加防抖;

yang yi 11 tháng trước cách đây
mục cha
commit
1a784bd2bb

+ 153 - 133
src/main/java/space/anyi/amountVerification/App.java

@@ -1,21 +1,14 @@
 package space.anyi.amountVerification;
 
 import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelWriter;
 import com.alibaba.excel.read.listener.PageReadListener;
+import com.alibaba.excel.write.metadata.WriteSheet;
 import javafx.application.Application;
-import javafx.application.Platform;
 import javafx.geometry.Insets;
 import javafx.geometry.Pos;
 import javafx.scene.Scene;
-import javafx.scene.control.Alert;
-import javafx.scene.control.Button;
-import javafx.scene.control.ButtonType;
-import javafx.scene.control.Label;
-import javafx.scene.control.Menu;
-import javafx.scene.control.MenuBar;
-import javafx.scene.control.MenuItem;
-import javafx.scene.control.ProgressBar;
-import javafx.scene.control.TextField;
+import javafx.scene.control.*;
 import javafx.scene.layout.BorderPane;
 import javafx.scene.layout.GridPane;
 import javafx.scene.layout.VBox;
@@ -26,6 +19,7 @@ import org.apache.pdfbox.Loader;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.text.PDFTextStripper;
 import space.anyi.amountVerification.model.InputModel;
+import space.anyi.amountVerification.model.OutputErrorModel;
 import space.anyi.amountVerification.model.OutputModel;
 
 import java.io.File;
@@ -43,7 +37,16 @@ import java.util.regex.Pattern;
  * @Description:
  */
 public class App extends Application {
+    /**
+     * 主窗口
+     * @see Stage
+     */
     private Stage primaryStage;
+    /**
+     * 开始按钮
+     * @see Button
+     */
+    private Button startButton;
     /**
      * 文件
      * @see TextField
@@ -148,7 +151,7 @@ public class App extends Application {
         progressLabel.setVisible(false);
 
         // 开始按钮
-        Button startButton = new Button("开始验证");
+        startButton = new Button("开始验证");
         startButton.setPrefWidth(150);
         startButton.setPrefHeight(40);
 
@@ -253,140 +256,157 @@ public class App extends Application {
     }
 
     private void startVerification() {
-        String filePath = fileField.getText();
-        String folderPath = folderField.getText();
-        System.out.println("filePath = " + filePath);
-        System.out.println(folderPath);
-
-        if (filePath.isEmpty() || folderPath.isEmpty()) {
-            Alert alert = new Alert(Alert.AlertType.ERROR);
-            alert.setHeaderText("请选择文件和文件夹");
-            alert.showAndWait();
-            return;
-        }
 
-        // 初始化进度条
-        progressBar.setProgress(0);
-        progressBar.setVisible(true);
-        progressLabel.setVisible(true);
-        updateProgress("准备开始验证...", 0);
-
-        inputModelList = new ArrayList<>();
-        outputModelList = new ArrayList<>();
-        pdfFileMap = new HashMap<>();
-        List<String> errorImeiList = new ArrayList<>();
-
-        // 读取excel
-        updateProgress("正在读取Excel文件...", 0.1);
-        EasyExcel.read(filePath, InputModel.class, new PageReadListener<InputModel>(dataList -> {
-            for (InputModel inputModel : dataList) {
-                inputModelList.add(inputModel);
-            }
-        })).sheet().doRead();
-        updateProgress("Excel文件读取完成,正在扫描文件夹...", 0.25);
-        //读取文件夹
-        File folder = new File(folderPath);
-        File[] files = folder.listFiles();
-
-        // 扫描文件夹并建立IMEI-PDF映射
-        int totalFolders = files.length;
-        int processedFolders = 0;
-
-        for (File file : files) {
-            if (file.isFile()) {
-                continue;
+        try {
+            startButton.setDisable(true);
+            String filePath = fileField.getText();
+            String folderPath = folderField.getText();
+            System.out.println("filePath = " + filePath);
+            System.out.println(folderPath);
+
+            if (filePath.isEmpty() || folderPath.isEmpty()) {
+                Alert alert = new Alert(Alert.AlertType.ERROR);
+                alert.setHeaderText("请选择文件和文件夹");
+                alert.showAndWait();
+                return;
             }
-            File[] listFiles = file.listFiles();
-            String imei = file.getName().split("-")[2];
-            //拿到文件夹里的pdf
-            for (File pdfFile : listFiles) {
-                if (pdfFile.getName().endsWith(".pdf")) {
-                    if (pdfFileMap.containsKey(imei)) {
-                        //出现重复的imei
-                        Alert alert = new Alert(Alert.AlertType.ERROR);
-                        alert.setHeaderText("出现重复的imei");
-                        alert.setContentText("请检查文件夹中是否出现重复的imei:" + imei);
-                        alert.showAndWait();
-                        return;
+
+            // 初始化进度条
+            progressBar.setProgress(0);
+            progressBar.setVisible(true);
+            progressLabel.setVisible(true);
+            updateProgress("准备开始验证...", 0);
+
+            inputModelList = new ArrayList<>();
+            outputModelList = new ArrayList<>();
+            pdfFileMap = new HashMap<>();
+            List<OutputErrorModel> errorImeiList = new ArrayList<>();
+
+            // 读取excel
+            updateProgress("正在读取Excel文件...", 0.1);
+            EasyExcel.read(filePath, InputModel.class, new PageReadListener<InputModel>(dataList -> {
+                for (InputModel inputModel : dataList) {
+                    inputModelList.add(inputModel);
+                }
+            })).sheet().doRead();
+            updateProgress("Excel文件读取完成,正在扫描文件夹...", 0.25);
+            //读取文件夹
+            File folder = new File(folderPath);
+            File[] files = folder.listFiles();
+
+            // 扫描文件夹并建立IMEI-PDF映射
+            int totalFolders = files.length;
+            int processedFolders = 0;
+
+            for (File file : files) {
+                if (file.isFile()) {
+                    continue;
+                }
+                File[] listFiles = file.listFiles();
+                String imei = file.getName().split("-")[2];
+                //拿到文件夹里的pdf
+                for (File pdfFile : listFiles) {
+                    if (pdfFile.getName().endsWith(".pdf")) {
+                        if (pdfFileMap.containsKey(imei)) {
+                            //出现重复的imei
+                            Alert alert = new Alert(Alert.AlertType.ERROR);
+                            alert.setHeaderText("出现重复的imei");
+                            alert.setContentText("请检查文件夹中是否出现重复的imei:" + imei);
+                            alert.showAndWait();
+                            return;
+                        }
+                        pdfFileMap.put(imei, pdfFile);
+                        break;
                     }
-                    pdfFileMap.put(imei, pdfFile);
-                    break;
                 }
+
+                processedFolders++;
+                double progress = 0.25 + (0.25 * processedFolders / totalFolders);
+                updateProgress("正在扫描文件夹: " + processedFolders + "/" + totalFolders, progress);
             }
+            updateProgress("文件夹扫描完成,开始解析PDF文件...", 0.5);
+
+            // 解析PDF文件
+            int totalPDFs = inputModelList.size();
+            int processedPDFs = 0;
+
+            for (InputModel inputModel : inputModelList) {
+                OutputModel outputModel = new OutputModel(inputModel);
+                String imei = inputModel.getImei();
+                //找到对应pdf的文件路径
+                File pdfFile = pdfFileMap.getOrDefault(imei, null);
+                if (Objects.isNull(pdfFile)) {
+                    errorImeiList.add(new OutputErrorModel(imei));
+                    processedPDFs++;
+                    double progress = 0.5 + (0.4 * processedPDFs / totalPDFs);
+                    updateProgress("解析PDF文件: " + processedPDFs + "/" + totalPDFs + " (IMEI未找到: " + imei + ")", progress);
+                    continue;
+                }
+                //解析pdf
+                String text = "";
+                try (PDDocument pdDocument = Loader.loadPDF(pdfFile)) {
+                    PDFTextStripper textStripper = new PDFTextStripper();
+                    //提取文本
+                    text = textStripper.getText(pdDocument);
+                    //解析金额
+                    Matcher matcher = pattern.matcher(text);
+                    if (matcher.find()) {
+                        String money = matcher.group("money"); // 获取命名分组 "money"
+                        //System.out.println("金额: " + money);  // 输出: 金额
+                        BigDecimal amount1 = new BigDecimal(inputModel.getAmount());
+                        BigDecimal amount2 = new BigDecimal(money);
+                        outputModel.setPdfAmount(amount2.doubleValue());
+                        outputModel.setMargin(amount1.subtract(amount2).doubleValue());
+                    } else {
+                        System.out.println("未匹配到金额");
+                        errorImeiList.add(new OutputErrorModel(imei));
+                    }
+                } catch (IOException | NumberFormatException e) {
+                    e.printStackTrace();
+                    errorImeiList.add(new OutputErrorModel(imei));
+                }
+                outputModelList.add(outputModel);
 
-            processedFolders++;
-            double progress = 0.25 + (0.25 * processedFolders / totalFolders);
-            updateProgress("正在扫描文件夹: " + processedFolders + "/" + totalFolders, progress);
-        }
-        updateProgress("文件夹扫描完成,开始解析PDF文件...", 0.5);
-
-        // 解析PDF文件
-        int totalPDFs = inputModelList.size();
-        int processedPDFs = 0;
-
-        for (InputModel inputModel : inputModelList) {
-            OutputModel outputModel = new OutputModel(inputModel);
-            String imei = inputModel.getImei();
-            //找到对应pdf的文件路径
-            File pdfFile = pdfFileMap.getOrDefault(imei, null);
-            if (Objects.isNull(pdfFile)) {
-                errorImeiList.add(imei);
                 processedPDFs++;
                 double progress = 0.5 + (0.4 * processedPDFs / totalPDFs);
-                updateProgress("解析PDF文件: " + processedPDFs + "/" + totalPDFs + " (IMEI未找到: " + imei + ")", progress);
-                continue;
+                updateProgress("解析PDF文件: " + processedPDFs + "/" + totalPDFs, progress);
             }
-            //解析pdf
-            String text = "";
-            try (PDDocument pdDocument = Loader.loadPDF(pdfFile)) {
-                PDFTextStripper textStripper = new PDFTextStripper();
-                //提取文本
-                text = textStripper.getText(pdDocument);
-                //解析金额
-                Matcher matcher = pattern.matcher(text);
-                if (matcher.find()) {
-                    String money = matcher.group("money"); // 获取命名分组 "money"
-                    //System.out.println("金额: " + money);  // 输出: 金额
-                    BigDecimal amount1 = new BigDecimal(inputModel.getAmount());
-                    BigDecimal amount2 = new BigDecimal(money);
-                    outputModel.setPdfAmount(amount2.doubleValue());
-                    outputModel.setMargin(amount1.subtract(amount2).doubleValue());
-                } else {
-                    System.out.println("未匹配到金额");
-                    errorImeiList.add(imei);
-                }
-            } catch (IOException | NumberFormatException e) {
-                e.printStackTrace();
-                errorImeiList.add(imei);
+            updateProgress("PDF文件解析完成,正在生成输出文件...", 0.9);
+            //输出最终文件
+            String outputFileName = filePath.substring(0, filePath.lastIndexOf(".")) + "_" + System.currentTimeMillis() + "_output.xlsx";
+            try (ExcelWriter excelWriter = EasyExcel.write(outputFileName).build()) {
+                //正常的sheet
+                WriteSheet writeSheet = EasyExcel.writerSheet("正常").head(OutputModel.class).build();
+                excelWriter.write(outputModelList, writeSheet);
+                //错误的sheet
+                WriteSheet writeSheet1 = EasyExcel.writerSheet("错误").head(OutputErrorModel.class).build();
+                excelWriter.write(errorImeiList, writeSheet1);
             }
-            outputModelList.add(outputModel);
-
-            processedPDFs++;
-            double progress = 0.5 + (0.4 * processedPDFs / totalPDFs);
-            updateProgress("解析PDF文件: " + processedPDFs + "/" + totalPDFs, progress);
-        }
-        updateProgress("PDF文件解析完成,正在生成输出文件...", 0.9);
-        //输出最终文件
-        String outputFileName = filePath.substring(0, filePath.lastIndexOf(".")) + "_" + System.currentTimeMillis() + "_output.xlsx";
-        EasyExcel.write(outputFileName, OutputModel.class).sheet("模板").doWrite(outputModelList);
-        updateProgress("验证完成!输出文件: " + outputFileName, 1.0);
-
-        // 显示完成信息
-        javafx.application.Platform.runLater(() -> {
-            Alert alert = new Alert(Alert.AlertType.INFORMATION);
-            alert.setTitle("验证完成");
-            alert.setHeaderText("金额验证已完成");
-            alert.setContentText("输出文件已生成: " + outputFileName +
-                               (errorImeiList.isEmpty() ? "" : "\n\n以下IMEI处理失败: " + String.join(", ", errorImeiList)));
-            alert.showAndWait();
-
+            //EasyExcel.write(outputFileName, OutputModel.class).sheet("模板").doWrite(outputModelList);
+            updateProgress("验证完成!输出文件: " + outputFileName, 1.0);
+
+            // 显示完成信息
+            javafx.application.Platform.runLater(() -> {
+                Alert alert = new Alert(Alert.AlertType.INFORMATION);
+                alert.setTitle("验证完成");
+                alert.setHeaderText("金额验证已完成");
+                alert.setContentText("输出文件已生成: " + outputFileName +
+                                   (errorImeiList.isEmpty() ? "" : "\n\nIMEI处理失败数量: " + errorImeiList.size()));
+                alert.showAndWait();
+            });
+        } finally {
             // 隐藏进度条
             progressBar.setVisible(false);
             progressLabel.setVisible(false);
-        });
-
-        inputModelList = null;
-        outputModelList = null;
-        pdfFileMap = null;
+            // 恢复按钮状态
+            startButton.setDisable(false);
+            // 清空输入框
+            fileField.clear();
+            folderField.clear();
+            //资源清理
+            inputModelList = null;
+            outputModelList = null;
+            pdfFileMap = null;
+        }
     }
 }

+ 37 - 0
src/main/java/space/anyi/amountVerification/model/OutputErrorModel.java

@@ -0,0 +1,37 @@
+package space.anyi.amountVerification.model;
+
+import com.alibaba.excel.annotation.ExcelProperty;
+
+/**
+ * @ProjectName: amount-verification
+ * @FileName: OutErrorModel
+ * @Author: 杨逸
+ * @Data:2025/11/27 21:39
+ * @Description:
+ */
+public class OutputErrorModel {
+    @ExcelProperty(value = "IMEI码",order = 1)
+    private String imei;
+
+    public OutputErrorModel() {
+    }
+
+    public OutputErrorModel(String imei) {
+        this.imei = imei;
+    }
+
+    public String getImei() {
+        return imei;
+    }
+
+    public void setImei(String imei) {
+        this.imei = imei;
+    }
+
+    @Override
+    public String toString() {
+        return "OutErrorModel{" +
+                "imei='" + imei + '\'' +
+                '}';
+    }
+}