CREATE TABLE t_user (
    id BIGSERIAL PRIMARY KEY,
    username VARCHAR(50),
    nickname VARCHAR(100),
    avatar_url VARCHAR(500),
    phone VARCHAR(20),
    password VARCHAR(255),
    salt VARCHAR(32),
    gender SMALLINT,
    status SMALLINT DEFAULT 1,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    delFlag SMALLINT DEFAULT 0,
    UNIQUE (phone)
);

COMMENT ON TABLE t_user IS '用户基础信息表';
COMMENT ON COLUMN t_user.id IS '全局唯一用户ID（自增）';
COMMENT ON COLUMN t_user.username IS '用户名（可选，后台管理用）';
COMMENT ON COLUMN t_user.nickname IS '用户昵称（各平台统一）';
COMMENT ON COLUMN t_user.avatar_url IS '用户头像URL';
COMMENT ON COLUMN t_user.phone IS '手机号（脱敏存储，如138****1234）';
COMMENT ON COLUMN t_user.password IS '登录密码';
COMMENT ON COLUMN t_user.salt IS '密码盐值';
COMMENT ON COLUMN t_user.gender IS '性别：0-未知，1-男，2-女';
COMMENT ON COLUMN t_user.status IS '状态：0-禁用，1-正常';
COMMENT ON COLUMN t_user.create_time IS '创建时间';
COMMENT ON COLUMN t_user.update_time IS '更新时间';
COMMENT ON COLUMN t_user.delFlag IS '软删除：0-未删，1-已删';

CREATE OR REPLACE FUNCTION set_update_time()
RETURNS TRIGGER AS $$
BEGIN
    NEW.update_time = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER t_user_set_update_time
BEFORE UPDATE ON t_user
FOR EACH ROW
EXECUTE FUNCTION set_update_time();
