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
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
package com.trafficaudit.freightforecast.service;
 
import com.trafficaudit.freightforecast.entity.FreightForecastAboveHistory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
@Slf4j
@Component
@Order(95)
public class FreightForecastAboveHistoryInitializer implements ApplicationRunner {
 
    private static final String SEED_RESOURCE =
            "freight-forecast-above-history-2026-10.tsv";
    private static final String INSERT_SQL =
            "INSERT INTO freight_forecast_above_history ("
                    + "stat_year, through_period, city, enterprise_name, "
                    + "first_half_forecast, month7_forecast, month8_forecast, "
                    + "month9_forecast, month10_forecast, month11_forecast, "
                    + "month12_forecast, cumulative_forecast, source_cumulative_forecast, "
                    + "corrected, calculation_note, source_file_name, imported_at"
                    + ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
 
    @Resource
    private JdbcTemplate jdbcTemplate;
 
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<FreightForecastAboveHistory> seedRows;
        try (InputStream input = new ClassPathResource(SEED_RESOURCE).getInputStream()) {
            seedRows = FreightForecastAboveHistorySeedLoader.load(input);
        }
 
        FreightForecastAboveHistory sample = seedRows.get(0);
        Set<String> existingNames = new HashSet<>(jdbcTemplate.queryForList(
                "SELECT enterprise_name FROM freight_forecast_above_history "
                        + "WHERE stat_year = ? AND through_period = ?",
                String.class, sample.getStatYear(), sample.getThroughPeriod()));
        List<FreightForecastAboveHistory> missingRows = new ArrayList<>();
        for (FreightForecastAboveHistory row : seedRows) {
            if (!existingNames.contains(row.getEnterpriseName())) {
                missingRows.add(row);
            }
        }
        if (missingRows.isEmpty()) {
            log.info("Freight forecast historical seed already present: {}={} {} rows",
                    sample.getStatYear(), sample.getThroughPeriod(), seedRows.size());
            return;
        }
 
        LocalDateTime importedAt = LocalDateTime.now();
        jdbcTemplate.batchUpdate(INSERT_SQL, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement statement, int index) throws SQLException {
                FreightForecastAboveHistory row = missingRows.get(index);
                statement.setInt(1, row.getStatYear());
                statement.setString(2, row.getThroughPeriod());
                statement.setString(3, row.getCity());
                statement.setString(4, row.getEnterpriseName());
                statement.setDouble(5, row.getFirstHalfForecast());
                statement.setDouble(6, row.getMonth7Forecast());
                statement.setDouble(7, row.getMonth8Forecast());
                statement.setDouble(8, row.getMonth9Forecast());
                statement.setDouble(9, row.getMonth10Forecast());
                statement.setDouble(10, row.getMonth11Forecast());
                statement.setDouble(11, row.getMonth12Forecast());
                statement.setDouble(12, row.getCumulativeForecast());
                statement.setDouble(13, row.getSourceCumulativeForecast());
                statement.setBoolean(14, Boolean.TRUE.equals(row.getCorrected()));
                statement.setString(15, row.getCalculationNote());
                statement.setString(16, row.getSourceFileName());
                statement.setObject(17, importedAt);
            }
 
            @Override
            public int getBatchSize() {
                return missingRows.size();
            }
        });
        log.info("Freight forecast historical seed inserted: {}={} inserted={} total={}",
                sample.getStatYear(), sample.getThroughPeriod(),
                missingRows.size(), seedRows.size());
    }
}