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
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
package com.trafficaudit.freightforecast.service;
 
import com.trafficaudit.auditengine.service.TrackMileageCitySourceRuleService;
import com.trafficaudit.dataimport.entity.H2032EnterpriseMonthly;
import com.trafficaudit.dataimport.mapper.H2032EnterpriseMonthlyMapper;
import com.trafficaudit.dataimport.mapper.ScaleSplitTransportMapper;
import com.trafficaudit.dataimport.mapper.TransportAuthVehicleMapper;
import com.trafficaudit.dataimport.mapper.VehicleTrackMileageMapper;
import com.trafficaudit.freightforecast.dto.FreightForecastAnalysis;
import com.trafficaudit.freightforecast.entity.FreightForecastAboveEnterprise;
import com.trafficaudit.freightforecast.entity.FreightForecastAboveHistory;
import com.trafficaudit.freightforecast.entity.FreightForecastBaseMonth;
import com.trafficaudit.freightforecast.entity.FreightForecastBelowResult;
import com.trafficaudit.freightforecast.entity.FreightForecastReportRound;
import com.trafficaudit.freightforecast.mapper.FreightForecastAboveEnterpriseMapper;
import com.trafficaudit.freightforecast.mapper.FreightForecastAboveHistoryMapper;
import com.trafficaudit.freightforecast.mapper.FreightForecastBaseMonthMapper;
import com.trafficaudit.freightforecast.mapper.FreightForecastBelowResultMapper;
import com.trafficaudit.freightforecast.mapper.FreightForecastReportRoundMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.jdbc.core.JdbcTemplate;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;
 
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
 
@ExtendWith(MockitoExtension.class)
class FreightForecastServiceHistoryTest {
 
    private static final long HISTORY_TOTAL_TON_KM = 48_433_591_740L;
    private static final double HISTORY_TOTAL_FRACTION_TON_KM = 0.7395;
    private static final double CURRENT_FORECAST_TON_KM = 1_000_000.0;
 
    @Mock
    private FreightForecastBaseMonthMapper baseMapper;
    @Mock
    private FreightForecastBelowResultMapper belowMapper;
    @Mock
    private FreightForecastAboveEnterpriseMapper aboveMapper;
    @Mock
    private FreightForecastAboveHistoryMapper historyMapper;
    @Mock
    private FreightForecastReportRoundMapper roundMapper;
    @Mock
    private H2032EnterpriseMonthlyMapper h2032Mapper;
    @Mock
    private TransportAuthVehicleMapper authMapper;
    @Mock
    private VehicleTrackMileageMapper trackMapper;
    @Mock
    private TrackMileageCitySourceRuleService trackCitySourceRuleService;
    @Mock
    private ScaleSplitTransportMapper scaleSplitMapper;
    @Mock
    private JdbcTemplate jdbcTemplate;
 
    @InjectMocks
    private FreightForecastService service;
 
    @Test
    void defaultsToHistoryAndAddsOnlyTheMonthAfterHistoricalCutoff() {
        prepareAnalysisInputs(historyRow());
 
        FreightForecastAnalysis analysis = service.analyze(
                "2026-11", FreightForecastService.BELOW_SCALE_SPLIT_PLUS_MODULE, null);
 
        assertEquals(FreightForecastService.FORECAST_HISTORY_PLUS_MODULE,
                analysis.getForecastHistorySource());
        assertEquals(FreightForecastService.REPORT_H2032, analysis.getReportHistorySource());
        assertEquals(7L, analysis.getRoundId());
        assertEquals("2026-10", analysis.getHistoryThroughPeriod());
        assertEquals(4_843_459.17407395, analysis.getCumulativeForecastWan(), 0.000001);
        FreightForecastAnalysis.CityRow wuhan = analysis.getCities().stream()
                .filter(row -> "武汉市".equals(row.getCity()))
                .findFirst()
                .orElseThrow(() -> new AssertionError("缺少武汉市汇总行"));
        assertEquals(100.0, wuhan.getMonthlyReportWan(), 0.000001);
        assertTrue(analysis.getSourceSummary().contains("当月上报=H2032当前工作数据"));
        assertTrue(analysis.getSourceSummary().contains("历史上报=H2032逐月"));
        assertFalse(analysis.getSourceSummary().contains("历史测算"));
        assertFalse(analysis.getSourceSummary().contains("规下"));
        assertFalse(analysis.getWarnings().stream().anyMatch(warning -> warning.contains("历史累计")));
    }
 
    @Test
    void rejectsHistoryPlusModuleWhenNoHistoricalSnapshotExists() {
        prepareAnalysisInputs(null);
 
        IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
                () -> service.analyze(
                        "2026-11", FreightForecastService.BELOW_SCALE_SPLIT_PLUS_MODULE,
                        FreightForecastService.FORECAST_HISTORY_PLUS_MODULE));
 
