package com.ytlk.ocr.table; import com.ytlk.ocr.OCRSwingArea; import com.ytlk.ocr.util.CompareDataUtil; import com.ytlk.ocr.util.FileUtils; import com.ytlk.ocr.vo.UserErrorVo; import com.ytlk.ocr.vo.UserVo; import com.ytlk.util.OcrUtil; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import java.io.File; import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; /** * @className:HandleDataUtil * @author:cwchen * @date:2025-03-11-11:17 * @version:1.0 * @description:处理excel数据 */ public class HandleDataUtil { public static Object handleData() { ExecutorService executor = null; List> errorDataList = new ArrayList<>(); List> megerDataList = new ArrayList<>(); List> excelDataList = new ArrayList<>(); List> pdfDataList = new ArrayList<>(); try { int rowCount = OCRSwingArea.table.getRowCount(); int rowCount2 = OCRSwingArea.table2.getRowCount(); // 使用 ExecutorService 创建线程池 来读取数据 executor = Executors.newFixedThreadPool(rowCount + rowCount2); List>> futures = new ArrayList<>(); List>> futures2 = new ArrayList<>(); List>> futures3 = new ArrayList<>(); // excel表格读取数据 for (int i = 0; i < rowCount; i++) { Map map = new HashMap<>(); File file = (File) OCRSwingArea.table.getValueAt(i, 0); String path = file.getAbsoluteFile().toPath().toString(); List userVos = FileUtils.getExcelUsers(path); map.put("fileName", file.getName()); map.put("list", userVos); excelDataList.add(map); } // pdf读取数据 for (int i = 0; i < rowCount2; i++) { Map map = new HashMap<>(); File file = (File) OCRSwingArea.table2.getValueAt(i, 0); String path = file.getAbsoluteFile().toPath().toString(); List userVos = OcrUtil.ocrHandle(path); map.put("fileName", file.getName()); map.put("list", userVos); pdfDataList.add(map); } // 校验excel表格数据 for (Map map : excelDataList) { String result = checkData(map); if (StringUtils.isNotBlank(result)) { return result; } } // 校验pdf表格数据 for (Map map : pdfDataList) { String result = checkData(map); if (StringUtils.isNotBlank(result)) { return result; } } // 分析对比数据 // 1.将数据进行合并 megerDataList = mergeData(excelDataList,pdfDataList); // 2.比较数据是否存在实发工资不一致、比较数据是否存在人员不一致 for (Map map : megerDataList) { Future> future3 = executor.submit(new Callable>() { @Override public Map call() throws Exception { Map dataMap = new HashMap<>(); List dataList = new ArrayList<>(); String fileName = (String)map.get("fileName"); List excelList = (List)map.get("list"); List pdfList = (List)map.get("pdfList"); // 3.比较excel中存在的人员是否在pdf中存在 List userErrorVos = CompareDataUtil.compareData(excelList, pdfList); // 4.比较pdf中存在的人员是否在excel中存在 List userErrorVos2 = CompareDataUtil.compareData2(excelList, pdfList); // 5.比较pdf和excel中人员的实发工资不一致的数据 List userErrorVos3 = CompareDataUtil.compareData3(excelList, pdfList); // 6.人员重复 List userErrorVos4 = CompareDataUtil.compareData4(excelList, pdfList); // 合并数据 dataList.addAll(userErrorVos); dataList.addAll(userErrorVos2); dataList.addAll(userErrorVos3); dataList.addAll(userErrorVos4); dataMap.put("fileName", fileName.substring(0, fileName.lastIndexOf("."))); dataMap.put("errorList", dataList); return dataMap; } }); futures3.add(future3); } for (Future> future : futures3) { Map map = future.get(); errorDataList.add(map); } } catch (Exception e) { e.printStackTrace(); } finally { if (executor != null) { executor.shutdown(); } } return errorDataList; } /** * 校验数据 * * @param map * @return String * @author cwchen * @date 2025/3/11 16:42 */ public static String checkData(Map map) { String fileName = (String) map.get("fileName"); List list = (List) map.get("list"); // 核对表格数据是否为空 if (CollectionUtils.isEmpty(list)) { return "文件:" + fileName + ",数据为空"; } // 核对数据是否有为空的存在 for (int j = 0; j < list.size(); j++) { UserVo userVo = list.get(j); if (StringUtils.isBlank(userVo.getName())) { return "文件:" + fileName + ",表格中数据行第" + (j + 1) + "行,姓名为空,请检查"; } if (userVo.getWage() == null) { return "文件:" + fileName + ",表格中数据行第" + (j + 1) + "行,实发工资为空,请检查"; } } return null; } /** * 合并数据 * @param excelDataList * @param pdfDataList * @return List> * @author cwchen * @date 2025/3/11 16:43 */ public static List> mergeData(List> excelDataList, List> pdfDataList) { for (Map map : excelDataList) { String fileName = (String) map.get("fileName"); String subFileName = fileName.substring(0, fileName.lastIndexOf(".")); for (Map map2 : pdfDataList) { String fileName2 = (String) map2.get("fileName"); String subFileName2 = fileName2.substring(0, fileName2.lastIndexOf(".")); if (Objects.equals(subFileName, subFileName2)) { map.put("pdfList", map2.get("list")); } } } return excelDataList; } }