| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?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
- /**
- * @property int $id 主键ID
- * @property int $backup_id 备份记录ID
- * @property string $table_name 表名
- * @property string $file_name 文件名
- * @property string $file_path 文件路径
- * @property int $file_size 文件大小(字节)
- * @property string $file_hash 文件SHA256哈希
- * @property int $backup_type 备份类型:1SQL,2JSON,3CSV
- * @property int $compression_type 压缩类型:1none,2gzip,3zip
- * @property \Carbon\Carbon $created_at 创建时间
- */
- // 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];
- }
- }
|