| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace App\Module\Farm\Logics;
- use App\Module\Farm\Models\FarmTeamProfit;
- use App\Module\Farm\Models\FarmUserReferral;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use App\Module\Team\Services\TalentService;
- use App\Module\Team\Services\ReferralService;
- use UCore\Db\Helper;
- /**
- * 团队管理逻辑
- */
- class TeamLogic
- {
- /**
- * 添加推荐关系
- *
- * @param int $userId
- * @param int $referrerId
- * @return bool
- */
- public function addReferral(int $userId, int $referrerId): bool
- {
- try {
- Helper::check_tr();
- // 检查用户ID和推荐人ID是否相同
- if ($userId === $referrerId) {
- throw new \Exception('用户不能推荐自己');
- }
- // 检查是否已存在推荐关系
- $existingReferral = FarmUserReferral::where('user_id', $userId)
- ->where('referrer_id', $referrerId)
- ->first();
- if ($existingReferral) {
- return true; // 已存在推荐关系,直接返回成功
- }
- TalentService::checkAndUpdateUserTalentLevel($referrerId);
- Log::info('添加推荐关系成功', [
- 'user_id' => $userId,
- 'referrer_id' => $referrerId,
- 'indirect_referrer_id' => $indirectReferrerId ?? null
- ]);
- return true;
- } catch (\Exception $e) {
- Log::error('添加推荐关系失败', [
- 'user_id' => $userId,
- 'referrer_id' => $referrerId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return false;
- }
- }
- /**
- * 获取用户的团队收益记录
- *
- * @param int $userId
- * @param int $limit
- * @return Collection
- */
- public function getTeamProfits(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,
- 'limit' => $limit,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return collect();
- }
- }
- }
|