PreviewPlanAction.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Services\CleanupService;
  4. use Dcat\Admin\Grid\RowAction;
  5. use Dcat\Admin\Actions\Response;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 预览计划Action
  9. *
  10. * 用于预览清理计划的执行效果
  11. */
  12. class PreviewPlanAction extends RowAction
  13. {
  14. /**
  15. * 按钮标题
  16. */
  17. protected $title = '预览效果';
  18. /**
  19. * 处理请求
  20. */
  21. public function handle(Request $request)
  22. {
  23. try {
  24. $planId = $this->getKey();
  25. // 调用预览服务
  26. $result = CleanupService::previewPlan($planId);
  27. if (!$result['success']) {
  28. return $this->response()
  29. ->error('预览失败:' . $result['message']);
  30. }
  31. $data = $result['data'];
  32. $html = '<div class="row">';
  33. $html .= '<div class="col-md-6">';
  34. $html .= '<h5>总体统计</h5>';
  35. $html .= '<ul class="list-unstyled">';
  36. $html .= '<li><strong>包含表数:</strong>' . number_format($data['total_tables']) . '</li>';
  37. $html .= '<li><strong>总记录数:</strong>' . number_format($data['total_records']) . '</li>';
  38. $html .= '<li><strong>预计删除:</strong>' . number_format($data['estimated_delete']) . '</li>';
  39. $html .= '<li><strong>预计保留:</strong>' . number_format($data['estimated_remain']) . '</li>';
  40. $html .= '<li><strong>删除比例:</strong>' . $data['delete_percentage'] . '%</li>';
  41. $html .= '<li><strong>预计执行时间:</strong>' . $data['estimated_time'] . '</li>';
  42. $html .= '</ul>';
  43. $html .= '</div>';
  44. $html .= '<div class="col-md-6">';
  45. $html .= '<h5>风险评估</h5>';
  46. $html .= '<ul class="list-unstyled">';
  47. $riskLevel = $data['risk_level'];
  48. $riskColor = $riskLevel === 'high' ? 'danger' : ($riskLevel === 'medium' ? 'warning' : 'success');
  49. $riskText = $riskLevel === 'high' ? '高风险' : ($riskLevel === 'medium' ? '中风险' : '低风险');
  50. $html .= '<li><strong>风险等级:</strong><span class="badge badge-' . $riskColor . '">' . $riskText . '</span></li>';
  51. $html .= '<li><strong>高风险表:</strong>' . count($data['high_risk_tables']) . ' 个</li>';
  52. $html .= '<li><strong>备份建议:</strong>' . ($data['backup_recommended'] ? '强烈建议' : '可选') . '</li>';
  53. $html .= '</ul>';
  54. $html .= '</div>';
  55. $html .= '</div>';
  56. if (!empty($data['warnings'])) {
  57. $html .= '<div class="alert alert-warning mt-3">';
  58. $html .= '<h6>⚠️ 注意事项:</h6>';
  59. $html .= '<ul class="mb-0">';
  60. foreach ($data['warnings'] as $warning) {
  61. $html .= '<li>' . $warning . '</li>';
  62. }
  63. $html .= '</ul>';
  64. $html .= '</div>';
  65. }
  66. if (!empty($data['high_risk_tables'])) {
  67. $html .= '<div class="mt-3">';
  68. $html .= '<h6>🔴 高风险表:</h6>';
  69. $html .= '<div class="table-responsive">';
  70. $html .= '<table class="table table-sm table-striped">';
  71. $html .= '<thead><tr><th>表名</th><th>当前记录数</th><th>预计删除</th><th>删除比例</th></tr></thead>';
  72. $html .= '<tbody>';
  73. foreach ($data['high_risk_tables'] as $table) {
  74. $html .= '<tr>';
  75. $html .= '<td>' . $table['table_name'] . '</td>';
  76. $html .= '<td>' . number_format($table['current_count']) . '</td>';
  77. $html .= '<td>' . number_format($table['estimated_delete']) . '</td>';
  78. $html .= '<td><span class="badge badge-danger">' . $table['delete_percentage'] . '%</span></td>';
  79. $html .= '</tr>';
  80. }
  81. $html .= '</tbody></table>';
  82. $html .= '</div>';
  83. $html .= '</div>';
  84. }
  85. return $this->response()
  86. ->success('预览完成')
  87. ->detail($html);
  88. } catch (\Exception $e) {
  89. return $this->response()
  90. ->error('预览失败:' . $e->getMessage());
  91. }
  92. }
  93. /**
  94. * 确认对话框
  95. */
  96. public function confirm()
  97. {
  98. return [
  99. '确认预览计划?',
  100. '此操作将分析计划的执行效果,不会实际删除数据。'
  101. ];
  102. }
  103. }