xyc
5 小时以前 b509d59af23c6afba086fa3fb4c2afb47ab0becb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package com.trafficaudit.holiday.service;
 
import com.trafficaudit.holiday.HolidayNumberUtils;
import com.trafficaudit.holiday.service.HolidayStatService.DailyMetrics;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
 
@Slf4j
@Service
public class HolidayBulletinService {
 
    private static final String WORD_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
    private static final BigDecimal EPSILON = new BigDecimal("0.0000001");
 
    @Value("${holiday.bulletin-template:docs/专项统计/重要节假日/湖北省国庆假期综合运输客流快报模板.docx}")
    private String bulletinTemplatePath;
 
    private final HolidayStatService statService;
    private static final ThreadLocal<Document> CURRENT_DOCUMENT = new ThreadLocal<>();
 
    public HolidayBulletinService(HolidayStatService statService) {
        this.statService = statService;
    }
 
    public byte[] generate(LocalDate date) {
        DailyMetrics current = statService.findMetricsByDate(date);
        if (current == null) {
            throw new IllegalArgumentException("所选日期不在已配置的节假日范围内");
        }
        DailyMetrics previousDay = current.getDayIndex() > 1
                ? statService.dailyMetrics(current.getYear(), current.getHolidayType(), current.getDayIndex() - 1) : null;
        DailyMetrics previousYear = statService.dailyMetrics(current.getYear() - 1, current.getHolidayType(), current.getDayIndex());
        DailyMetrics currentCumulative = statService.cumulativeMetrics(current.getYear(), current.getHolidayType(), current.getDayIndex());
        DailyMetrics previousYearCumulative = statService.cumulativeMetrics(current.getYear() - 1, current.getHolidayType(), current.getDayIndex());
 
        if (current.getTotalPersonFlow() == null) {
            throw new IllegalArgumentException("所选日期的全社会跨区域人员流动量没有足够数据,无法生成快报");
        }
 
        String currentTotal = formatNumber(current.getTotalPersonFlow());
        String cumulativeTotalYi = formatNumber(currentCumulative.getTotalPersonFlow() == null
                ? null : HolidayNumberUtils.divide(currentCumulative.getTotalPersonFlow(), BigDecimal.valueOf(10000)));
        String ringRatioClause = previousDay == null ? "" : ",环比" + ratioPhrase(
                current.getTotalPersonFlow(), previousDay.getTotalPersonFlow());
        String paragraph1 = String.format("%d月%d日(假期第%d天),全省全社会跨区域人员流动量%s万人次%s,比2025年同期%s。",
                date.getMonthValue(), date.getDayOfMonth(), current.getDayIndex(), currentTotal,
                ringRatioClause,
                ratioPhrase(current.getTotalPersonFlow(), previousYear == null ? null : previousYear.getTotalPersonFlow()));
        String paragraph2 = String.format("假期以来,全省全社会跨区域人员流动量累计%s亿人次,比2025年同期%s。",
                cumulativeTotalYi,
                ratioPhrase(currentCumulative.getTotalPersonFlow(),
                        previousYearCumulative == null ? null : previousYearCumulative.getTotalPersonFlow()));
 
        File template = resolveTemplateFile();
        if (!template.exists() || !template.isFile()) {
            throw new IllegalArgumentException("快报模板文件不存在: " + template.getAbsolutePath());
        }
 
        try {
            byte[] templateBytes;
            try (InputStream in = new FileInputStream(template)) {
                templateBytes = readAll(in);
            }
            byte[] documentXml = readZipEntry(templateBytes, "word/document.xml");
            Document document = parseDocument(documentXml);
            CURRENT_DOCUMENT.set(document);
            try {
                replaceParagraph(document, text -> text.startsWith("10月") && text.contains("假期第"), paragraph1);
                replaceParagraph(document, text -> text.startsWith("假期以来"), paragraph2);
                fillTable(document, current, previousDay, previousYear, currentCumulative, previousYearCumulative);
                byte[] replacedXml = serializeDocument(document);
                return replaceZipEntry(templateBytes, "word/document.xml", replacedXml);
            } finally {
                CURRENT_DOCUMENT.remove();
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("快报生成失败: " + e.getMessage(), e);
        }
    }
 
    private void fillTable(Document document, DailyMetrics current, DailyMetrics previousDay,
                           DailyMetrics previousYear, DailyMetrics currentCumulative,
                           DailyMetrics previousYearCumulative) {
        Element table = firstElement(document, "tbl");
        if (table == null) {
            throw new IllegalArgumentException("快报模板中找不到数据表");
        }
        writeTableRow(table, 2, new String[]{
                number(current.getNonBusinessPersonFlow()),
                ratio(current.getNonBusinessPersonFlow(), previousDay == null ? null : previousDay.getNonBusinessPersonFlow()),
                ratio(current.getNonBusinessPersonFlow(), previousYear == null ? null : previousYear.getNonBusinessPersonFlow()),
                "—",
                number(currentCumulative.getNonBusinessPersonFlow()),
                ratio(currentCumulative.getNonBusinessPersonFlow(),
                        previousYearCumulative == null ? null : previousYearCumulative.getNonBusinessPersonFlow()),
                "—"
        });
        writeTableRow(table, 3, new String[]{
                number(current.getRoad()),
                ratio(current.getRoad(), previousDay == null ? null : previousDay.getRoad()),
                ratio(current.getRoad(), previousYear == null ? null : previousYear.getRoad()),
                "—",
                number(currentCumulative.getRoad()),
                ratio(currentCumulative.getRoad(), previousYearCumulative == null ? null : previousYearCumulative.getRoad()),
                "—"
        });
        writeTableRow(table, 4, new String[]{
                number(current.getRoadPersonFlow()),
                ratio(current.getRoadPersonFlow(), previousDay == null ? null : previousDay.getRoadPersonFlow()),
                ratio(current.getRoadPersonFlow(), previousYear == null ? null : previousYear.getRoadPersonFlow()),
                "—",
                number(currentCumulative.getRoadPersonFlow()),
                ratio(currentCumulative.getRoadPersonFlow(), previousYearCumulative == null ? null : previousYearCumulative.getRoadPersonFlow()),
                "—"
        });
        writeTableRow(table, 5, transportRow(current.getRail(), previousDay == null ? null : previousDay.getRail(),
                previousYear == null ? null : previousYear.getRail(), currentCumulative.getRail(),
                previousYearCumulative == null ? null : previousYearCumulative.getRail()));
        writeTableRow(table, 6, transportRow(current.getCivilAviation(), previousDay == null ? null : previousDay.getCivilAviation(),
                previousYear == null ? null : previousYear.getCivilAviation(), currentCumulative.getCivilAviation(),
                previousYearCumulative == null ? null : previousYearCumulative.getCivilAviation()));
        writeTableRow(table, 7, transportRow(current.getWater(), previousDay == null ? null : previousDay.getWater(),
                previousYear == null ? null : previousYear.getWater(), currentCumulative.getWater(),
                previousYearCumulative == null ? null : previousYearCumulative.getWater()));
        writeTableRow(table, 8, transportRow(current.getTotalPersonFlow(), previousDay == null ? null : previousDay.getTotalPersonFlow(),
                previousYear == null ? null : previousYear.getTotalPersonFlow(), currentCumulative.getTotalPersonFlow(),
                previousYearCumulative == null ? null : previousYearCumulative.getTotalPersonFlow()));
    }
 
    private String[] transportRow(BigDecimal current, BigDecimal previousDay, BigDecimal previousYear,
                                  BigDecimal currentCumulative, BigDecimal previousYearCumulative) {
        return new String[]{
                number(current),
                ratio(current, previousDay),
                ratio(current, previousYear),
                "—",
                number(currentCumulative),
                ratio(currentCumulative, previousYearCumulative),
                "—"
        };
    }
 
    private void writeTableRow(Element table, int rowIndex, String[] values) {
        List<Element> rows = childElements(table, "tr");
        if (rowIndex >= rows.size()) return;
        Element row = rows.get(rowIndex);
        List<Element> cells = cellRefs(row);
        int logicalColumn = 0;
        for (Element cell : cells) {
            int span = gridSpan(cell);
            if (logicalColumn >= 2 && logicalColumn < 9) {
                int valueIndex = logicalColumn - 2;
                if (valueIndex < values.length) {
                    setCellText(cell, values[valueIndex]);
                }
            }
            logicalColumn += span;
        }
    }
 
    private List<Element> cellRefs(Element row) {
        List<Element> cells = new ArrayList<>();
        for (Element child : childElements(row, "tc")) cells.add(child);
        return cells;
    }
 
    private int gridSpan(Element cell) {
        Element tcPr = firstChildElement(cell, "tcPr");
        if (tcPr == null) return 1;
        Element span = firstChildElement(tcPr, "gridSpan");
        if (span == null) return 1;
        String value = span.getAttributeNS(WORD_NS, "val");
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
            return 1;
        }
    }
 
