FixCropFinalOutputCommand.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Module\Farm\Commands;
  3. use App\Module\Farm\Enums\GROWTH_STAGE;
  4. use App\Module\Farm\Logics\CropLogic;
  5. use App\Module\Farm\Models\FarmCrop;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 修复作物final_output_item_id的命令
  10. *
  11. * 这个命令用于修复那些应该有final_output_item_id但没有设置的作物
  12. */
  13. class FixCropFinalOutputCommand extends Command
  14. {
  15. /**
  16. * 命令名称
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'farm:fix-crop-final-output {--dry-run : 只显示需要修复的数据,不实际修复}';
  21. /**
  22. * 命令描述
  23. *
  24. * @var string
  25. */
  26. protected $description = '修复作物的final_output_item_id字段';
  27. /**
  28. * 执行命令
  29. *
  30. * @return int
  31. */
  32. public function handle()
  33. {
  34. $isDryRun = $this->option('dry-run');
  35. if ($isDryRun) {
  36. $this->info('执行干运行模式,只显示需要修复的数据...');
  37. } else {
  38. $this->info('开始修复作物final_output_item_id...');
  39. }
  40. try {
  41. $cropLogic = new CropLogic();
  42. // 查找所有发芽期及以后但没有final_output_item_id的作物
  43. $problematicCrops = FarmCrop::whereNull('final_output_item_id')
  44. ->where('growth_stage', '>=', GROWTH_STAGE::SPROUT->value)
  45. ->with(['seed', 'land'])
  46. ->get();
  47. $this->info("找到 {$problematicCrops->count()} 个需要修复的作物");
  48. if ($problematicCrops->isEmpty()) {
  49. $this->info('没有需要修复的作物');
  50. return 0;
  51. }
  52. // 显示详细信息
  53. $this->table(
  54. ['作物ID', '用户ID', '土地ID', '种子ID', '当前阶段', '种植时间'],
  55. $problematicCrops->map(function ($crop) {
  56. return [
  57. $crop->id,
  58. $crop->user_id,
  59. $crop->land_id,
  60. $crop->seed_id,
  61. GROWTH_STAGE::getName($crop->growth_stage->value),
  62. $crop->plant_time
  63. ];
  64. })->toArray()
  65. );
  66. if ($isDryRun) {
  67. $this->warn('这是干运行模式,没有实际修复数据');
  68. return 0;
  69. }
  70. // 确认是否继续
  71. if (!$this->confirm('确定要修复这些作物吗?')) {
  72. $this->info('操作已取消');
  73. return 0;
  74. }
  75. $fixedCount = 0;
  76. $errorCount = 0;
  77. foreach ($problematicCrops as $crop) {
  78. try {
  79. // 为作物确定最终产出果实ID
  80. $outputInfo = $cropLogic->getRandomOutput($crop->seed_id);
  81. $crop->final_output_item_id = $outputInfo['item_id'];
  82. $crop->save();
  83. $fixedCount++;
  84. Log::info('修复作物final_output_item_id', [
  85. 'crop_id' => $crop->id,
  86. 'user_id' => $crop->user_id,
  87. 'seed_id' => $crop->seed_id,
  88. 'growth_stage' => $crop->growth_stage,
  89. 'final_output_item_id' => $crop->final_output_item_id
  90. ]);
  91. $this->info("修复作物 ID: {$crop->id}, 确定产出果实 ID: {$crop->final_output_item_id}");
  92. } catch (\Exception $e) {
  93. $errorCount++;
  94. Log::error('修复作物final_output_item_id失败', [
  95. 'crop_id' => $crop->id,
  96. 'user_id' => $crop->user_id,
  97. 'seed_id' => $crop->seed_id,
  98. 'error' => $e->getMessage()
  99. ]);
  100. $this->error("修复作物 ID: {$crop->id} 失败: {$e->getMessage()}");
  101. }
  102. }
  103. $this->info("修复完成!成功修复 {$fixedCount} 个作物,失败 {$errorCount} 个");
  104. Log::info('作物final_output_item_id修复完成', [
  105. 'total' => $problematicCrops->count(),
  106. 'fixed' => $fixedCount,
  107. 'errors' => $errorCount
  108. ]);
  109. return 0;
  110. } catch (\Exception $e) {
  111. $this->error('修复过程中发生错误: ' . $e->getMessage());
  112. Log::error('作物final_output_item_id修复失败', [
  113. 'error' => $e->getMessage(),
  114. 'trace' => $e->getTraceAsString()
  115. ]);
  116. return 1;
  117. }
  118. }
  119. }