|
|
@@ -1,103 +0,0 @@
|
|
|
-<?php
|
|
|
-
|
|
|
-namespace App\Module\Farm\Listeners;
|
|
|
-
|
|
|
-use App\Module\Farm\Events\CropHarvestedEvent;
|
|
|
-use App\Module\Farm\Events\TeamProfitCreatedEvent;
|
|
|
-use App\Module\Farm\Models\FarmTeamProfit;
|
|
|
-use App\Module\Farm\Models\FarmUserReferral;
|
|
|
-use App\Module\Team\Services\TalentService;
|
|
|
-
|
|
|
-
|
|
|
-use Illuminate\Support\Facades\Log;
|
|
|
-
|
|
|
-/**
|
|
|
- * 分配团队收益监听器
|
|
|
- *
|
|
|
- * 监听作物收获事件,计算并分配团队收益
|
|
|
- */
|
|
|
-class DistributeTeamProfitListener
|
|
|
-{
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理事件
|
|
|
- *
|
|
|
- * @param CropHarvestedEvent $event
|
|
|
- * @return void
|
|
|
- */
|
|
|
- public function handle(CropHarvestedEvent $event)
|
|
|
- {
|
|
|
- try {
|
|
|
- $userId = $event->userId;
|
|
|
- $harvestLog = $event->harvestLog;
|
|
|
- $outputAmount = $harvestLog->output_amount;
|
|
|
-
|
|
|
- // 查找用户的推荐人
|
|
|
- $referrals = FarmUserReferral::where('user_id', $userId)
|
|
|
- ->orderBy('level')
|
|
|
- ->get();
|
|
|
-
|
|
|
- if ($referrals->isEmpty()) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- foreach ($referrals as $referral) {
|
|
|
- $referrerId = $referral->referrer_id;
|
|
|
- $level = $referral->level;
|
|
|
-
|
|
|
- // 从Team模块获取推荐人的达人等级信息
|
|
|
- $talentInfo = TalentService::getUserTalentInfo($referrerId);
|
|
|
-
|
|
|
- if (!$talentInfo || $talentInfo['talent_level'] === 0) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- // 获取达人等级对应的分成比例
|
|
|
- $profitRate = $talentInfo['profit_rate'];
|
|
|
-
|
|
|
- // 根据推荐层级调整分成比例
|
|
|
- if ($level === 2) { // 间推
|
|
|
- $profitRate = $profitRate * 0.5; // 间推只有直推的一半分成
|
|
|
- }
|
|
|
-
|
|
|
- // 计算分成收益
|
|
|
- $profitAmount = (int)($outputAmount * $profitRate);
|
|
|
-
|
|
|
- if ($profitAmount <= 0) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- // 创建团队收益记录
|
|
|
- $teamProfit = new FarmTeamProfit();
|
|
|
- $teamProfit->user_id = $referrerId;
|
|
|
- $teamProfit->team_member_id = $userId;
|
|
|
- $teamProfit->harvest_id = $harvestLog->id;
|
|
|
- $teamProfit->profit_amount = $profitAmount;
|
|
|
- $teamProfit->profit_rate = $profitRate;
|
|
|
- $teamProfit->created_at = now();
|
|
|
- $teamProfit->save();
|
|
|
-
|
|
|
- // 触发团队收益创建事件
|
|
|
- event(new TeamProfitCreatedEvent($referrerId, $userId, $harvestLog, $teamProfit));
|
|
|
-
|
|
|
- Log::info('团队收益分配成功', [
|
|
|
- 'user_id' => $referrerId,
|
|
|
- 'team_member_id' => $userId,
|
|
|
- 'harvest_id' => $harvestLog->id,
|
|
|
- 'profit_amount' => $profitAmount,
|
|
|
- 'profit_rate' => $profitRate,
|
|
|
- 'level' => $level,
|
|
|
- 'talent_level' => $talentInfo['talent_level']
|
|
|
- ]);
|
|
|
- }
|
|
|
- } catch (\Exception $e) {
|
|
|
- Log::error('团队收益分配失败', [
|
|
|
- 'user_id' => $event->userId,
|
|
|
- 'harvest_id' => $event->harvestLog->id ?? null,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
- ]);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|