| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace App\Module\Cleanup\AdminControllers\Actions;
- use App\Module\Cleanup\Models\CleanupPlanContent;
- use App\Module\Cleanup\Models\CleanupConfig;
- use Dcat\Admin\Grid\RowAction;
- use Dcat\Admin\Actions\Response;
- use Illuminate\Http\Request;
- /**
- * 迁移到Model类Action
- *
- * 将使用表名的旧数据迁移为使用Model类
- */
- class MigrateToModelAction extends RowAction
- {
- /**
- * 按钮标题
- */
- protected $title = '迁移到Model';
- /**
- * 按钮图标
- */
- protected $icon = 'fa-arrow-up';
- /**
- * 处理请求
- */
- public function handle(Request $request)
- {
- $contentId = $this->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 '<a class="btn btn-sm btn-warning ' . $this->getElementClass() . '" title="' . $this->title . '">
- <i class="' . $this->icon . '"></i> ' . $this->title . '
- </a>';
- }
- }
|