CREATE TABLE IF NOT EXISTS sys_user_config (
    config_id BIGSERIAL PRIMARY KEY,
    user_id BIGINT NOT NULL,
    config_key VARCHAR(100) NOT NULL,
    config_value TEXT,
    remark VARCHAR(500),
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 添加注释
COMMENT ON TABLE sys_user_config IS '用户配置表';
COMMENT ON COLUMN sys_user_config.config_id IS '配置ID';
COMMENT ON COLUMN sys_user_config.user_id IS '用户ID';
COMMENT ON COLUMN sys_user_config.config_key IS '配置键名';
COMMENT ON COLUMN sys_user_config.config_value IS '配置值';
COMMENT ON COLUMN sys_user_config.remark IS '备注';
COMMENT ON COLUMN sys_user_config.create_time IS '创建时间';
COMMENT ON COLUMN sys_user_config.update_time IS '更新时间';

-- 创建唯一索引
CREATE UNIQUE INDEX IF NOT EXISTS uk_user_config ON sys_user_config (user_id, config_key);
CREATE INDEX IF NOT EXISTS idx_user_id ON sys_user_config (user_id);

-- 创建更新时间触发器函数
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.update_time = CURRENT_TIMESTAMP;
    RETURN NEW;
END;
$$ language 'plpgsql';

-- 创建触发器
CREATE TRIGGER update_sys_user_config_updated_at
    BEFORE UPDATE ON sys_user_config
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column();