-- ============================================
-- member_user 表 - 简洁版建表语句
-- ============================================
-- 用途：直接复制执行，快速创建会员表
-- ============================================

CREATE TABLE IF NOT EXISTS member_user (
    -- 主键和基础字段
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    
    -- 会员核心字段
    member_no VARCHAR(50) NOT NULL UNIQUE,
    nickname VARCHAR(100),
    phone VARCHAR(255),
    gender INTEGER DEFAULT 0,
    birthday TIMESTAMP,
    address VARCHAR(500),
    avatar VARCHAR(500),
    subscribed BOOLEAN DEFAULT FALSE,
    last_login_at TIMESTAMP,
    
    -- 微信相关字段
    union_id VARCHAR(100),
    miniapp_open_id VARCHAR(100),
    official_open_id VARCHAR(100),
    
    -- 软删除字段
    is_deleted BOOLEAN DEFAULT FALSE
);

-- 创建索引
CREATE UNIQUE INDEX IF NOT EXISTS idx_member_user_member_no ON member_user(member_no);
CREATE INDEX IF NOT EXISTS idx_member_user_union_id ON member_user(union_id);
CREATE INDEX IF NOT EXISTS idx_member_user_miniapp_openid ON member_user(miniapp_open_id);
CREATE INDEX IF NOT EXISTS idx_member_user_official_openid ON member_user(official_open_id);
CREATE INDEX IF NOT EXISTS idx_member_user_phone ON member_user(phone);
CREATE INDEX IF NOT EXISTS idx_member_user_is_deleted ON member_user(is_deleted);

-- 添加注释
COMMENT ON TABLE member_user IS '会员表';
COMMENT ON COLUMN member_user.id IS '主键ID';
COMMENT ON COLUMN member_user.created_at IS '创建时间';
COMMENT ON COLUMN member_user.updated_at IS '更新时间';
COMMENT ON COLUMN member_user.member_no IS '会员编号（唯一）';
COMMENT ON COLUMN member_user.nickname IS '昵称';
COMMENT ON COLUMN member_user.phone IS '手机号（AES加密存储）';
COMMENT ON COLUMN member_user.gender IS '性别：0-未知，1-男，2-女';
COMMENT ON COLUMN member_user.birthday IS '生日';
COMMENT ON COLUMN member_user.address IS '地址';
COMMENT ON COLUMN member_user.avatar IS '头像URL';
COMMENT ON COLUMN member_user.subscribed IS '是否关注服务号';
COMMENT ON COLUMN member_user.last_login_at IS '最后登录时间';
COMMENT ON COLUMN member_user.union_id IS '微信UnionID（跨应用唯一标识）';
COMMENT ON COLUMN member_user.miniapp_open_id IS '小程序OpenID';
COMMENT ON COLUMN member_user.official_open_id IS '服务号OpenID';
COMMENT ON COLUMN member_user.is_deleted IS '是否删除（软删除标记）';