    private void setCellText(Element cell, String text) {
        Element tcPr = firstChildElement(cell, "tcPr");
        Element templateParagraph = firstChildElement(cell, "p");
        Element paragraph = createElement("p");
        if (templateParagraph != null) {
            Element pPr = firstChildElement(templateParagraph, "pPr");
            if (pPr != null) paragraph.appendChild(pPr.cloneNode(true));
        }
        Element run = createElement("r");
        Element t = createElement("t");
        t.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:space", "preserve");
        t.setTextContent(text == null ? "" : text);
        run.appendChild(t);
        paragraph.appendChild(run);
 
        List<Node> removals = new ArrayList<>();
        Node child = cell.getFirstChild();
        while (child != null) {
            Node next = child.getNextSibling();
            if (child instanceof Element && "p".equals(localName(child))) {
                removals.add(child);
            }
            child = next;
        }
        for (Node node : removals) cell.removeChild(node);
        if (tcPr != null && tcPr.getNextSibling() != null) {
            cell.insertBefore(paragraph, tcPr.getNextSibling());
        } else if (tcPr != null) {
            cell.appendChild(paragraph);
        } else {
            cell.insertBefore(paragraph, cell.getFirstChild());
        }
    }
 
    private void replaceParagraph(Document document, ParagraphMatcher matcher, String replacement) {
        NodeList paragraphs = document.getElementsByTagNameNS(WORD_NS, "p");
        for (int i = 0; i < paragraphs.getLength(); i++) {
            Element paragraph = (Element) paragraphs.item(i);
            String text = paragraphText(paragraph);
            if (matcher.matches(text)) {
                setParagraphText(paragraph, replacement);
                return;
            }
        }
        throw new IllegalArgumentException("快报模板缺少可替换段落: " + replacement);
    }
 
