TaskUserTask.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Module\Task\Models;
  3. use UCore\ModelCore;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use Illuminate\Database\Eloquent\Relations\HasMany;
  6. /**
  7. * 用户任务关联模型
  8. *
  9. * field start
  10. * @property int $id 主键
  11. * @property int $user_id 用户ID
  12. * @property int $task_id 任务ID,外键关联task_tasks表
  13. * @property int $status 状态(0:未接取, 1:进行中, 2:已完成, 3:已领取奖励, 4:已失败, 5:已过期)
  14. * @property int $progress 当前进度
  15. * @property string $completed_at 完成时间
  16. * @property string $rewarded_at 奖励发放时间
  17. * @property string $expire_at 过期时间
  18. * @property \Carbon\Carbon $created_at 创建时间
  19. * @property \Carbon\Carbon $updated_at 更新时间
  20. * @property string $next_reset_time 下次重置时间,用于快速判断是否需要重置
  21. * @property string $last_reset_time 上次重置时间,用于计算下次重置时间
  22. * @property int $reset_count 重置次数,用于统计和分析
  23. * field end
  24. */
  25. class TaskUserTask extends ModelCore
  26. {
  27. /**
  28. * 与模型关联的表名
  29. *
  30. * @var string
  31. */
  32. protected $table = 'task_user_tasks';
  33. /**
  34. * 主键
  35. *
  36. * @var string
  37. */
  38. protected $primaryKey = 'id';
  39. /**
  40. * 应该被转换为日期的属性
  41. *
  42. * @var array
  43. */
  44. protected $dates = [
  45. 'completed_at',
  46. 'rewarded_at',
  47. 'expire_at',
  48. 'created_at',
  49. 'updated_at',
  50. ];
  51. // attrlist start
  52. protected $fillable = [
  53. 'id',
  54. 'user_id',
  55. 'task_id',
  56. 'status',
  57. 'progress',
  58. 'completed_at',
  59. 'rewarded_at',
  60. 'expire_at',
  61. 'next_reset_time',
  62. 'last_reset_time',
  63. 'reset_count',
  64. ];
  65. // attrlist end
  66. /**
  67. * 获取关联的任务
  68. *
  69. * @return BelongsTo
  70. */
  71. public function task(): BelongsTo
  72. {
  73. return $this->belongsTo(Task::class, 'task_id', 'id');
  74. }
  75. /**
  76. * 获取用户任务进度
  77. *
  78. * @return HasMany
  79. */
  80. public function progress(): HasMany
  81. {
  82. return $this->hasMany(TaskUserProgress::class, 'task_id', 'task_id')
  83. ->where('user_id', $this->user_id);
  84. }
  85. /**
  86. * 获取任务完成日志
  87. *
  88. * @return HasMany
  89. */
  90. public function completionLogs(): HasMany
  91. {
  92. return $this->hasMany(TaskCompletionLog::class, 'user_id', 'user_id')
  93. ->where('task_id', $this->task_id);
  94. }
  95. /**
  96. * 获取任务奖励日志
  97. *
  98. * @return HasMany
  99. */
  100. public function rewardLogs(): HasMany
  101. {
  102. return $this->hasMany(TaskRewardLog::class, 'user_task_id', 'id');
  103. }
  104. /**
  105. * 获取任务消耗日志
  106. *
  107. * @return HasMany
  108. */
  109. public function costLogs(): HasMany
  110. {
  111. return $this->hasMany(TaskCostLog::class, 'user_id', 'user_id')
  112. ->where('task_id', $this->task_id);
  113. }
  114. }