Ver Fonte

feat(urs-promotion): 补充推荐奖励发放逻辑并优化用户映射

- 在 UrsBackfillRewardLogic 中添加了创建收益记录的逻辑,包括正常发放和跳过的情况
- 修改了 UrsUserMapping::getFarmUserIdByUrsUserId 方法,使其返回 int 类型
- 优化了 UrsReferralService 和 UrsRewardDistributionService 中的相关逻辑
- 调整了事务处理方式,提高了代码的健壮性
notfff há 6 meses atrás
pai
commit
3278026dfc

+ 5 - 0
AiWork/WORK2.md

@@ -16,6 +16,11 @@ php artisan debug:reproduce-error 68991486
 请求  request_1749722768045
 cd /data/wwwroot/urs/kku_laravel
 
+php artisan debug:reproduce-error 68994830
+68994830 用户10004登陆
+
+php artisan debug:reproduce-error 68994846
+68994846  用户10003登陆
 
 Fund模块扩展,资金数储存 从bigint 改用 DECIMAL(30,10);依旧沿用 硬编码的精度设置,精度设置是为了处理php的运算精度,不同的币种采用的精度不同,与数据库精度兼容
 

+ 112 - 5
app/Module/UrsPromotion/Logics/UrsBackfillRewardLogic.php

@@ -2,6 +2,8 @@
 
 namespace App\Module\UrsPromotion\Logics;
 
+use App\Module\UrsPromotion\Enums\UrsProfitType;
+use App\Module\UrsPromotion\Models\UrsProfit;
 use App\Module\UrsPromotion\Services\UrsReferralService;
 use App\Module\UrsPromotion\Services\UrsTalentService;
 use App\Module\UrsPromotion\Models\UrsTalentConfig;
