xyc
3 天以前 1c9505d00bd076898cdc5a7d916106e175a91119
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
package com.trafficaudit.freightforecast.service;
 
import com.trafficaudit.freightforecast.dto.FreightForecastAnalysis;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
 
import java.io.ByteArrayInputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
 
class FreightForecastExcelExporterTest {
 
    private final FreightForecastExcelExporter exporter = new FreightForecastExcelExporter();
 
    @Test
    void exportsLeaderReportWithPreviousMonthAndTemplateTypography() throws Exception {
        FreightForecastAnalysis analysis = analysis();
        analysis.setMonthlyForecastWan(10000);
        analysis.setMonthlyReportWan(12000);
        analysis.setMonthlyReportYoy(0.05);
        analysis.setMonthlyDiffRate(0.20);
        analysis.setPreviousMonthDiffRate(0.10);
        analysis.setCumulativeReportWan(90000);
        analysis.setCumulativeReportYoy(0.04);
        analysis.setCumulativeForecastWan(88000);
        analysis.setCumulativeDiffRate(0.0227);
        analysis.setTotalCumulativeYoy(0.035);
 
        FreightForecastAnalysis.CityRow city = new FreightForecastAnalysis.CityRow();
        city.setCity("武汉市");
        city.setPreviousMonthDiffRate(0.08);
        city.setMonthlyForecastWan(1000);
        city.setMonthlyReportWan(1350);
        city.setMonthlyReportYoy(0.06);
        city.setMonthlyDiffRate(0.35);
        city.setCumulativeReportWan(9000);
        city.setCumulativeReportYoy(0.05);
        city.setCumulativeForecastWan(8700);
        city.setCumulativeDiffRate(0.0345);
        analysis.getCities().add(city);
 
        try (XSSFWorkbook workbook = workbook(exporter.exportDifferenceRateReport(analysis))) {
            assertEquals("市州_上报情况", workbook.getSheetAt(0).getSheetName());
            Sheet sheet = workbook.getSheet("市州_上报情况");
            assertEquals("湖北省规上货运分市州11月周转量上报情况", sheet.getRow(0).getCell(0).getStringCellValue());
            assertEquals("10月差异率", sheet.getRow(2).getCell(1).getStringCellValue());
            assertEquals(10, sheet.getRow(2).getLastCellNum());
            assertEquals(36, sheet.getRow(0).getHeightInPoints(), 0.001);
            assertEquals(47, sheet.getRow(2).getHeightInPoints(), 0.001);
            assertEquals(30, sheet.getRow(3).getHeightInPoints(), 0.001);
            assertEquals("SimHei", titleStyle(sheet).getFont().getFontName());
            assertEquals(18, titleStyle(sheet).getFont().getFontHeightInPoints());
            assertTrue(titleStyle(sheet).getFont().getBold());
            assertTemplateFont((XSSFCellStyle) sheet.getRow(2).getCell(0).getCellStyle(), true);
            assertTemplateFont((XSSFCellStyle) sheet.getRow(3).getCell(0).getCellStyle(), false);
            assertEquals("总周转量(规上+规下)累计同比 : 3.5%", sheet.getRow(1).getCell(6).getStringCellValue());
            assertEquals(IndexedColors.RED.getIndex(), sheet.getRow(4).getCell(0).getCellStyle().getFillForegroundColor());
            assertEquals(IndexedColors.RED.getIndex(), sheet.getRow(4).getCell(5).getCellStyle().getFillForegroundColor());
            assertEquals(VerticalAlignment.CENTER, sheet.getRow(3).getCell(0).getCellStyle().getVerticalAlignment());
        }
    }
 
