| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace App\Module\Farm\Commands;
- use App\Module\Farm\Enums\GROWTH_STAGE;
- use App\Module\Farm\Logics\CropLogic;
- use App\Module\Farm\Models\FarmCrop;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- /**
- * 修复作物final_output_item_id的命令
- *
- * 这个命令用于修复那些应该有final_output_item_id但没有设置的作物
- */
- class FixCropFinalOutputCommand extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'farm:fix-crop-final-output {--dry-run : 只显示需要修复的数据,不实际修复}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '修复作物的final_output_item_id字段';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $isDryRun = $this->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;
- }
- }
- }
|