CREATE TABLE IF NOT EXISTS operation_audit_log (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    user_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
    action_code VARCHAR(128) NOT NULL,
    biz_type VARCHAR(64) DEFAULT NULL,
    biz_id VARCHAR(64) DEFAULT NULL,
    http_method VARCHAR(16) NOT NULL,
    request_uri VARCHAR(255) NOT NULL,
    request_query VARCHAR(1000) DEFAULT NULL,
    status_code INT NOT NULL DEFAULT 200,
    success TINYINT(1) NOT NULL DEFAULT 1,
    error_message VARCHAR(500) DEFAULT NULL,
    ip VARCHAR(64) DEFAULT NULL,
    user_agent VARCHAR(255) DEFAULT NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_tenant_time (tenant_id, created_at),
    KEY idx_user_time (user_id, created_at),
    KEY idx_action_time (action_code, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO permission (id, permission_code, permission_name, module)
VALUES
    (1042, 'audit.log.read', '查看审计日志', 'system')
ON DUPLICATE KEY UPDATE permission_name = VALUES(permission_name), module = VALUES(module);

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
VALUES
    (43, 1, 101, 1042)
ON DUPLICATE KEY UPDATE permission_id = VALUES(permission_id);
