TableScannerLogic.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. namespace App\Module\Cleanup\Logics;
  3. use App\Module\Cleanup\Models\CleanupConfig;
  4. use App\Module\Cleanup\Models\CleanupTableStats;
  5. use App\Module\Cleanup\Enums\DATA_CATEGORY;
  6. use App\Module\Cleanup\Enums\CLEANUP_TYPE;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Schema;
  9. use Illuminate\Support\Facades\Log;
  10. /**
  11. * 表扫描逻辑类(已废弃)
  12. *
  13. * 负责扫描系统中的数据表并生成清理配置
  14. *
  15. * @deprecated 此类已废弃,请使用 ModelScannerLogic 类
  16. * @see ModelScannerLogic
  17. */
  18. class TableScannerLogic
  19. {
  20. /**
  21. * 扫描所有数据表
  22. *
  23. * @param bool $forceRefresh 是否强制刷新
  24. * @return array 扫描结果
  25. */
  26. public static function scanAllTables(bool $forceRefresh = false): array
  27. {
  28. $startTime = microtime(true);
  29. // 获取所有以 kku_ 开头的表
  30. $tables = static::getAllTables();
  31. $result = [
  32. 'total_tables' => count($tables),
  33. 'scanned_tables' => 0,
  34. 'new_tables' => 0,
  35. 'updated_tables' => 0,
  36. 'tables' => [],
  37. 'scan_time' => 0,
  38. ];
  39. foreach ($tables as $tableName) {
  40. try {
  41. $tableInfo = static::scanTable($tableName, $forceRefresh);
  42. $result['tables'][] = $tableInfo;
  43. $result['scanned_tables']++;
  44. if ($tableInfo['is_new']) {
  45. $result['new_tables']++;
  46. } elseif ($tableInfo['is_updated']) {
  47. $result['updated_tables']++;
  48. }
  49. } catch (\Exception $e) {
  50. Log::error("扫描表 {$tableName} 失败: " . $e->getMessage());
  51. }
  52. }
  53. $result['scan_time'] = round(microtime(true) - $startTime, 3);
  54. return $result;
  55. }
  56. /**
  57. * 扫描单个表
  58. *
  59. * @param string $tableName 表名
  60. * @param bool $forceRefresh 是否强制刷新
  61. * @return array 表信息
  62. */
  63. public static function scanTable(string $tableName, bool $forceRefresh = false): array
  64. {
  65. // 检查是否已存在配置
  66. $existingConfig = CleanupConfig::where('table_name', $tableName)->first();
  67. $isNew = !$existingConfig;
  68. $isUpdated = false;
  69. // 获取表的基本信息
  70. $tableInfo = static::getTableInfo($tableName);
  71. // 分析表结构
  72. $structure = static::analyzeTableStructure($tableName);
  73. // 自动识别数据分类
  74. $dataCategory = DATA_CATEGORY::detectFromTableName($tableName);
  75. // 识别模块名称
  76. $moduleName = static::detectModuleName($tableName);
  77. // 生成默认清理配置
  78. $defaultConfig = static::generateDefaultConfig($dataCategory, $structure);
  79. if ($isNew || $forceRefresh) {
  80. // 创建或更新配置
  81. $configData = [
  82. 'table_name' => $tableName,
  83. 'module_name' => $moduleName,
  84. 'data_category' => $dataCategory->value,
  85. 'default_cleanup_type' => $defaultConfig['cleanup_type'],
  86. 'default_conditions' => $defaultConfig['conditions'],
  87. 'is_enabled' => $defaultConfig['is_enabled'],
  88. 'priority' => $defaultConfig['priority'],
  89. 'batch_size' => $defaultConfig['batch_size'],
  90. 'description' => $defaultConfig['description'],
  91. ];
  92. if ($isNew) {
  93. CleanupConfig::create($configData);
  94. } else {
  95. $existingConfig->update($configData);
  96. $isUpdated = true;
  97. }
  98. }
  99. // 更新表统计信息
  100. static::updateTableStats($tableName, $tableInfo, $structure);
  101. return [
  102. 'table_name' => $tableName,
  103. 'module_name' => $moduleName,
  104. 'data_category' => $dataCategory->value,
  105. 'data_category_name' => $dataCategory->getDescription(),
  106. 'record_count' => $tableInfo['record_count'],
  107. 'table_size_mb' => $tableInfo['table_size_mb'],
  108. 'has_time_field' => !empty($structure['time_fields']),
  109. 'time_fields' => $structure['time_fields'],
  110. 'has_user_field' => !empty($structure['user_fields']),
  111. 'user_fields' => $structure['user_fields'],
  112. 'has_status_field' => !empty($structure['status_fields']),
  113. 'status_fields' => $structure['status_fields'],
  114. 'default_cleanup_type' => $defaultConfig['cleanup_type'],
  115. 'default_priority' => $defaultConfig['priority'],
  116. 'is_new' => $isNew,
  117. 'is_updated' => $isUpdated,
  118. ];
  119. }
  120. /**
  121. * 获取所有数据表列表
  122. *
  123. * @return array 表名列表
  124. */
  125. private static function getAllTables(): array
  126. {
  127. $tables = DB::select("SHOW TABLES LIKE 'kku_%'");
  128. if (empty($tables)) {
  129. return [];
  130. }
  131. // 获取第一个表对象的属性名
  132. $firstTable = $tables[0];
  133. $properties = get_object_vars($firstTable);
  134. $tableColumn = array_keys($properties)[0]; // 获取第一个属性名
  135. return array_map(function($table) use ($tableColumn) {
  136. return $table->$tableColumn;
  137. }, $tables);
  138. }
  139. /**
  140. * 获取表的基本信息
  141. *
  142. * @param string $tableName 表名
  143. * @return array 表信息
  144. */
  145. private static function getTableInfo(string $tableName): array
  146. {
  147. // 获取表状态信息
  148. $tableStatus = DB::select("SHOW TABLE STATUS LIKE '{$tableName}'");
  149. if (empty($tableStatus)) {
  150. throw new \Exception("表 {$tableName} 不存在");
  151. }
  152. $status = $tableStatus[0];
  153. // 获取记录数量(移除前缀,因为Laravel会自动添加)
  154. $tableNameWithoutPrefix = preg_replace('/^kku_/', '', $tableName);
  155. $recordCount = DB::table($tableNameWithoutPrefix)->count();
  156. return [
  157. 'record_count' => $recordCount,
  158. 'table_size_mb' => round(($status->Data_length + $status->Index_length) / 1024 / 1024, 2),
  159. 'index_size_mb' => round($status->Index_length / 1024 / 1024, 2),
  160. 'data_free_mb' => round($status->Data_free / 1024 / 1024, 2),
  161. 'avg_row_length' => $status->Avg_row_length,
  162. 'auto_increment' => $status->Auto_increment,
  163. 'engine' => $status->Engine,
  164. 'collation' => $status->Collation,
  165. ];
  166. }
  167. /**
  168. * 分析表结构
  169. *
  170. * @param string $tableName 表名
  171. * @return array 结构信息
  172. */
  173. private static function analyzeTableStructure(string $tableName): array
  174. {
  175. $columns = Schema::getColumnListing($tableName);
  176. $timeFields = [];
  177. $userFields = [];
  178. $statusFields = [];
  179. $primaryKey = null;
  180. foreach ($columns as $column) {
  181. $columnInfo = DB::select("SHOW COLUMNS FROM {$tableName} LIKE '{$column}'");
  182. if (!empty($columnInfo)) {
  183. $info = $columnInfo[0];
  184. // 检查主键
  185. if ($info->Key === 'PRI') {
  186. $primaryKey = $column;
  187. }
  188. // 检查时间字段
  189. if (static::isTimeField($column, $info->Type)) {
  190. $timeFields[] = $column;
  191. }
  192. // 检查用户字段
  193. if (static::isUserField($column)) {
  194. $userFields[] = $column;
  195. }
  196. // 检查状态字段
  197. if (static::isStatusField($column)) {
  198. $statusFields[] = $column;
  199. }
  200. }
  201. }
  202. // 获取时间范围
  203. $timeRange = static::getTimeRange($tableName, $timeFields);
  204. return [
  205. 'columns' => $columns,
  206. 'primary_key' => $primaryKey,
  207. 'time_fields' => $timeFields,
  208. 'user_fields' => $userFields,
  209. 'status_fields' => $statusFields,
  210. 'time_range' => $timeRange,
  211. ];
  212. }
  213. /**
  214. * 判断是否为时间字段
  215. *
  216. * @param string $columnName 字段名
  217. * @param string $columnType 字段类型
  218. * @return bool
  219. */
  220. private static function isTimeField(string $columnName, string $columnType): bool
  221. {
  222. // 按字段名判断
  223. $timeFieldNames = [
  224. 'created_at', 'updated_at', 'deleted_at',
  225. 'start_time', 'end_time', 'expire_time',
  226. 'login_time', 'logout_time', 'last_login',
  227. 'published_at', 'completed_at', 'processed_at'
  228. ];
  229. if (in_array($columnName, $timeFieldNames)) {
  230. return true;
  231. }
  232. // 按字段名模式判断
  233. if (preg_match('/(time|date|at)$/i', $columnName)) {
  234. return true;
  235. }
  236. // 按字段类型判断
  237. if (preg_match('/^(datetime|timestamp|date)/i', $columnType)) {
  238. return true;
  239. }
  240. return false;
  241. }
  242. /**
  243. * 判断是否为用户字段
  244. *
  245. * @param string $columnName 字段名
  246. * @return bool
  247. */
  248. private static function isUserField(string $columnName): bool
  249. {
  250. $userFieldNames = [
  251. 'user_id', 'urs_user_id', 'created_by', 'updated_by',
  252. 'owner_id', 'author_id', 'operator_id'
  253. ];
  254. return in_array($columnName, $userFieldNames);
  255. }
  256. /**
  257. * 判断是否为状态字段
  258. *
  259. * @param string $columnName 字段名
  260. * @return bool
  261. */
  262. private static function isStatusField(string $columnName): bool
  263. {
  264. $statusFieldNames = [
  265. 'status', 'state', 'is_active', 'is_enabled',
  266. 'is_deleted', 'is_published', 'is_completed'
  267. ];
  268. return in_array($columnName, $statusFieldNames);
  269. }
  270. /**
  271. * 获取时间范围
  272. *
  273. * @param string $tableName 表名
  274. * @param array $timeFields 时间字段
  275. * @return array 时间范围
  276. */
  277. private static function getTimeRange(string $tableName, array $timeFields): array
  278. {
  279. if (empty($timeFields)) {
  280. return [];
  281. }
  282. $timeField = $timeFields[0]; // 使用第一个时间字段
  283. try {
  284. // 移除前缀,因为Laravel会自动添加
  285. $tableNameWithoutPrefix = preg_replace('/^kku_/', '', $tableName);
  286. $result = DB::table($tableNameWithoutPrefix)
  287. ->selectRaw("MIN({$timeField}) as min_time, MAX({$timeField}) as max_time")
  288. ->whereNotNull($timeField)
  289. ->first();
  290. return [
  291. 'field' => $timeField,
  292. 'min_time' => $result->min_time,
  293. 'max_time' => $result->max_time,
  294. ];
  295. } catch (\Exception $e) {
  296. return [];
  297. }
  298. }
  299. /**
  300. * 识别模块名称
  301. *
  302. * @param string $tableName 表名
  303. * @return string 模块名称
  304. */
  305. private static function detectModuleName(string $tableName): string
  306. {
  307. // 移除表前缀
  308. $name = preg_replace('/^kku_/', '', $tableName);
  309. // 常见模块映射
  310. $moduleMap = [
  311. 'farm' => 'Farm',
  312. 'item' => 'GameItems',
  313. 'pet' => 'Pet',
  314. 'user' => 'User',
  315. 'fund' => 'Fund',
  316. 'mex' => 'Mex',
  317. 'promotion' => 'Promotion',
  318. 'config' => 'Config',
  319. 'log' => 'Log',
  320. 'cache' => 'Cache',
  321. 'session' => 'Session',
  322. ];
  323. foreach ($moduleMap as $prefix => $module) {
  324. if (str_starts_with($name, $prefix . '_')) {
  325. return $module;
  326. }
  327. }
  328. // 尝试从表名中提取模块名
  329. $parts = explode('_', $name);
  330. if (!empty($parts)) {
  331. return ucfirst($parts[0]);
  332. }
  333. return 'Unknown';
  334. }
  335. /**
  336. * 生成默认清理配置
  337. *
  338. * @param DATA_CATEGORY $dataCategory 数据分类
  339. * @param array $structure 表结构
  340. * @return array 默认配置
  341. */
  342. private static function generateDefaultConfig(DATA_CATEGORY $dataCategory, array $structure): array
  343. {
  344. $cleanupType = $dataCategory->getDefaultCleanupType();
  345. $priority = $dataCategory->getDefaultPriority();
  346. $isEnabled = $dataCategory->isDefaultEnabled();
  347. $conditions = null;
  348. $batchSize = 1000;
  349. // 根据清理类型生成默认条件
  350. if ($cleanupType === CLEANUP_TYPE::DELETE_BY_TIME && !empty($structure['time_fields'])) {
  351. $conditions = [
  352. 'time_field' => $structure['time_fields'][0],
  353. 'time_condition' => 'older_than',
  354. 'time_value' => 30,
  355. 'time_unit' => 'days'
  356. ];
  357. }
  358. // 根据数据分类调整批处理大小
  359. if ($dataCategory === DATA_CATEGORY::LOG_DATA) {
  360. $batchSize = 5000; // 日志数据可以使用更大的批处理
  361. } elseif ($dataCategory === DATA_CATEGORY::CACHE_DATA) {
  362. $batchSize = 10000; // 缓存数据可以使用更大的批处理
  363. }
  364. $description = static::generateDescription($dataCategory, $cleanupType, $conditions);
  365. return [
  366. 'cleanup_type' => $cleanupType->value,
  367. 'conditions' => $conditions,
  368. 'is_enabled' => $isEnabled,
  369. 'priority' => $priority,
  370. 'batch_size' => $batchSize,
  371. 'description' => $description,
  372. ];
  373. }
  374. /**
  375. * 生成配置描述
  376. *
  377. * @param DATA_CATEGORY $dataCategory 数据分类
  378. * @param CLEANUP_TYPE $cleanupType 清理类型
  379. * @param array|null $conditions 清理条件
  380. * @return string 描述
  381. */
  382. private static function generateDescription(DATA_CATEGORY $dataCategory, CLEANUP_TYPE $cleanupType, ?array $conditions): string
  383. {
  384. $description = $dataCategory->getDescription() . ' - ' . $cleanupType->getDescription();
  385. if ($conditions && $cleanupType === CLEANUP_TYPE::DELETE_BY_TIME) {
  386. $description .= ",保留{$conditions['time_value']}{$conditions['time_unit']}内的数据";
  387. }
  388. return $description;
  389. }
  390. /**
  391. * 更新表统计信息
  392. *
  393. * @param string $tableName 表名
  394. * @param array $tableInfo 表信息
  395. * @param array $structure 表结构
  396. * @return void
  397. */
  398. private static function updateTableStats(string $tableName, array $tableInfo, array $structure): void
  399. {
  400. $statsData = [
  401. 'table_name' => $tableName,
  402. 'record_count' => $tableInfo['record_count'],
  403. 'table_size_mb' => $tableInfo['table_size_mb'],
  404. 'index_size_mb' => $tableInfo['index_size_mb'],
  405. 'data_free_mb' => $tableInfo['data_free_mb'],
  406. 'avg_row_length' => $tableInfo['avg_row_length'],
  407. 'auto_increment' => $tableInfo['auto_increment'],
  408. 'oldest_record_time' => $structure['time_range']['min_time'] ?? null,
  409. 'newest_record_time' => $structure['time_range']['max_time'] ?? null,
  410. 'scan_time' => now(),
  411. ];
  412. // 这里应该创建 CleanupTableStats 模型,暂时跳过
  413. // CleanupTableStats::create($statsData);
  414. }
  415. }