CleanupSqlBackup.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Module\Cleanup\Models;
  3. use UCore\ModelCore;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. /**
  6. * SQL备份记录模型
  7. *
  8. * 存储INSERT语句到数据库表中
  9. */
  10. class CleanupSqlBackup extends ModelCore
  11. {
  12. /**
  13. * 数据表名
  14. */
  15. protected $table = 'cleanup_sql_backups';
  16. // field start
  17. /**
  18. * 可批量赋值的字段
  19. */
  20. protected $fillable = [
  21. 'backup_id',
  22. 'table_name',
  23. 'sql_content',
  24. 'records_count',
  25. 'content_size',
  26. 'content_hash',
  27. 'backup_conditions',
  28. ];
  29. // field end
  30. /**
  31. * 字段类型转换
  32. */
  33. protected $casts = [
  34. 'backup_conditions' => 'array',
  35. 'records_count' => 'integer',
  36. 'content_size' => 'integer',
  37. ];
  38. /**
  39. * 隐藏字段
  40. */
  41. protected $hidden = [
  42. 'sql_content', // 默认隐藏SQL内容,避免大量数据传输
  43. ];
  44. /**
  45. * 关联备份记录
  46. */
  47. public function backup(): BelongsTo
  48. {
  49. return $this->belongsTo(CleanupBackup::class, 'backup_id');
  50. }
  51. /**
  52. * 获取格式化的内容大小
  53. */
  54. public function getFormattedSizeAttribute(): string
  55. {
  56. $size = $this->content_size;
  57. if ($size < 1024) {
  58. return $size . ' B';
  59. } elseif ($size < 1024 * 1024) {
  60. return round($size / 1024, 2) . ' KB';
  61. } elseif ($size < 1024 * 1024 * 1024) {
  62. return round($size / (1024 * 1024), 2) . ' MB';
  63. } else {
  64. return round($size / (1024 * 1024 * 1024), 2) . ' GB';
  65. }
  66. }
  67. /**
  68. * 获取SQL内容预览(前100个字符)
  69. */
  70. public function getSqlPreviewAttribute(): string
  71. {
  72. if (empty($this->sql_content)) {
  73. return '';
  74. }
  75. $preview = substr($this->sql_content, 0, 100);
  76. if (strlen($this->sql_content) > 100) {
  77. $preview .= '...';
  78. }
  79. return $preview;
  80. }
  81. /**
  82. * 验证内容哈希
  83. */
  84. public function verifyContentHash(): bool
  85. {
  86. if (empty($this->sql_content) || empty($this->content_hash)) {
  87. return false;
  88. }
  89. $currentHash = hash('sha256', $this->sql_content);
  90. return $currentHash === $this->content_hash;
  91. }
  92. /**
  93. * 作用域:按备份ID筛选
  94. */
  95. public function scopeByBackup($query, int $backupId)
  96. {
  97. return $query->where('backup_id', $backupId);
  98. }
  99. /**
  100. * 作用域:按表名筛选
  101. */
  102. public function scopeByTable($query, string $tableName)
  103. {
  104. return $query->where('table_name', $tableName);
  105. }
  106. /**
  107. * 作用域:按记录数量排序
  108. */
  109. public function scopeOrderByRecords($query, string $direction = 'desc')
  110. {
  111. return $query->orderBy('records_count', $direction);
  112. }
  113. /**
  114. * 作用域:按内容大小排序
  115. */
  116. public function scopeOrderBySize($query, string $direction = 'desc')
  117. {
  118. return $query->orderBy('content_size', $direction);
  119. }
  120. /**
  121. * 获取统计信息
  122. */
  123. public static function getStats(): array
  124. {
  125. return [
  126. 'total_count' => static::count(),
  127. 'total_records' => static::sum('records_count'),
  128. 'total_size' => static::sum('content_size'),
  129. 'avg_records_per_backup' => static::avg('records_count'),
  130. 'avg_size_per_backup' => static::avg('content_size'),
  131. ];
  132. }
  133. /**
  134. * 获取按表名分组的统计
  135. */
  136. public static function getTableStats(): array
  137. {
  138. return static::selectRaw('
  139. table_name,
  140. COUNT(*) as backup_count,
  141. SUM(records_count) as total_records,
  142. SUM(content_size) as total_size,
  143. AVG(records_count) as avg_records,
  144. AVG(content_size) as avg_size
  145. ')
  146. ->groupBy('table_name')
  147. ->orderBy('total_size', 'desc')
  148. ->get()
  149. ->toArray();
  150. }
  151. }