-- ============================================
-- A股智投分析平台 - 数据库初始化脚本
-- 如果使用 Prisma，这些表会自动创建
-- 此脚本作为备用或独立部署时使用
-- ============================================

-- 设置字符集
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ============================================
-- 1. 市场指数表 (market_indices)
-- 用途: 存储上证指数、深证成指、创业板指等市场指数
-- ============================================
CREATE TABLE IF NOT EXISTS `market_indices` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(100) NOT NULL COMMENT '指数名称，如上证指数',
  `code` VARCHAR(50) NOT NULL COMMENT '指数代码，如000001',
  `current` FLOAT DEFAULT 0 COMMENT '当前点位',
  `change` FLOAT DEFAULT 0 COMMENT '涨跌额',
  `change_percent` FLOAT DEFAULT 0 COMMENT '涨跌幅(%)',
  `volume` BIGINT DEFAULT 0 COMMENT '成交量',
  `turnover` BIGINT DEFAULT 0 COMMENT '成交额',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_market_indices_name` (`name`),
  UNIQUE KEY `uk_market_indices_code` (`code`),
  KEY `idx_market_indices_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='市场指数表';

-- ============================================
-- 2. 板块信息表 (sectors)
-- 用途: 存储行业板块信息，如半导体、新能源等
-- ============================================
CREATE TABLE IF NOT EXISTS `sectors` (
  `id` VARCHAR(36) NOT NULL COMMENT 'UUID',
  `name` VARCHAR(100) NOT NULL COMMENT '板块名称',
  `code` VARCHAR(50) NOT NULL COMMENT '板块代码',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_sectors_name` (`name`),
  UNIQUE KEY `uk_sectors_code` (`code`),
  KEY `idx_sectors_code` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='板块信息表';

-- ============================================
-- 3. 板块行情表 (sector_quotes)
-- 用途: 存储板块实时行情数据
-- ============================================
CREATE TABLE IF NOT EXISTS `sector_quotes` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `sector_code` VARCHAR(50) NOT NULL COMMENT '板块代码',
  `current` FLOAT DEFAULT 0 COMMENT '当前点位',
  `change` FLOAT DEFAULT 0 COMMENT '涨跌额',
  `change_percent` FLOAT DEFAULT 0 COMMENT '涨跌幅(%)',
  `volume` BIGINT DEFAULT 0 COMMENT '成交量',
  `turnover` BIGINT DEFAULT 0 COMMENT '成交额',
  `momentum_score` FLOAT DEFAULT 50 COMMENT '动量分数(0-100)',
  `rank` INT DEFAULT 0 COMMENT '排名',
  `previous_rank` INT DEFAULT 0 COMMENT '上一期排名',
  `quote_time` DATETIME NOT NULL COMMENT '行情时间',
  PRIMARY KEY (`id`),
  KEY `idx_sector_quotes_code` (`sector_code`),
  KEY `idx_sector_quotes_time` (`quote_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='板块行情表';

-- ============================================
-- 4. 板块K线数据表 (sector_klines)
-- 用途: 存储板块的历史K线数据
-- ============================================
CREATE TABLE IF NOT EXISTS `sector_klines` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `sector_code` VARCHAR(50) NOT NULL COMMENT '板块代码',
  `period` VARCHAR(20) NOT NULL COMMENT '周期: day/week/month',
  `date` DATE NOT NULL COMMENT '日期',
  `open` FLOAT DEFAULT 0 COMMENT '开盘价',
  `high` FLOAT DEFAULT 0 COMMENT '最高价',
  `low` FLOAT DEFAULT 0 COMMENT '最低价',
  `close` FLOAT DEFAULT 0 COMMENT '收盘价',
  `volume` BIGINT DEFAULT 0 COMMENT '成交量',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_sector_klines` (`sector_code`, `period`, `date`),
  KEY `idx_sector_klines_code` (`sector_code`),
  KEY `idx_sector_klines_date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='板块K线数据表';

-- ============================================
-- 5. 股票信息表 (stocks)
-- 用途: 存储股票基础信息
-- ============================================
CREATE TABLE IF NOT EXISTS `stocks` (
  `id` VARCHAR(36) NOT NULL COMMENT 'UUID',
  `code` VARCHAR(50) NOT NULL COMMENT '股票代码，如000001',
  `name` VARCHAR(100) NOT NULL COMMENT '股票名称',
  `sector_code` VARCHAR(50) DEFAULT NULL COMMENT '所属板块代码',
  `market_cap` BIGINT DEFAULT NULL COMMENT '总市值',
  `pe` FLOAT DEFAULT NULL COMMENT '市盈率',
  `pb` FLOAT DEFAULT NULL COMMENT '市净率',
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_stocks_code` (`code`),
  KEY `idx_stocks_code` (`code`),
  KEY `idx_stocks_sector` (`sector_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='股票信息表';

-- ============================================
-- 6. 股票行情表 (stock_quotes)
-- 用途: 存储股票实时行情快照
-- ============================================
CREATE TABLE IF NOT EXISTS `stock_quotes` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `stock_code` VARCHAR(50) NOT NULL COMMENT '股票代码',
  `price` FLOAT DEFAULT 0 COMMENT '当前价',
  `open` FLOAT DEFAULT 0 COMMENT '开盘价',
  `high` FLOAT DEFAULT 0 COMMENT '最高价',
  `low` FLOAT DEFAULT 0 COMMENT '最低价',
  `pre_close` FLOAT DEFAULT 0 COMMENT '昨收价',
  `volume` BIGINT DEFAULT 0 COMMENT '成交量',
  `turnover` BIGINT DEFAULT 0 COMMENT '成交额',
  `change_percent` FLOAT DEFAULT 0 COMMENT '涨跌幅(%)',
  `turnover_rate` FLOAT DEFAULT NULL COMMENT '换手率(%)',
  `amplitude` FLOAT DEFAULT NULL COMMENT '振幅(%)',
  `quote_time` DATETIME NOT NULL COMMENT '行情时间',
  PRIMARY KEY (`id`),
  KEY `idx_stock_quotes_code` (`stock_code`),
  KEY `idx_stock_quotes_time` (`quote_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='股票行情表';

-- ============================================
-- 7. 股票K线数据表 (stock_klines)
-- 用途: 存储股票历史K线数据（日K/周K/月K）
-- ============================================
CREATE TABLE IF NOT EXISTS `stock_klines` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `stock_code` VARCHAR(50) NOT NULL COMMENT '股票代码',
  `period` VARCHAR(20) NOT NULL COMMENT '周期: day/week/month',
  `date` DATE NOT NULL COMMENT '日期',
  `open` FLOAT DEFAULT 0 COMMENT '开盘价',
  `high` FLOAT DEFAULT 0 COMMENT '最高价',
  `low` FLOAT DEFAULT 0 COMMENT '最低价',
  `close` FLOAT DEFAULT 0 COMMENT '收盘价',
  `volume` BIGINT DEFAULT 0 COMMENT '成交量',
  `ma5` FLOAT DEFAULT NULL COMMENT '5日均线',
  `ma10` FLOAT DEFAULT NULL COMMENT '10日均线',
  `ma20` FLOAT DEFAULT NULL COMMENT '20日均线',
  `ma30` FLOAT DEFAULT NULL COMMENT '30日均线',
  `ma60` FLOAT DEFAULT NULL COMMENT '60日均线',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_stock_klines` (`stock_code`, `period`, `date`),
  KEY `idx_stock_klines_code` (`stock_code`),
  KEY `idx_stock_klines_date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='股票K线数据表';

-- ============================================
-- 8. 新高新低股票记录表 (high_low_stocks)
-- 用途: 存储创N日新高/新低的股票记录
-- ============================================
CREATE TABLE IF NOT EXISTS `high_low_stocks` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `stock_code` VARCHAR(50) NOT NULL COMMENT '股票代码',
  `type` VARCHAR(10) NOT NULL COMMENT '类型: high新高/low新低',
  `price` FLOAT DEFAULT 0 COMMENT '触发价格',
  `date` DATE NOT NULL COMMENT '触发日期',
  `days_to_highlow` INT DEFAULT 0 COMMENT '距离前高/低的天数',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_high_low_code` (`stock_code`),
  KEY `idx_high_low_type` (`type`),
  KEY `idx_high_low_date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='新高新低股票记录表';

-- ============================================
-- 9. 动量股票推荐表 (momentum_stocks)
-- 用途: 存储动量策略选出的推荐股票
-- ============================================
CREATE TABLE IF NOT EXISTS `momentum_stocks` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `stock_code` VARCHAR(50) NOT NULL COMMENT '股票代码',
  `momentum_score` FLOAT DEFAULT 0 COMMENT '动量分数(0-100)',
  `tags` TEXT COMMENT '标签JSON数组，如["强势突破","量价齐升"]',
  `volume_ratio` FLOAT DEFAULT 0 COMMENT '量比',
  `break_through` TINYINT(1) DEFAULT 0 COMMENT '是否突破: 0/1',
  `date` DATE NOT NULL COMMENT '日期',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_momentum_code` (`stock_code`),
  KEY `idx_momentum_date` (`date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='动量股票推荐表';

-- ============================================
-- 10. 用户表 (users)
-- 用途: 存储用户信息
-- ============================================
CREATE TABLE IF NOT EXISTS `users` (
  `id` VARCHAR(36) NOT NULL COMMENT 'UUID',
  `username` VARCHAR(50) NOT NULL COMMENT '用户名',
  `email` VARCHAR(100) NOT NULL COMMENT '邮箱',
  `password` VARCHAR(255) NOT NULL COMMENT '加密密码',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_users_username` (`username`),
  UNIQUE KEY `uk_users_email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表';

-- ============================================
-- 11. 用户自选股表 (user_favorites)
-- 用途: 存储用户的自选股列表
-- ============================================
CREATE TABLE IF NOT EXISTS `user_favorites` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` VARCHAR(36) NOT NULL COMMENT '用户ID',
  `stock_code` VARCHAR(50) NOT NULL COMMENT '股票代码',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_favorites` (`user_id`, `stock_code`),
  KEY `idx_user_favorites_user` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户自选股表';

-- 恢复外键检查
SET FOREIGN_KEY_CHECKS = 1;

-- 输出创建结果
SELECT 'Database tables initialized successfully' AS result;