    private void setParagraphText(Element paragraph, String text) {
        Element pPr = firstChildElement(paragraph, "pPr");
        Element firstRun = firstChildElement(paragraph, "r");
        Element rPr = firstRun == null ? null : firstChildElement(firstRun, "rPr");
        List<Node> removals = new ArrayList<>();
        Node child = paragraph.getFirstChild();
        while (child != null) {
            Node next = child.getNextSibling();
            if (child != pPr) removals.add(child);
            child = next;
        }
        for (Node node : removals) paragraph.removeChild(node);
 
        Element run = createElement("r");
        if (rPr != null) run.appendChild(rPr.cloneNode(true));
        Element t = createElement("t");
        t.setAttributeNS("http://www.w3.org/XML/1998/namespace", "xml:space", "preserve");
        t.setTextContent(text);
        run.appendChild(t);
        paragraph.appendChild(run);
    }
 
    private String paragraphText(Element paragraph) {
        StringBuilder builder = new StringBuilder();
        NodeList texts = paragraph.getElementsByTagNameNS(WORD_NS, "t");
        for (int i = 0; i < texts.getLength(); i++) {
            builder.append(texts.item(i).getTextContent());
        }
        return builder.toString();
    }
 
    private String ratioPhrase(BigDecimal current, BigDecimal base) {
        if (current == null || base == null || base.abs().compareTo(EPSILON) < 0) return "无可比数据";
        BigDecimal ratio = HolidayNumberUtils.divide(current, base);
        BigDecimal percent = HolidayNumberUtils.multiply(ratio.subtract(BigDecimal.ONE), BigDecimal.valueOf(100));
        if (percent == null || percent.abs().compareTo(EPSILON) < 0) return "持平";
        return (percent.signum() > 0 ? "增长" : "下降") + HolidayNumberUtils.format1(percent.abs()) + "%";
    }
 
