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
package com.trafficaudit.freightforecast.service;
 
import com.trafficaudit.auditengine.service.TrackMileageSelector;
import com.trafficaudit.dataimport.entity.VehicleTrackMileage;
import com.trafficaudit.freightforecast.entity.FreightForecastBaseMonth;
import com.trafficaudit.freightforecast.dto.FreightForecastReportRow;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
 
class FreightForecastFormulaTest {
 
    @Test
    void aboveFormulaMatchesReferenceWorkbook() {
        VehicleTrackMileage track = new VehicleTrackMileage();
        track.setMonthlyMileage(720754.86);
        track.setAnomalyMileage(139462.4);
        track.setTrackedVehicles(91);
        double effective = TrackMileageSelector.effectiveMonthlyMileage(track);
        double result = FreightForecastFormula.aboveTurnover(0, 871.56, effective, 91);
        assertEquals(8238801.704676922, result, 0.000001);
    }
 
    @Test
    void belowGrowthMatchesOctoberReference() {
        double result = FreightForecastFormula.belowForward(12024.6585546586, 0.0529, 0.1226);
        assertEquals(13079.8223428299, result, 0.000001);
    }
 
    @Test
    void belowActualGrowthMatchesOctoberReference() {
        double highwayGrowth = FreightForecastFormula.growth(14901.6365, 14428.6154);
        double provincialGrowth = FreightForecastFormula.growth(4019, 3719);
        double freight = FreightForecastFormula.belowForward(12024.6585546586, highwayGrowth, provincialGrowth);
        double highwayTurnoverGrowth = FreightForecastFormula.growth(1284524.5735, 1281859.0612);
        double provincialTurnoverGrowth = FreightForecastFormula.growth(4019, 3719);
        double turnover = FreightForecastFormula.belowForward(1567319.68194779,
                highwayTurnoverGrowth, provincialTurnoverGrowth);
        assertEquals(12706.7596356086, freight, 0.000001);
        assertEquals(1632164.60077778, turnover, 0.000001);
    }
 
    @Test
    void anomalousMileageIsExcludedOnlyAboveThreshold() {
        VehicleTrackMileage track = new VehicleTrackMileage();
        track.setMonthlyMileage(1000.0);
        track.setAnomalyMileage(1001.0);
        track.setTrackedVehicles(1);
        assertEquals(1000, TrackMileageSelector.effectiveMonthlyMileage(track), 0.000001);
 
        track.setAnomalyMileage(500.0);
        assertEquals(1500, TrackMileageSelector.effectiveMonthlyMileage(track), 0.000001);
    }
 
    @Test
    void totalTurnoverCombinesAboveTonKmWithBelowWan() {
        assertEquals(150.0, FreightForecastFormula.totalTurnoverWan(1_000_000, 50), 0.000001);
        assertEquals(0.25, FreightForecastFormula.totalTurnoverYoy(
                1_000_000, 50, 800_000, 40), 0.000001);
    }
 
    @Test
    void baseParserReadsL14ToW15() throws Exception {
        byte[] bytes;
        try (XSSFWorkbook workbook = new XSSFWorkbook();
             ByteArrayOutputStream output = new ByteArrayOutputStream()) {
            Sheet sheet = workbook.createSheet("湖北");
            Row freight = sheet.createRow(13);
            Row turnover = sheet.createRow(14);
            freight.createCell(0).setCellValue("货运量(万吨)");
            turnover.createCell(0).setCellValue("货物周转量(万吨公里)");
            for (int i = 0; i < 12; i++) {
                freight.createCell(11 + i).setCellValue(100 + i);
                turnover.createCell(11 + i).setCellValue(1000 + i);
            }
            workbook.write(output);
            bytes = output.toByteArray();
        }
        List<FreightForecastBaseMonth> rows = FreightForecastBaseParser.parse(
                new ByteArrayInputStream(bytes), 2026, "42-2026年基数.xlsx");
        assertEquals(12, rows.size());
        assertEquals(1, rows.get(0).getReportMonth());
        assertEquals(100, rows.get(0).getBelowFreight(), 0.000001);
        assertEquals(1011, rows.get(11).getBelowTurnover(), 0.000001);
    }
 
    @Test
    void reportParserUsesAnnualCityAndRejectsDuplicateRows() throws Exception {
        Map<String, String> cities = new LinkedHashMap<>();
        cities.put(FreightForecastReportParser.normalizeName("测试物流有限公司"), "武汉市");
        byte[] valid = reportWorkbook("测试物流有限公司", 100, 1000);
        List<FreightForecastReportRow> rows = FreightForecastReportParser.parse(
                new ByteArrayInputStream(valid), cities);
        assertEquals(1, rows.size());
        assertEquals("武汉市", rows.get(0).getCity());
 
        byte[] duplicate = reportWorkbook("测试物流有限公司\n测试物流有限公司", 100, 1000);
        assertThrows(IllegalArgumentException.class,
                () -> FreightForecastReportParser.parse(new ByteArrayInputStream(duplicate), cities));
    }
 
    private byte[] reportWorkbook(String enterpriseName, double freight, double turnover) throws Exception {
        try (XSSFWorkbook workbook = new XSSFWorkbook();
             ByteArrayOutputStream output = new ByteArrayOutputStream()) {
            Sheet sheet = workbook.createSheet("道路货物运输月度生产情况");
            Row header = sheet.createRow(0);
            header.createCell(0).setCellValue("所属地区");
            header.createCell(1).setCellValue("企业名称");
            header.createCell(2).setCellValue("货运量_总计");
            header.createCell(3).setCellValue("货物周转量_总计");
            String[] names = enterpriseName.contains("\n")
                    ? new String[]{"测试物流有限公司", "测试物流有限公司"} : new String[]{enterpriseName};
            for (int i = 0; i < names.length; i++) {
                Row row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue("420100");
                row.createCell(1).setCellValue(names[i]);
                row.createCell(2).setCellValue(freight);
                row.createCell(3).setCellValue(turnover);
            }
            workbook.write(output);
            return output.toByteArray();
        }
    }
}