Browse Source

优化自动收获事务顺序:先收获后清理枯萎作物

- 调整事务执行顺序:先执行收获逻辑,再清理剩余枯萎作物
- 移除processAutoHarvest方法中的预清理逻辑,避免重复清理
- 收获和收获后铲除在同一事务中执行,保证数据一致性
- 清理剩余枯萎作物在独立事务中执行,提高容错性
- 简化统计信息,移除重复的withered_cleared_count字段
Your Name 6 months ago
parent
commit
3cb89e50ab

+ 3 - 4
app/Module/Pet/Logic/PetAutoSkillLogic.php

@@ -39,8 +39,7 @@ class PetAutoSkillLogic
             'user_id' => $userId
         ]);
 
-        // 首先清理所有已存在的枯萎作物(不使用道具)
-        $witheredClearCount = $this->clearAllWitheredCrops($userId);
+        
 
         // 获取用户所有可收获的土地
         $harvestableLands = LandService::getHarvestableLands($userId);
@@ -103,7 +102,7 @@ class PetAutoSkillLogic
         $this->recordSkillStatistics($activeSkill, 'auto_harvest', [
             'harvest_count' => $harvestCount,
             'auto_cleared_count' => $autoClearedCount,
-            'withered_cleared_count' => $witheredClearCount,
+
             'total_lands_checked' => $harvestableLands->count(),
             'harvest_results' => $harvestResults
         ]);
@@ -114,7 +113,7 @@ class PetAutoSkillLogic
             'user_id' => $userId,
             'harvest_count' => $harvestCount,
             'auto_cleared_count' => $autoClearedCount,
-            'withered_cleared_count' => $witheredClearCount,
+
             'total_lands' => $harvestableLands->count()
         ]);
     }

+ 10 - 8
app/Module/Pet/Services/PetActiveSkillService.php

@@ -304,7 +304,16 @@ class PetActiveSkillService
             'user_id' => $userId
         ]);
 
-        // 第一个事务:清理已存在的枯萎作物
+     
+
+        // 执行收获逻辑
+        // 注意:这里直接调用原有的processAutoHarvest方法,它已经包含了收获和铲除逻辑
+        // 但是铲除部分会在收获事务内执行,这符合用户要求的分离事务
+        DB::transaction(function () use ($autoSkillLogic, $activeSkill) {
+            $autoSkillLogic->processAutoHarvest($activeSkill);
+        });
+
+           // 清理已存在的枯萎作物
         $witheredClearCount = 0;
         try {
             $witheredClearCount = DB::transaction(function () use ($autoSkillLogic, $userId) {
@@ -317,13 +326,6 @@ class PetActiveSkillService
             ]);
         }
 
-        // 第二个事务:执行收获逻辑
-        // 注意:这里直接调用原有的processAutoHarvest方法,它已经包含了收获和铲除逻辑
-        // 但是铲除部分会在收获事务内执行,这符合用户要求的分离事务
-        DB::transaction(function () use ($autoSkillLogic, $activeSkill) {
-            $autoSkillLogic->processAutoHarvest($activeSkill);
-        });
-
         $finalResult = [
             'withered_cleared_count' => $witheredClearCount,
             'harvest_completed' => true