wwh недель назад: 2
Родитель
Сommit
b046fbc679

+ 17 - 5
baqing-admin/src/main/java/com/ruoyi/web/modules/industryservice/support/FarmerHouseholdExcelExporter.java

@@ -181,7 +181,8 @@ public class FarmerHouseholdExcelExporter
181
                     continue;
181
                     continue;
182
                 }
182
                 }
183
                 String text = formatter.formatCellValue(cell);
183
                 String text = formatter.formatCellValue(cell);
184
-                if (StringUtils.isEmpty(text) || !text.contains("年"))
184
+                // 跳过填表人/填表时间行,避免把「XXXX年X月」先改成统计年导致填表日期无法落到当月
185
+                if (StringUtils.isEmpty(text) || !text.contains("年") || isFillerMetaText(text))
185
                 {
186
                 {
186
                     continue;
187
                     continue;
187
                 }
188
                 }
@@ -205,6 +206,9 @@ public class FarmerHouseholdExcelExporter
205
         }
206
         }
206
     }
207
     }
207
 
208
 
209
+    /**
210
+     * 将填表人、填表时间/日期写为当前登录用户与<strong>系统当月</strong>(非导出统计年月)。
211
+     */
208
     static void updateFiller(Sheet sheet, String fillerName)
212
     static void updateFiller(Sheet sheet, String fillerName)
209
     {
213
     {
210
         java.time.LocalDate today = java.time.LocalDate.now();
214
         java.time.LocalDate today = java.time.LocalDate.now();
@@ -231,7 +235,7 @@ public class FarmerHouseholdExcelExporter
231
                     continue;
235
                     continue;
232
                 }
236
                 }
233
                 String text = formatter.formatCellValue(cell);
237
                 String text = formatter.formatCellValue(cell);
234
-                if (StringUtils.isEmpty(text) || (!text.contains("填表人") && !text.contains("填表时间")))
238
+                if (StringUtils.isEmpty(text) || !isFillerMetaText(text))
235
                 {
239
                 {
236
                     continue;
240
                     continue;
237
                 }
241
                 }
@@ -240,9 +244,12 @@ public class FarmerHouseholdExcelExporter
240
                 {
244
                 {
241
                     updated = updated.replace("登录用户名称", fillerName);
245
                     updated = updated.replace("登录用户名称", fillerName);
242
                 }
246
                 }
243
-                updated = updated.replaceAll("填表时间:\\s*XXXX年X月", "填表时间:" + fillTime);
244
-                updated = updated.replaceAll("填表时间:\\s*\\d{4}年\\d{1,2}月", "填表时间:" + fillTime);
245
-                if (updated.contains("XXXX年") && updated.contains("填表时间"))
247
+                // 兼容「填表时间」「填表日期」及占位 XXXX年X月 / 已被误替换的 yyyy年X月
248
+                updated = updated.replaceAll("(填表时间|填表日期):\\s*XXXX年X月", "$1:" + fillTime);
249
+                updated = updated.replaceAll("(填表时间|填表日期):\\s*\\d{4}年X月", "$1:" + fillTime);
250
+                updated = updated.replaceAll("(填表时间|填表日期):\\s*\\d{4}年\\d{1,2}月", "$1:" + fillTime);
251
+                if (updated.contains("XXXX年")
252
+                        && (updated.contains("填表时间") || updated.contains("填表日期")))
246
                 {
253
                 {
247
                     updated = updated.replaceFirst("XXXX年X?月?", fillTime);
254
                     updated = updated.replaceFirst("XXXX年X?月?", fillTime);
248
                 }
255
                 }
@@ -251,6 +258,11 @@ public class FarmerHouseholdExcelExporter
251
         }
258
         }
252
     }
259
     }
253
 
260
 
261
+    private static boolean isFillerMetaText(String text)
262
+    {
263
+        return text.contains("填表人") || text.contains("填表时间") || text.contains("填表日期");
264
+    }
265
+
254
     private static void clearDataCells(Row row)
266
     private static void clearDataCells(Row row)
255
     {
267
     {
256
         short lastCell = row.getLastCellNum();
268
         short lastCell = row.getLastCellNum();

+ 20 - 4
baqing-admin/src/main/java/com/ruoyi/web/modules/industryservice/support/VaccinationDataExcelExporter.java

@@ -171,7 +171,8 @@ public class VaccinationDataExcelExporter
171
                     continue;
171
                     continue;
172
                 }
172
                 }
173
                 String text = formatter.formatCellValue(cell);
173
                 String text = formatter.formatCellValue(cell);
174
-                if (StringUtils.isEmpty(text) || !text.contains("年"))
174
+                // 跳过填表人/填表时间行,避免把「XXXX年X月」先改成统计年导致填表日期无法落到当月
175
+                if (StringUtils.isEmpty(text) || !text.contains("年") || isFillerMetaText(text))
175
                 {
176
                 {
176
                     continue;
177
                     continue;
177
                 }
178
                 }
