TaskUserTask.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 string $next_reset_time 下次重置时间,用于快速判断是否需要重置
  19. * @property string $last_reset_time 上次重置时间,用于计算下次重置时间
  20. * @property int $reset_count 重置次数,用于统计和分析
  21. * @property \Carbon\Carbon $created_at 创建时间
  22. * @property \Carbon\Carbon $updated_at 更新时间
  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. 'next_reset_time',
  49. 'last_reset_time',
  50. 'created_at',
  51. 'updated_at',
  52. ];
  53. // attrlist start
  54. protected $fillable = [
  55. 'id',
  56. 'user_id',
  57. 'task_id',
  58. 'status',
  59. 'progress',
  60. 'completed_at',
  61. 'rewarded_at',
  62. 'expire_at',
  63. 'next_reset_time',
  64. 'last_reset_time',
  65. 'reset_count',
  66. ];
  67. // attrlist end
  68. /**
  69. * 获取关联的任务
  70. *
  71. * @return BelongsTo
  72. */
  73. public function task(): BelongsTo
  74. {
  75. return $this->belongsTo(Task::class, 'task_id', 'id');
  76. }
  77. /**
  78. * 获取用户任务进度
  79. *
  80. * @return HasMany
  81. */
  82. public function progress(): HasMany
  83. {
  84. return $this->hasMany(TaskUserProgress::class, 'task_id', 'task_id')
  85. ->where('user_id', $this->user_id);
  86. }
  87. /**
  88. * 获取任务完成日志
  89. *
  90. * @return HasMany
  91. */
  92. public function completionLogs(): HasMany
  93. {
  94. return $this->hasMany(TaskCompletionLog::class, 'user_id', 'user_id')
  95. ->where('task_id', $this->task_id);
  96. }
  97. /**
  98. * 获取任务奖励日志
  99. *
  100. * @return HasMany
  101. */
  102. public function rewardLogs(): HasMany
  103. {
  104. return $this->hasMany(TaskRewardLog::class, 'user_task_id', 'id');
  105. }
  106. /**
  107. * 获取任务消耗日志
  108. *
  109. * @return HasMany
  110. */
  111. public function costLogs(): HasMany
  112. {
  113. return $this->hasMany(TaskCostLog::class, 'user_id', 'user_id')
  114. ->where('task_id', $this->task_id);
  115. }
  116. }