@@ -12,14 +14,14 @@ use Illuminate\Support\Facades\DB;
 
 /**
  * URS推荐奖励补发逻辑层
- * 
+ *
  * 处理用户首次进入农场时的推荐奖励补发核心逻辑
  */
 class UrsBackfillRewardLogic
 {
     /**
      * 为用户补发推荐奖励
-     * 
+     *
      * @param int $ursUserId URS用户ID
      * @param int $farmUserId 农场用户ID
      * @return array 补发结果
@@ -106,6 +108,41 @@ class UrsBackfillRewardLogic
                     'items_count' => count($directResult->items)
                 ];
                 $totalBackfilledCount += count($directResult->items);
+                // 创建收益记录
+                $profit = UrsProfit::create([
+                                                'urs_user_id' => $ursUserId,
+                                                'urs_promotion_member_id' => 0,
+                                                'promotion_member_farm_user_id' => 0,
+                                                'farm_user_id' => $farmUserId,
+                                                'source_id' => $directResult->sourceId,
+                                                'source_type' => REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value(),
+                                                'profit_type' => UrsProfitType::PLANTING_REWARD->value,
+                                                'relation_level' => 1,
+                                                'original_amount' => (string)0,
+                                                'profit_amount' => (string)0, // 记录实际发放的物品数量
+                                                'profit_rate' => 1,
+                                                'reward_group_id' =>  $rewardGroups['direct'], // 种植收益不使用奖励组
+                                                'talent_level' => $talentLevel,
+                                                'status' => UrsProfit::STATUS_NORMAL,
+                                            ]);
+            }else{
+                // 创建收益记录-跳过
+                $profit = UrsProfit::create([
+                                                'urs_user_id' => $ursUserId,
+                                                'urs_promotion_member_id' => 0,
+                                                'promotion_member_farm_user_id' => 0,
+                                                'farm_user_id' => $farmUserId,
+                                                'source_id' => 0,
+                                                'source_type' => REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value(),
+                                                'profit_type' => UrsProfitType::PLANTING_REWARD->value,
+                                                'relation_level' => 1,
+                                                'original_amount' => (string)0,
+                                                'profit_amount' => (string)0, // 记录实际发放的物品数量
+                                                'profit_rate' => 1,
+                                                'reward_group_id' =>  $rewardGroups['direct'], // 种植收益不使用奖励组
+                                                'talent_level' => $talentLevel,
+                                                'status' => UrsProfit::STATUS_SKIPPED,
+                                            ]);
             }
 
             // 间推奖励
@@ -137,6 +174,41 @@ class UrsBackfillRewardLogic
                     'items_count' => count($indirectResult->items)
                 ];
                 $totalBackfilledCount += count($indirectResult->items);
+                // 创建收益记录
+                $profit = UrsProfit::create([
+                                                'urs_user_id' => $ursUserId,
+                                                'urs_promotion_member_id' => 0,
+                                                'promotion_member_farm_user_id' => 0,
+                                                'farm_user_id' => $farmUserId,
+                                                'source_id' => $directResult->sourceId,
+                                                'source_type' => REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value(),
+                                                'profit_type' => UrsProfitType::PLANTING_REWARD->value,
+                                                'relation_level' => 2,
+                                                'original_amount' => (string)0,
+                                                'profit_amount' => (string)0, // 记录实际发放的物品数量
+                                                'profit_rate' => 1,
+                                                'reward_group_id' =>  $rewardGroups['direct'], // 种植收益不使用奖励组
+                                                'talent_level' => $talentLevel,
+                                                'status' => UrsProfit::STATUS_NORMAL,
+                                            ]);
+            }else{
+                // 创建收益记录-跳过
+                $profit = UrsProfit::create([
+                                                'urs_user_id' => $ursUserId,
+                                                'urs_promotion_member_id' => 0,
+                                                'promotion_member_farm_user_id' => 0,
+                                                'farm_user_id' => $farmUserId,
+                                                'source_id' => 0,
+                                                'source_type' => REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value(),
+                                                'profit_type' => UrsProfitType::PLANTING_REWARD->value,
+                                                'relation_level' => 2,
+                                                'original_amount' => (string)0,
+                                                'profit_amount' => (string)0, // 记录实际发放的物品数量
+                                                'profit_rate' => 1,
+                                                'reward_group_id' =>  $rewardGroups['direct'], // 种植收益不使用奖励组
+                                                'talent_level' => $talentLevel,
+                                                'status' => UrsProfit::STATUS_SKIPPED,
+                                            ]);
             }
 
             // 三推奖励
@@ -168,6 +240,41 @@ class UrsBackfillRewardLogic
                     'items_count' => count($thirdResult->items)
                 ];
                 $totalBackfilledCount += count($thirdResult->items);
+                // 创建收益记录
+                $profit = UrsProfit::create([
+                                                'urs_user_id' => $ursUserId,
+                                                'urs_promotion_member_id' => 0,
+                                                'promotion_member_farm_user_id' => 0,
+                                                'farm_user_id' => $farmUserId,
+                                                'source_id' => $directResult->sourceId,
+                                                'source_type' => REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value(),
+                                                'profit_type' => UrsProfitType::PLANTING_REWARD->value,
+                                                'relation_level' => 2,
+                                                'original_amount' => (string)0,
+                                                'profit_amount' => (string)0, // 记录实际发放的物品数量
+                                                'profit_rate' => 1,
+                                                'reward_group_id' =>  $rewardGroups['direct'], // 种植收益不使用奖励组
+                                                'talent_level' => $talentLevel,
+                                                'status' => UrsProfit::STATUS_NORMAL,
+                                            ]);
+            }else{
+                // 创建收益记录-跳过
+                $profit = UrsProfit::create([
+                                                'urs_user_id' => $ursUserId,
+                                                'urs_promotion_member_id' => 0,
+                                                'promotion_member_farm_user_id' => 0,
+                                                'farm_user_id' => $farmUserId,
+                                                'source_id' => 0,
+                                                'source_type' => REWARD_SOURCE_TYPE::URSPROMOTION_BACKFILL->value(),
+                                                'profit_type' => UrsProfitType::PLANTING_REWARD->value,
+                                                'relation_level' => 3,
+                                                'original_amount' => (string)0,
+                                                'profit_amount' => (string)0, // 记录实际发放的物品数量
+                                                'profit_rate' => 1,
+                                                'reward_group_id' =>  $rewardGroups['direct'], // 种植收益不使用奖励组
+                                                'talent_level' => $talentLevel,
+                                                'status' => UrsProfit::STATUS_SKIPPED,
+                                            ]);
             }
 
             DB::commit();
@@ -198,7 +305,7 @@ class UrsBackfillRewardLogic
 
     /**
      * 检查用户是否需要补发推荐奖励
-     * 
+     *
      * @param int $ursUserId URS用户ID
      * @return bool
      */
@@ -249,14 +356,14 @@ class UrsBackfillRewardLogic
 
     /**
      * 获取用户当前达人等级
-     * 
+     *
      * @param int $ursUserId URS用户ID
      * @return int 达人等级,最低为0(青铜级)
      */
     private function getUserTalentLevel(int $ursUserId): int
     {
         $talentDto = UrsTalentService::getTalentInfo($ursUserId);
-        
+
         // 如果用户没有达人等级记录,返回最低级(青铜级)
         if (!$talentDto) {
             return 0;

+ 6 - 5
app/Module/UrsPromotion/Logics/UrsProfitLogic.php

@@ -366,7 +366,7 @@ class UrsProfitLogic
         }
 
         // 开启事务发放物品奖励
-        DB::beginTransaction();
+
 
         try {
             // 使用物品模块服务发放物品
@@ -384,7 +384,7 @@ class UrsProfitLogic
             ]);
 
             if (!$addResult['success']) {
-                DB::rollBack();
+
                 Log::error("种植收益物品发放失败", [
                     'referrer_id' => $referrerId,
                     'item_id' => $itemId,
@@ -420,17 +420,18 @@ class UrsProfitLogic
                 'status' => UrsProfit::STATUS_NORMAL,
             ]);
 
-            DB::commit();
+
 
         } catch (\Exception $e) {
-            DB::rollBack();
+
             Log::error("种植收益发放事务失败", [
                 'referrer_id' => $referrerId,
                 'item_id' => $itemId,
                 'quantity' => $rewardQuantity,
                 'error' => $e->getMessage()
             ]);
-            return null;
+
+            throw $e;
         }
 
         Log::info("URS种植收益记录创建", [

+ 3 - 3
app/Module/UrsPromotion/Models/UrsUserMapping.php

@@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
 /**
  * URS用户与农场用户关系映射模型
  *
- * field start 
+ * field start
  * @property  int  $id  主键ID
  * @property  int  $urs_user_id  URS用户ID
  * @property  string  $user_key  URS用户凭证(userKey)
@@ -90,13 +90,13 @@ class UrsUserMapping extends ModelCore
      * @param int $ursUserId URS用户ID
      * @return int|null 农场用户ID,如果未找到返回null
      */
-    public static function getFarmUserIdByUrsUserId(int $ursUserId): ?int
+    public static function getFarmUserIdByUrsUserId(int $ursUserId): int
     {
         $mapping = self::where('urs_user_id', $ursUserId)
             ->where('status', self::STATUS_VALID)
             ->first();
 
-        return $mapping ? $mapping->user_id : null;
+        return(integer)( $mapping ? $mapping->user_id : 0);
     }
 
     /**

+ 4 - 2
app/Module/UrsPromotion/Services/UrsReferralService.php

@@ -59,10 +59,11 @@ class UrsReferralService
                 'status' => UrsUserReferral::STATUS_VALID,
             ]);
 
-            // 更新推荐人的团队统计
-            self::updateReferrerStats($ursReferrerId);
+
 
             DB::commit();
+            // 更新推荐人的团队统计
+            self::updateReferrerStats($ursReferrerId);
 
             Log::info('URS推荐关系创建成功', [
                 'urs_user_id' => $ursUserId,
@@ -190,6 +191,7 @@ class UrsReferralService
     private static function updateReferrerStats(int $ursReferrerId): void
     {
         $user_id = UrsUserMapping::getFarmUserIdByUrsUserId($ursReferrerId);
+
         $teamMembers = self::getTeamMembers($ursReferrerId);
 
         $directCount = count($teamMembers[1] ?? []);

+ 1 - 1
app/Module/UrsPromotion/Services/UrsRewardDistributionService.php

@@ -57,7 +57,7 @@ class UrsRewardDistributionService
             // 检查推荐人是否已进入农场
             $farmUserId = UrsUserMapping::getFarmUserIdByUrsUserId($referrerUrsUserId);
 
-            if ($farmUserId === null) {
+            if ($farmUserId === 0) {
                 // 推荐人未进入农场,记录跳过状态
                 $results[] = self::recordSkippedReward(
                     $referrerUrsUserId,

+ 1 - 1
app/Module/UrsPromotion/Services/UrsUserMappingService.php

@@ -112,7 +112,7 @@ class UrsUserMappingService
     {
         // 首先尝试获取现有映射关系
         $existingFarmUserId = UrsUserMapping::getFarmUserIdByUrsUserId($ursUserId);
-        if ($existingFarmUserId !== null) {
+        if ($existingFarmUserId !== 0) {
             return $existingFarmUserId;
         }
         if ($userKey == null) {

+ 1 - 1
database/test/delete.sql

@@ -49,7 +49,7 @@ delete  from   kku_task_completion_logs where 1=1;
 delete  from   kku_task_categories where 1=1;
 delete  from   kku_task_achievement_conditions where 1=1;
 
-delete  from  kku_sys_request_logs where 1=1;
+# delete  from  kku_sys_request_logs where 1=1;
 
 # kku_sys_configs
 # kku_sms_sms