| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace App\Module\Task\Repositorys;
- use App\Module\Task\Models\TaskCostLog;
- use Dcat\Admin\Repositories\EloquentRepository;
- /**
- * 任务消耗日志数据仓库类
- *
- * 提供任务消耗日志数据的访问和操作功能。
- * 该类是任务消耗日志模块与后台管理系统的桥梁,用于处理任务消耗日志数据的CRUD操作。
- */
- class TaskCostLogRepository extends EloquentRepository
- {
- /**
- * 关联的Eloquent模型类
- *
- * @var string
- */
- protected $eloquentClass = TaskCostLog::class;
-
- /**
- * 获取用户的任务消耗日志
- *
- * @param int $userId 用户ID
- * @param int|null $taskId 任务ID
- * @param int $limit 限制数量
- * @return array 任务消耗日志列表
- */
- public function getUserCostLogs(int $userId, ?int $taskId = null, int $limit = 100): array
- {
- $query = $this->eloquentClass::where('user_id', $userId);
-
- if ($taskId) {
- $query->where('task_id', $taskId);
- }
-
- return $query->orderBy('cost_at', 'desc')
- ->limit($limit)
- ->get()
- ->toArray();
- }
-
- /**
- * 获取特定时间段内的任务消耗日志
- *
- * @param string $startDate 开始日期
- * @param string $endDate 结束日期
- * @return array 任务消耗日志列表
- */
- public function getCostLogsByDateRange(string $startDate, string $endDate): array
- {
- return $this->eloquentClass::whereBetween('cost_at', [$startDate, $endDate])
- ->orderBy('cost_at', 'desc')
- ->get()
- ->toArray();
- }
-
- /**
- * 获取特定消耗类型的任务消耗日志
- *
- * @param string $costType 消耗类型
- * @param int $limit 限制数量
- * @return array 任务消耗日志列表
- */
- public function getCostLogsByType(string $costType, int $limit = 100): array
- {
- return $this->eloquentClass::where('cost_type', $costType)
- ->orderBy('cost_at', 'desc')
- ->limit($limit)
- ->get()
- ->toArray();
- }
- }
|