Quellcode durchsuchen

feat: 睿行车辆行驶里程日数据入库定时任务日期从文件名获取

weijianghai vor 3 Monaten
Ursprung
Commit
11140a4bb8

+ 24 - 10
src/main/java/com/nokia/finance/tasks/jobs/car/ruixing/CarLiChengDayJob.java

@@ -45,6 +45,8 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 import java.util.function.Function;
 import java.util.function.Predicate;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Stream;
 
 /**
@@ -141,10 +143,10 @@ public class CarLiChengDayJob {
     public List<Map<String, String>> readFile(Path path) throws IOException {
         log.info("读取: {}", path);
         List<String> rawHeaders = Stream.of("车辆所属单位", "车牌号码", "车辆类型", "车辆来源", "车辆使用性质",
-                "行驶时长(小时)", "行驶天数", "总里程(公里)", "OBD总油耗(升)", "百公里油耗(升)", "日期").toList();
+                "行驶时长(小时)", "行驶天数", "总里程(公里)", "OBD总油耗(升)", "百公里油耗(升)").toList();
         List<String> headers = Stream.of("che_liang_suo_shu_dan_wei", "che_pai_hao", "che_liang_lei_xing",
                 "che_liang_lai_yuan", "che_liang_shi_yong_xing_zhi", "xing_shi_shi_chang", "xing_shi_tian_shu",
-                "zong_li_cheng", "obd_you_hao", "bai_gong_li_you_hao", "data_date").toList();
+                "zong_li_cheng", "obd_you_hao", "bai_gong_li_you_hao").toList();
         try (InputStream inputStream = Files.newInputStream(path);
              Workbook workbook = new XSSFWorkbook(inputStream)
         ) {
@@ -156,11 +158,12 @@ public class CarLiChengDayJob {
             // 列数
             int columnCount = headerRow.getPhysicalNumberOfCells();
             log.info("columnCount: {}", columnCount);
+            int headersSize = headers.size();
             // 检查表头
-            if (headers.size() != columnCount) {
+            if (headersSize > columnCount) {
                 throw new MyRuntimeException(path.getFileName() + "列数错误");
             }
-            for (int i = 0; i < columnCount; i++) {
+            for (int i = 0; i < headersSize; i++) {
                 Cell cell = headerRow.getCell(i);
                 if (cell == null || !rawHeaders.get(i).equals(cell.getStringCellValue())) {
                     throw new MyRuntimeException(path.getFileName() + " 表头错误");
@@ -181,7 +184,7 @@ public class CarLiChengDayJob {
                 }
                 Map<String, String> rowMap = new LinkedHashMap<>();
                 // 遍历列
-                for (int j = 0; j < columnCount; j++) {
+                for (int j = 0; j < headersSize; j++) {
                     String cellValue = "";
                     rowMap.put(headers.get(j), cellValue);
                     Cell cell = row.getCell(j);
@@ -222,6 +225,16 @@ public class CarLiChengDayJob {
      * @param list 数据
      */
     public List<Map<String, String>> dataProcessing(Path path, List<Map<String, String>> list) {
+        // 从文件名提取日期
+        String regex = "\\d{8}";
+        Pattern pattern = Pattern.compile(regex);
+        Matcher matcher = pattern.matcher(path.getFileName().toString());
+        String dateString;
+        if (matcher.find()) {
+            dateString = matcher.group();
+        } else {
+            throw new MyRuntimeException(path.getFileName() + " 提取日期失败");
+        }
         List<OrganizationPo> secondOrgs = organizationService.getSecondOrgs();
         List<OrganizationPo> thirdOrgs = organizationService.getThirdOrgs();
         Map<String, OrganizationPo> orgMap = organizationService.getOrgMap(secondOrgs, thirdOrgs);
@@ -231,12 +244,13 @@ public class CarLiChengDayJob {
         List<AreaPo> districts = areaService.getDistricts();
         Map<String, AreaPo> areaMap = areaService.getAreaMap(cities, districts);
         Map<String, List<AreaPo>> districtListMap = areaService.getDistrictListMap(cities, districts);
+        LocalDate localDate = LocalDate.parse(dateString, DateTimeFormatter.ofPattern("yyyyMMdd"));
+        String dataDate = localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+        String yearMonth = localDate.format(DateTimeFormatter.ofPattern("yyyyMM"));
+        String year = String.valueOf(localDate.getYear());
+        String month = String.valueOf(localDate.getMonthValue());
         for (Map<String, String> map : list) {
-            String dataDate = map.get("data_date");
-            LocalDate localDate = LocalDate.parse(dataDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
-            String yearMonth = localDate.format(DateTimeFormatter.ofPattern("yyyyMM"));
-            String year = String.valueOf(localDate.getYear());
-            String month = String.valueOf(localDate.getMonthValue());
+            map.put("data_date", dataDate);
             map.put("year_month", yearMonth);
             map.put("year_no", year);
             map.put("month_no", month);