| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace App\Module\Cleanup\Models;
- use App\Module\Cleanup\Enums\CLEANUP_TYPE;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- /**
- * 清理日志模型
- *
- * 记录每个表的清理操作详情
- */
- class CleanupLog extends ModelCore
- {
- /**
- * 数据表名
- */
- protected $table = 'cleanup_logs';
- // field start
- /**
- * 可批量赋值的字段
- */
- protected $fillable = [
- 'task_id',
- 'table_name',
- 'cleanup_type',
- 'before_count',
- 'after_count',
- 'deleted_records',
- 'execution_time',
- 'conditions',
- 'error_message',
- ];
- // field end
- /**
- * 字段类型转换
- */
- protected $casts = [
- 'task_id' => 'integer',
- 'cleanup_type' => 'integer',
- 'before_count' => 'integer',
- 'after_count' => 'integer',
- 'deleted_records' => 'integer',
- 'execution_time' => 'float',
- 'conditions' => 'array',
- 'created_at' => 'datetime',
- 'updated_at' => 'datetime',
- ];
- /**
- * 获取清理类型枚举
- */
- public function getCleanupTypeEnumAttribute(): CLEANUP_TYPE
- {
- return CLEANUP_TYPE::from($this->cleanup_type);
- }
- /**
- * 关联任务
- */
- public function task(): BelongsTo
- {
- return $this->belongsTo(CleanupTask::class, 'task_id');
- }
- /**
- * 获取成功的日志
- */
- public function scopeSuccessful($query)
- {
- return $query->whereNull('error_message');
- }
- /**
- * 获取失败的日志
- */
- public function scopeFailed($query)
- {
- return $query->whereNotNull('error_message');
- }
- /**
- * 按表名筛选
- */
- public function scopeByTable($query, string $tableName)
- {
- return $query->where('table_name', $tableName);
- }
- /**
- * 按清理类型筛选
- */
- public function scopeByCleanupType($query, CLEANUP_TYPE $cleanupType)
- {
- return $query->where('cleanup_type', $cleanupType->value);
- }
- }
|