|
|
@@ -0,0 +1,152 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Module\Farm\Listeners;
|
|
|
+
|
|
|
+use App\Module\Farm\Events\HouseUpgradedEvent;
|
|
|
+use App\Module\Farm\Logics\HouseLogic;
|
|
|
+use App\Module\Farm\Logics\LandLogic;
|
|
|
+use App\Module\Farm\Models\FarmLand;
|
|
|
+use App\Module\Farm\Enums\LAND_TYPE;
|
|
|
+use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
+use Illuminate\Queue\InteractsWithQueue;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
+
|
|
|
+class AddLandAfterHouseUpgradeListener implements ShouldQueue
|
|
|
+{
|
|
|
+ use InteractsWithQueue;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 房屋逻辑类
|
|
|
+ *
|
|
|
+ * @var HouseLogic
|
|
|
+ */
|
|
|
+ protected $houseLogic;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 土地逻辑类
|
|
|
+ *
|
|
|
+ * @var LandLogic
|
|
|
+ */
|
|
|
+ protected $landLogic;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Create the event listener.
|
|
|
+ */
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->houseLogic = new HouseLogic();
|
|
|
+ $this->landLogic = new LandLogic();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Handle the event.
|
|
|
+ */
|
|
|
+ public function handle(HouseUpgradedEvent $event): void
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ // 只处理升级事件,不处理降级事件
|
|
|
+ if ($event->newLevel <= $event->oldLevel) {
|
|
|
+ Log::info('房屋降级,不创建新土地', [
|
|
|
+ 'user_id' => $event->userId,
|
|
|
+ 'old_level' => $event->oldLevel,
|
|
|
+ 'new_level' => $event->newLevel
|
|
|
+ ]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取新等级可用的土地数量
|
|
|
+ $newAvailableLands = $this->houseLogic->getAvailableLandsCount($event->newLevel);
|
|
|
+
|
|
|
+ // 获取旧等级可用的土地数量
|
|
|
+ $oldAvailableLands = $this->houseLogic->getAvailableLandsCount($event->oldLevel);
|
|
|
+
|
|
|
+ // 计算需要新增的土地数量
|
|
|
+ $landsToAdd = $newAvailableLands - $oldAvailableLands;
|
|
|
+
|
|
|
+ if ($landsToAdd <= 0) {
|
|
|
+ Log::info('房屋升级后无需增加土地', [
|
|
|
+ 'user_id' => $event->userId,
|
|
|
+ 'old_level' => $event->oldLevel,
|
|
|
+ 'new_level' => $event->newLevel,
|
|
|
+ 'old_available_lands' => $oldAvailableLands,
|
|
|
+ 'new_available_lands' => $newAvailableLands
|
|
|
+ ]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ Log::info('房屋升级后需要增加土地', [
|
|
|
+ 'user_id' => $event->userId,
|
|
|
+ 'old_level' => $event->oldLevel,
|
|
|
+ 'new_level' => $event->newLevel,
|
|
|
+ 'lands_to_add' => $landsToAdd
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 获取用户当前的土地
|
|
|
+ $userLands = FarmLand::where('user_id', $event->userId)->get();
|
|
|
+
|
|
|
+ // 获取已使用的位置
|
|
|
+ $usedPositions = $userLands->pluck('position')->toArray();
|
|
|
+
|
|
|
+ // 开始创建新土地
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 从位置1开始,找到未使用的位置创建土地
|
|
|
+ $position = 1;
|
|
|
+ $createdCount = 0;
|
|
|
+
|
|
|
+ while ($createdCount < $landsToAdd) {
|
|
|
+ // 如果位置未被使用,创建新土地
|
|
|
+ if (!in_array($position, $usedPositions)) {
|
|
|
+ $land = $this->landLogic->createLand($event->userId, $position, LAND_TYPE::NORMAL->value);
|
|
|
+
|
|
|
+ if ($land) {
|
|
|
+ $createdCount++;
|
|
|
+ Log::info('创建新土地成功', [
|
|
|
+ 'user_id' => $event->userId,
|
|
|
+ 'land_id' => $land->id,
|
|
|
+ 'position' => $position
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $position++;
|
|
|
+
|
|
|
+ // 防止无限循环
|
|
|
+ if ($position > 100) {
|
|
|
+ Log::warning('创建土地时达到最大位置限制', [
|
|
|
+ 'user_id' => $event->userId,
|
|
|
+ 'max_position' => 100
|
|
|
+ ]);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+
|
|
|
+ Log::info('房屋升级后创建土地完成', [
|
|
|
+ 'user_id' => $event->userId,
|
|
|
+ 'created_count' => $createdCount,
|
|
|
+ 'target_count' => $landsToAdd
|
|
|
+ ]);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+
|
|
|
+ Log::error('房屋升级后创建土地失败', [
|
|
|
+ 'user_id' => $event->userId,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('处理房屋升级事件失败', [
|
|
|
+ 'user_id' => $event->userId,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|