ScanTablesCommand.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Module\Cleanup\Commands;
  3. use App\Module\Cleanup\Services\CleanupService;
  4. use Illuminate\Console\Command;
  5. /**
  6. * 扫描数据表命令(已废弃,建议使用 cleanup:scan-models)
  7. *
  8. * 扫描系统中的所有Model类并生成清理配置
  9. * 注意:此命令现在内部使用Model扫描逻辑
  10. */
  11. class ScanTablesCommand extends Command
  12. {
  13. /**
  14. * 命令签名
  15. */
  16. protected $signature = 'cleanup:scan-tables
  17. {--force : 强制重新扫描所有Model}
  18. {--show-details : 显示详细信息}';
  19. /**
  20. * 命令描述
  21. */
  22. protected $description = '扫描系统中的所有Model类并生成清理配置(已废弃,建议使用 cleanup:scan-models)';
  23. /**
  24. * 执行命令
  25. */
  26. public function handle(): int
  27. {
  28. $this->warn('⚠️ 此命令已废弃,建议使用 cleanup:scan-models 命令');
  29. $this->info('开始扫描Model类(通过兼容模式)...');
  30. $this->newLine();
  31. $forceRefresh = $this->option('force');
  32. $showDetails = $this->option('show-details');
  33. if ($forceRefresh) {
  34. $this->warn('强制刷新模式:将重新生成所有Model的配置');
  35. }
  36. try {
  37. // 执行扫描
  38. $result = CleanupService::scanTables($forceRefresh);
  39. // 显示扫描结果
  40. $this->displayScanResults($result, $showDetails);
  41. $this->newLine();
  42. $this->info('✅ 数据表扫描完成!');
  43. return Command::SUCCESS;
  44. } catch (\Exception $e) {
  45. $this->error('❌ 扫描失败: ' . $e->getMessage());
  46. $this->error('详细错误: ' . $e->getTraceAsString());
  47. return Command::FAILURE;
  48. }
  49. }
  50. /**
  51. * 显示扫描结果
  52. *
  53. * @param array $result 扫描结果
  54. * @param bool $showDetails 是否显示详细信息
  55. */
  56. private function displayScanResults(array $result, bool $showDetails): void
  57. {
  58. // 显示统计信息
  59. $this->info("📊 扫描统计:");
  60. $this->table(
  61. ['项目', '数量'],
  62. [
  63. ['总表数', $result['total_tables']],
  64. ['已扫描', $result['scanned_tables']],
  65. ['新增配置', $result['new_tables']],
  66. ['更新配置', $result['updated_tables']],
  67. ['扫描耗时', $result['scan_time'] . ' 秒'],
  68. ]
  69. );
  70. if (!$showDetails) {
  71. return;
  72. }
  73. $this->newLine();
  74. $this->info("📋 表详细信息:");
  75. // 按数据分类分组显示
  76. $tablesByCategory = [];
  77. foreach ($result['tables'] as $table) {
  78. $category = $table['data_category_name'];
  79. if (!isset($tablesByCategory[$category])) {
  80. $tablesByCategory[$category] = [];
  81. }
  82. $tablesByCategory[$category][] = $table;
  83. }
  84. foreach ($tablesByCategory as $category => $tables) {
  85. $this->newLine();
  86. $tableCount = count($tables);
  87. $this->line("<fg=cyan>📁 {$category} ({$tableCount} 个表)</>");
  88. $tableData = [];
  89. foreach ($tables as $table) {
  90. $status = [];
  91. if ($table['is_new']) {
  92. $status[] = '<fg=green>新增</>';
  93. }
  94. if ($table['is_updated']) {
  95. $status[] = '<fg=yellow>更新</>';
  96. }
  97. $tableData[] = [
  98. $table['table_name'],
  99. $table['module_name'],
  100. number_format($table['record_count']),
  101. $table['table_size_mb'] . ' MB',
  102. $table['has_time_field'] ? '✅' : '❌',
  103. $table['has_user_field'] ? '✅' : '❌',
  104. implode(' ', $status) ?: '-',
  105. ];
  106. }
  107. $this->table(
  108. ['表名', '模块', '记录数', '大小', '时间字段', '用户字段', '状态'],
  109. $tableData
  110. );
  111. }
  112. // 显示字段分析
  113. $this->newLine();
  114. $this->info("🔍 字段分析:");
  115. $timeFieldTables = array_filter($result['tables'], fn($t) => $t['has_time_field']);
  116. $userFieldTables = array_filter($result['tables'], fn($t) => $t['has_user_field']);
  117. $this->table(
  118. ['分析项', '数量', '百分比'],
  119. [
  120. ['包含时间字段的表', count($timeFieldTables), round(count($timeFieldTables) / $result['total_tables'] * 100, 1) . '%'],
  121. ['包含用户字段的表', count($userFieldTables), round(count($userFieldTables) / $result['total_tables'] * 100, 1) . '%'],
  122. ]
  123. );
  124. // 显示推荐的清理计划
  125. $this->newLine();
  126. $this->info("💡 推荐的清理计划:");
  127. $recommendations = CleanupService::getRecommendedPlans();
  128. if (empty($recommendations)) {
  129. $this->line('暂无推荐的清理计划');
  130. } else {
  131. foreach ($recommendations as $recommendation) {
  132. $this->line("• {$recommendation['title']} - {$recommendation['description']} (预计 {$recommendation['estimated_tables']} 个表)");
  133. }
  134. }
  135. }
  136. }