-- 创建数据库（如果不存在）
CREATE DATABASE IF NOT EXISTS stock_analysis;

-- 使用数据库
USE stock_analysis;

-- 股票基本信息表
CREATE TABLE IF NOT EXISTS stock_basic (
    code VARCHAR(10) PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    sector_code VARCHAR(10),
    list_date DATE,
    is_etf BOOLEAN DEFAULT FALSE,
    market VARCHAR(10),
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 股票实时行情表
CREATE TABLE IF NOT EXISTS stock_quote (
    code VARCHAR(10) PRIMARY KEY,
    price DECIMAL(10,2),
    change_pct DECIMAL(6,2),
    change_amount DECIMAL(10,2),
    volume BIGINT,
    amount DECIMAL(18,2),
    turnover_rate DECIMAL(8,4),
    amplitude DECIMAL(6,2),
    high DECIMAL(10,2),
    low DECIMAL(10,2),
    open DECIMAL(10,2),
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 股票K线数据表
CREATE TABLE IF NOT EXISTS stock_kline (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    code VARCHAR(10) NOT NULL,
    date DATE NOT NULL,
    open DECIMAL(10,2),
    high DECIMAL(10,2),
    low DECIMAL(10,2),
    close DECIMAL(10,2),
    volume BIGINT,
    amount DECIMAL(18,2),
    pct_chg DECIMAL(6,2),
    INDEX idx_code_date (code, date)
);

-- 板块信息表
CREATE TABLE IF NOT EXISTS sector_info (
    code VARCHAR(10) PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    stock_count INT DEFAULT 0,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 板块动量表
CREATE TABLE IF NOT EXISTS sector_momentum (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    sector_code VARCHAR(10) NOT NULL,
    date DATE NOT NULL,
    momentum_value DECIMAL(10,4),
    n INT,
    N INT,
    rank INT,
    rank_change INT,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_sector_date (sector_code, date)
);

-- 市场统计表
CREATE TABLE IF NOT EXISTS market_stats (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    date DATE NOT NULL UNIQUE,
    up_count INT DEFAULT 0,
    down_count INT DEFAULT 0,
    flat_count INT DEFAULT 0,
    limit_up_count INT DEFAULT 0,
    limit_down_count INT DEFAULT 0,
    total_amount DECIMAL(20,2),
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 创建外键约束（可选）
-- ALTER TABLE stock_basic ADD FOREIGN KEY (sector_code) REFERENCES sector_info(code);
-- ALTER TABLE sector_momentum ADD FOREIGN KEY (sector_code) REFERENCES sector_info(code);

-- 创建索引
CREATE INDEX IF NOT EXISTS idx_stock_basic_sector ON stock_basic(sector_code);
CREATE INDEX IF NOT EXISTS idx_sector_momentum_rank ON sector_momentum(rank);
CREATE INDEX IF NOT EXISTS idx_market_stats_date ON market_stats(date);

-- 插入示例数据
-- 板块信息
INSERT IGNORE INTO sector_info (code, name, stock_count) VALUES
('HY001', '银行', 42),
('HY002', '证券', 48),
('HY003', '保险', 10),
('HY004', '房地产', 130),
('HY005', '医药', 300),
('HY006', '科技', 500),
('HY007', '消费', 200),
('HY008', '能源', 80),
('HY009', '材料', 150),
('HY010', '工业', 250);

-- 股票基本信息（示例）
INSERT IGNORE INTO stock_basic (code, name, sector_code, is_etf, market) VALUES
('600000', '浦发银行', 'HY001', FALSE, '沪市'),
('600519', '贵州茅台', 'HY007', FALSE, '沪市'),
('000001', '平安银行', 'HY001', FALSE, '深市'),
('000858', '五粮液', 'HY007', FALSE, '深市'),
('510300', '沪深300ETF', 'HY006', TRUE, '沪市'),
('510500', '500ETF', 'HY006', TRUE, '沪市');

-- 市场统计（示例）
INSERT IGNORE INTO market_stats (date, up_count, down_count, flat_count, limit_up_count, limit_down_count, total_amount) VALUES
(CURDATE(), 2500, 2000, 500, 100, 20, 1500000000000);
