TaskCostLogRepository.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Module\Task\Repositorys;
  3. use App\Module\Task\Models\TaskCostLog;
  4. use Dcat\Admin\Repositories\EloquentRepository;
  5. /**
  6. * 任务消耗日志数据仓库类
  7. *
  8. * 提供任务消耗日志数据的访问和操作功能。
  9. * 该类是任务消耗日志模块与后台管理系统的桥梁,用于处理任务消耗日志数据的CRUD操作。
  10. */
  11. class TaskCostLogRepository extends EloquentRepository
  12. {
  13. /**
  14. * 关联的Eloquent模型类
  15. *
  16. * @var string
  17. */
  18. protected $eloquentClass = TaskCostLog::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 getUserCostLogs(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('cost_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 getCostLogsByDateRange(string $startDate, string $endDate): array
  46. {
  47. return $this->eloquentClass::whereBetween('cost_at', [$startDate, $endDate])
  48. ->orderBy('cost_at', 'desc')
  49. ->get()
  50. ->toArray();
  51. }
  52. /**
  53. * 获取特定消耗类型的任务消耗日志
  54. *
  55. * @param string $costType 消耗类型
  56. * @param int $limit 限制数量
  57. * @return array 任务消耗日志列表
  58. */
  59. public function getCostLogsByType(string $costType, int $limit = 100): array
  60. {
  61. return $this->eloquentClass::where('cost_type', $costType)
  62. ->orderBy('cost_at', 'desc')
  63. ->limit($limit)
  64. ->get()
  65. ->toArray();
  66. }
  67. }