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;
|
}
|
}
|