@@ -195,6 +196,9 @@ public class VaccinationDataExcelExporter
195
         }
196
         }
196
     }
197
     }
197
 
198
 
199
+    /**
200
+     * 将填表人、填表时间/日期写为当前登录用户与<strong>系统当月</strong>(非导出统计年月)。
201
+     */
198
     static void updateFiller(Sheet sheet, String fillerName)
202
     static void updateFiller(Sheet sheet, String fillerName)
199
     {
203
     {
200
         java.time.LocalDate today = java.time.LocalDate.now();
204
         java.time.LocalDate today = java.time.LocalDate.now();
@@ -221,7 +225,7 @@ public class VaccinationDataExcelExporter
221
                     continue;
225
                     continue;
222
                 }
226
                 }
223
                 String text = formatter.formatCellValue(cell);
227
                 String text = formatter.formatCellValue(cell);
224
-                if (StringUtils.isEmpty(text) || (!text.contains("填表人") && !text.contains("填表时间")))
228
+                if (StringUtils.isEmpty(text) || !isFillerMetaText(text))
225
                 {
229
                 {
226
                     continue;
230
                     continue;
227
                 }
231
                 }
@@ -230,13 +234,25 @@ public class VaccinationDataExcelExporter
230
                 {
234
                 {
231
                     updated = updated.replace("登录用户名称", fillerName);
235
                     updated = updated.replace("登录用户名称", fillerName);
232
                 }
236
                 }
233
-                updated = updated.replaceAll("填表时间:\\s*XXXX年X月", "填表时间:" + fillTime);
234
-                updated = updated.replaceAll("填表时间:\\s*\\d{4}年\\d{1,2}月", "填表时间:" + fillTime);
237
+                // 兼容「填表时间」「填表日期」及占位 XXXX年X月 / 已被误替换的 yyyy年X月
238
+                updated = updated.replaceAll("(填表时间|填表日期):\\s*XXXX年X月", "$1:" + fillTime);
239
+                updated = updated.replaceAll("(填表时间|填表日期):\\s*\\d{4}年X月", "$1:" + fillTime);
240
+                updated = updated.replaceAll("(填表时间|填表日期):\\s*\\d{4}年\\d{1,2}月", "$1:" + fillTime);
241
+                if (updated.contains("XXXX年")
242
+                        && (updated.contains("填表时间") || updated.contains("填表日期")))
243
+                {
244
+                    updated = updated.replaceFirst("XXXX年X?月?", fillTime);
245
+                }
235
                 cell.setCellValue(updated);
246
                 cell.setCellValue(updated);
236
             }
247
             }
237
         }
248
         }
238
     }
249
     }
239
 
250
 
251
+    private static boolean isFillerMetaText(String text)
252
+    {
253
+        return text.contains("填表人") || text.contains("填表时间") || text.contains("填表日期");
254
+    }
255
+
240
     private static Row getOrCreateRow(Sheet sheet, int rowIndex)
256
     private static Row getOrCreateRow(Sheet sheet, int rowIndex)
