Explorar o código

feat:使用pdfbox获取pdf中的文本,使用正则表达式获取目标的文本;pdf的数据与excel输入的数据进行比较

yang yi hai 11 meses
pai
achega
d63c015a06
Modificáronse 2 ficheiros con 111 adicións e 16 borrados
  1. 6 0
      pom.xml
  2. 105 16
      src/main/java/space/anyi/amountVerification/App.java

+ 6 - 0
pom.xml

@@ -14,6 +14,12 @@
             <artifactId>easyexcel</artifactId>
             <version>4.0.3</version>
         </dependency>
+        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
+        <dependency>
+            <groupId>org.apache.pdfbox</groupId>
+            <artifactId>pdfbox</artifactId>
+            <version>3.0.6</version>
+        </dependency>
     </dependencies>
 
     <properties>

+ 105 - 16
src/main/java/space/anyi/amountVerification/App.java

@@ -20,13 +20,18 @@ import javafx.scene.layout.VBox;
 import javafx.stage.DirectoryChooser;
 import javafx.stage.FileChooser;
 import javafx.stage.Stage;
+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.OutputModel;
 
 import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Optional;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * @ProjectName: amount-verification
@@ -36,6 +41,7 @@ import java.util.Optional;
  * @Description:
  */
 public class App extends Application {
+    private Stage primaryStage;
     /**
      * 文件
      * @see TextField
@@ -55,6 +61,23 @@ public class App extends Application {
      * @see List<OutputModel>
      */
     private List<OutputModel> outputModelList;
+    /**
+     * IMEI码与PDF文件映射
+     * @see Map<String, File>
+     */
+    private Map<String,File> pdfFileMap;
+    /**
+     * 匹配金额使用的正则表达式对象
+     * @see Pattern
+     */
+    private Pattern pattern;
+    {
+        /**
+         * 代码快初始化匹配金额的正则表达式对象
+         */
+        String regex = "理赔金额合计:¥(?<money>\\d+\\.\\d{2})";
+        this.pattern = Pattern.compile(regex);
+    }
 
     public static void main(String[] args) {
         Application.launch(App.class, args);
@@ -62,6 +85,7 @@ public class App extends Application {
 
     @Override
     public void start(Stage primaryStage) throws Exception {
+        this.primaryStage = primaryStage;
         primaryStage.setTitle("金额验证工具");
         primaryStage.setResizable(false);
         // 设置窗口关闭事件处理
@@ -198,32 +222,97 @@ 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()) {
-            System.out.println("请至少选择文件或文件夹");
+        if (filePath.isEmpty() || folderPath.isEmpty()) {
+            Alert alert = new Alert(Alert.AlertType.ERROR);
+            alert.setHeaderText("请选择文件和文件夹");
+            alert.showAndWait();
             return;
         }
+
         inputModelList = new ArrayList<>();
         outputModelList = new ArrayList<>();
+        pdfFileMap = new HashMap<>();
+        List<String> errorImeiList = new ArrayList<>();
+        //todo:进度条1%
         //读取excel
-        String fileName = fileField.getText();
-        EasyExcel.read(fileName, InputModel.class, new PageReadListener<InputModel>(dataList -> {
+        EasyExcel.read(filePath, InputModel.class, new PageReadListener<InputModel>(dataList -> {
             for (InputModel inputModel : dataList) {
                 //todo:日志
                 inputModelList.add(inputModel);
-                System.out.println("inputModel = " + inputModel);
                 //log.info("读取到一条数据{}", JSON.toJSONString(demoData));
             }
         })).sheet().doRead();
-        //todo:进度条
+        //todo:进度条25%
+        //读取文件夹
+        File folder = new File(folderPath);
+        File[] files = folder.listFiles();
+        //遍历含有IMEI的文件夹
+        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;
+                }
+            }
+        }
+        //todo:进度条50%
         for (InputModel inputModel : inputModelList) {
-            //todo:读取pdf金额
+            OutputModel outputModel = new OutputModel(inputModel);
+            String imei = inputModel.getImei();
+            //找到对应pdf的文件路径
+            File pdfFile = pdfFileMap.getOrDefault(imei, null);
+            if (Objects.isNull(pdfFile)) {
+                errorImeiList.add(imei);
+                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(imei);
+                }
+            } catch (IOException | NumberFormatException e) {
+                e.printStackTrace();
+            }
+            outputModelList.add(outputModel);
         }
-        System.out.println("开始验证...");
-        System.out.println("文件路径: " + filePath);
-        System.out.println("文件夹路径: " + folderPath);
-
-        // 这里可以添加实际的验证逻辑
-        // 例如:读取文件内容、处理文件夹中的文件等
+        //todo:进度条90%
+        //输出最终文件
+        String outputFileName = filePath.substring(0, filePath.lastIndexOf(".")) + "_" + System.currentTimeMillis() + "_output.xlsx";
+        EasyExcel.write(outputFileName, OutputModel.class).sheet("模板").doWrite(outputModelList);
+        //todo:进度条100%
+        inputModelList = null;
+        outputModelList = null;
+        pdfFileMap = null;
     }
 }