| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- namespace App\Module\Cleanup\Models;
- use App\Module\Cleanup\Enums\BACKUP_TYPE;
- use App\Module\Cleanup\Enums\COMPRESSION_TYPE;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- /**
- * 清理备份文件模型
- *
- * 记录备份文件的详细信息
- */
- class CleanupBackupFile extends ModelCore
- {
- /**
- * 数据表名
- */
- protected $table = 'cleanup_backup_files';
- // field start
- /**
- * 可批量赋值的字段
- */
- protected $fillable = [
- 'backup_id',
- 'table_name',
- 'file_name',
- 'file_path',
- 'file_size',
- 'file_hash',
- 'backup_type',
- 'compression_type',
- ];
- // field end
- /**
- * 字段类型转换
- */
- protected $casts = [
- 'backup_id' => 'integer',
- 'file_size' => 'integer',
- 'backup_type' => 'integer',
- 'compression_type' => 'integer',
- 'created_at' => 'datetime',
- 'updated_at' => 'datetime',
- ];
- /**
- * 获取备份类型枚举
- */
- public function getBackupTypeEnumAttribute(): BACKUP_TYPE
- {
- return BACKUP_TYPE::from($this->backup_type);
- }
- /**
- * 获取压缩类型枚举
- */
- public function getCompressionTypeEnumAttribute(): COMPRESSION_TYPE
- {
- return COMPRESSION_TYPE::from($this->compression_type);
- }
- /**
- * 关联备份记录
- */
- public function backup(): BelongsTo
- {
- return $this->belongsTo(CleanupBackup::class, 'backup_id');
- }
- /**
- * 按表名筛选
- */
- public function scopeByTable($query, string $tableName)
- {
- return $query->where('table_name', $tableName);
- }
- /**
- * 按备份类型筛选
- */
- public function scopeByBackupType($query, BACKUP_TYPE $backupType)
- {
- return $query->where('backup_type', $backupType->value);
- }
- /**
- * 按压缩类型筛选
- */
- public function scopeByCompressionType($query, COMPRESSION_TYPE $compressionType)
- {
- return $query->where('compression_type', $compressionType->value);
- }
- /**
- * 获取文件大小的可读格式
- */
- public function getFileSizeHumanAttribute(): string
- {
- $bytes = $this->file_size;
- $units = ['B', 'KB', 'MB', 'GB', 'TB'];
-
- for ($i = 0; $bytes > 1024 && $i < count($units) - 1; $i++) {
- $bytes /= 1024;
- }
-
- return round($bytes, 2) . ' ' . $units[$i];
- }
- }
|