-- ============================================
-- A股智投分析平台 - 数据库初始化脚本
-- 在Docker容器启动时自动执行
-- 分区范围：2024年1月 至 2026年12月
-- ============================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- 使用已创建的数据库
USE aguzhitou;

-- ============================================
-- 1. 市场指数表 (数据量小，无需分区)
-- ============================================
CREATE TABLE IF NOT EXISTS `market_indices` (
    `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `name` VARCHAR(50) NOT NULL COMMENT '指数名称',
    `code` VARCHAR(10) NOT NULL COMMENT '指数代码',
    `current` DECIMAL(10, 2) NOT NULL DEFAULT 0 COMMENT '当前点数',
    `change` DECIMAL(10, 2) NOT NULL DEFAULT 0 COMMENT '涨跌额',
    `changePercent` DECIMAL(5, 2) NOT NULL DEFAULT 0 COMMENT '涨跌幅(%)',
    `volume` BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '成交量',
    `turnover` BIGINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '成交额',
    `sortOrder` INT NOT NULL DEFAULT 0 COMMENT '排序',
    `updatedAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `createdAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY `uk_code` (`code`),
    UNIQUE KEY `uk_name` (`name`),
    KEY `idx_sort` (`sortOrder`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='市场指数表';

-- ============================================
-- 2. 版块表 (数据量小，无需分区)
-- ============================================
CREATE TABLE IF NOT EXISTS `sectors` (
    `id` VARCHAR(36) NOT NULL PRIMARY KEY DEFAULT (UUID()),
    `name` VARCHAR(50) NOT NULL COMMENT '版块名称',
    `code` VARCHAR(10) NOT NULL COMMENT '版块代码',
    `updatedAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `createdAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY `uk_code` (`code`),
    UNIQUE KEY `uk_name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='版块表';

-- ============================================
-- 3. 股票表 (数据量小，无需分区)
-- ============================================
CREATE TABLE IF NOT EXISTS `stocks` (
    `id` VARCHAR(36) NOT NULL PRIMARY KEY DEFAULT (UUID()),
    `code` VARCHAR(10) NOT NULL COMMENT '股票代码',
    `name` VARCHAR(50) NOT NULL COMMENT '股票名称',
    `sector_code` VARCHAR(10) DEFAULT NULL COMMENT '所属版块代码',
    `market_cap` BIGINT UNSIGNED DEFAULT NULL COMMENT '总市值',
    `pe` DECIMAL(8, 2) DEFAULT NULL COMMENT '市盈率',
    `pb` DECIMAL(8, 2) DEFAULT NULL COMMENT '市净率',
    `updatedAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `createdAt` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY `uk_code` (`code`),
    KEY `idx_sector` (`sector_code`),
    CONSTRAINT `fk_stock_sector` FOREIGN KEY (`sector_code`) REFERENCES `sectors` (`code`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='股票表';

-- ============================================
-- 4. 股票行情表 - 热数据 (最近7天，高频查询)
-- ============================================
CREATE TABLE IF NOT EXISTS `stock_quotes_hot` (
    `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `stock_code` VARCHAR(10) NOT NULL COMMENT '股票代码',
    `price` DECIMAL(10, 2) NOT NULL COMMENT '当前价格',
    `open` DECIMAL(10, 2) NOT NULL COMMENT '开盘价',
    `high` DECIMAL(10, 2) NOT NULL COMMENT '最高价',
    `low` DECIMAL(10, 2) NOT NULL COMMENT '最低价',
    `pre_close` DECIMAL(10, 2) NOT NULL COMMENT '昨收价',
    `volume` BIGINT UNSIGNED NOT NULL COMMENT '成交量',
    `turnover` BIGINT UNSIGNED NOT NULL COMMENT '成交额',
    `change_percent` DECIMAL(5, 2) NOT NULL COMMENT '涨跌幅(%)',
    `turnover_rate` DECIMAL(5, 2) DEFAULT NULL COMMENT '换手率(%)',
    `amplitude` DECIMAL(5, 2) DEFAULT NULL COMMENT '振幅(%)',
    `quote_time` DATETIME NOT NULL COMMENT '行情时间',
    KEY `idx_stock_time` (`stock_code`, `quote_time`),
    KEY `idx_time` (`quote_time`),
    KEY `idx_stock` (`stock_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='股票行情热数据表(最近7天)';

-- ============================================
-- 5. 股票行情表 - 历史数据 (2024-01 至 2026-12)
-- ============================================
CREATE TABLE IF NOT EXISTS `stock_quotes_history` (
    `id` BIGINT UNSIGNED AUTO_INCREMENT,
    `stock_code` VARCHAR(10) NOT NULL COMMENT '股票代码',
    `price` DECIMAL(10, 2) NOT NULL COMMENT '当前价格',
    `open` DECIMAL(10, 2) NOT NULL COMMENT '开盘价',
    `high` DECIMAL(10, 2) NOT NULL COMMENT '最高价',
    `low` DECIMAL(10, 2) NOT NULL COMMENT '最低价',
    `pre_close` DECIMAL(10, 2) NOT NULL COMMENT '昨收价',
    `volume` BIGINT UNSIGNED NOT NULL COMMENT '成交量',
    `turnover` BIGINT UNSIGNED NOT NULL COMMENT '成交额',
    `change_percent` DECIMAL(5, 2) NOT NULL COMMENT '涨跌幅(%)',
    `turnover_rate` DECIMAL(5, 2) DEFAULT NULL COMMENT '换手率(%)',
    `amplitude` DECIMAL(5, 2) DEFAULT NULL COMMENT '振幅(%)',
    `quote_time` DATETIME NOT NULL COMMENT '行情时间',
    PRIMARY KEY (`id`, `quote_time`),
    KEY `idx_stock_time` (`stock_code`, `quote_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='股票行情历史表'
PARTITION BY RANGE COLUMNS(`quote_time`) (
    PARTITION `p_2024_01` VALUES LESS THAN ('2024-02-01'),
    PARTITION `p_2024_02` VALUES LESS THAN ('2024-03-01'),
    PARTITION `p_2024_03` VALUES LESS THAN ('2024-04-01'),
    PARTITION `p_2024_04` VALUES LESS THAN ('2024-05-01'),
    PARTITION `p_2024_05` VALUES LESS THAN ('2024-06-01'),
    PARTITION `p_2024_06` VALUES LESS THAN ('2024-07-01'),
    PARTITION `p_2024_07` VALUES LESS THAN ('2024-08-01'),
    PARTITION `p_2024_08` VALUES LESS THAN ('2024-09-01'),
    PARTITION `p_2024_09` VALUES LESS THAN ('2024-10-01'),
    PARTITION `p_2024_10` VALUES LESS THAN ('2024-11-01'),
    PARTITION `p_2024_11` VALUES LESS THAN ('2024-12-01'),
    PARTITION `p_2024_12` VALUES LESS THAN ('2025-01-01'),
    PARTITION `p_2025_01` VALUES LESS THAN ('2025-02-01'),
    PARTITION `p_2025_02` VALUES LESS THAN ('2025-03-01'),
    PARTITION `p_2025_03` VALUES LESS THAN ('2025-04-01'),
    PARTITION `p_2025_04` VALUES LESS THAN ('2025-05-01'),
    PARTITION `p_2025_05` VALUES LESS THAN ('2025-06-01'),
    PARTITION `p_2025_06` VALUES LESS THAN ('2025-07-01'),
    PARTITION `p_2025_07` VALUES LESS THAN ('2025-08-01'),
    PARTITION `p_2025_08` VALUES LESS THAN ('2025-09-01'),
    PARTITION `p_2025_09` VALUES LESS THAN ('2025-10-01'),
    PARTITION `p_2025_10` VALUES LESS THAN ('2025-11-01'),
    PARTITION `p_2025_11` VALUES LESS THAN ('2025-12-01'),
    PARTITION `p_2025_12` VALUES LESS THAN ('2026-01-01'),
    PARTITION `p_2026_01` VALUES LESS THAN ('2026-02-01'),
    PARTITION `p_2026_02` VALUES LESS THAN ('2026-03-01'),
    PARTITION `p_2026_03` VALUES LESS THAN ('2026-04-01'),
    PARTITION `p_2026_04` VALUES LESS THAN ('2026-05-01'),
    PARTITION `p_2026_05` VALUES LESS THAN ('2026-06-01'),
    PARTITION `p_2026_06` VALUES LESS THAN ('2026-07-01'),
    PARTITION `p_2026_07` VALUES LESS THAN ('2026-08-01'),
    PARTITION `p_2026_08` VALUES LESS THAN ('2026-09-01'),
    PARTITION `p_2026_09` VALUES LESS THAN ('2026-10-01'),
    PARTITION `p_2026_10` VALUES LESS THAN ('2026-11-01'),
    PARTITION `p_2026_11` VALUES LESS THAN ('2026-12-01'),
    PARTITION `p_2026_12` VALUES LESS THAN ('2027-01-01'),
    PARTITION `p_future` VALUES LESS THAN (MAXVALUE)
);

-- ============================================
-- 6. 股票K线表
-- ============================================
CREATE TABLE IF NOT EXISTS `stock_klines` (
    `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `stock_code` VARCHAR(10) NOT NULL COMMENT '股票代码',
    `period` VARCHAR(10) NOT NULL COMMENT '周期:day/week/month',
    `date` DATE NOT NULL COMMENT '日期',
    `open` DECIMAL(10, 2) NOT NULL COMMENT '开盘价',
    `high` DECIMAL(10, 2) NOT NULL COMMENT '最高价',
    `low` DECIMAL(10, 2) NOT NULL COMMENT '最低价',
    `close` DECIMAL(10, 2) NOT NULL COMMENT '收盘价',
    `volume` BIGINT UNSIGNED NOT NULL COMMENT '成交量',
    `ma5` DECIMAL(10, 2) DEFAULT NULL COMMENT 'MA5',
    `ma10` DECIMAL(10, 2) DEFAULT NULL COMMENT 'MA10',
    `ma20` DECIMAL(10, 2) DEFAULT NULL COMMENT 'MA20',
    `ma30` DECIMAL(10, 2) DEFAULT NULL COMMENT 'MA30',
    `ma60` DECIMAL(10, 2) DEFAULT NULL COMMENT 'MA60',
    UNIQUE KEY `uk_stock_period_date` (`stock_code`, `period`, `date`),
    KEY `idx_stock` (`stock_code`),
    KEY `idx_date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='股票K线表';

-- ============================================
-- 7. 版块行情表 (2024-01 至 2026-12)
-- ============================================
CREATE TABLE IF NOT EXISTS `sector_quotes` (
    `id` INT UNSIGNED AUTO_INCREMENT,
    `sector_code` VARCHAR(10) NOT NULL COMMENT '版块代码',
    `current` DECIMAL(10, 2) NOT NULL COMMENT '当前点数',
    `change` DECIMAL(10, 2) NOT NULL COMMENT '涨跌额',
    `changePercent` DECIMAL(5, 2) NOT NULL COMMENT '涨跌幅(%)',
    `volume` BIGINT UNSIGNED NOT NULL COMMENT '成交量',
    `turnover` BIGINT UNSIGNED NOT NULL COMMENT '成交额',
    `momentumScore` DECIMAL(5, 2) NOT NULL DEFAULT 50 COMMENT '动量分数',
    `rank` INT NOT NULL DEFAULT 0 COMMENT '当前排名',
    `previous_rank` INT NOT NULL DEFAULT 0 COMMENT '昨日排名',
    `quote_time` DATETIME NOT NULL COMMENT '行情时间',
    PRIMARY KEY (`id`, `quote_time`),
    KEY `idx_sector_time` (`sector_code`, `quote_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='版块行情表'
PARTITION BY RANGE COLUMNS(`quote_time`) (
    PARTITION `p_sector_2024_01` VALUES LESS THAN ('2024-02-01'),
    PARTITION `p_sector_2024_02` VALUES LESS THAN ('2024-03-01'),
    PARTITION `p_sector_2024_03` VALUES LESS THAN ('2024-04-01'),
    PARTITION `p_sector_2024_04` VALUES LESS THAN ('2024-05-01'),
    PARTITION `p_sector_2024_05` VALUES LESS THAN ('2024-06-01'),
    PARTITION `p_sector_2024_06` VALUES LESS THAN ('2024-07-01'),
    PARTITION `p_sector_2024_07` VALUES LESS THAN ('2024-08-01'),
    PARTITION `p_sector_2024_08` VALUES LESS THAN ('2024-09-01'),
    PARTITION `p_sector_2024_09` VALUES LESS THAN ('2024-10-01'),
    PARTITION `p_sector_2024_10` VALUES LESS THAN ('2024-11-01'),
    PARTITION `p_sector_2024_11` VALUES LESS THAN ('2024-12-01'),
    PARTITION `p_sector_2024_12` VALUES LESS THAN ('2025-01-01'),
    PARTITION `p_sector_2025_01` VALUES LESS THAN ('2025-02-01'),
    PARTITION `p_sector_2025_02` VALUES LESS THAN ('2025-03-01'),
    PARTITION `p_sector_2025_03` VALUES LESS THAN ('2025-04-01'),
    PARTITION `p_sector_2025_04` VALUES LESS THAN ('2025-05-01'),
    PARTITION `p_sector_2025_05` VALUES LESS THAN ('2025-06-01'),
    PARTITION `p_sector_2025_06` VALUES LESS THAN ('2025-07-01'),
    PARTITION `p_sector_2025_07` VALUES LESS THAN ('2025-08-01'),
    PARTITION `p_sector_2025_08` VALUES LESS THAN ('2025-09-01'),
    PARTITION `p_sector_2025_09` VALUES LESS THAN ('2025-10-01'),
    PARTITION `p_sector_2025_10` VALUES LESS THAN ('2025-11-01'),
    PARTITION `p_sector_2025_11` VALUES LESS THAN ('2025-12-01'),
    PARTITION `p_sector_2025_12` VALUES LESS THAN ('2026-01-01'),
    PARTITION `p_sector_2026_01` VALUES LESS THAN ('2026-02-01'),
    PARTITION `p_sector_2026_02` VALUES LESS THAN ('2026-03-01'),
    PARTITION `p_sector_2026_03` VALUES LESS THAN ('2026-04-01'),
    PARTITION `p_sector_2026_04` VALUES LESS THAN ('2026-05-01'),
    PARTITION `p_sector_2026_05` VALUES LESS THAN ('2026-06-01'),
    PARTITION `p_sector_2026_06` VALUES LESS THAN ('2026-07-01'),
    PARTITION `p_sector_2026_07` VALUES LESS THAN ('2026-08-01'),
    PARTITION `p_sector_2026_08` VALUES LESS THAN ('2026-09-01'),
    PARTITION `p_sector_2026_09` VALUES LESS THAN ('2026-10-01'),
    PARTITION `p_sector_2026_10` VALUES LESS THAN ('2026-11-01'),
    PARTITION `p_sector_2026_11` VALUES LESS THAN ('2026-12-01'),
    PARTITION `p_sector_2026_12` VALUES LESS THAN ('2027-01-01'),
    PARTITION `p_sector_future` VALUES LESS THAN (MAXVALUE)
);

-- ============================================
-- 8. 版块K线表
-- ============================================
CREATE TABLE IF NOT EXISTS `sector_klines` (
    `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `sector_code` VARCHAR(10) NOT NULL COMMENT '版块代码',
    `period` VARCHAR(10) NOT NULL COMMENT '周期:day/week/month',
    `date` DATE NOT NULL COMMENT '日期',
    `open` DECIMAL(10, 2) NOT NULL COMMENT '开盘价',
    `high` DECIMAL(10, 2) NOT NULL COMMENT '最高价',
    `low` DECIMAL(10, 2) NOT NULL COMMENT '最低价',
    `close` DECIMAL(10, 2) NOT NULL COMMENT '收盘价',
    `volume` BIGINT UNSIGNED NOT NULL COMMENT '成交量',
    UNIQUE KEY `uk_sector_period_date` (`sector_code`, `period`, `date`),
    KEY `idx_sector` (`sector_code`),
    KEY `idx_date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='版块K线表';

-- ============================================
-- 9. 用户表
-- ============================================
CREATE TABLE IF NOT EXISTS `users` (
    `id` VARCHAR(36) NOT NULL PRIMARY KEY DEFAULT (UUID()),
    `username` VARCHAR(20) NOT NULL COMMENT '用户名',
    `email` VARCHAR(100) NOT NULL COMMENT '邮箱',
    `password` VARCHAR(255) NOT NULL COMMENT '加密密码',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY `uk_username` (`username`),
    UNIQUE KEY `uk_email` (`email`),
    KEY `idx_created` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表';

-- ============================================
-- 10. 用户自选股表
-- ============================================
CREATE TABLE IF NOT EXISTS `user_favorites` (
    `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `user_id` VARCHAR(36) NOT NULL COMMENT '用户ID',
    `stock_code` VARCHAR(10) NOT NULL COMMENT '股票代码',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY `uk_user_stock` (`user_id`, `stock_code`),
    KEY `idx_user` (`user_id`),
    KEY `idx_stock` (`stock_code`),
    CONSTRAINT `fk_fav_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户自选股表';

-- ============================================
-- 11. 新高新低股票记录表 (2024-01 至 2026-12)
-- ============================================
CREATE TABLE IF NOT EXISTS `high_low_stocks` (
    `id` INT UNSIGNED AUTO_INCREMENT,
    `stock_code` VARCHAR(10) NOT NULL COMMENT '股票代码',
    `type` VARCHAR(10) NOT NULL COMMENT '类型:high/low',
    `price` DECIMAL(10, 2) NOT NULL COMMENT '价格',
    `date` DATE NOT NULL COMMENT '日期',
    `days_to_highlow` INT NOT NULL COMMENT '距离新高/低天数',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`, `date`),
    KEY `idx_stock` (`stock_code`),
    KEY `idx_type_date` (`type`, `date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='新高新低股票记录表'
PARTITION BY RANGE COLUMNS(`date`) (
    PARTITION `p_hl_2024_01` VALUES LESS THAN ('2024-02-01'),
    PARTITION `p_hl_2024_02` VALUES LESS THAN ('2024-03-01'),
    PARTITION `p_hl_2024_03` VALUES LESS THAN ('2024-04-01'),
    PARTITION `p_hl_2024_04` VALUES LESS THAN ('2024-05-01'),
    PARTITION `p_hl_2024_05` VALUES LESS THAN ('2024-06-01'),
    PARTITION `p_hl_2024_06` VALUES LESS THAN ('2024-07-01'),
    PARTITION `p_hl_2024_07` VALUES LESS THAN ('2024-08-01'),
    PARTITION `p_hl_2024_08` VALUES LESS THAN ('2024-09-01'),
    PARTITION `p_hl_2024_09` VALUES LESS THAN ('2024-10-01'),
    PARTITION `p_hl_2024_10` VALUES LESS THAN ('2024-11-01'),
    PARTITION `p_hl_2024_11` VALUES LESS THAN ('2024-12-01'),
    PARTITION `p_hl_2024_12` VALUES LESS THAN ('2025-01-01'),
    PARTITION `p_hl_2025_01` VALUES LESS THAN ('2025-02-01'),
    PARTITION `p_hl_2025_02` VALUES LESS THAN ('2025-03-01'),
    PARTITION `p_hl_2025_03` VALUES LESS THAN ('2025-04-01'),
    PARTITION `p_hl_2025_04` VALUES LESS THAN ('2025-05-01'),
    PARTITION `p_hl_2025_05` VALUES LESS THAN ('2025-06-01'),
    PARTITION `p_hl_2025_06` VALUES LESS THAN ('2025-07-01'),
    PARTITION `p_hl_2025_07` VALUES LESS THAN ('2025-08-01'),
    PARTITION `p_hl_2025_08` VALUES LESS THAN ('2025-09-01'),
    PARTITION `p_hl_2025_09` VALUES LESS THAN ('2025-10-01'),
    PARTITION `p_hl_2025_10` VALUES LESS THAN ('2025-11-01'),
    PARTITION `p_hl_2025_11` VALUES LESS THAN ('2025-12-01'),
    PARTITION `p_hl_2025_12` VALUES LESS THAN ('2026-01-01'),
    PARTITION `p_hl_2026_01` VALUES LESS THAN ('2026-02-01'),
    PARTITION `p_hl_2026_02` VALUES LESS THAN ('2026-03-01'),
    PARTITION `p_hl_2026_03` VALUES LESS THAN ('2026-04-01'),
    PARTITION `p_hl_2026_04` VALUES LESS THAN ('2026-05-01'),
    PARTITION `p_hl_2026_05` VALUES LESS THAN ('2026-06-01'),
    PARTITION `p_hl_2026_06` VALUES LESS THAN ('2026-07-01'),
    PARTITION `p_hl_2026_07` VALUES LESS THAN ('2026-08-01'),
    PARTITION `p_hl_2026_08` VALUES LESS THAN ('2026-09-01'),
    PARTITION `p_hl_2026_09` VALUES LESS THAN ('2026-10-01'),
    PARTITION `p_hl_2026_10` VALUES LESS THAN ('2026-11-01'),
    PARTITION `p_hl_2026_11` VALUES LESS THAN ('2026-12-01'),
    PARTITION `p_hl_2026_12` VALUES LESS THAN ('2027-01-01'),
    PARTITION `p_hl_future` VALUES LESS THAN (MAXVALUE)
);

-- ============================================
-- 12. 动量股票推荐表 (2024-01 至 2026-12)
-- ============================================
CREATE TABLE IF NOT EXISTS `momentum_stocks` (
    `id` INT UNSIGNED AUTO_INCREMENT,
    `stock_code` VARCHAR(10) NOT NULL COMMENT '股票代码',
    `momentum_score` DECIMAL(5, 2) NOT NULL COMMENT '动量分数',
    `tags` VARCHAR(500) DEFAULT NULL COMMENT '标签JSON数组',
    `volume_ratio` DECIMAL(5, 2) NOT NULL COMMENT '量比',
    `break_through` BOOLEAN NOT NULL DEFAULT FALSE COMMENT '是否突破',
    `date` DATE NOT NULL COMMENT '日期',
    `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`, `date`),
    KEY `idx_stock` (`stock_code`),
    KEY `idx_date_score` (`date`, `momentum_score`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='动量股票推荐表'
PARTITION BY RANGE COLUMNS(`date`) (
    PARTITION `p_ms_2024_01` VALUES LESS THAN ('2024-02-01'),
    PARTITION `p_ms_2024_02` VALUES LESS THAN ('2024-03-01'),
    PARTITION `p_ms_2024_03` VALUES LESS THAN ('2024-04-01'),
    PARTITION `p_ms_2024_04` VALUES LESS THAN ('2024-05-01'),
    PARTITION `p_ms_2024_05` VALUES LESS THAN ('2024-06-01'),
    PARTITION `p_ms_2024_06` VALUES LESS THAN ('2024-07-01'),
    PARTITION `p_ms_2024_07` VALUES LESS THAN ('2024-08-01'),
    PARTITION `p_ms_2024_08` VALUES LESS THAN ('2024-09-01'),
    PARTITION `p_ms_2024_09` VALUES LESS THAN ('2024-10-01'),
    PARTITION `p_ms_2024_10` VALUES LESS THAN ('2024-11-01'),
    PARTITION `p_ms_2024_11` VALUES LESS THAN ('2024-12-01'),
    PARTITION `p_ms_2024_12` VALUES LESS THAN ('2025-01-01'),
    PARTITION `p_ms_2025_01` VALUES LESS THAN ('2025-02-01'),
    PARTITION `p_ms_2025_02` VALUES LESS THAN ('2025-03-01'),
    PARTITION `p_ms_2025_03` VALUES LESS THAN ('2025-04-01'),
    PARTITION `p_ms_2025_04` VALUES LESS THAN ('2025-05-01'),
    PARTITION `p_ms_2025_05` VALUES LESS THAN ('2025-06-01'),
    PARTITION `p_ms_2025_06` VALUES LESS THAN ('2025-07-01'),
    PARTITION `p_ms_2025_07` VALUES LESS THAN ('2025-08-01'),
    PARTITION `p_ms_2025_08` VALUES LESS THAN ('2025-09-01'),
    PARTITION `p_ms_2025_09` VALUES LESS THAN ('2025-10-01'),
    PARTITION `p_ms_2025_10` VALUES LESS THAN ('2025-11-01'),
    PARTITION `p_ms_2025_11` VALUES LESS THAN ('2025-12-01'),
    PARTITION `p_ms_2025_12` VALUES LESS THAN ('2026-01-01'),
    PARTITION `p_ms_2026_01` VALUES LESS THAN ('2026-02-01'),
    PARTITION `p_ms_2026_02` VALUES LESS THAN ('2026-03-01'),
    PARTITION `p_ms_2026_03` VALUES LESS THAN ('2026-04-01'),
    PARTITION `p_ms_2026_04` VALUES LESS THAN ('2026-05-01'),
    PARTITION `p_ms_2026_05` VALUES LESS THAN ('2026-06-01'),
    PARTITION `p_ms_2026_06` VALUES LESS THAN ('2026-07-01'),
    PARTITION `p_ms_2026_07` VALUES LESS THAN ('2026-08-01'),
    PARTITION `p_ms_2026_08` VALUES LESS THAN ('2026-09-01'),
    PARTITION `p_ms_2026_09` VALUES LESS THAN ('2026-10-01'),
    PARTITION `p_ms_2026_10` VALUES LESS THAN ('2026-11-01'),
    PARTITION `p_ms_2026_11` VALUES LESS THAN ('2026-12-01'),
    PARTITION `p_ms_2026_12` VALUES LESS THAN ('2027-01-01'),
    PARTITION `p_ms_future` VALUES LESS THAN (MAXVALUE)
);

SET FOREIGN_KEY_CHECKS = 1;

-- 创建完成标记
SELECT 'Database initialization completed successfully' AS status;
