option('dry-run'); if ($isDryRun) { $this->info('执行干运行模式,只显示需要修复的数据...'); } else { $this->info('开始修复作物final_output_item_id...'); } try { $cropLogic = new CropLogic(); // 查找所有发芽期及以后但没有final_output_item_id的作物 $problematicCrops = FarmCrop::whereNull('final_output_item_id') ->where('growth_stage', '>=', GROWTH_STAGE::SPROUT->value) ->with(['seed', 'land']) ->get(); $this->info("找到 {$problematicCrops->count()} 个需要修复的作物"); if ($problematicCrops->isEmpty()) { $this->info('没有需要修复的作物'); return 0; } // 显示详细信息 $this->table( ['作物ID', '用户ID', '土地ID', '种子ID', '当前阶段', '种植时间'], $problematicCrops->map(function ($crop) { return [ $crop->id, $crop->user_id, $crop->land_id, $crop->seed_id, GROWTH_STAGE::getName($crop->growth_stage->value), $crop->plant_time ]; })->toArray() ); if ($isDryRun) { $this->warn('这是干运行模式,没有实际修复数据'); return 0; } // 确认是否继续 if (!$this->confirm('确定要修复这些作物吗?')) { $this->info('操作已取消'); return 0; } $fixedCount = 0; $errorCount = 0; foreach ($problematicCrops as $crop) { try { // 为作物确定最终产出果实ID $outputInfo = $cropLogic->getRandomOutput($crop->seed_id); $crop->final_output_item_id = $outputInfo['item_id']; $crop->save(); $fixedCount++; Log::info('修复作物final_output_item_id', [ 'crop_id' => $crop->id, 'user_id' => $crop->user_id, 'seed_id' => $crop->seed_id, 'growth_stage' => $crop->growth_stage, 'final_output_item_id' => $crop->final_output_item_id ]); $this->info("修复作物 ID: {$crop->id}, 确定产出果实 ID: {$crop->final_output_item_id}"); } catch (\Exception $e) { $errorCount++; Log::error('修复作物final_output_item_id失败', [ 'crop_id' => $crop->id, 'user_id' => $crop->user_id, 'seed_id' => $crop->seed_id, 'error' => $e->getMessage() ]); $this->error("修复作物 ID: {$crop->id} 失败: {$e->getMessage()}"); } } $this->info("修复完成!成功修复 {$fixedCount} 个作物,失败 {$errorCount} 个"); Log::info('作物final_output_item_id修复完成', [ 'total' => $problematicCrops->count(), 'fixed' => $fixedCount, 'errors' => $errorCount ]); return 0; } catch (\Exception $e) { $this->error('修复过程中发生错误: ' . $e->getMessage()); Log::error('作物final_output_item_id修复失败', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return 1; } } }