浏览代码

农村三资数据填报

wwh 2 周之前
父节点
当前提交
d4fc40393a

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

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

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

@@ -100,6 +100,32 @@ class CooperativeDevelopmentExcelExporterTest
100 100
         }
101 101
     }
102 102
 
103
+    @Test
104
+    @DisplayName("导出填表时间默认为系统当月,且标题统计月与填表月可不同")
105
+    void exportFillerDateDefaultsToCurrentMonth() throws Exception
106
+    {
107
+        java.time.LocalDate today = java.time.LocalDate.now();
108
+        String expectedFill = today.getYear() + "年" + today.getMonthValue() + "月";
109
+        int exportMonth = today.getMonthValue() == 1 ? 12 : today.getMonthValue() - 1;
110
+        int exportYear = today.getMonthValue() == 1 ? today.getYear() - 1 : today.getYear();
111
+        byte[] bytes = exporter.export(exportYear, exportMonth,
112
+                Collections.singletonList(sample("雅安镇", "雅安镇荣嘎村", "合作社A", 10, 20)), "测试用户");
113
+        try (Workbook wb = WorkbookFactory.create(new ByteArrayInputStream(bytes)))
114
+        {
115
+            Sheet sheet = CooperativeDevelopmentExcelParser.resolveSheet(wb);
116
+            Row fillerRow = sheet.getRow(1);
117
+            assertNotNull(fillerRow);
118
+            String fillerText = CooperativeDevelopmentExcelParser.cellString(fillerRow, 0);
119
+            assertNotNull(fillerText);
120
+            assertTrue(fillerText.contains("测试用户"), fillerText);
121
+            assertTrue(fillerText.contains(expectedFill), "填表时间应为当月: " + fillerText);
122
+            assertTrue(!fillerText.contains("X月"), fillerText);
123
+            String title = CooperativeDevelopmentExcelParser.cellString(sheet.getRow(0), 0);
124
+            assertTrue(title.contains(exportYear + "年"), title);
125
+            assertTrue(title.contains("(" + exportMonth + "月)"), title);
126
+        }
127
+    }
128
+
103 129
     private static BizCooperativeDevelopment sample(String town, String village, String coop, int hh, int pop)
104 130
     {
105 131
         BizCooperativeDevelopment row = new BizCooperativeDevelopment();