        assertTrue(error.getMessage().contains("未找到截至本月或更早月份的系统内置历史累计测算基准"));
    }
 
    @Test
    void rejectsZeroMonthlyForecastAfterHistoricalCutoff() {
        prepareAnalysisInputs(historyRow());
        FreightForecastAboveEnterprise zero = currentForecast();
        zero.setForecastTurnover(0.0);
        when(aboveMapper.selectList(any())).thenReturn(Collections.singletonList(zero));
 
        IllegalArgumentException error = assertThrows(IllegalArgumentException.class,
                () -> service.analyze(
                        "2026-11", FreightForecastService.BELOW_SCALE_SPLIT_PLUS_MODULE,
                        FreightForecastService.FORECAST_HISTORY_PLUS_MODULE));
 
        assertTrue(error.getMessage().contains("2026-11"));
        assertTrue(error.getMessage().contains("空快照或全零快照"));
    }
 
    private void prepareAnalysisInputs(FreightForecastAboveHistory history) {
        Map<String, Object> enterprise = new LinkedHashMap<>();
        enterprise.put("enterprise_name", "测试物流有限公司");
        enterprise.put("city", "武汉市");
        when(jdbcTemplate.queryForList(anyString(), eq(2026)))
                .thenReturn(Collections.singletonList(enterprise));
        when(roundMapper.selectOne(any())).thenReturn(currentRound());
        when(aboveMapper.selectList(any()))
                .thenReturn(Collections.singletonList(currentForecast()));
        when(historyMapper.selectOne(any())).thenReturn(history);
        when(h2032Mapper.selectList(any()))
                .thenReturn(Collections.singletonList(h2032Row()));
        if (history == null) return;
        when(historyMapper.selectList(any())).thenReturn(
                Collections.singletonList(history));
        when(belowMapper.selectList(any()))
                .thenReturn(Collections.singletonList(belowResult()));
        when(baseMapper.selectList(any())).thenReturn(baseMonths());
    }
 
    private FreightForecastReportRound currentRound() {
        FreightForecastReportRound row = new FreightForecastReportRound();
        row.setId(7L);
        row.setReportPeriod("2026-11");
        row.setReportStage(FreightForecastService.STAGE_AUDIT);
        row.setRevisionNo(1);
        row.setFileName("2026年11月货运月报.xlsx");
        return row;
    }
 
    private FreightForecastAboveEnterprise currentForecast() {
        FreightForecastAboveEnterprise row = new FreightForecastAboveEnterprise();
        row.setReportPeriod("2026-11");
        row.setCity("武汉市");
        row.setEnterpriseName("测试物流有限公司");
        row.setForecastTurnover(CURRENT_FORECAST_TON_KM);
        row.setCalculationStatus("OK");
        return row;
    }
 
    private FreightForecastAboveHistory historyRow() {
        FreightForecastAboveHistory row = new FreightForecastAboveHistory();
        row.setStatYear(2026);
        row.setThroughPeriod("2026-10");
        row.setCity("武汉市");
        row.setEnterpriseName("测试物流有限公司");
        row.setCumulativeForecast(HISTORY_TOTAL_TON_KM + HISTORY_TOTAL_FRACTION_TON_KM);
        row.setSourceCumulativeForecast(HISTORY_TOTAL_TON_KM + HISTORY_TOTAL_FRACTION_TON_KM);
        row.setCorrected(false);
        row.setSourceFileName("2026年11月货运测算(最新).xls");
        return row;
    }
 
    private FreightForecastBelowResult belowResult() {
        FreightForecastBelowResult row = new FreightForecastBelowResult();
        row.setResultType(FreightForecastService.BELOW_GROWTH);
        row.setBelowTurnover(50.0);
        return row;
    }
 
    private List<FreightForecastBaseMonth> baseMonths() {
        List<FreightForecastBaseMonth> rows = new ArrayList<>();
        for (int month = 1; month <= 12; month++) {
            FreightForecastBaseMonth row = new FreightForecastBaseMonth();
            row.setStatYear(2026);
            row.setReportMonth(month);
            row.setBelowTurnover(10.0);
            rows.add(row);
        }
        return rows;
    }
 
    private H2032EnterpriseMonthly h2032Row() {
        H2032EnterpriseMonthly row = new H2032EnterpriseMonthly();
        row.setReportPeriod("2026-11");
        row.setRegionCode(null);
        row.setEnterpriseName("测试物流有限公司");
        row.setTurnoverTotal(CURRENT_FORECAST_TON_KM);
        return row;
    }
}