argument('user_id'); $landId = (int) $this->argument('land_id'); $itemId = (int) $this->argument('item_id'); $this->info("开始测试作物种植bug修复..."); $this->info("用户ID: {$userId}, 土地ID: {$landId}, 种子物品ID: {$itemId}"); // 1. 检查初始状态 $this->info("\n=== 1. 检查初始状态 ==="); $land = FarmLand::find($landId); if (!$land) { $this->error("土地不存在"); return 1; } $existingCrop = FarmCrop::where('land_id', $landId)->first(); $this->info("土地状态: " . LAND_STATUS::getName($land->status) . " ({$land->status})"); $this->info("是否有作物: " . ($land->has_crop ? '是' : '否')); $this->info("作物记录: " . ($existingCrop ? "存在 (ID: {$existingCrop->id}, 阶段: {$existingCrop->growth_stage})" : '不存在')); // 2. 模拟bug场景:土地状态为空闲但有作物记录 $this->info("\n=== 2. 模拟bug场景 ==="); if ($existingCrop) { $this->info("已存在作物记录,模拟土地状态错误重置为空闲的情况"); $land->status = LAND_STATUS::IDLE->value; $land->has_crop = false; $land->save(); $this->info("已将土地状态重置为空闲,但保留作物记录"); } else { $this->info("创建模拟的枯萎作物记录"); $seed = FarmSeed::where('item_id', $itemId)->first(); if (!$seed) { $this->error("种子配置不存在"); return 1; } $mockCrop = new FarmCrop(); $mockCrop->land_id = $landId; $mockCrop->user_id = $userId; $mockCrop->seed_id = $seed->id; $mockCrop->plant_time = now()->subHours(2); $mockCrop->growth_stage = GROWTH_STAGE::WITHERED; $mockCrop->stage_start_time = now()->subHours(1); $mockCrop->stage_end_time = null; $mockCrop->disasters = []; $mockCrop->fertilized = false; $mockCrop->last_disaster_check_time = now()->subHours(2); $mockCrop->can_disaster = false; $mockCrop->save(); $land->status = LAND_STATUS::IDLE->value; $land->has_crop = false; $land->save(); $this->info("已创建模拟枯萎作物记录 (ID: {$mockCrop->id})"); } // 3. 测试修复后的种植逻辑 $this->info("\n=== 3. 测试修复后的种植逻辑 ==="); DB::beginTransaction(); try { $result = CropService::plantCrop($userId, $landId, $itemId); if ($result) { $this->error("❌ 种植成功了!这表明bug修复可能有问题"); $this->info("返回结果: " . json_encode($result, JSON_UNESCAPED_UNICODE)); } else { $this->info("✅ 种植失败,符合预期"); } DB::rollBack(); } catch (\Exception $e) { DB::rollBack(); $this->info("✅ 抛出异常,符合预期"); $this->info("异常信息: " . $e->getMessage()); } // 4. 清理并测试正常种植 $this->info("\n=== 4. 测试正常种植流程 ==="); FarmCrop::where('land_id', $landId)->delete(); $land->status = LAND_STATUS::IDLE->value; $land->has_crop = false; $land->save(); $this->info("已清理作物记录,土地状态重置为空闲"); DB::beginTransaction(); try { $result = CropService::plantCrop($userId, $landId, $itemId); if ($result) { $this->info("✅ 正常种植成功"); $this->info("新作物ID: " . $result['crop']->id); $this->info("种植日志ID: " . $result['log_id']); } else { $this->error("❌ 正常种植失败"); } DB::rollBack(); } catch (\Exception $e) { DB::rollBack(); $this->error("❌ 正常种植抛出异常: " . $e->getMessage()); } // 5. 清理测试数据 $this->info("\n=== 5. 清理测试数据 ==="); FarmCrop::where('land_id', $landId)->delete(); $land->status = LAND_STATUS::IDLE->value; $land->has_crop = false; $land->save(); $this->info("测试数据已清理"); $this->info("\n测试完成!"); return 0; } }