MigrateToModelAction.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Models\CleanupPlanContent;
  4. use App\Module\Cleanup\Models\CleanupConfig;
  5. use Dcat\Admin\Grid\RowAction;
  6. use Dcat\Admin\Actions\Response;
  7. use Illuminate\Http\Request;
  8. /**
  9. * 迁移到Model类Action
  10. *
  11. * 将使用表名的旧数据迁移为使用Model类
  12. */
  13. class MigrateToModelAction extends RowAction
  14. {
  15. /**
  16. * 按钮标题
  17. */
  18. protected $title = '迁移到Model';
  19. /**
  20. * 按钮图标
  21. */
  22. protected $icon = 'fa-arrow-up';
  23. /**
  24. * 处理请求
  25. */
  26. public function handle(Request $request)
  27. {
  28. $contentId = $this->getKey();
  29. try {
  30. $content = CleanupPlanContent::findOrFail($contentId);
  31. // 检查是否已经使用Model类
  32. if (!empty($content->model_class)) {
  33. return $this->response()
  34. ->error('此记录已经使用Model类,无需迁移');
  35. }
  36. // 检查是否有表名
  37. if (empty($content->table_name)) {
  38. return $this->response()
  39. ->error('此记录没有表名,无法迁移');
  40. }
  41. // 查找对应的Model类
  42. // 首先尝试直接匹配表名
  43. $config = CleanupConfig::where('table_name', $content->table_name)
  44. ->whereNotNull('model_class')
  45. ->where('model_class', '!=', '')
  46. ->first();
  47. // 如果没找到,尝试去掉kku_前缀再查找
  48. if (!$config && str_starts_with($content->table_name, 'kku_')) {
  49. $tableNameWithoutPrefix = substr($content->table_name, 4); // 去掉 'kku_' 前缀
  50. $config = CleanupConfig::where('table_name', $tableNameWithoutPrefix)
  51. ->whereNotNull('model_class')
  52. ->where('model_class', '!=', '')
  53. ->first();
  54. }
  55. if (!$config) {
  56. return $this->response()
  57. ->error('未找到表 ' . $content->table_name . ' 对应的Model类配置');
  58. }
  59. // 验证Model类是否存在
  60. if (!class_exists($config->model_class)) {
  61. return $this->response()
  62. ->error('Model类不存在:' . $config->model_class);
  63. }
  64. // 更新为使用Model类
  65. $content->update([
  66. 'model_class' => $config->model_class,
  67. ]);
  68. return $this->response()
  69. ->success('迁移成功!已更新为使用Model类:' . class_basename($config->model_class))
  70. ->refresh();
  71. } catch (\Exception $e) {
  72. return $this->response()
  73. ->error('迁移失败:' . $e->getMessage());
  74. }
  75. }
  76. /**
  77. * 确认对话框
  78. */
  79. public function confirm()
  80. {
  81. return [
  82. '确认迁移到Model类?',
  83. '此操作将查找对应的Model类并更新配置,建议在迁移前备份数据。'
  84. ];
  85. }
  86. /**
  87. * 权限检查
  88. */
  89. public function allowed()
  90. {
  91. // 只对没有Model类的记录显示此按钮
  92. $content = $this->row;
  93. return empty($content->model_class);
  94. }
  95. /**
  96. * 按钮样式
  97. */
  98. public function html()
  99. {
  100. return '<a class="btn btn-sm btn-warning ' . $this->getElementClass() . '" title="' . $this->title . '">
  101. <i class="' . $this->icon . '"></i> ' . $this->title . '
  102. </a>';
  103. }
  104. }