Explorar o código

feat:给处理程序添加进度条

yang yi hai 11 meses
pai
achega
cfcae42ec7
Modificáronse 1 ficheiros con 86 adicións e 12 borrados
  1. 86 12
      src/main/java/space/anyi/amountVerification/App.java

+ 86 - 12
src/main/java/space/anyi/amountVerification/App.java

@@ -3,6 +3,7 @@ package space.anyi.amountVerification;
 import com.alibaba.excel.EasyExcel;
 import com.alibaba.excel.read.listener.PageReadListener;
 import javafx.application.Application;
+import javafx.application.Platform;
 import javafx.geometry.Insets;
 import javafx.geometry.Pos;
 import javafx.scene.Scene;
@@ -13,6 +14,7 @@ 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.layout.BorderPane;
 import javafx.scene.layout.GridPane;
@@ -66,6 +68,16 @@ public class App extends Application {
      * @see Map<String, File>
      */
     private Map<String,File> pdfFileMap;
+    /**
+     * 进度条控件
+     * @see ProgressBar
+     */
+    private ProgressBar progressBar;
+    /**
+     * 进度标签
+     * @see Label
+     */
+    private Label progressLabel;
     /**
      * 匹配金额使用的正则表达式对象
      * @see Pattern
@@ -126,6 +138,15 @@ public class App extends Application {
         folderField.setPrefWidth(300);
         Button folderButton = new Button("浏览");
 
+        // 进度条组件
+        progressBar = new ProgressBar(0);
+        progressBar.setPrefWidth(300);
+        progressBar.setVisible(false);
+
+        // 进度标签
+        progressLabel = new Label("准备开始...");
+        progressLabel.setVisible(false);
+
         // 开始按钮
         Button startButton = new Button("开始验证");
         startButton.setPrefWidth(150);
@@ -146,11 +167,11 @@ public class App extends Application {
         gridPane.add(folderButton, 2, 1);
 
         // 添加到内容布局
-        contentLayout.getChildren().addAll(gridPane, startButton);
+        contentLayout.getChildren().addAll(gridPane, progressLabel, progressBar, startButton);
         mainLayout.setCenter(contentLayout);
 
         // 创建场景并显示
-        Scene scene = new Scene(mainLayout, 500, 350);
+        Scene scene = new Scene(mainLayout, 500, 400);
         primaryStage.setScene(scene);
         primaryStage.show();
     }
@@ -219,6 +240,18 @@ public class App extends Application {
         }
     }
 
+    /**
+     * 更新进度条和进度标签
+     * @param message 进度消息
+     * @param progress 进度值 (0.0 - 1.0)
+     */
+    private void updateProgress(String message, double progress) {
+        javafx.application.Platform.runLater(() -> {
+            progressLabel.setText(message);
+            progressBar.setProgress(progress);
+        });
+    }
+
     private void startVerification() {
         String filePath = fileField.getText();
         String folderPath = folderField.getText();
@@ -232,24 +265,33 @@ public class App extends Application {
             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<>();
-        //todo:进度条1%
-        //读取excel
+
+        // 读取excel
+        updateProgress("正在读取Excel文件...", 0.1);
         EasyExcel.read(filePath, InputModel.class, new PageReadListener<InputModel>(dataList -> {
             for (InputModel inputModel : dataList) {
-                //todo:日志
                 inputModelList.add(inputModel);
-                //log.info("读取到一条数据{}", JSON.toJSONString(demoData));
             }
         })).sheet().doRead();
-        //todo:进度条25%
+        updateProgress("Excel文件读取完成,正在扫描文件夹...", 0.25);
         //读取文件夹
         File folder = new File(folderPath);
         File[] files = folder.listFiles();
-        //遍历含有IMEI的文件夹
+
+        // 扫描文件夹并建立IMEI-PDF映射
+        int totalFolders = files.length;
+        int processedFolders = 0;
+
         for (File file : files) {
             if (file.isFile()) {
                 continue;
@@ -271,8 +313,17 @@ public class App extends Application {
                     break;
                 }
             }
+
+            processedFolders++;
+            double progress = 0.25 + (0.25 * processedFolders / totalFolders);
+            updateProgress("正在扫描文件夹: " + processedFolders + "/" + totalFolders, progress);
         }
-        //todo:进度条50%
+        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();
@@ -280,6 +331,9 @@ public class App extends Application {
             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;
             }
             //解析pdf
@@ -292,7 +346,7 @@ public class App extends Application {
                 Matcher matcher = pattern.matcher(text);
                 if (matcher.find()) {
                     String money = matcher.group("money"); // 获取命名分组 "money"
-                    System.out.println("金额: " + money);  // 输出: 金额
+                    //System.out.println("金额: " + money);  // 输出: 金额
                     BigDecimal amount1 = new BigDecimal(inputModel.getAmount());
                     BigDecimal amount2 = new BigDecimal(money);
                     outputModel.setPdfAmount(amount2.doubleValue());
@@ -303,14 +357,34 @@ public class App extends Application {
                 }
             } catch (IOException | NumberFormatException e) {
                 e.printStackTrace();
+                errorImeiList.add(imei);
             }
             outputModelList.add(outputModel);
+
+            processedPDFs++;
+            double progress = 0.5 + (0.4 * processedPDFs / totalPDFs);
+            updateProgress("解析PDF文件: " + processedPDFs + "/" + totalPDFs, progress);
         }
-        //todo:进度条90%
+        updateProgress("PDF文件解析完成,正在生成输出文件...", 0.9);
         //输出最终文件
         String outputFileName = filePath.substring(0, filePath.lastIndexOf(".")) + "_" + System.currentTimeMillis() + "_output.xlsx";
         EasyExcel.write(outputFileName, OutputModel.class).sheet("模板").doWrite(outputModelList);
-        //todo:进度条100%
+        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();
+
+            // 隐藏进度条
+            progressBar.setVisible(false);
+            progressLabel.setVisible(false);
+        });
+
         inputModelList = null;
         outputModelList = null;
         pdfFileMap = null;