FixCropMatureOutputCommand.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. * 修复成熟期作物产量命令
  10. *
  11. * 为现有的成熟期作物计算并设置final_output_amount字段
  12. */
  13. class FixCropMatureOutputCommand extends Command
  14. {
  15. /**
  16. * 命令签名
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'farm:fix-crop-mature-output
  21. {--dry-run : 仅查看需要修复的数据,不实际修复}
  22. {--limit=100 : 每次处理的数据量限制}';
  23. /**
  24. * 命令描述
  25. *
  26. * @var string
  27. */
  28. protected $description = '修复成熟期作物的产量数据,为没有final_output_amount的成熟期作物计算产量';
  29. /**
  30. * 执行命令
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. $this->info('开始检查成熟期作物产量数据...');
  37. $dryRun = $this->option('dry-run');
  38. $limit = (int) $this->option('limit');
  39. try {
  40. // 查找需要修复的作物:成熟期且有产出物品ID但没有final_output_amount的作物
  41. $problematicCrops = FarmCrop::where('growth_stage', GROWTH_STAGE::MATURE->value)
  42. ->whereNotNull('final_output_item_id')
  43. ->whereNull('final_output_amount')
  44. ->with(['seed', 'land.landType', 'user.houseConfig'])
  45. ->limit($limit)
  46. ->get();
  47. if ($problematicCrops->isEmpty()) {
  48. $this->info('没有发现需要修复的成熟期作物数据');
  49. return 0;
  50. }
  51. $this->info("发现 {$problematicCrops->count()} 个需要修复的成熟期作物");
  52. // 显示详细信息
  53. $this->table(
  54. ['作物ID', '用户ID', '种子ID', '土地ID', '生长阶段', '产出物品ID', '产量状态'],
  55. $problematicCrops->map(function ($crop) {
  56. return [
  57. $crop->id,
  58. $crop->user_id,
  59. $crop->seed_id,
  60. $crop->land_id,
  61. $crop->growth_stage->value,
  62. $crop->final_output_item_id ?? '未确定',
  63. $crop->final_output_amount ? '已确定' : '未确定'
  64. ];
  65. })->toArray()
  66. );
  67. if ($dryRun) {
  68. $this->info('--dry-run 模式,不执行实际修复');
  69. return 0;
  70. }
  71. // 确认是否继续
  72. if (!$this->confirm('确定要修复这些作物的产量吗?')) {
  73. $this->info('操作已取消');
  74. return 0;
  75. }
  76. $fixedCount = 0;
  77. $errorCount = 0;
  78. $cropLogic = new CropLogic();
  79. foreach ($problematicCrops as $crop) {
  80. try {
  81. // 计算成熟期产量
  82. $finalAmount = $cropLogic->calculateMatureOutput($crop);
  83. $crop->final_output_amount = $finalAmount;
  84. $crop->save();
  85. $fixedCount++;
  86. Log::info('修复作物final_output_amount', [
  87. 'crop_id' => $crop->id,
  88. 'user_id' => $crop->user_id,
  89. 'seed_id' => $crop->seed_id,
  90. 'growth_stage' => $crop->growth_stage->value,
  91. 'final_output_item_id' => $crop->final_output_item_id,
  92. 'final_output_amount' => $crop->final_output_amount
  93. ]);
  94. $this->info("修复作物 ID: {$crop->id}, 确定产量: {$crop->final_output_amount}");
  95. } catch (\Exception $e) {
  96. $errorCount++;
  97. Log::error('修复作物final_output_amount失败', [
  98. 'crop_id' => $crop->id,
  99. 'user_id' => $crop->user_id,
  100. 'error' => $e->getMessage(),
  101. 'trace' => $e->getTraceAsString()
  102. ]);
  103. $this->error("修复作物 ID: {$crop->id} 失败: " . $e->getMessage());
  104. }
  105. }
  106. $this->info("修复完成!成功修复: {$fixedCount} 个,失败: {$errorCount} 个");
  107. if ($errorCount > 0) {
  108. $this->warn("有 {$errorCount} 个作物修复失败,请检查日志");
  109. return 1;
  110. }
  111. return 0;
  112. } catch (\Exception $e) {
  113. $this->error('命令执行失败: ' . $e->getMessage());
  114. Log::error('FixCropMatureOutputCommand执行失败', [
  115. 'error' => $e->getMessage(),
  116. 'trace' => $e->getTraceAsString()
  117. ]);
  118. return 1;
  119. }
  120. }
  121. }