|
|
@@ -0,0 +1,163 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Module\Farm\Logics;
|
|
|
+
|
|
|
+use App\Module\Farm\Models\FarmTeamProfit;
|
|
|
+use Illuminate\Database\Eloquent\Collection;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 团队收益记录逻辑
|
|
|
+ *
|
|
|
+ * 提供团队收益记录数据的业务逻辑处理。
|
|
|
+ */
|
|
|
+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) {
|
|
|
+ Log::error('获取用户团队收益记录失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ 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) {
|
|
|
+ Log::error('获取团队成员收益记录失败', [
|
|
|
+ 'team_member_id' => $teamMemberId,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ 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) {
|
|
|
+ Log::error('获取收获记录团队收益失败', [
|
|
|
+ 'harvest_id' => $harvestId,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ 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) {
|
|
|
+ Log::error('获取时间段团队收益记录失败', [
|
|
|
+ 'start_time' => $startTime,
|
|
|
+ 'end_time' => $endTime,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ 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) {
|
|
|
+ Log::error('获取用户时间段团队收益记录失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'start_time' => $startTime,
|
|
|
+ 'end_time' => $endTime,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ 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) {
|
|
|
+ Log::error('清理过期团队收益记录失败', [
|
|
|
+ 'days' => $days,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|