-- 股票数据表结构

-- 1. 股票基本信息表
CREATE TABLE IF NOT EXISTS stock_info (
    code VARCHAR(10) PRIMARY KEY,
    name VARCHAR(50),
    industry VARCHAR(50),
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 2. 实时价格表
CREATE TABLE IF NOT EXISTS stock_realtime_price (
    code VARCHAR(10) PRIMARY KEY,
    name VARCHAR(50),
    price DECIMAL(12, 4),
    change_pct DECIMAL(8, 4),
    change_amount DECIMAL(12, 4),
    volume BIGINT,
    amount DECIMAL(20, 2),
    high DECIMAL(12, 4),
    low DECIMAL(12, 4),
    open DECIMAL(12, 4),
    prev_close DECIMAL(12, 4),
    pe DECIMAL(12, 4),
    pb DECIMAL(12, 4),
    total_market_cap DECIMAL(20, 2),
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 3. 今日资金流向表
CREATE TABLE IF NOT EXISTS stock_fund_flow_today (
    code VARCHAR(10) PRIMARY KEY,
    name VARCHAR(50),
    main_net_inflow DECIMAL(20, 2),           -- 主力净流入
    main_net_inflow_pct DECIMAL(8, 4),        -- 主力净流入占比
    super_net_inflow DECIMAL(20, 2),          -- 超大单净流入
    super_net_inflow_pct DECIMAL(8, 4),       -- 超大单净流入占比
    big_net_inflow DECIMAL(20, 2),            -- 大单净流入
    big_net_inflow_pct DECIMAL(8, 4),         -- 大单净流入占比
    mid_net_inflow DECIMAL(20, 2),            -- 中单净流入
    mid_net_inflow_pct DECIMAL(8, 4),         -- 中单净流入占比
    small_net_inflow DECIMAL(20, 2),          -- 小单净流入
    small_net_inflow_pct DECIMAL(8, 4),       -- 小单净流入占比
    price DECIMAL(12, 4),
    change_pct DECIMAL(8, 4),
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 4. 历史资金流向表（保留最近6个月）
CREATE TABLE IF NOT EXISTS stock_fund_flow_history (
    id SERIAL PRIMARY KEY,
    code VARCHAR(10) NOT NULL,
    trade_date DATE NOT NULL,
    close_price DECIMAL(12, 4),
    change_pct DECIMAL(8, 4),
    main_net_inflow DECIMAL(20, 2),
    main_net_inflow_pct DECIMAL(8, 4),
    super_net_inflow DECIMAL(20, 2),
    super_net_inflow_pct DECIMAL(8, 4),
    big_net_inflow DECIMAL(20, 2),
    big_net_inflow_pct DECIMAL(8, 4),
    mid_net_inflow DECIMAL(20, 2),
    mid_net_inflow_pct DECIMAL(8, 4),
    small_net_inflow DECIMAL(20, 2),
    small_net_inflow_pct DECIMAL(8, 4),
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(code, trade_date)
);

-- 5. 日K线表（供全景扫描、回测等使用）
CREATE TABLE IF NOT EXISTS stock_kline_daily (
    code VARCHAR(10) NOT NULL,
    trade_date DATE NOT NULL,
    open DECIMAL(12, 4),
    high DECIMAL(12, 4),
    low DECIMAL(12, 4),
    close DECIMAL(12, 4),
    volume BIGINT,
    amount DECIMAL(20, 2),
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (code, trade_date)
);
CREATE INDEX IF NOT EXISTS idx_kline_daily_date ON stock_kline_daily(trade_date);
CREATE INDEX IF NOT EXISTS idx_kline_daily_code_date ON stock_kline_daily(code, trade_date);

-- 6. 数据更新日志表
CREATE TABLE IF NOT EXISTS data_update_log (
    id SERIAL PRIMARY KEY,
    data_type VARCHAR(50) NOT NULL,           -- realtime_price, fund_flow_today, fund_flow_history
    status VARCHAR(20) NOT NULL,              -- success, failed
    records_count INT,
    error_message TEXT,
    started_at TIMESTAMP,
    finished_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 7. 历史全景扫描结果表（回测专用，每日 11:30 + 16:30 两次扫描）
CREATE TABLE IF NOT EXISTS stock_scan_history (
    id SERIAL PRIMARY KEY,
    scan_date DATE NOT NULL,
    scan_time VARCHAR(5) NOT NULL,           -- '11:30' 或 '16:30'
    code VARCHAR(10) NOT NULL,
    recommend_display VARCHAR(10),            -- '买入','卖出','加仓','持有','关注','观察','观望'
    recommend_type VARCHAR(10),               -- 'buy','sell','watch'
    recommend_reason TEXT,
    recommend_rate INT DEFAULT 0,             -- 0~100
    triggered_count INT DEFAULT 0,
    signal_status JSONB,
    indicators JSONB,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(scan_date, scan_time, code)
);
CREATE INDEX IF NOT EXISTS idx_scan_hist_date_time ON stock_scan_history(scan_date, scan_time);
CREATE INDEX IF NOT EXISTS idx_scan_hist_code ON stock_scan_history(code);
CREATE INDEX IF NOT EXISTS idx_scan_hist_display ON stock_scan_history(scan_date, scan_time, recommend_display);

-- 8. 5分钟K线表（盘中价格历史，供精确回测使用）
CREATE TABLE IF NOT EXISTS stock_kline_5min (
    code VARCHAR(10) NOT NULL,
    dt TIMESTAMP NOT NULL,                    -- K线时间戳 (如 2026-02-25 09:35:00)
    open DECIMAL(12, 4),
    high DECIMAL(12, 4),
    low DECIMAL(12, 4),
    close DECIMAL(12, 4),
    volume BIGINT,
    amount DECIMAL(20, 2),
    change_pct DECIMAL(8, 4),                 -- 涨跌幅
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (code, dt)
);
CREATE INDEX IF NOT EXISTS idx_kline_5min_dt ON stock_kline_5min(dt);
CREATE INDEX IF NOT EXISTS idx_kline_5min_code_date ON stock_kline_5min(code, (dt::date));

-- 创建索引
CREATE INDEX IF NOT EXISTS idx_fund_flow_history_code ON stock_fund_flow_history(code);
CREATE INDEX IF NOT EXISTS idx_fund_flow_history_date ON stock_fund_flow_history(trade_date);
CREATE INDEX IF NOT EXISTS idx_fund_flow_history_code_date ON stock_fund_flow_history(code, trade_date);
CREATE INDEX IF NOT EXISTS idx_realtime_price_updated ON stock_realtime_price(updated_at);
CREATE INDEX IF NOT EXISTS idx_fund_flow_today_updated ON stock_fund_flow_today(updated_at);
