فهرست منبع

发放奖励,错误修复

Your Name 5 ماه پیش
والد
کامیت
cd5c8394b9
1فایلهای تغییر یافته به همراه71 افزوده شده و 55 حذف شده
  1. 71 55
      app/Module/UrsPromotion/Logics/UrsProfitLogic.php

+ 71 - 55
app/Module/UrsPromotion/Logics/UrsProfitLogic.php

@@ -14,6 +14,7 @@ use App\Module\UrsPromotion\Enums\UrsPromotionRelationLevel;
 use App\Module\Game\Services\RewardService;
 use App\Module\GameItems\Services\ItemService;
 
+use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
 
 /**
@@ -32,10 +33,11 @@ class UrsProfitLogic
      * @return array 分成记录
      */
     public function distributePromotionReward(
-        int $userId,
+        int    $userId,
         string $sourceType,
-        int $sourceId
-    ): array {
+        int    $sourceId
+    ): array
+    {
         $profits = [];
 
         try {
@@ -52,14 +54,28 @@ class UrsProfitLogic
 
             // 为每一级推荐人发放奖励
             foreach ($referralChain as $level => $referrerId) {
-                $profit = $this->calculatePromotionReward(
-                    $referrerId,
-                    $userId,
-                    $sourceType,
-                    $sourceId,
-                    $level,
-                    $talentConfigs
-                );
+                DB::beginTransaction();
+                try {
+                    $profit = $this->calculatePromotionReward(
+                        $referrerId,
+                        $userId,
+                        $sourceType,
+                        $sourceId,
+                        $level,
+                        $talentConfigs
+                    );
+                    DB::commit();
+                } catch (\Exception $exception) {
+                    DB::rollBack();
+                    Log::error("用户 {$userId} 推广收益分失败", [
+                        'referrerId' => $referrerId,
+                        'userId' => $userId,
+                        'sourceType' => $sourceType,
+                        'sourceId' => $sourceId,
+                        'level' => $level
+                    ]);
+                }
+
 
                 if ($profit) {
                     $profits[] = $profit;
@@ -96,12 +112,13 @@ class UrsProfitLogic
      * @return array 分成记录
      */
     public function distributePlantingReward(
-        int $userId,
+        int    $userId,
         string $sourceType,
-        int $sourceId,
-        int $originalAmount,
-        int $itemId
-    ): array {
+        int    $sourceId,
+        int    $originalAmount,
+        int    $itemId
+    ): array
+    {
         $profits = [];
 
         try {
@@ -273,13 +290,14 @@ class UrsProfitLogic
      * @return UrsProfit|null
      */
     private function calculatePromotionReward(
-        int $referrerId,
-        int $memberId,
+        int    $referrerId,
+        int    $memberId,
         string $sourceType,
-        int $sourceId,
-        int $relationLevel,
-        array $talentConfigs
-    ): ?UrsProfit {
+        int    $sourceId,
+        int    $relationLevel,
+        array  $talentConfigs
+    ): ?UrsProfit
+    {
         // 获取推荐人的达人等级
         $talent = UrsUserTalent::where('user_id', $referrerId)->first();
         $talentLevel = $talent ? $talent->talent_level : 0;
@@ -384,15 +402,16 @@ class UrsProfitLogic
      * @return UrsProfit|null
      */
     private function calculatePlantingReward(
-        int $referrerId,
-        int $memberId,
+        int    $referrerId,
+        int    $memberId,
         string $sourceType,
-        int $sourceId,
-        int $relationLevel,
-        int $originalAmount,
-        array $talentConfigs,
-        int $itemId
-    ): ?UrsProfit {
+        int    $sourceId,
+        int    $relationLevel,
+        int    $originalAmount,
+        array  $talentConfigs,
+        int    $itemId
+    ): ?UrsProfit
+    {
         // 获取推荐人的达人等级
         $talent = UrsUserTalent::where('user_id', $referrerId)->first();
         $talentLevel = $talent ? $talent->talent_level : 0;
@@ -422,21 +441,21 @@ class UrsProfitLogic
 
             // 创建收益记录
             $profit = UrsProfit::create([
-                                            'urs_user_id' => 0,
-                                            'urs_promotion_member_id' => 0,
-                                            'promotion_member_farm_user_id' => $referrerId,
-                                            'farm_user_id' => $memberId,
-                                            'source_id' => $sourceId,
-                                            'source_type' => $sourceType,
-                                            'profit_type' => UrsProfitType::PLANTING_REWARD->value,
-                                            'relation_level' => $relationLevel,
-                                            'original_amount' => (string)$originalAmount,
-                                            'profit_amount' => (string)$rewardQuantity, // 记录实际发放的物品数量
-                                            'profit_rate' => $profitRate,
-                                            'reward_group_id' => null, // 种植收益不使用奖励组
-                                            'talent_level' => $talentLevel,
-                                            'status' => UrsProfit::STATUS_NORMAL,
-                                        ]);
+                'urs_user_id' => 0,
+                'urs_promotion_member_id' => 0,
+                'promotion_member_farm_user_id' => $referrerId,
+                'farm_user_id' => $memberId,
+                'source_id' => $sourceId,
+                'source_type' => $sourceType,
+                'profit_type' => UrsProfitType::PLANTING_REWARD->value,
+                'relation_level' => $relationLevel,
+                'original_amount' => (string)$originalAmount,
+                'profit_amount' => (string)$rewardQuantity, // 记录实际发放的物品数量
+                'profit_rate' => $profitRate,
+                'reward_group_id' => null, // 种植收益不使用奖励组
+                'talent_level' => $talentLevel,
+                'status' => UrsProfit::STATUS_NORMAL,
+            ]);
             return null;
         }
 
@@ -496,7 +515,6 @@ class UrsProfitLogic
             ]);
 
 
-
         } catch (\Exception $e) {
 
             Log::error("种植收益发放事务失败", [
@@ -525,7 +543,6 @@ class UrsProfitLogic
     }
 
 
-
     /**
      * 计算奖励总金额(用于记录)
      *
@@ -540,15 +557,13 @@ class UrsProfitLogic
             // 计算货币类型的奖励金额
             if (in_array($item->rewardType, ['fund', 'currency', 'fund_config'])) {
                 $totalAmount = bcadd($totalAmount, (string)$item->quantity, 10);
-            }
-            // 计算物品类型的奖励价值(按物品售价计算,如果没有售价则按数量计算)
+            } // 计算物品类型的奖励价值(按物品售价计算,如果没有售价则按数量计算)
             elseif ($item->rewardType === 'item') {
                 // 获取物品售价作为价值参考
                 $itemValue = $this->getItemValue($item->targetId);
                 $itemTotalValue = bcmul((string)$itemValue, (string)$item->quantity, 10);
                 $totalAmount = bcadd($totalAmount, $itemTotalValue, 10);
-            }
-            // 其他类型奖励按数量计算基础价值
+            } // 其他类型奖励按数量计算基础价值
             else {
                 $totalAmount = bcadd($totalAmount, (string)$item->quantity, 10);
             }
@@ -639,11 +654,12 @@ class UrsProfitLogic
      * @return array
      */
     public function getUserProfitStats(
-        int $userId,
+        int            $userId,
         ?UrsProfitType $profitType = null,
-        ?string $startDate = null,
-        ?string $endDate = null
-    ): array {
+        ?string        $startDate = null,
+        ?string        $endDate = null
+    ): array
+    {
         $query = UrsProfit::where('urs_user_id', $userId)
             ->where('status', UrsProfit::STATUS_NORMAL);