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
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
package com.trafficaudit.freightforecast.service;
 
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
 
/** Creates the optional freight forecast tables on existing deployments. */
@Component
@Order(90)
public class FreightForecastSchemaInitializer implements ApplicationRunner {
 
    @Resource
    private JdbcTemplate jdbcTemplate;
 
    @Override
    public void run(ApplicationArguments args) {
        createBaseMonth();
        createBelowResult();
        createAboveEnterprise();
        createAboveHistory();
        createReportRound();
    }
 
    private void createBaseMonth() {
        execute("CREATE TABLE IF NOT EXISTS freight_forecast_base_month ("
                + "id BIGINT AUTO_INCREMENT PRIMARY KEY,"
                + "stat_year SMALLINT NOT NULL,"
                + "report_month TINYINT NOT NULL,"
                + "below_freight DOUBLE NOT NULL,"
                + "below_turnover DOUBLE NOT NULL,"
                + "source_file VARCHAR(255) NULL,"
                + "created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,"
                + "updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,"
                + "UNIQUE KEY uk_freight_forecast_base_month (stat_year, report_month)"
                + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Freight forecast annual base months'");
    }
 
    private void createBelowResult() {
        execute("CREATE TABLE IF NOT EXISTS freight_forecast_below_result ("
                + "id BIGINT AUTO_INCREMENT PRIMARY KEY,"
                + "report_period VARCHAR(20) NOT NULL,"
                + "result_type VARCHAR(10) NOT NULL,"
                + "base_freight DOUBLE NOT NULL,"
                + "base_turnover DOUBLE NOT NULL,"
                + "highway_freight_base DOUBLE NULL,"
                + "highway_freight_current DOUBLE NULL,"
                + "highway_freight_growth DOUBLE NULL,"
                + "provincial_freight_base DOUBLE NULL,"
                + "provincial_freight_current DOUBLE NULL,"
                + "provincial_freight_growth DOUBLE NULL,"
                + "highway_turnover_base DOUBLE NULL,"
                + "highway_turnover_current DOUBLE NULL,"
                + "highway_turnover_growth DOUBLE NULL,"
                + "provincial_turnover_base DOUBLE NULL,"
                + "provincial_turnover_current DOUBLE NULL,"
                + "provincial_turnover_growth DOUBLE NULL,"
                + "below_freight DOUBLE NOT NULL,"
                + "below_turnover DOUBLE NOT NULL,"
                + "below_freight_yoy DOUBLE NULL,"
                + "below_turnover_yoy DOUBLE NULL,"
                + "note VARCHAR(500) NULL,"
                + "calculated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,"
                + "UNIQUE KEY uk_freight_forecast_below_result (report_period, result_type)"
                + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Below-scale monthly forecast result'");
    }
 
    private void createAboveEnterprise() {
        execute("CREATE TABLE IF NOT EXISTS freight_forecast_above_enterprise ("
                + "id BIGINT AUTO_INCREMENT PRIMARY KEY,"
                + "report_period VARCHAR(20) NOT NULL,"
                + "city VARCHAR(50) NOT NULL,"
                + "enterprise_name VARCHAR(200) NOT NULL,"
                + "tractor_count INT NOT NULL DEFAULT 0,"
                + "other_tons DOUBLE NOT NULL DEFAULT 0,"
                + "monthly_mileage DOUBLE NOT NULL DEFAULT 0,"
                + "anomaly_mileage DOUBLE NOT NULL DEFAULT 0,"
                + "tracked_vehicles INT NOT NULL DEFAULT 0,"
                + "effective_mileage DOUBLE NOT NULL DEFAULT 0,"
                + "forecast_turnover DOUBLE NOT NULL DEFAULT 0,"
                + "track_source VARCHAR(30) NULL,"
                + "calculation_status VARCHAR(30) NOT NULL,"
                + "calculated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,"
                + "UNIQUE KEY uk_freight_forecast_above_enterprise (report_period, enterprise_name),"
                + "KEY idx_freight_forecast_above_enterprise_city (report_period, city)"
                + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Above-scale enterprise turnover forecast snapshot'");
    }
 
    private void createAboveHistory() {
        execute("CREATE TABLE IF NOT EXISTS freight_forecast_above_history ("
                + "id BIGINT AUTO_INCREMENT PRIMARY KEY,"
                + "stat_year SMALLINT NOT NULL,"
                + "through_period VARCHAR(20) NOT NULL,"
                + "city VARCHAR(50) NOT NULL,"
                + "enterprise_name VARCHAR(200) NOT NULL,"
                + "first_half_forecast DOUBLE NULL,"
                + "month7_forecast DOUBLE NULL,"
                + "month8_forecast DOUBLE NULL,"
                + "month9_forecast DOUBLE NULL,"
                + "month10_forecast DOUBLE NULL,"
                + "month11_forecast DOUBLE NULL,"
                + "month12_forecast DOUBLE NULL,"
                + "cumulative_forecast DOUBLE NOT NULL,"
                + "source_cumulative_forecast DOUBLE NOT NULL,"
                + "corrected TINYINT(1) NOT NULL DEFAULT 0,"
                + "calculation_note VARCHAR(500) NULL,"
                + "source_file_name VARCHAR(255) NULL,"
                + "imported_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,"
                + "UNIQUE KEY uk_freight_forecast_above_history (stat_year, through_period, enterprise_name),"
                + "KEY idx_freight_forecast_above_history_period (stat_year, through_period)"
                + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Historical above-scale forecast by period'");
    }
 
    private void createReportRound() {
        execute("CREATE TABLE IF NOT EXISTS freight_forecast_report_round ("
                + "id BIGINT AUTO_INCREMENT PRIMARY KEY,"
                + "report_period VARCHAR(20) NOT NULL,"
                + "report_stage VARCHAR(10) NOT NULL,"
                + "revision_no INT NOT NULL,"
                + "file_name VARCHAR(255) NULL,"
                + "row_count INT NOT NULL DEFAULT 0,"
                + "note VARCHAR(500) NULL,"
                + "imported_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,"
                + "UNIQUE KEY uk_freight_forecast_report_round (report_period, report_stage, revision_no),"
                + "KEY idx_freight_forecast_report_round_period (report_period, report_stage, imported_at)"
                + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Freight monthly report import rounds'");
    }
 
    private void execute(String sql) {
        jdbcTemplate.execute(sql);
    }
}