241
     {
257
     {
242
         Row row = sheet.getRow(rowIndex);
258
         Row row = sheet.getRow(rowIndex);

+ 51 - 0
baqing-admin/src/test/java/com/ruoyi/web/modules/industryservice/support/FarmerHouseholdExcelExporterTest.java

@@ -0,0 +1,51 @@
1
+package com.ruoyi.web.modules.industryservice.support;
2
+
3
+import static org.junit.jupiter.api.Assertions.assertNotNull;
4
+import static org.junit.jupiter.api.Assertions.assertTrue;
5
+
6
+import java.io.ByteArrayInputStream;
7
+import java.util.Collections;
8
+import org.apache.poi.ss.usermodel.Row;
9
+import org.apache.poi.ss.usermodel.Sheet;
10
+import org.apache.poi.ss.usermodel.Workbook;
11
+import org.apache.poi.ss.usermodel.WorkbookFactory;
12
+import org.junit.jupiter.api.DisplayName;
13
+import org.junit.jupiter.api.Test;
14
+import com.ruoyi.web.modules.industryservice.domain.BizFarmerHousehold;
15
+
16
+@DisplayName("FarmerHouseholdExcelExporter")
17
+class FarmerHouseholdExcelExporterTest
18
+{
19
+    private final FarmerHouseholdExcelExporter exporter = new FarmerHouseholdExcelExporter();
20
+
21
+    @Test
22
+    @DisplayName("导出填表时间默认为系统当月,且标题统计月与填表月可不同")
23
+    void exportFillerDateDefaultsToCurrentMonth() throws Exception
24
+    {
25
+        java.time.LocalDate today = java.time.LocalDate.now();
26
+        String expectedFill = today.getYear() + "年" + today.getMonthValue() + "月";
27
+        int exportMonth = today.getMonthValue() == 1 ? 12 : today.getMonthValue() - 1;
28
+        int exportYear = today.getMonthValue() == 1 ? today.getYear() - 1 : today.getYear();
29
+
30
+        BizFarmerHousehold row = new BizFarmerHousehold();
31
+        row.setTownName("雅安镇");
32
+        row.setVillageDeptId(201L);
33
+        row.setVillageName("雅安镇荣嘎村");
34
+
35
+        byte[] bytes = exporter.export(exportYear, exportMonth, Collections.singletonList(row), "测试用户");
36
+        try (Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(bytes)))
37
+        {
38
+            Sheet sheet = FarmerHouseholdExcelParser.resolveSheet(wb);
39
+            Row fillerRow = sheet.getRow(1);
40
+            assertNotNull(fillerRow);
41
+            String fillerText = FarmerHouseholdExcelParser.cellString(fillerRow, 0);
42
+            assertNotNull(fillerText);
43
+            assertTrue(fillerText.contains("测试用户"), fillerText);
44
+            assertTrue(fillerText.contains(expectedFill), "填表时间应为当月: " + fillerText);
45
+            assertTrue(!fillerText.contains("X月"), fillerText);
46
+            String title = FarmerHouseholdExcelParser.cellString(sheet.getRow(0), 0);
47
+            assertTrue(title.contains(exportYear + "年"), title);
48
+            assertTrue(title.contains("(" + exportMonth + "月)"), title);
49
+        }
50
+    }
51
+}

+ 53 - 0
baqing-admin/src/test/java/com/ruoyi/web/modules/industryservice/support/VaccinationDataExcelExporterTest.java

@@ -0,0 +1,53 @@
1
+package com.ruoyi.web.modules.industryservice.support;
2
+
3
+import static org.junit.jupiter.api.Assertions.assertNotNull;
4
+import static org.junit.jupiter.api.Assertions.assertTrue;
5
+
6
+import java.io.ByteArrayInputStream;
7
+import java.util.Collections;
8
+import org.apache.poi.ss.usermodel.Row;
9
+import org.apache.poi.ss.usermodel.Sheet;
10
+import org.apache.poi.ss.usermodel.Workbook;
11
+import org.apache.poi.ss.usermodel.WorkbookFactory;
12
+import org.junit.jupiter.api.DisplayName;
13
+import org.junit.jupiter.api.Test;
14
+import com.ruoyi.web.modules.industryservice.domain.BizVaccinationData;
15
+
16
+@DisplayName("VaccinationDataExcelExporter")
17
+class VaccinationDataExcelExporterTest
18
+{
19
+    private final VaccinationDataExcelExporter exporter = new VaccinationDataExcelExporter();
20
+
21
+    @Test
22
+    @DisplayName("导出填表时间默认为系统当月,且标题统计月与填表月可不同")
23
+    void exportFillerDateDefaultsToCurrentMonth() throws Exception
24
+    {
25
+        java.time.LocalDate today = java.time.LocalDate.now();
26
+        String expectedFill = today.getYear() + "年" + today.getMonthValue() + "月";
27
+        int exportMonth = today.getMonthValue() == 1 ? 12 : today.getMonthValue() - 1;
28
+        int exportYear = today.getMonthValue() == 1 ? today.getYear() - 1 : today.getYear();
29
+
30
+        BizVaccinationData row = new BizVaccinationData();
31
+        row.setTownName("雅安镇");
32
+        row.setVillageDeptId(201L);
33
+        row.setVillageName("雅安镇荣嘎村");
34
+        row.setShouldVaccinateCount(100);
35
+        row.setActualVaccinateCount(90);
36
+
37
+        byte[] bytes = exporter.export(exportYear, exportMonth, Collections.singletonList(row), "测试用户");
38
+        try (Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(bytes)))
39
+        {
40
+            Sheet sheet = VaccinationDataExcelParser.resolveSheet(wb);
41
+            Row fillerRow = sheet.getRow(1);
42
+            assertNotNull(fillerRow);
43
+            String fillerText = VaccinationDataExcelParser.cellString(fillerRow, 0);
44
+            assertNotNull(fillerText);
45
+            assertTrue(fillerText.contains("测试用户"), fillerText);
46
+            assertTrue(fillerText.contains(expectedFill), "填表时间应为当月: " + fillerText);
47
+            assertTrue(!fillerText.contains("X月"), fillerText);
48
+            String title = VaccinationDataExcelParser.cellString(sheet.getRow(0), 0);
49
+            assertTrue(title.contains(exportYear + "年"), title);
50
+            assertTrue(title.contains("(" + exportMonth + "月)"), title);
51
+        }
52
+    }
53
+}