CREATE TABLE players
(
    id BIGINT PRIMARY KEY,
    CREATE TABLE players (
    id BIGINT PRIMARY KEY,
    user_id BIGINT NOT NULL UNIQUE,
    status VARCHAR(20) NOT NULL DEFAULT 'offline',
    rating DECIMAL(3,2) DEFAULT 5.00,
    total_orders INT DEFAULT 0,
    completed_orders INT DEFAULT 0,

    -- [注意] 此字段为冗余缓存，通过消息队列与 shop_players 表保持一致
    shop_id BIGINT,

    tags TEXT[] DEFAULT ARRAY[]::TEXT[],
    games BIGINT[] DEFAULT ARRAY[]::BIGINT[],
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- 基础索引
CREATE INDEX idx_players_user ON players (user_id);
CREATE INDEX idx_players_shop ON players (shop_id) WHERE shop_id IS NOT NULL;
CREATE INDEX idx_players_status ON players (status);
CREATE INDEX idx_players_rating ON players (rating DESC);

-- 复合索引优化多条件筛选
CREATE INDEX idx_players_status_rating ON players (status, rating DESC) WHERE status IN ('available', 'busy');

-- GIN 索引优化数组查询
CREATE INDEX idx_players_games_gin ON players USING gin(games);
CREATE INDEX idx_players_tags_gin ON players USING gin(tags);

-- 店铺+状态复合索引
CREATE INDEX idx_players_shop_status ON players (shop_id, status, rating DESC) WHERE shop_id IS NOT NULL;

-- CREATE TRIGGER trigger_players_updated_at
--     BEFORE UPDATE
--     ON players
--     FOR EACH ROW
--     EXECUTE FUNCTION update_updated_at_column();
