CleanupLog.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Module\Cleanup\Models;
  3. use App\Module\Cleanup\Enums\CLEANUP_TYPE;
  4. use UCore\ModelCore;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. /**
  7. * 清理日志模型
  8. *
  9. * 记录每个表的清理操作详情
  10. */
  11. class CleanupLog extends ModelCore
  12. {
  13. /**
  14. * 数据表名
  15. */
  16. protected $table = 'cleanup_logs';
  17. // field start
  18. /**
  19. * 可批量赋值的字段
  20. */
  21. protected $fillable = [
  22. 'task_id',
  23. 'table_name',
  24. 'cleanup_type',
  25. 'before_count',
  26. 'after_count',
  27. 'deleted_records',
  28. 'execution_time',
  29. 'conditions',
  30. 'error_message',
  31. ];
  32. // field end
  33. /**
  34. * 字段类型转换
  35. */
  36. protected $casts = [
  37. 'task_id' => 'integer',
  38. 'cleanup_type' => 'integer',
  39. 'before_count' => 'integer',
  40. 'after_count' => 'integer',
  41. 'deleted_records' => 'integer',
  42. 'execution_time' => 'float',
  43. 'conditions' => 'array',
  44. 'created_at' => 'datetime',
  45. 'updated_at' => 'datetime',
  46. ];
  47. /**
  48. * 获取清理类型枚举
  49. */
  50. public function getCleanupTypeEnumAttribute(): CLEANUP_TYPE
  51. {
  52. return CLEANUP_TYPE::from($this->cleanup_type);
  53. }
  54. /**
  55. * 关联任务
  56. */
  57. public function task(): BelongsTo
  58. {
  59. return $this->belongsTo(CleanupTask::class, 'task_id');
  60. }
  61. /**
  62. * 获取成功的日志
  63. */
  64. public function scopeSuccessful($query)
  65. {
  66. return $query->whereNull('error_message');
  67. }
  68. /**
  69. * 获取失败的日志
  70. */
  71. public function scopeFailed($query)
  72. {
  73. return $query->whereNotNull('error_message');
  74. }
  75. /**
  76. * 按表名筛选
  77. */
  78. public function scopeByTable($query, string $tableName)
  79. {
  80. return $query->where('table_name', $tableName);
  81. }
  82. /**
  83. * 按清理类型筛选
  84. */
  85. public function scopeByCleanupType($query, CLEANUP_TYPE $cleanupType)
  86. {
  87. return $query->where('cleanup_type', $cleanupType->value);
  88. }
  89. }