Sfoglia il codice sorgente

农村三资数据填报

wwh 2 settimane fa
parent
commit
bb8161d046

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

@@ -181,7 +181,8 @@ public class FamilyRanchExcelExporter
181 181
                     continue;
182 182
                 }
183 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 187
                     continue;
187 188
                 }
@@ -205,6 +206,9 @@ public class FamilyRanchExcelExporter
205 206
         }
206 207
     }
207 208
 
209
+    /**
210
+     * 将填表人、填表时间/日期写为当前登录用户与<strong>系统当月</strong>(非导出统计年月)。
211
+     */
208 212
     static void updateFiller(Sheet sheet, String fillerName)
209 213
     {
210 214
         java.time.LocalDate today = java.time.LocalDate.now();
@@ -231,7 +235,7 @@ public class FamilyRanchExcelExporter
231 235
                     continue;
232 236
                 }
233 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 240
                     continue;
237 241
                 }
@@ -240,9 +244,12 @@ public class FamilyRanchExcelExporter
240 244
                 {
241 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 254
                     updated = updated.replaceFirst("XXXX年X?月?", fillTime);
248 255
                 }
@@ -251,6 +258,11 @@ public class FamilyRanchExcelExporter
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 266
     private static void clearDataCells(Row row)
255 267
     {
256 268
         short lastCell = row.getLastCellNum();

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

@@ -193,7 +193,8 @@ public class LargeLivestockFarmerExcelExporter
193 193
                     continue;
194 194
                 }
195 195
                 String text = formatter.formatCellValue(cell);
196
-                if (StringUtils.isEmpty(text) || !text.contains("年"))
196
+                // 跳过填表人/填表时间行,避免把「XXXX年X月」先改成统计年导致填表日期无法落到当月
197
+                if (StringUtils.isEmpty(text) || !text.contains("年") || isFillerMetaText(text))
197 198
                 {
198 199
                     continue;
199 200
                 }
@@ -217,6 +218,9 @@ public class LargeLivestockFarmerExcelExporter
217 218
         }
218 219
     }
219 220
 
221
+    /**
222
+     * 将填表人、填表时间/日期写为当前登录用户与<strong>系统当月</strong>(非导出统计年月)。
223
+     */
220 224
     static void updateFiller(Sheet sheet, String fillerName)
221 225
     {
222 226
         java.time.LocalDate today = java.time.LocalDate.now();
@@ -243,7 +247,7 @@ public class LargeLivestockFarmerExcelExporter
243 247
                     continue;
244 248
                 }
245 249
                 String text = formatter.formatCellValue(cell);
246
-                if (StringUtils.isEmpty(text) || (!text.contains("填表人") && !text.contains("填表时间")))
250
+                if (StringUtils.isEmpty(text) || !isFillerMetaText(text))
247 251
                 {
248 252
                     continue;
249 253
                 }
@@ -252,9 +256,12 @@ public class LargeLivestockFarmerExcelExporter
252 256
                 {
253 257
                     updated = updated.replace("登录用户名称", fillerName);
254 258
                 }
255
-                updated = updated.replaceAll("填表时间:\\s*XXXX年X月", "填表时间:" + fillTime);
256
-                updated = updated.replaceAll("填表时间:\\s*\\d{4}年\\d{1,2}月", "填表时间:" + fillTime);
257
-                if (updated.contains("XXXX年") && updated.contains("填表时间"))
259
+                // 兼容「填表时间」「填表日期」及占位 XXXX年X月 / 已被误替换的 yyyy年X月
260
+                updated = updated.replaceAll("(填表时间|填表日期):\\s*XXXX年X月", "$1:" + fillTime);
261
+                updated = updated.replaceAll("(填表时间|填表日期):\\s*\\d{4}年X月", "$1:" + fillTime);
262
+                updated = updated.replaceAll("(填表时间|填表日期):\\s*\\d{4}年\\d{1,2}月", "$1:" + fillTime);
263
+                if (updated.contains("XXXX年")
264
+                        && (updated.contains("填表时间") || updated.contains("填表日期")))
258 265
                 {
259 266
                     updated = updated.replaceFirst("XXXX年X?月?", fillTime);
260 267
                 }
