CleanupSqlBackup.php 4.2 KB

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