| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?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_amount字段
- */
- class FixCropMatureOutputCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'farm:fix-crop-mature-output
- {--dry-run : 仅查看需要修复的数据,不实际修复}
- {--limit=100 : 每次处理的数据量限制}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '修复成熟期作物的产量数据,为没有final_output_amount的成熟期作物计算产量';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $this->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;
- }
- }
- }
|