eloquentClass::where('user_id', $userId); if ($status !== null) { $query->where('status', $status); } return $query->with(['task', 'task.category']) ->get() ->toArray(); } /** * 获取用户的特定任务 * * @param int $userId 用户ID * @param int $taskId 任务ID * @return TaskUserTask|null 用户任务对象 */ public function getUserTask(int $userId, int $taskId): ?TaskUserTask { return $this->eloquentClass::where('user_id', $userId) ->where('task_id', $taskId) ->first(); } /** * 获取即将过期的任务 * * @param int $hoursThreshold 过期时间阈值(小时) * @return array 即将过期的任务列表 */ public function getExpiringTasks(int $hoursThreshold = 24): array { $expiryTime = now()->addHours($hoursThreshold); return $this->eloquentClass::where('status', 1) // 进行中 ->whereNotNull('expire_at') ->where('expire_at', '<=', $expiryTime) ->where('expire_at', '>', now()) ->with(['task']) ->get() ->toArray(); } }