| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace App\Module\Farm\Logics;
- use App\Module\Farm\Models\FarmTeamProfit;
- use Illuminate\Database\Eloquent\Collection;
- use UCore\Helper\Logger;
- /**
- * 团队收益记录逻辑
- *
- * 提供团队收益记录数据的业务逻辑处理。
- */
- class TeamProfitLogic
- {
- /**
- * 获取用户的团队收益记录
- *
- * @param int $userId
- * @param int $limit
- * @return Collection
- */
- public function findByUserId(int $userId, int $limit = 100): Collection
- {
- try {
- return FarmTeamProfit::where('user_id', $userId)
- ->orderByDesc('created_at')
- ->limit($limit)
- ->get();
- } catch (\Exception $e) {
- Logger::exception('获取用户团队收益记录失败', $e, [
- 'user_id' => $userId,
- ]);
- return collect();
- }
- }
- /**
- * 获取团队成员产生的收益记录
- *
- * @param int $teamMemberId
- * @param int $limit
- * @return Collection
- */
- public function findByTeamMemberId(int $teamMemberId, int $limit = 100): Collection
- {
- try {
- return FarmTeamProfit::where('team_member_id', $teamMemberId)
- ->orderByDesc('created_at')
- ->limit($limit)
- ->get();
- } catch (\Exception $e) {
- Logger::exception('获取团队成员收益记录失败', $e, [
- 'team_member_id' => $teamMemberId,
- ]);
-
- return collect();
- }
- }
- /**
- * 获取指定收获记录产生的团队收益
- *
- * @param int $harvestId
- * @return Collection
- */
- public function findByHarvestId(int $harvestId): Collection
- {
- try {
- return FarmTeamProfit::where('harvest_id', $harvestId)
- ->orderByDesc('created_at')
- ->get();
- } catch (\Exception $e) {
- Logger::exception('获取收获记录团队收益失败', $e, [
- 'harvest_id' => $harvestId,
- ]);
- return collect();
- }
- }
- /**
- * 获取指定时间段内的团队收益记录
- *
- * @param string $startTime
- * @param string $endTime
- * @return Collection
- */
- public function findByTimeRange(string $startTime, string $endTime): Collection
- {
- try {
- return FarmTeamProfit::whereBetween('created_at', [$startTime, $endTime])
- ->orderByDesc('created_at')
- ->get();
- } catch (\Exception $e) {
- Logger::exception('获取时间段团队收益记录失败', $e, [
- 'start_time' => $startTime,
- 'end_time' => $endTime,
- ]);
-
- return collect();
- }
- }
- /**
- * 获取用户指定时间段内的团队收益记录
- *
- * @param int $userId
- * @param string $startTime
- * @param string $endTime
- * @return Collection
- */
- public function findByUserIdAndTimeRange(int $userId, string $startTime, string $endTime): Collection
- {
- try {
- return FarmTeamProfit::where('user_id', $userId)
- ->whereBetween('created_at', [$startTime, $endTime])
- ->orderByDesc('created_at')
- ->get();
- } catch (\Exception $e) {
- Logger::exception('获取用户时间段团队收益记录失败', $e, [
- 'user_id' => $userId,
- 'start_time' => $startTime,
- 'end_time' => $endTime,
- ]);
-
- return collect();
- }
- }
- /**
- * 清理过期的团队收益记录
- *
- * @param int $days 保留天数
- * @return int
- */
- public function cleanupOldLogs(int $days = 90): int
- {
- try {
- $date = now()->subDays($days);
- return FarmTeamProfit::where('created_at', '<', $date)->delete();
- } catch (\Exception $e) {
- Logger::exception('清理过期团队收益记录失败', $e, [
- 'days' => $days,
- ]);
-
- return 0;
- }
- }
- }
|