| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace App\Module\Cleanup\Commands;
- use App\Module\Cleanup\Services\CleanupService;
- use Illuminate\Console\Command;
- /**
- * 扫描数据表命令
- *
- * 扫描系统中的所有数据表并生成清理配置
- */
- class ScanTablesCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'cleanup:scan-tables
- {--force : 强制重新扫描所有表}
- {--show-details : 显示详细信息}';
- /**
- * 命令描述
- */
- protected $description = '扫描系统中的所有数据表并生成清理配置';
- /**
- * 执行命令
- */
- public function handle(): int
- {
- $this->info('开始扫描数据表...');
- $this->newLine();
- $forceRefresh = $this->option('force');
- $showDetails = $this->option('show-details');
- if ($forceRefresh) {
- $this->warn('强制刷新模式:将重新生成所有表的配置');
- }
- try {
- // 执行扫描
- $result = CleanupService::scanTables($forceRefresh);
- // 显示扫描结果
- $this->displayScanResults($result, $showDetails);
- $this->newLine();
- $this->info('✅ 数据表扫描完成!');
-
- return Command::SUCCESS;
- } catch (\Exception $e) {
- $this->error('❌ 扫描失败: ' . $e->getMessage());
- $this->error('详细错误: ' . $e->getTraceAsString());
-
- return Command::FAILURE;
- }
- }
- /**
- * 显示扫描结果
- *
- * @param array $result 扫描结果
- * @param bool $showDetails 是否显示详细信息
- */
- private function displayScanResults(array $result, bool $showDetails): void
- {
- // 显示统计信息
- $this->info("📊 扫描统计:");
- $this->table(
- ['项目', '数量'],
- [
- ['总表数', $result['total_tables']],
- ['已扫描', $result['scanned_tables']],
- ['新增配置', $result['new_tables']],
- ['更新配置', $result['updated_tables']],
- ['扫描耗时', $result['scan_time'] . ' 秒'],
- ]
- );
- if (!$showDetails) {
- return;
- }
- $this->newLine();
- $this->info("📋 表详细信息:");
- // 按数据分类分组显示
- $tablesByCategory = [];
- foreach ($result['tables'] as $table) {
- $category = $table['data_category_name'];
- if (!isset($tablesByCategory[$category])) {
- $tablesByCategory[$category] = [];
- }
- $tablesByCategory[$category][] = $table;
- }
- foreach ($tablesByCategory as $category => $tables) {
- $this->newLine();
- $this->line("<fg=cyan>📁 {$category} ({count($tables)} 个表)</>");
-
- $tableData = [];
- foreach ($tables as $table) {
- $status = [];
- if ($table['is_new']) {
- $status[] = '<fg=green>新增</>';
- }
- if ($table['is_updated']) {
- $status[] = '<fg=yellow>更新</>';
- }
-
- $tableData[] = [
- $table['table_name'],
- $table['module_name'],
- number_format($table['record_count']),
- $table['table_size_mb'] . ' MB',
- $table['has_time_field'] ? '✅' : '❌',
- $table['has_user_field'] ? '✅' : '❌',
- implode(' ', $status) ?: '-',
- ];
- }
- $this->table(
- ['表名', '模块', '记录数', '大小', '时间字段', '用户字段', '状态'],
- $tableData
- );
- }
- // 显示字段分析
- $this->newLine();
- $this->info("🔍 字段分析:");
-
- $timeFieldTables = array_filter($result['tables'], fn($t) => $t['has_time_field']);
- $userFieldTables = array_filter($result['tables'], fn($t) => $t['has_user_field']);
-
- $this->table(
- ['分析项', '数量', '百分比'],
- [
- ['包含时间字段的表', count($timeFieldTables), round(count($timeFieldTables) / $result['total_tables'] * 100, 1) . '%'],
- ['包含用户字段的表', count($userFieldTables), round(count($userFieldTables) / $result['total_tables'] * 100, 1) . '%'],
- ]
- );
- // 显示推荐的清理计划
- $this->newLine();
- $this->info("💡 推荐的清理计划:");
-
- $recommendations = CleanupService::getRecommendedPlans();
- if (empty($recommendations)) {
- $this->line('暂无推荐的清理计划');
- } else {
- foreach ($recommendations as $recommendation) {
- $this->line("• {$recommendation['title']} - {$recommendation['description']} (预计 {$recommendation['estimated_tables']} 个表)");
- }
- }
- }
- }
|