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