CleanupLog.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * @property int $id 主键ID
  19. * @property int $task_id 任务ID
  20. * @property string $table_name 表名
  21. * @property int $cleanup_type 清理类型:1清空表,2删除所有,3按时间删除,4按用户删除,5按条件删除
  22. * @property int $before_count 清理前记录数
  23. * @property int $after_count 清理后记录数
  24. * @property int $deleted_records 删除记录数
  25. * @property float $execution_time 执行时间(秒)
  26. * @property array $conditions 使用的清理条件
  27. * @property string $error_message 错误信息
  28. * @property \Carbon\Carbon $created_at 创建时间
  29. * field end
  30. /**
  31. * 字段类型转换
  32. */
  33. protected $casts = [
  34. 'task_id' => 'integer',
  35. 'cleanup_type' => 'integer',
  36. 'before_count' => 'integer',
  37. 'after_count' => 'integer',
  38. 'deleted_records' => 'integer',
  39. 'execution_time' => 'float',
  40. 'conditions' => 'array',
  41. 'created_at' => 'datetime',
  42. 'updated_at' => 'datetime',
  43. ];
  44. /**
  45. * 获取清理类型枚举
  46. */
  47. public function getCleanupTypeEnumAttribute(): CLEANUP_TYPE
  48. {
  49. return CLEANUP_TYPE::from($this->cleanup_type);
  50. }
  51. /**
  52. * 关联任务
  53. */
  54. public function task(): BelongsTo
  55. {
  56. return $this->belongsTo(CleanupTask::class, 'task_id');
  57. }
  58. /**
  59. * 获取成功的日志
  60. */
  61. public function scopeSuccessful($query)
  62. {
  63. return $query->whereNull('error_message');
  64. }
  65. /**
  66. * 获取失败的日志
  67. */
  68. public function scopeFailed($query)
  69. {
  70. return $query->whereNotNull('error_message');
  71. }
  72. /**
  73. * 按表名筛选
  74. */
  75. public function scopeByTable($query, string $tableName)
  76. {
  77. return $query->where('table_name', $tableName);
  78. }
  79. /**
  80. * 按清理类型筛选
  81. */
  82. public function scopeByCleanupType($query, CLEANUP_TYPE $cleanupType)
  83. {
  84. return $query->where('cleanup_type', $cleanupType->value);
  85. }
  86. }