TaskUserTask.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. * field end
  21. */
  22. class TaskUserTask extends ModelCore
  23. {
  24. /**
  25. * 与模型关联的表名
  26. *
  27. * @var string
  28. */
  29. protected $table = 'task_user_tasks';
  30. /**
  31. * 主键
  32. *
  33. * @var string
  34. */
  35. protected $primaryKey = 'id';
  36. /**
  37. * 应该被转换为日期的属性
  38. *
  39. * @var array
  40. */
  41. protected $dates = [
  42. 'completed_at',
  43. 'rewarded_at',
  44. 'expire_at',
  45. 'created_at',
  46. 'updated_at',
  47. ];
  48. // attrlist start
  49. protected $fillable = [
  50. 'id',
  51. 'user_id',
  52. 'task_id',
  53. 'status',
  54. 'progress',
  55. 'completed_at',
  56. 'rewarded_at',
  57. 'expire_at',
  58. ];
  59. // attrlist end
  60. /**
  61. * 获取关联的任务
  62. *
  63. * @return BelongsTo
  64. */
  65. public function task(): BelongsTo
  66. {
  67. return $this->belongsTo(Task::class, 'task_id', 'id');
  68. }
  69. /**
  70. * 获取用户任务进度
  71. *
  72. * @return HasMany
  73. */
  74. public function progress(): HasMany
  75. {
  76. return $this->hasMany(TaskUserProgress::class, 'task_id', 'task_id')
  77. ->where('user_id', $this->user_id);
  78. }
  79. /**
  80. * 获取任务完成日志
  81. *
  82. * @return HasMany
  83. */
  84. public function completionLogs(): HasMany
  85. {
  86. return $this->hasMany(TaskCompletionLog::class, 'user_id', 'user_id')
  87. ->where('task_id', $this->task_id);
  88. }
  89. /**
  90. * 获取任务奖励日志
  91. *
  92. * @return HasMany
  93. */
  94. public function rewardLogs(): HasMany
  95. {
  96. return $this->hasMany(TaskRewardLog::class, 'user_task_id', 'id');
  97. }
  98. /**
  99. * 获取任务消耗日志
  100. *
  101. * @return HasMany
  102. */
  103. public function costLogs(): HasMany
  104. {
  105. return $this->hasMany(TaskCostLog::class, 'user_id', 'user_id')
  106. ->where('task_id', $this->task_id);
  107. }
  108. }