ViewBackupAction.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace App\Module\Cleanup\AdminControllers\Actions;
  3. use App\Module\Cleanup\Models\CleanupBackup;
  4. use Dcat\Admin\Actions\RowAction;
  5. use Dcat\Admin\Actions\Response;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 查看备份Action
  9. *
  10. * 用于查看备份的详细信息
  11. */
  12. class ViewBackupAction extends RowAction
  13. {
  14. /**
  15. * 按钮标题
  16. */
  17. protected $title = '查看备份';
  18. /**
  19. * 处理请求
  20. */
  21. public function handle(Request $request)
  22. {
  23. try {
  24. $backupId = $this->getKey();
  25. $backup = CleanupBackup::with(['files', 'sqlBackups'])->find($backupId);
  26. if (!$backup) {
  27. return $this->response()
  28. ->error('备份不存在');
  29. }
  30. $html = '<div class="row">';
  31. // 基本信息
  32. $html .= '<div class="col-md-6">';
  33. $html .= '<h5>基本信息</h5>';
  34. $html .= '<ul class="list-unstyled">';
  35. $html .= '<li><strong>备份名称:</strong>' . $backup->backup_name . '</li>';
  36. $html .= '<li><strong>备份类型:</strong>' . $this->getBackupTypeName($backup->backup_type) . '</li>';
  37. $html .= '<li><strong>压缩类型:</strong>' . $this->getCompressionTypeName($backup->compression_type) . '</li>';
  38. $html .= '<li><strong>备份状态:</strong>' . $this->getBackupStatusBadge($backup->backup_status) . '</li>';
  39. $html .= '<li><strong>表数量:</strong>' . number_format($backup->tables_count) . '</li>';
  40. $html .= '<li><strong>记录数量:</strong>' . number_format($backup->records_count) . '</li>';
  41. $html .= '</ul>';
  42. $html .= '</div>';
  43. // 大小信息
  44. $html .= '<div class="col-md-6">';
  45. $html .= '<h5>大小信息</h5>';
  46. $html .= '<ul class="list-unstyled">';
  47. $html .= '<li><strong>备份大小:</strong>' . $this->formatBytes($backup->backup_size) . '</li>';
  48. $html .= '<li><strong>原始大小:</strong>' . $this->formatBytes($backup->original_size) . '</li>';
  49. if ($backup->original_size > 0) {
  50. $compressionRatio = (1 - $backup->backup_size / $backup->original_size) * 100;
  51. $html .= '<li><strong>压缩率:</strong>' . number_format($compressionRatio, 2) . '%</li>';
  52. }
  53. $html .= '<li><strong>开始时间:</strong>' . $backup->started_at . '</li>';
  54. $html .= '<li><strong>完成时间:</strong>' . $backup->completed_at . '</li>';
  55. if ($backup->expires_at) {
  56. $html .= '<li><strong>过期时间:</strong>' . $backup->expires_at . '</li>';
  57. }
  58. $html .= '</ul>';
  59. $html .= '</div>';
  60. $html .= '</div>';
  61. // 备份文件列表
  62. if ($backup->files->isNotEmpty()) {
  63. $html .= '<div class="mt-4">';
  64. $html .= '<h5>备份文件</h5>';
  65. $html .= '<div class="table-responsive">';
  66. $html .= '<table class="table table-sm table-striped">';
  67. $html .= '<thead><tr><th>表名</th><th>文件名</th><th>文件大小</th><th>类型</th><th>压缩</th></tr></thead>';
  68. $html .= '<tbody>';
  69. foreach ($backup->files as $file) {
  70. $html .= '<tr>';
  71. $html .= '<td>' . $file->table_name . '</td>';
  72. $html .= '<td>' . $file->file_name . '</td>';
  73. $html .= '<td>' . $this->formatBytes($file->file_size) . '</td>';
  74. $html .= '<td>' . $this->getBackupTypeName($file->backup_type) . '</td>';
  75. $html .= '<td>' . $this->getCompressionTypeName($file->compression_type) . '</td>';
  76. $html .= '</tr>';
  77. }
  78. $html .= '</tbody></table>';
  79. $html .= '</div>';
  80. $html .= '</div>';
  81. }
  82. // SQL备份内容
  83. if ($backup->sqlBackups->isNotEmpty()) {
  84. $html .= '<div class="mt-4">';
  85. $html .= '<h5>SQL备份内容</h5>';
  86. $html .= '<div class="table-responsive">';
  87. $html .= '<table class="table table-sm table-striped">';
  88. $html .= '<thead><tr><th>表名</th><th>记录数量</th><th>内容大小</th><th>创建时间</th></tr></thead>';
  89. $html .= '<tbody>';
  90. foreach ($backup->sqlBackups as $sqlBackup) {
  91. $html .= '<tr>';
  92. $html .= '<td>' . $sqlBackup->table_name . '</td>';
  93. $html .= '<td>' . number_format($sqlBackup->records_count) . '</td>';
  94. $html .= '<td>' . $this->formatBytes($sqlBackup->content_size) . '</td>';
  95. $html .= '<td>' . $sqlBackup->created_at . '</td>';
  96. $html .= '</tr>';
  97. }
  98. $html .= '</tbody></table>';
  99. $html .= '</div>';
  100. $html .= '</div>';
  101. }
  102. return $this->response()
  103. ->success('备份详情')
  104. ->detail($html);
  105. } catch (\Exception $e) {
  106. return $this->response()
  107. ->error('查看失败:' . $e->getMessage());
  108. }
  109. }
  110. /**
  111. * 获取备份类型名称
  112. */
  113. private function getBackupTypeName($type)
  114. {
  115. $types = [1 => 'SQL', 2 => 'JSON', 3 => 'CSV'];
  116. return $types[$type] ?? '未知';
  117. }
  118. /**
  119. * 获取压缩类型名称
  120. */
  121. private function getCompressionTypeName($type)
  122. {
  123. $types = [1 => '无压缩', 2 => 'GZIP', 3 => 'ZIP'];
  124. return $types[$type] ?? '未知';
  125. }
  126. /**
  127. * 获取备份状态标签
  128. */
  129. private function getBackupStatusBadge($status)
  130. {
  131. $badges = [
  132. 1 => '<span class="badge badge-info">进行中</span>',
  133. 2 => '<span class="badge badge-success">已完成</span>',
  134. 3 => '<span class="badge badge-danger">已失败</span>',
  135. ];
  136. return $badges[$status] ?? '<span class="badge badge-secondary">未知</span>';
  137. }
  138. /**
  139. * 格式化字节大小
  140. */
  141. private function formatBytes($bytes)
  142. {
  143. if ($bytes == 0) return '0 B';
  144. $units = ['B', 'KB', 'MB', 'GB', 'TB'];
  145. $base = log($bytes, 1024);
  146. $index = floor($base);
  147. if ($index >= count($units)) {
  148. $index = count($units) - 1;
  149. }
  150. $size = round(pow(1024, $base - $index), 2);
  151. return $size . ' ' . $units[$index];
  152. }
  153. /**
  154. * 渲染按钮
  155. */
  156. public function render()
  157. {
  158. return <<<HTML
  159. <a href="javascript:void(0);" class="btn btn-info btn-xs" data-action="{$this->getHandleRoute()}">
  160. <i class="fa fa-eye"></i> {$this->title}
  161. </a>
  162. HTML;
  163. }
  164. }