@@ -263,6 +270,11 @@ public class LargeLivestockFarmerExcelExporter
263 270
         }
264 271
     }
265 272
 
273
+    private static boolean isFillerMetaText(String text)
274
+    {
275
+        return text.contains("填表人") || text.contains("填表时间") || text.contains("填表日期");
276
+    }
277
+
266 278
     private static void clearDataCells(Row row)
267 279
     {
268 280
         short lastCell = row.getLastCellNum();

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

@@ -0,0 +1,52 @@
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.BizFamilyRanch;
15
+
16
+@DisplayName("FamilyRanchExcelExporter")
17
+class FamilyRanchExcelExporterTest
18
+{
19
+    private final FamilyRanchExcelExporter exporter = new FamilyRanchExcelExporter();
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
+        BizFamilyRanch row = new BizFamilyRanch();
31
+        row.setTownName("雅安镇");
32
+        row.setVillageDeptId(201L);
33
+        row.setVillageName("雅安镇荣嘎村");
34
+        row.setEnterpriseName("测试牧场");
35
+
36
+        byte[] bytes = exporter.export(exportYear, exportMonth, Collections.singletonList(row), "测试用户");
37
+        try (Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(bytes)))
38
+        {
39
+            Sheet sheet = FamilyRanchExcelParser.resolveSheet(wb);
40
+            Row fillerRow = sheet.getRow(1);
41
+            assertNotNull(fillerRow);
42
+            String fillerText = FamilyRanchExcelParser.cellString(fillerRow, 0);
43
+            assertNotNull(fillerText);
44
+            assertTrue(fillerText.contains("测试用户"), fillerText);
45
+            assertTrue(fillerText.contains(expectedFill), "填表时间应为当月: " + fillerText);
46
+            assertTrue(!fillerText.contains("X月"), fillerText);
47
+            String title = FamilyRanchExcelParser.cellString(sheet.getRow(0), 0);
48
+            assertTrue(title.contains(exportYear + "年"), title);
49
+            assertTrue(title.contains("(" + exportMonth + "月)"), title);
50
+        }
51
+    }
52
+}

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

@@ -0,0 +1,52 @@
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.BizLargeLivestockFarmer;
15
+
16
+@DisplayName("LargeLivestockFarmerExcelExporter")
17
+class LargeLivestockFarmerExcelExporterTest
18
+{
19
+    private final LargeLivestockFarmerExcelExporter exporter = new LargeLivestockFarmerExcelExporter();
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
+        BizLargeLivestockFarmer row = new BizLargeLivestockFarmer();
31
+        row.setTownName("雅安镇");
32
+        row.setVillageDeptId(201L);
33
+        row.setVillageName("雅安镇荣嘎村");
34
+        row.setEnterpriseName("测试大户");
35
+
36
+        byte[] bytes = exporter.export(exportYear, exportMonth, Collections.singletonList(row), "测试用户");
37
+        try (Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(bytes)))
38
+        {
39
+            Sheet sheet = LargeLivestockFarmerExcelParser.resolveSheet(wb);
40
+            Row fillerRow = sheet.getRow(1);
41
+            assertNotNull(fillerRow);
42
+            String fillerText = LargeLivestockFarmerExcelParser.cellString(fillerRow, 0);
43
+            assertNotNull(fillerText);
44
+            assertTrue(fillerText.contains("测试用户"), fillerText);
45
+            assertTrue(fillerText.contains(expectedFill), "填表时间应为当月: " + fillerText);
46
+            assertTrue(!fillerText.contains("X月"), fillerText);
47
+            String title = LargeLivestockFarmerExcelParser.cellString(sheet.getRow(0), 0);
48
+            assertTrue(title.contains(exportYear + "年"), title);
49
+            assertTrue(title.contains("(" + exportMonth + "月)"), title);
50
+        }
51
+    }
52
+}