TaskRewardLogRepository.php 1.8 KB

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