    @Test
    void exportsOneWorkbookPerCityWithoutForecastTurnover() throws Exception {
        FreightForecastAnalysis analysis = analysis();
 
        Map<String, byte[]> files = unzip(exporter.exportCityEnterpriseReports(analysis));
 
        assertEquals(2, files.size());
        assertTrue(files.containsKey("01_武汉市_2026年11月规上企业上报差异率情况.xlsx"));
        assertTrue(files.containsKey("02_襄阳市_2026年11月规上企业上报差异率情况.xlsx"));
 
        try (XSSFWorkbook workbook = workbook(files.get("01_武汉市_2026年11月规上企业上报差异率情况.xlsx"))) {
            Sheet sheet = workbook.getSheetAt(0);
            Row header = sheet.getRow(2);
            assertEquals("序号", header.getCell(0).getStringCellValue());
            assertEquals("市州", header.getCell(1).getStringCellValue());
            assertEquals("企业名称", header.getCell(2).getStringCellValue());
            assertEquals("上报周转量(万吨公里)", header.getCell(3).getStringCellValue());
            assertEquals("差额(上报-测算,万吨公里)", header.getCell(4).getStringCellValue());
            assertEquals("差异率(上报/测算-1)", header.getCell(5).getStringCellValue());
            assertEquals(6, header.getLastCellNum());
            assertFalse(sheetContainsHeader(sheet, "测算周转量(万吨公里)"));
            assertFalse(sheetContainsHeader(sheet, "状态"));
 
            Row first = sheet.getRow(3);
            assertEquals(1200.0, first.getCell(3).getNumericCellValue(), 0.000001);
            assertEquals(200.0, first.getCell(4).getNumericCellValue(), 0.000001);
            assertEquals(0.20, first.getCell(5).getNumericCellValue(), 0.000001);
            assertFalse(sheetContainsNumber(sheet, 1000.0));
            assertFalse(sheetContainsNumber(sheet, 500.0));
        }
    }
 
    private XSSFCellStyle titleStyle(Sheet sheet) {
        return (XSSFCellStyle) sheet.getRow(0).getCell(0).getCellStyle();
    }
 
    private void assertTemplateFont(XSSFCellStyle style, boolean bold) {
        assertEquals("FangSong_GB2312", style.getFont().getFontName());
        assertEquals(14, style.getFont().getFontHeightInPoints());
        assertEquals(bold, style.getFont().getBold());
    }
 
    private FreightForecastAnalysis analysis() {
        FreightForecastAnalysis analysis = new FreightForecastAnalysis();
        analysis.setPeriod("2026-11");
        analysis.setMonth(11);
        analysis.getEnterprises().add(enterprise("武汉市", "武汉甲运输有限公司", 1000, 1200, 0.20));
        analysis.getEnterprises().add(enterprise("武汉市", "武汉乙运输有限公司", 500, 450, -0.10));
        analysis.getEnterprises().add(enterprise("襄阳市", "襄阳丙运输有限公司", 800, 880, 0.10));
        return analysis;
    }
 
    private FreightForecastAnalysis.EnterpriseRow enterprise(
            String city, String name, double forecast, double reported, double diffRate) {
        FreightForecastAnalysis.EnterpriseRow row = new FreightForecastAnalysis.EnterpriseRow();
        row.setCity(city);
        row.setEnterpriseName(name);
        row.setForecastTurnoverWan(forecast);
        row.setReportedTurnoverWan(reported);
        row.setDiffRate(diffRate);
        row.setStatus("OK");
        return row;
    }
 
    private Map<String, byte[]> unzip(byte[] bytes) throws Exception {
        Map<String, byte[]> files = new LinkedHashMap<>();
        try (ZipInputStream zip = new ZipInputStream(new ByteArrayInputStream(bytes))) {
            ZipEntry entry;
            byte[] buffer = new byte[8192];
            while ((entry = zip.getNextEntry()) != null) {
                int length;
                java.io.ByteArrayOutputStream output = new java.io.ByteArrayOutputStream();
                while ((length = zip.read(buffer)) > 0) output.write(buffer, 0, length);
                files.put(entry.getName(), output.toByteArray());
            }
        }
        return files;
    }
 
    private XSSFWorkbook workbook(byte[] bytes) throws Exception {
        assertNotNull(bytes);
        return new XSSFWorkbook(new ByteArrayInputStream(bytes));
    }
 
    private boolean sheetContainsHeader(Sheet sheet, String text) {
        Row header = sheet.getRow(2);
        for (Cell cell : header) {
            if (text.equals(cell.getStringCellValue())) return true;
        }
        return false;
    }
 
    private boolean sheetContainsNumber(Sheet sheet, double expected) {
        for (Row row : sheet) {
            for (Cell cell : row) {
                if (cell.getCellType() == org.apache.poi.ss.usermodel.CellType.NUMERIC
                        && Math.abs(cell.getNumericCellValue() - expected) < 0.000001) {
                    return true;
                }
            }
        }
        return false;
    }
}