    private String ratio(BigDecimal current, BigDecimal base) {
        if (current == null || base == null || base.abs().compareTo(EPSILON) < 0) return "—";
        return ratioPhrase(current, base);
    }
 
    private String number(BigDecimal value) {
        return value == null ? "—" : formatNumber(value);
    }
 
    private String formatNumber(BigDecimal value) {
        if (value == null) return "";
        return HolidayNumberUtils.format2(value);
    }
 
    private File resolveTemplateFile() {
        List<File> candidates = new ArrayList<>();
        File configured = new File(bulletinTemplatePath);
        candidates.add(configured);
        if (!configured.isAbsolute()) {
            candidates.add(new File(System.getProperty("user.dir"), bulletinTemplatePath));
            candidates.add(new File(System.getProperty("user.dir"), "../" + bulletinTemplatePath));
        }
        for (File candidate : candidates) {
            try {
                if (candidate.exists() && candidate.isFile()) {
                    return candidate.getCanonicalFile();
                }
            } catch (Exception ignored) {
                // Keep checking.
            }
        }
        return configured;
    }
 
    private byte[] readZipEntry(byte[] zipBytes, String entryName) throws Exception {
        try (ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(zipBytes))) {
            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                if (entryName.equals(entry.getName())) {
                    return readAll(zin);
                }
            }
        }
        throw new IllegalArgumentException("模板中缺少 " + entryName);
    }
 
    private byte[] replaceZipEntry(byte[] zipBytes, String entryName, byte[] replacement) throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(zipBytes));
             ZipOutputStream zout = new ZipOutputStream(out)) {
            ZipEntry entry;
            byte[] buffer = new byte[8192];
            while ((entry = zin.getNextEntry()) != null) {
                ZipEntry target = new ZipEntry(entry.getName());
                zout.putNextEntry(target);
                if (entryName.equals(entry.getName())) {
                    zout.write(replacement);
                } else {
                    int length;
                    while ((length = zin.read(buffer)) > 0) {
                        zout.write(buffer, 0, length);
                    }
                }
                zout.closeEntry();
            }
        }
        return out.toByteArray();
    }
 
    private Document parseDocument(byte[] xml) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        return factory.newDocumentBuilder().parse(new ByteArrayInputStream(xml));
    }
 
    private byte[] serializeDocument(Document document) throws Exception {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        transformer.transform(new DOMSource(document), new StreamResult(out));
        return out.toByteArray();
    }
 
    private Element firstElement(Document document, String localName) {
        NodeList list = document.getElementsByTagNameNS(WORD_NS, localName);
        return list.getLength() == 0 ? null : (Element) list.item(0);
    }
 
    private Element createElement(String localName) {
        Document document = CURRENT_DOCUMENT.get();
        if (document == null) {
            throw new IllegalStateException("Word document is not initialized");
        }
        return document.createElementNS(WORD_NS, "w:" + localName);
    }
 
    private List<Element> childElements(Element parent, String localName) {
        List<Element> result = new ArrayList<>();
        Node child = parent.getFirstChild();
        while (child != null) {
            if (child instanceof Element && localName.equals(localName(child))) {
                result.add((Element) child);
            }
            child = child.getNextSibling();
        }
        return result;
    }
 
    private Element firstChildElement(Element parent, String localName) {
        Node child = parent.getFirstChild();
        while (child != null) {
            if (child instanceof Element && localName.equals(localName(child))) {
                return (Element) child;
            }
            child = child.getNextSibling();
        }
        return null;
    }
 
    private String localName(Node node) {
        return node.getLocalName() == null ? node.getNodeName().replaceFirst("^.*:", "") : node.getLocalName();
    }
 
    private byte[] readAll(InputStream in) throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[8192];
        int length;
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }
        return out.toByteArray();
    }
 
    private interface ParagraphMatcher {
        boolean matches(String text);
    }
}