| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace App\Module\Cleanup\AdminControllers\Actions;
- use App\Module\Cleanup\Models\CleanupBackup;
- use Dcat\Admin\Actions\RowAction;
- use Dcat\Admin\Actions\Response;
- use Illuminate\Http\Request;
- /**
- * 查看备份Action
- *
- * 用于查看备份的详细信息
- */
- class ViewBackupAction extends RowAction
- {
- /**
- * 按钮标题
- */
- protected $title = '查看备份';
- /**
- * 处理请求
- */
- public function handle(Request $request)
- {
- try {
- $backupId = $this->getKey();
-
- $backup = CleanupBackup::with(['files', 'sqlBackups'])->find($backupId);
-
- if (!$backup) {
- return $this->response()
- ->error('备份不存在');
- }
-
- $html = '<div class="row">';
-
- // 基本信息
- $html .= '<div class="col-md-6">';
- $html .= '<h5>基本信息</h5>';
- $html .= '<ul class="list-unstyled">';
- $html .= '<li><strong>备份名称:</strong>' . $backup->backup_name . '</li>';
- $html .= '<li><strong>备份类型:</strong>' . $this->getBackupTypeName($backup->backup_type) . '</li>';
- $html .= '<li><strong>压缩类型:</strong>' . $this->getCompressionTypeName($backup->compression_type) . '</li>';
- $html .= '<li><strong>备份状态:</strong>' . $this->getBackupStatusBadge($backup->backup_status) . '</li>';
- $html .= '<li><strong>表数量:</strong>' . number_format($backup->tables_count) . '</li>';
- $html .= '<li><strong>记录数量:</strong>' . number_format($backup->records_count) . '</li>';
- $html .= '</ul>';
- $html .= '</div>';
-
- // 大小信息
- $html .= '<div class="col-md-6">';
- $html .= '<h5>大小信息</h5>';
- $html .= '<ul class="list-unstyled">';
- $html .= '<li><strong>备份大小:</strong>' . $this->formatBytes($backup->backup_size) . '</li>';
- $html .= '<li><strong>原始大小:</strong>' . $this->formatBytes($backup->original_size) . '</li>';
-
- if ($backup->original_size > 0) {
- $compressionRatio = (1 - $backup->backup_size / $backup->original_size) * 100;
- $html .= '<li><strong>压缩率:</strong>' . number_format($compressionRatio, 2) . '%</li>';
- }
-
- $html .= '<li><strong>开始时间:</strong>' . $backup->started_at . '</li>';
- $html .= '<li><strong>完成时间:</strong>' . $backup->completed_at . '</li>';
-
- if ($backup->expires_at) {
- $html .= '<li><strong>过期时间:</strong>' . $backup->expires_at . '</li>';
- }
-
- $html .= '</ul>';
- $html .= '</div>';
- $html .= '</div>';
-
- // 备份文件列表
- if ($backup->files->isNotEmpty()) {
- $html .= '<div class="mt-4">';
- $html .= '<h5>备份文件</h5>';
- $html .= '<div class="table-responsive">';
- $html .= '<table class="table table-sm table-striped">';
- $html .= '<thead><tr><th>表名</th><th>文件名</th><th>文件大小</th><th>类型</th><th>压缩</th></tr></thead>';
- $html .= '<tbody>';
-
- foreach ($backup->files as $file) {
- $html .= '<tr>';
- $html .= '<td>' . $file->table_name . '</td>';
- $html .= '<td>' . $file->file_name . '</td>';
- $html .= '<td>' . $this->formatBytes($file->file_size) . '</td>';
- $html .= '<td>' . $this->getBackupTypeName($file->backup_type) . '</td>';
- $html .= '<td>' . $this->getCompressionTypeName($file->compression_type) . '</td>';
- $html .= '</tr>';
- }
-
- $html .= '</tbody></table>';
- $html .= '</div>';
- $html .= '</div>';
- }
-
- // SQL备份内容
- if ($backup->sqlBackups->isNotEmpty()) {
- $html .= '<div class="mt-4">';
- $html .= '<h5>SQL备份内容</h5>';
- $html .= '<div class="table-responsive">';
- $html .= '<table class="table table-sm table-striped">';
- $html .= '<thead><tr><th>表名</th><th>记录数量</th><th>内容大小</th><th>创建时间</th></tr></thead>';
- $html .= '<tbody>';
-
- foreach ($backup->sqlBackups as $sqlBackup) {
- $html .= '<tr>';
- $html .= '<td>' . $sqlBackup->table_name . '</td>';
- $html .= '<td>' . number_format($sqlBackup->records_count) . '</td>';
- $html .= '<td>' . $this->formatBytes($sqlBackup->content_size) . '</td>';
- $html .= '<td>' . $sqlBackup->created_at . '</td>';
- $html .= '</tr>';
- }
-
- $html .= '</tbody></table>';
- $html .= '</div>';
- $html .= '</div>';
- }
-
- return $this->response()
- ->success('备份详情')
- ->detail($html);
-
- } catch (\Exception $e) {
- return $this->response()
- ->error('查看失败:' . $e->getMessage());
- }
- }
- /**
- * 获取备份类型名称
- */
- private function getBackupTypeName($type)
- {
- $types = [1 => 'SQL', 2 => 'JSON', 3 => 'CSV'];
- return $types[$type] ?? '未知';
- }
- /**
- * 获取压缩类型名称
- */
- private function getCompressionTypeName($type)
- {
- $types = [1 => '无压缩', 2 => 'GZIP', 3 => 'ZIP'];
- return $types[$type] ?? '未知';
- }
- /**
- * 获取备份状态标签
- */
- private function getBackupStatusBadge($status)
- {
- $badges = [
- 1 => '<span class="badge badge-info">进行中</span>',
- 2 => '<span class="badge badge-success">已完成</span>',
- 3 => '<span class="badge badge-danger">已失败</span>',
- ];
- return $badges[$status] ?? '<span class="badge badge-secondary">未知</span>';
- }
- /**
- * 格式化字节大小
- */
- private function formatBytes($bytes)
- {
- if ($bytes == 0) return '0 B';
-
- $units = ['B', 'KB', 'MB', 'GB', 'TB'];
- $base = log($bytes, 1024);
- $index = floor($base);
-
- if ($index >= count($units)) {
- $index = count($units) - 1;
- }
-
- $size = round(pow(1024, $base - $index), 2);
- return $size . ' ' . $units[$index];
- }
- /**
- * 渲染按钮
- */
- public function render()
- {
- return <<<HTML
- <a href="javascript:void(0);" class="btn btn-info btn-xs" data-action="{$this->getHandleRoute()}">
- <i class="fa fa-eye"></i> {$this->title}
- </a>
- HTML;
- }
- }
|