From e9f7ae6cb6591bf7255957c4012dd2841e21886a Mon Sep 17 00:00:00 2001
From: zhizhijie <zhizhijie@users.noreply.gitee.com>
Date: 星期日, 18 十月 2026 20:23:47 +0800
Subject: [PATCH] fix(汇总大表): 《班线包车》按用户改后样式补齐 20 格(蓝底/细边框)并明确无数据保留上月格式(待验收)
---
traffic-audit-server/src/main/java/com/trafficaudit/reportexport/service/ReportExportService.java | 286 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 281 insertions(+), 5 deletions(-)
diff --git a/traffic-audit-server/src/main/java/com/trafficaudit/reportexport/service/ReportExportService.java b/traffic-audit-server/src/main/java/com/trafficaudit/reportexport/service/ReportExportService.java
index c4476dd..6d852a5 100644
--- a/traffic-audit-server/src/main/java/com/trafficaudit/reportexport/service/ReportExportService.java
+++ b/traffic-audit-server/src/main/java/com/trafficaudit/reportexport/service/ReportExportService.java
@@ -3937,7 +3937,8 @@
if (i >= 0) {
int j = xml.indexOf('>', i);
if (j > 0) {
- String attrs = addAttr(stripAttr(xml.substring(i + 10, j), "topLeftCell"), "topLeftCell", target);
+ // 2026-10-14锛氭粴鍔ㄨ捣鐐瑰浐瀹氬洖 A1锛堝彧鎸厜鏍囷紝涓嶆尓瑙嗗彛锛夛紝鍚﹀垯鎵撳紑鏃惰〃澶磋涓庡乏渚у垪浼氳婊氬嚭瑙嗛噹
+ String attrs = addAttr(stripAttr(xml.substring(i + 10, j), "topLeftCell"), "topLeftCell", "A1");
xml = xml.substring(0, i) + "<sheetView" + attrs + ">" + xml.substring(j + 1);
}
}
@@ -3948,7 +3949,7 @@
if (q > 0) {
String raw = xml.substring(p + 5, q);
activePane = attrValue(raw, "activePane");
- String attrs = addAttr(stripAttr(raw, "topLeftCell"), "topLeftCell", target);
+ String attrs = addAttr(stripAttr(raw, "topLeftCell"), "topLeftCell", "A1");
xml = xml.substring(0, p) + "<pane" + attrs + "/>" + xml.substring(q + 2);
}
}
@@ -5164,13 +5165,288 @@
return null;
}
- /** 姹囨�昏〃姣嶇増瀹氫綅锛氫粎绮剧‘鍖归厤 {year}骞磠month}鏈堚�︼紙P0-P1 楠ㄦ灦鏈熶笉鍏佽璺ㄦ湀/璺ㄥ勾闈欓粯鍥為��锛�
- * 鐩爣鏈熸棤瀵瑰簲姣嶇増鏃舵槑纭姤閿欙紝鏈堜唤鎵╁睍瑙勫垯寰� P0 姣嶇増瀹氱鍚庡啀鏀惧紑锛� */
+ /**
+ * 姹囨�昏〃姣嶇増瀹氫綅锛氬厛绮剧‘鍖归厤 {year}骞磠month}鏈堥亾璺繍杈撻噺姹囨�昏〃.xlsx锛�
+ * 涓嶅瓨鍦ㄦ椂锛�2026-10-16 璧凤級**鑷姩鐢ㄤ笂涓�鏈熶骇鐗╂墿鍒楃敓鎴愬綋鏈熸瘝鐗堝苟钀界洏**锛堟墿鍒楀悗鍋氳〃澶村畬鏁存�ц嚜妫�+灏卞湴琛ラ綈锛夛紝
+ * 鑷姩鎵╁垪澶辫触鍒欎繚鎸佸師鏈夈�屾槑纭姤閿欍�嶇殑琛屼负銆傝法骞达紙1 鏈堬級涓嶈嚜鍔ㄦ墿鍒楋紝浠嶉渶浜哄伐鏂板紑涓�鏈��
+ */
private File resolveSummaryMother(String period, String mode) throws Exception {
String year = period != null && period.contains("-") ? period.split("-")[0] : (period == null ? "" : period);
int month = period != null && period.contains("-") ? monthOf(period, mode) : 12;
String name = year + "骞�" + month + "鏈堥亾璺繍杈撻噺姹囨�昏〃.xlsx";
- return resolveAnyTemplate(summaryTemplateDir, name);
+ try {
+ return resolveAnyTemplate(summaryTemplateDir, name);
+ } catch (RuntimeException notFound) {
+ File made = autoExpandMother(period, year, month);
+ if (made != null) return made;
+ throw notFound;
+ }
+ }
+
+ /**
+ * 缂哄綋鏈熸瘝鐗堟椂鑷姩鎵╁垪锛�2026-10-16锛夛細浠ヤ笂涓�鏈熴�屼骇鐗╃紦瀛樸�嶄负搴曪紙鍙栦笉鍒板垯鍥為��涓婁竴鏈熸瘝鐗堝苟鍛婅锛夛紝
+ * 鍙仛鐗堝紡锛堝綋鏈熷垪鐣欑┖锛夛紝鎵╁垪鍚庡仛銆岃〃澶村畬鏁存�ц嚜妫� + 灏卞湴琛ラ綈銆嶏紝钀界洏涓� {骞磢骞磠鏈坿鏈堥亾璺繍杈撻噺姹囨�昏〃.xlsx銆�
+ * 骞傜瓑锛氱洰鏍囨枃浠跺凡瀛樺湪鍒欑洿鎺ヨ繑鍥炪�佺粷涓嶈鐩栵紱浠讳綍涓�姝ュけ璐ラ兘鏀惧純锛堜繚鎸佸師鏈夋姤閿欒涓猴級銆�
+ */
+ private File autoExpandMother(String period, String year, int month) {
+ try {
+ if (month <= 1) {
+ log.warn("姹囨�诲ぇ琛ㄨ嚜鍔ㄦ墿鍒楋細{} 涓� 1 鏈堬紙璺ㄥ勾锛夛紝闇�浜哄伐鏂板紑涓�鏈紝宸叉斁寮冭嚜鍔ㄦ墿鍒�", period);
+ return null;
+ }
+ File dir = existingSummaryDir();
+ if (dir == null) { log.warn("姹囨�诲ぇ琛ㄨ嚜鍔ㄦ墿鍒楋細鎵句笉鍒版瘝鐗堢洰褰曪紝鏀惧純"); return null; }
+ String targetName = year + "骞�" + month + "鏈堥亾璺繍杈撻噺姹囨�昏〃.xlsx";
+ File target = new File(dir, targetName);
+ if (target.isFile()) return target;
+ String prevPeriod = String.format("%04d-%02d", Integer.parseInt(year), month - 1);
+ File src = resolveLatestProduct(prevPeriod);
+ String srcTag = "浜х墿";
+ if (src == null) {
+ try {
+ src = resolveAnyTemplate(summaryTemplateDir, year + "骞�" + (month - 1) + "鏈堥亾璺繍杈撻噺姹囨�昏〃.xlsx");
+ } catch (Exception ignore) { src = null; }
+ srcTag = "涓婁竴鏈熸瘝鐗�";
+ log.warn("姹囨�诲ぇ琛ㄨ嚜鍔ㄦ墿鍒楋細鏈壘鍒� {} 鐨勪骇鐗╃紦瀛橈紝鏀逛互{}涓哄簳锛堝巻鍙插垪鍙兘缂鸿鏈熸暟鎹級", prevPeriod, srcTag);
+ }
+ if (src == null) { log.warn("姹囨�诲ぇ琛ㄨ嚜鍔ㄦ墿鍒楋細{} 鐨勪骇鐗╀笌姣嶇増閮芥病鏈夛紝鏀惧純", prevPeriod); return null; }
+ byte[] srcBytes;
+ try (java.io.FileInputStream in = new java.io.FileInputStream(src)) {
+ srcBytes = org.apache.poi.util.IOUtils.toByteArray(in);
+ }
+ double savedRatio = ZipSecureFile.getMinInflateRatio();
+ ZipSecureFile.setMinInflateRatio(0.0001);
+ byte[] expanded;
+ try {
+ expanded = summaryLayoutExpander.expand(srcBytes, Integer.parseInt(year), month);
+ } finally {
+ ZipSecureFile.setMinInflateRatio(savedRatio);
+ }
+ Object[] fixed = checkAndFixSummaryHeaderGaps(expanded, src);
+ byte[] bytes = (byte[]) fixed[0];
+ String note = (String) fixed[1];
+ File tmp = new File(dir, targetName + ".tmp");
+ try (java.io.FileOutputStream out = new java.io.FileOutputStream(tmp)) { out.write(bytes); }
+ if (!tmp.renameTo(target)) {
+ try (java.io.FileOutputStream out = new java.io.FileOutputStream(target)) { out.write(bytes); }
+ tmp.delete();
+ }
+ log.warn("姹囨�诲ぇ琛ㄨ嚜鍔ㄦ墿鍒楋細宸茬敓鎴愬綋鏈熸瘝鐗� {}锛堝簳={} {}锛涜〃澶磋嚜妫�锛歿}锛夈�傝鏍稿鐗堝紡鍚庡啀鎵归噺鍑鸿〃銆�",
+ targetName, srcTag, src.getName(), note);
+ return target;
+ } catch (Exception e) {
+ log.warn("姹囨�诲ぇ琛ㄨ嚜鍔ㄦ墿鍒楀け璐ワ紙淇濇寔鍘熸湁鎶ラ敊琛屼负锛夛細{}", e.toString());
+ return null;
+ }
+ }
+
+ /** 姣嶇増鐩綍锛氬彇涓変釜鍊欓�夋牴閲岀涓�涓瓨鍦ㄧ殑鐩綍 */
+ private File existingSummaryDir() {
+ String rel = summaryTemplateDir;
+ while (rel != null && rel.startsWith("./")) rel = rel.substring(2);
+ String[] roots = { summaryTemplateDir, System.getProperty("user.dir") + "/" + rel,
+ System.getProperty("user.dir") + "/../" + rel };
+ for (String root : roots) {
+ if (root == null || root.trim().isEmpty()) continue;
+ File d = new File(root);
+ if (d.isDirectory()) return d;
+ }
+ return null;
+ }
+
+ /**
+ * 琛ㄥご瀹屾暣鎬ц嚜妫� + 灏卞湴琛ラ綈锛�2026-10-16锛夛細鍚屼竴椤靛唴浠ャ�屾湀浠借〃澶存渶澶氱殑閭d竴琛屻�嶄负鍙傝�冭锛坆ig 琛岋級锛�
+ * 鍏跺畠琛岃嫢涔熷惈澶ч噺鏈堜唤琛ㄥご锛堚墺鍙傝�冭鐨� 60%锛岄伩鍏嶆妸鏁版嵁琛岃鍒ゆ垚琛ㄥご琛岋級鍗寸己浜嗗叾涓煇浜涙湀浠斤細
+ * 鈶� 鍏堜粠**搴曠锛堟墿鍒楁簮鏂囦欢锛夊悓 sheet/琛�/鍒�**鍙栧洖锛堟墿鍒楁妸鏌愭牸娓呯┖鏃舵渶鍑嗭級锛�
+ * 鈶� 鍙栦笉鍒板啀鎵�**鍚屽潡鍙傝�冭**鈥斺�旇姹備袱鑰呭湪鍏辨湁鍒椾笂鐨勬湀浠介敭瀹屽叏涓�鑷达紙鈮�80%锛夛紝閬垮厤鎶娿�婅建閬撱�佽疆娓°�嬩袱鍧�
+ * 锛�2026 骞存湀 / 2025 骞存湀鍚屽垪锛変簰鐩镐覆鍐欙紱鎵句笉鍒板悓鍧楄灏卞彧鎶ュ憡銆佷笉鏀癸紱
+ * 鈶� 宸叉湁鏈堜唤琛ㄥご鐨勬牸瀛�**缁濅笉瑕嗙洊**銆�
+ * 杩斿洖 {琛ラ綈鍚庣殑瀛楄妭, 璇存槑鏂囧瓧}銆�
+ */
+ private Object[] checkAndFixSummaryHeaderGaps(byte[] xlsx, File baseSrc) {
+ XSSFWorkbook base = null;
+ double savedRatio2 = ZipSecureFile.getMinInflateRatio();
+ try {
+ if (baseSrc != null && baseSrc.isFile()) {
+ ZipSecureFile.setMinInflateRatio(0.0001); // 浜х墿缂撳瓨鐢� POI 鍐欏嚭锛宻tyles 楂樺帇缂╀細瑙﹀彂 Zip-bomb 璇姤
+ base = new XSSFWorkbook(new java.io.FileInputStream(baseSrc));
+ }
+ } catch (Exception be) {
+ base = null;
+ log.warn("姹囨�诲ぇ琛ㄨ嚜鍔ㄦ墿鍒楋細璇诲彇搴曠澶辫触锛堣〃澶存仮澶嶅皢閫�鍖栦负浠呭悓鍧楄锛夛細{}", be.toString());
+ } finally {
+ ZipSecureFile.setMinInflateRatio(savedRatio2);
+ }
+ try (XSSFWorkbook wb = new XSSFWorkbook(new java.io.ByteArrayInputStream(xlsx))) {
+ int fromBase = 0, fromSibling = 0;
+ List<String> stillMissing = new ArrayList<>();
+ for (int si = 0; si < wb.getNumberOfSheets(); si++) {
+ Sheet sh = wb.getSheetAt(si);
+ Map<Integer, Map<Integer, String>> hdrRows = new LinkedHashMap<>();
+ for (int r = sh.getFirstRowNum(); r <= sh.getLastRowNum(); r++) {
+ Map<Integer, String> m = new HashMap<>();
+ Row row = sh.getRow(r);
+ if (row != null) {
+ for (int c = row.getFirstCellNum(); c >= 0 && c < row.getLastCellNum(); c++) {
+ String k = monthHeaderKey(row.getCell(c));
+ if (k != null) m.put(c, k);
+ }
+ }
+ if (m.size() >= 4) hdrRows.put(r, m);
+ }
+ if (hdrRows.isEmpty()) continue;
+ int bestRow = -1, bestCnt = 0;
+ for (Map.Entry<Integer, Map<Integer, String>> en : hdrRows.entrySet()) {
+ if (en.getValue().size() > bestCnt) { bestCnt = en.getValue().size(); bestRow = en.getKey(); }
+ }
+ Map<Integer, String> bestMap = hdrRows.get(bestRow);
+ Sheet baseSheet = base == null ? null : base.getSheet(sh.getSheetName());
+ // 2026-10-16锛氬簳绋块噷宸叉槸鏈堜唤琛ㄥご鐨勬牸瀛愶紝鎵╁垪鍚庤嫢琚啓鎴愩�屽埆鐨勫勾浠界殑鏈堜唤銆嶏紙鍙屽潡椤靛銆婅建閬撱�佽疆娓°��
+ // 涓ゅ潡鍚屽垪涓嶅悓骞存椂浼氫覆鍐欙級锛屾寜搴曠鎭㈠鈥斺�旀纭墿鍒椾笉搴旀敼鍙樻棦鏈夋湀浠借〃澶达紱璺ㄥ勾涓嶉�傜敤锛堝勾鍐呬笉鍙兘鍙樺勾锛夈��
+ if (baseSheet != null) {
+ for (int r = baseSheet.getFirstRowNum(); r <= baseSheet.getLastRowNum(); r++) {
+ Row brow = baseSheet.getRow(r);
+ if (brow == null) continue;
+ Row orow = sh.getRow(r);
+ if (orow == null) continue;
+ for (int c = brow.getFirstCellNum(); c >= 0 && c < brow.getLastCellNum(); c++) {
+ String bk = monthHeaderKey(brow.getCell(c));
+ if (bk == null) bk = numericSerialKey(brow.getCell(c)); // 搴曠鏃ユ湡搴忓垪涓㈡牸寮忔椂鍏滃簳
+ if (bk == null) continue;
+ Cell oc = orow.getCell(c);
+ String ok = monthHeaderKey(oc);
+ if (ok == null || ok.equals(bk)) continue;
+ if (bk.substring(0, 4).equals(ok.substring(0, 4))) continue; // 鍚屽勾涓嶅姩锛堝彲鑳芥槸鍒椾綅绉伙級
+ copyCellValueAndStyle(brow.getCell(c), oc);
+ fromBase++;
+ }
+ }
+ }
+ for (Map.Entry<Integer, Map<Integer, String>> en : hdrRows.entrySet()) {
+ int r = en.getKey();
+ if (r == bestRow) continue;
+ Map<Integer, String> mine = en.getValue();
+ if (mine.size() < Math.max(4, (bestCnt * 6) / 10)) continue;
+ // 鎵惧悓鍧楀弬鑰冭锛氬叡鏈夊垪鏈堜唤閿竴鑷寸殑鍒楁暟鏈�澶氥�佷笖鍗犳瘮 鈮�80%
+ int sibRow = -1, sibScore = 0;
+ for (Map.Entry<Integer, Map<Integer, String>> en2 : hdrRows.entrySet()) {
+ int o = en2.getKey();
+ if (o == r) continue;
+ Map<Integer, String> other = en2.getValue();
+ int same = 0;
+ for (Map.Entry<Integer, String> e3 : mine.entrySet()) {
+ String k2 = other.get(e3.getKey());
+ if (k2 != null && k2.equals(e3.getValue())) same++;
+ }
+ if (same * 5 >= mine.size() * 4 && same > sibScore) { sibScore = same; sibRow = o; }
+ }
+ List<String> fixedCols = new ArrayList<>();
+ for (Map.Entry<Integer, String> e3 : bestMap.entrySet()) {
+ int col = e3.getKey();
+ Cell cur = sh.getRow(r) == null ? null : sh.getRow(r).getCell(col);
+ if (cur != null && monthHeaderKey(cur) != null) continue; // 宸叉湁鏈堜唤琛ㄥご锛岀粷涓嶈鐩�
+ boolean done = false;
+ // 鈶� 搴曠鍚屼綅缃�
+ if (baseSheet != null) {
+ Row br = baseSheet.getRow(r);
+ Cell bc = br == null ? null : br.getCell(col);
+ if (bc != null && monthHeaderKey(bc) != null) {
+ Cell dst = cur != null ? cur : sh.getRow(r).createCell(col);
+ copyCellValueAndStyle(bc, dst);
+ fromBase++; fixedCols.add(e3.getValue()); done = true;
+ }
+ }
+ // 鈶� 鍚屽潡鍙傝�冭
+ if (!done && sibRow >= 0) {
+ Row sr = sh.getRow(sibRow);
+ Cell sc = sr == null ? null : sr.getCell(col);
+ if (sc != null && monthHeaderKey(sc) != null) {
+ Cell dst = cur != null ? cur : sh.getRow(r).createCell(col);
+ copyCellValueAndStyle(sc, dst);
+ fromSibling++; fixedCols.add(e3.getValue()); done = true;
+ }
+ }
+ if (!done) { /* 鏃犳硶瀹夊叏琛ラ綈 */ }
+ }
+ if (!fixedCols.isEmpty()) {
+ for (String k : fixedCols) {
+ for (Map.Entry<Integer, String> e3 : bestMap.entrySet()) {
+ if (!k.equals(e3.getValue())) continue;
+ Cell c2 = sh.getRow(r).getCell(e3.getKey());
+ Cell c3 = sh.getRow(r).getCell(e3.getKey() + 1);
+ if (c3 != null && c3.getCellType() == CellType.BLANK && bestMap.containsKey(e3.getKey())) {
+ Row br2 = bestRow >= 0 ? sh.getRow(bestRow) : null;
+ Cell r2 = br2 == null ? null : br2.getCell(e3.getKey() + 1);
+ if (r2 != null && r2.getCellType() != CellType.BLANK) copyCellValueAndStyle(r2, c3);
+ }
+ }
+ }
+ }
+ }
+ }
+ baseSafeClose(base);
+ int fixed = fromBase + fromSibling;
+ if (fixed == 0) return new Object[]{xlsx, stillMissing.isEmpty() ? "鏃犵己澶�" : "浠嶇己锛�" + String.join("锛�", stillMissing)};
+ ByteArrayOutputStream bos = new ByteArrayOutputStream(xlsx.length);
+ wb.write(bos);
+ String note = "琛ラ綈 " + fixed + " 澶勮〃澶达紙搴曠 " + fromBase + " / 鍚屽潡琛� " + fromSibling + "锛�"
+ + (stillMissing.isEmpty() ? "" : "锛涗粛鏈夋棤娉曞畨鍏ㄨˉ榻愮殑缂烘牸锛堣鏃ュ織锛�");
+ return new Object[]{bos.toByteArray(), note};
+ } catch (Exception e) {
+ baseSafeClose(base);
+ return new Object[]{xlsx, "鑷澶辫触锛堝拷鐣ワ級锛�" + e};
+ }
+ }
+
+ private void baseSafeClose(XSSFWorkbook b) {
+ try { if (b != null) b.close(); } catch (Exception ignore) { }
+ }
+
+ /** 鏁板�煎瀷 Excel 鏃ユ湡搴忓垪锛�20000~60000锛屽嵆 1954~2064 骞达級鈫� "YYYY-MM"锛涢潪璇ュ尯闂磋繑鍥� null銆�
+ * 浠呯敤浜庛�屽簳绋挎棤鏃ユ湡鏍煎紡浣嗗叾瀹炴槸鏈堜唤琛ㄥご銆嶇殑鍏滃簳姣斿銆� */
+ private String numericSerialKey(Cell c) {
+ if (c == null || c.getCellType() != CellType.NUMERIC) return null;
+ double v = c.getNumericCellValue();
+ if (v < 20000 || v > 60000) return null;
+ java.util.Date d = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(v);
+ java.util.Calendar cal = java.util.Calendar.getInstance();
+ cal.setTime(d);
+ return String.format("%04d-%02d", cal.get(java.util.Calendar.YEAR), cal.get(java.util.Calendar.MONTH) + 1);
+ }
+
+ /** 鏈堜唤琛ㄥご璇嗗埆锛氭棩鏈熷崟鍏冩牸锛堝惈鏃ユ湡搴忓垪锛夋垨 "YYYY骞碝鏈�" 鏂囨湰 鈫� 褰掍竴鍖栭敭锛屽惁鍒� null */
+ private String monthHeaderKey(Cell c) {
+ if (c == null) return null;
+ CellType t = c.getCellType();
+ if (t == CellType.NUMERIC && org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(c)) {
+ java.util.Date d = c.getDateCellValue();
+ if (d == null) return null;
+ java.util.Calendar cal = java.util.Calendar.getInstance();
+ cal.setTime(d);
+ return String.format("%04d-%02d", cal.get(java.util.Calendar.YEAR), cal.get(java.util.Calendar.MONTH) + 1);
+ }
+ if (t == CellType.STRING) {
+ String v = c.getStringCellValue();
+ if (v == null) return null;
+ String s = v.replace(" ", "");
+ if (s.endsWith("鏈�") && s.contains("骞�") && s.length() <= 12) return s;
+ }
+ return null;
+ }
+
+ /** 鍏嬮殕鍗曞厓鏍肩殑鍊间笌鏍峰紡锛堣〃澶磋ˉ榻愮敤锛� */
+ private void copyCellValueAndStyle(Cell src, Cell dst) {
+ if (src == null || dst == null) return;
+ switch (src.getCellType()) {
+ case STRING: dst.setCellValue(src.getStringCellValue()); break;
+ case NUMERIC:
+ if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(src)) dst.setCellValue(src.getDateCellValue());
+ else dst.setCellValue(src.getNumericCellValue());
+ break;
+ case FORMULA: dst.setCellFormula(src.getCellFormula()); break;
+ default: dst.setBlank();
+ }
+ dst.setCellStyle(src.getCellStyle());
}
// ==================== 缃戠害杞﹀杩愰噺鎷嗗垎琛紙鏈堝害锛� ====================
--
Gitblit v1.9.1