xyc
9 小时以前 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
package com.trafficaudit.freightforecast.service;
 
import com.trafficaudit.common.util.RegionUtil;
import com.trafficaudit.freightforecast.entity.FreightForecastAboveHistory;
 
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
 
/** Shared calculations for persisted historical above-scale forecast rows. */
public final class FreightForecastAboveHistoryValues {
 
    private FreightForecastAboveHistoryValues() {
    }
 
    public static double cumulativeTotal(List<FreightForecastAboveHistory> rows) {
        return rows.stream().mapToDouble(row -> value(row.getCumulativeForecast())).sum();
    }
 
    public static Map<String, Double> cumulativeByCity(List<FreightForecastAboveHistory> rows) {
        Map<String, Double> result = new LinkedHashMap<>();
        for (FreightForecastAboveHistory row : rows) {
            result.merge(RegionUtil.normalizeCityName(row.getCity()),
                    value(row.getCumulativeForecast()), Double::sum);
        }
        return result;
    }
 
    public static Map<String, Double> monthByCity(List<FreightForecastAboveHistory> rows, int month) {
        if (month < 7 || month > 12) return Collections.emptyMap();
        Map<String, Double> result = new LinkedHashMap<>();
        for (FreightForecastAboveHistory row : rows) {
            Double monthly = monthValue(row, month);
            result.merge(RegionUtil.normalizeCityName(row.getCity()), value(monthly), Double::sum);
        }
        return result;
    }
 
    public static double monthTotal(List<FreightForecastAboveHistory> rows, int month) {
        return monthByCity(rows, month).values().stream().mapToDouble(Double::doubleValue).sum();
    }
 
    public static Double monthValue(FreightForecastAboveHistory row, int month) {
        switch (month) {
            case 7:
                return row.getMonth7Forecast();
            case 8:
                return row.getMonth8Forecast();
            case 9:
                return row.getMonth9Forecast();
            case 10:
                return row.getMonth10Forecast();
            case 11:
                return row.getMonth11Forecast();
            case 12:
                return row.getMonth12Forecast();
            default:
                return null;
        }
    }
 
    private static double value(Double value) {
        return value == null ? 0.0 : value;
    }
}