targetId; $powerAmount = $item->quantity; // 验证宠物是否存在且属于该用户 $pet = \App\Module\Pet\Models\PetUser::where('id', $petId) ->where('user_id', $userId) ->first(); if (!$pet) { throw new Exception("宠物不存在或不属于该用户,宠物ID: {$petId},用户ID: {$userId}"); } // 使用宠物逻辑类来增加体力 $petLogic = new \App\Module\Pet\Logic\PetLogic(); $reflection = new ReflectionClass($petLogic); $method = $reflection->getMethod('addStamina'); $method->setAccessible(true); $actualGained = $method->invoke($petLogic, $petId, $powerAmount); Log::info("宠物体力奖励发放成功", [ 'userId' => $userId, 'petId' => $petId, 'petName' => $pet->name, 'powerAmount' => $powerAmount, 'actualGained' => $actualGained, 'currentPower' => $pet->fresh()->stamina, 'sourceType' => $sourceType, 'sourceId' => $sourceId ]); } catch (Exception $e) { Log::error("宠物体力奖励发放失败", [ 'userId' => $userId, 'petId' => $item->targetId, 'powerAmount' => $item->quantity, 'sourceType' => $sourceType, 'sourceId' => $sourceId, 'error' => $e->getMessage() ]); throw new Exception("宠物体力奖励发放失败: " . $e->getMessage()); } } }