getKey(); try { $content = CleanupPlanContent::findOrFail($contentId); // 检查是否已经使用Model类 if (!empty($content->model_class)) { return $this->response() ->error('此记录已经使用Model类,无需迁移'); } // 检查是否有表名 if (empty($content->table_name)) { return $this->response() ->error('此记录没有表名,无法迁移'); } // 查找对应的Model类 // 首先尝试直接匹配表名 $config = CleanupConfig::where('table_name', $content->table_name) ->whereNotNull('model_class') ->where('model_class', '!=', '') ->first(); // 如果没找到,尝试去掉kku_前缀再查找 if (!$config && str_starts_with($content->table_name, 'kku_')) { $tableNameWithoutPrefix = substr($content->table_name, 4); // 去掉 'kku_' 前缀 $config = CleanupConfig::where('table_name', $tableNameWithoutPrefix) ->whereNotNull('model_class') ->where('model_class', '!=', '') ->first(); } if (!$config) { return $this->response() ->error('未找到表 ' . $content->table_name . ' 对应的Model类配置'); } // 验证Model类是否存在 if (!class_exists($config->model_class)) { return $this->response() ->error('Model类不存在:' . $config->model_class); } // 更新为使用Model类 $content->update([ 'model_class' => $config->model_class, ]); return $this->response() ->success('迁移成功!已更新为使用Model类:' . class_basename($config->model_class)) ->refresh(); } catch (\Exception $e) { return $this->response() ->error('迁移失败:' . $e->getMessage()); } } /** * 确认对话框 */ public function confirm() { return [ '确认迁移到Model类?', '此操作将查找对应的Model类并更新配置,建议在迁移前备份数据。' ]; } /** * 权限检查 */ public function allowed() { // 只对没有Model类的记录显示此按钮 $content = $this->row; return empty($content->model_class); } /** * 按钮样式 */ public function html() { return ' ' . $this->title . ' '; } }