-- 审计日志持久化表 (H2兼容版本)
CREATE TABLE IF NOT EXISTS sys_audit_log (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
    user_id BIGINT,
    user_name VARCHAR(100),
    user_ip VARCHAR(50),
    operation VARCHAR(100) NOT NULL,
    resource_type VARCHAR(50),
    resource_id VARCHAR(100),
    operation_details TEXT,
    request_method VARCHAR(10),
    request_url VARCHAR(500),
    request_params TEXT,
    response_status INT,
    error_message TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX IF NOT EXISTS idx_audit_user_id ON sys_audit_log(user_id);
CREATE INDEX IF NOT EXISTS idx_audit_operation ON sys_audit_log(operation);
CREATE INDEX IF NOT EXISTS idx_audit_resource_type ON sys_audit_log(resource_type);
CREATE INDEX IF NOT EXISTS idx_audit_created_at ON sys_audit_log(created_at);
CREATE INDEX IF NOT EXISTS idx_audit_operation_time ON sys_audit_log(operation, created_at);
