TaskCompletionLogRepository.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Module\Task\Repositorys;
  3. use App\Module\Task\Models\TaskCompletionLog;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. /**
  6. * 任务完成日志数据仓库类
  7. *
  8. * 提供任务完成日志数据的访问和操作功能。
  9. * 该类是任务完成日志模块与后台管理系统的桥梁,用于处理任务完成日志数据的CRUD操作。
  10. */
  11. class TaskCompletionLogRepository extends EloquentRepository
  12. {
  13. /**
  14. * 关联的Eloquent模型类
  15. *
  16. * @var string
  17. */
  18. protected $eloquentClass = TaskCompletionLog::class;
  19. /**
  20. * 获取用户的任务完成日志
  21. *
  22. * @param int $userId 用户ID
  23. * @param int|null $taskId 任务ID
  24. * @param int $limit 限制数量
  25. * @return array 任务完成日志列表
  26. */
  27. public function getUserCompletionLogs(int $userId, ?int $taskId = null, int $limit = 100): array
  28. {
  29. $query = $this->eloquentClass::where('user_id', $userId);
  30. if ($taskId) {
  31. $query->where('task_id', $taskId);
  32. }
  33. return $query->orderBy('completed_at', 'desc')
  34. ->limit($limit)
  35. ->get()
  36. ->toArray();
  37. }
  38. /**
  39. * 获取特定时间段内的任务完成日志
  40. *
  41. * @param string $startDate 开始日期
  42. * @param string $endDate 结束日期
  43. * @return array 任务完成日志列表
  44. */
  45. public function getCompletionLogsByDateRange(string $startDate, string $endDate): array
  46. {
  47. return $this->eloquentClass::whereBetween('completed_at', [$startDate, $endDate])
  48. ->orderBy('completed_at', 'desc')
  49. ->get()
  50. ->toArray();
  51. }
  52. }