info('开始检查成熟期作物产量数据...'); $dryRun = $this->option('dry-run'); $limit = (int) $this->option('limit'); try { // 查找需要修复的作物:成熟期且有产出物品ID但没有final_output_amount的作物 $problematicCrops = FarmCrop::where('growth_stage', GROWTH_STAGE::MATURE->value) ->whereNotNull('final_output_item_id') ->whereNull('final_output_amount') ->with(['seed', 'land.landType', 'user.houseConfig']) ->limit($limit) ->get(); if ($problematicCrops->isEmpty()) { $this->info('没有发现需要修复的成熟期作物数据'); return 0; } $this->info("发现 {$problematicCrops->count()} 个需要修复的成熟期作物"); // 显示详细信息 $this->table( ['作物ID', '用户ID', '种子ID', '土地ID', '生长阶段', '产出物品ID', '产量状态'], $problematicCrops->map(function ($crop) { return [ $crop->id, $crop->user_id, $crop->seed_id, $crop->land_id, $crop->growth_stage->value, $crop->final_output_item_id ?? '未确定', $crop->final_output_amount ? '已确定' : '未确定' ]; })->toArray() ); if ($dryRun) { $this->info('--dry-run 模式,不执行实际修复'); return 0; } // 确认是否继续 if (!$this->confirm('确定要修复这些作物的产量吗?')) { $this->info('操作已取消'); return 0; } $fixedCount = 0; $errorCount = 0; $cropLogic = new CropLogic(); foreach ($problematicCrops as $crop) { try { // 计算成熟期产量 $finalAmount = $cropLogic->calculateMatureOutput($crop); $crop->final_output_amount = $finalAmount; $crop->save(); $fixedCount++; Log::info('修复作物final_output_amount', [ 'crop_id' => $crop->id, 'user_id' => $crop->user_id, 'seed_id' => $crop->seed_id, 'growth_stage' => $crop->growth_stage->value, 'final_output_item_id' => $crop->final_output_item_id, 'final_output_amount' => $crop->final_output_amount ]); $this->info("修复作物 ID: {$crop->id}, 确定产量: {$crop->final_output_amount}"); } catch (\Exception $e) { $errorCount++; Log::error('修复作物final_output_amount失败', [ 'crop_id' => $crop->id, 'user_id' => $crop->user_id, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); $this->error("修复作物 ID: {$crop->id} 失败: " . $e->getMessage()); } } $this->info("修复完成!成功修复: {$fixedCount} 个,失败: {$errorCount} 个"); if ($errorCount > 0) { $this->warn("有 {$errorCount} 个作物修复失败,请检查日志"); return 1; } return 0; } catch (\Exception $e) { $this->error('命令执行失败: ' . $e->getMessage()); Log::error('FixCropMatureOutputCommand执行失败', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return 1; } } }