-- 资产快照表
CREATE TABLE IF NOT EXISTS assets_snapshots (
    id SERIAL PRIMARY KEY,
    account_id VARCHAR(50) NOT NULL,
    cash DECIMAL(15, 2) NOT NULL DEFAULT 0,
    frozen_cash DECIMAL(15, 2) NOT NULL DEFAULT 0,
    market_value DECIMAL(15, 2) NOT NULL DEFAULT 0,
    profit DECIMAL(15, 2) NOT NULL DEFAULT 0,
    total_asset DECIMAL(15, 2) NOT NULL DEFAULT 0,
    data_hash VARCHAR(64) NOT NULL,
    collected_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_assets_account_id ON assets_snapshots(account_id);
CREATE INDEX idx_assets_collected_at ON assets_snapshots(collected_at);
CREATE INDEX idx_assets_data_hash ON assets_snapshots(data_hash);

-- 订单表
CREATE TABLE IF NOT EXISTS orders (
    id SERIAL PRIMARY KEY,
    order_id BIGINT NOT NULL,
    account_id VARCHAR(50) NOT NULL,
    stock_code VARCHAR(20) NOT NULL,
    price DECIMAL(10, 4) NOT NULL DEFAULT 0,
    volume INTEGER NOT NULL DEFAULT 0,
    traded_price DECIMAL(10, 4) NOT NULL DEFAULT 0,
    traded_volume INTEGER NOT NULL DEFAULT 0,
    order_status INTEGER NOT NULL DEFAULT 0,
    order_time BIGINT NOT NULL,
    order_remark TEXT,
    data_hash VARCHAR(64) NOT NULL,
    collected_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_orders_order_id ON orders(order_id);
CREATE INDEX idx_orders_stock_code ON orders(stock_code);
CREATE INDEX idx_orders_account_id ON orders(account_id);
CREATE INDEX idx_orders_collected_at ON orders(collected_at);

-- 持仓表
CREATE TABLE IF NOT EXISTS positions (
    id SERIAL PRIMARY KEY,
    account_id VARCHAR(50) NOT NULL,
    code VARCHAR(20) NOT NULL,
    volume INTEGER NOT NULL DEFAULT 0,
    can_use_volume INTEGER NOT NULL DEFAULT 0,
    frozen_volume INTEGER NOT NULL DEFAULT 0,
    avg_price DECIMAL(10, 4) NOT NULL DEFAULT 0,
    open_price DECIMAL(10, 4) NOT NULL DEFAULT 0,
    current_price DECIMAL(10, 4) NOT NULL DEFAULT 0,
    market_value DECIMAL(15, 2) NOT NULL DEFAULT 0,
    profit DECIMAL(15, 2) NOT NULL DEFAULT 0,
    profit_rate DECIMAL(10, 4) NOT NULL DEFAULT 0,
    min_profit_rate DECIMAL(10, 4) NOT NULL DEFAULT 0,
    data_hash VARCHAR(64) NOT NULL,
    collected_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_positions_code ON positions(code);
CREATE INDEX idx_positions_account_id ON positions(account_id);
CREATE INDEX idx_positions_collected_at ON positions(collected_at);

-- 行情数据表
CREATE TABLE IF NOT EXISTS tick_data (
    id SERIAL PRIMARY KEY,
    stock_code VARCHAR(20) NOT NULL,
    last_price DECIMAL(10, 4) NOT NULL DEFAULT 0,
    open DECIMAL(10, 4) NOT NULL DEFAULT 0,
    high DECIMAL(10, 4) NOT NULL DEFAULT 0,
    low DECIMAL(10, 4) NOT NULL DEFAULT 0,
    last_close DECIMAL(10, 4) NOT NULL DEFAULT 0,
    volume BIGINT NOT NULL DEFAULT 0,
    amount DECIMAL(15, 2) NOT NULL DEFAULT 0,
    pvolume BIGINT NOT NULL DEFAULT 0,
    bid_prices DECIMAL(10, 4)[],
    bid_volumes INTEGER[],
    ask_prices DECIMAL(10, 4)[],
    ask_volumes INTEGER[],
    time BIGINT NOT NULL,
    timetag VARCHAR(50),
    stock_status INTEGER NOT NULL DEFAULT 0,
    data_hash VARCHAR(64) NOT NULL,
    collected_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_tick_stock_code ON tick_data(stock_code);
CREATE INDEX idx_tick_time ON tick_data(time);
CREATE INDEX idx_tick_collected_at ON tick_data(collected_at);

-- 采集日志表
CREATE TABLE IF NOT EXISTS collection_logs (
    id SERIAL PRIMARY KEY,
    data_hash VARCHAR(64) NOT NULL,
    has_changed BOOLEAN NOT NULL DEFAULT FALSE,
    status_message TEXT,
    collected_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_collection_logs_hash ON collection_logs(data_hash);
CREATE INDEX idx_collection_logs_collected_at ON collection_logs(collected_at);
