UpdateCropGrowthCommand.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Module\Farm\Commands;
  3. use App\Module\Farm\Enums\GROWTH_STAGE;
  4. use App\Module\Farm\Events\CropGrowthStageChangedEvent;
  5. use App\Module\Farm\Logics\CropLogic;
  6. use App\Module\Farm\Models\FarmCrop;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\Log;
  9. /**
  10. * 更新作物生长状态命令
  11. */
  12. class UpdateCropGrowthCommand extends Command
  13. {
  14. /**
  15. * 命令名称
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'farm:update-crop-growth';
  20. /**
  21. * 命令描述
  22. *
  23. * @var string
  24. */
  25. protected $description = '更新作物生长状态';
  26. /**
  27. * 执行命令
  28. *
  29. * @return int
  30. */
  31. public function handle()
  32. {
  33. $this->info('开始更新作物生长状态...');
  34. try {
  35. // 获取需要更新生长阶段的作物
  36. /**
  37. * @var \Illuminate\Database\Eloquent\Collection $crops
  38. */
  39. $crops = FarmCrop::whereNotNull('stage_end_time')
  40. ->where('stage_end_time', '<=', now())
  41. ->where('growth_stage', '<', GROWTH_STAGE::WITHERED->value)
  42. ->get();
  43. $this->info("找到 {$crops->count()} 个需要更新的作物");
  44. $updatedCount = 0;
  45. foreach ($crops as $crop) {
  46. $userId = $crop->user_id;
  47. $oldStage = $crop->growth_stage;
  48. // 计算新的生长阶段
  49. $newStage = $this->calculateNextStage($crop);
  50. // 计算新阶段的结束时间
  51. $stageEndTime = $this->calculateStageEndTime($crop, $newStage);
  52. // 更新作物信息
  53. $crop->growth_stage = $newStage;
  54. $crop->stage_start_time = now(); // 设置新阶段的开始时间
  55. $crop->stage_end_time = $stageEndTime;
  56. $crop->fertilized = false; // 重置施肥状态
  57. $crop->save();
  58. // 触发生长阶段变更事件
  59. event(new CropGrowthStageChangedEvent($userId, $crop, $oldStage->value, $newStage));
  60. $updatedCount++;
  61. $this->info("作物 ID: {$crop->id}, 用户 ID: {$userId}, 阶段: {$oldStage} -> {$newStage}");
  62. }
  63. $this->info("成功更新 {$updatedCount} 个作物的生长状态");
  64. Log::info('作物生长状态更新成功', [
  65. 'total' => $crops->count(),
  66. 'updated' => $updatedCount
  67. ]);
  68. return 0;
  69. } catch (\Exception $e) {
  70. $this->error('作物生长状态更新失败: ' . $e->getMessage());
  71. Log::error('作物生长状态更新失败', [
  72. 'error' => $e->getMessage(),
  73. 'trace' => $e->getTraceAsString()
  74. ]);
  75. return 1;
  76. }
  77. }
  78. /**
  79. * 计算下一个生长阶段
  80. *
  81. * @param FarmCrop $crop
  82. * @return int
  83. */
  84. private function calculateNextStage(FarmCrop $crop): int
  85. {
  86. $logic = new CropLogic();
  87. return $logic->calculateNextStage($crop);
  88. }
  89. /**
  90. * 计算阶段结束时间
  91. *
  92. * @param FarmCrop $crop
  93. * @param int $stage
  94. * @return \Carbon\Carbon|null
  95. */
  96. private function calculateStageEndTime(FarmCrop $crop, int $stage)
  97. {
  98. $seed = $crop->seed;
  99. if (!$seed) {
  100. return null;
  101. }
  102. $now = now();
  103. switch ($stage) {
  104. case GROWTH_STAGE::SEED->valueInt():
  105. return $now->addSeconds($seed->seed_time);
  106. case GROWTH_STAGE::SPROUT->valueInt():
  107. return $now->addSeconds($seed->sprout_time);
  108. case GROWTH_STAGE::GROWTH->valueInt():
  109. return $now->addSeconds($seed->growth_time);
  110. case GROWTH_STAGE::MATURE->valueInt():
  111. // 成熟期持续24小时后进入枯萎期
  112. return $now->addHours(24);
  113. case GROWTH_STAGE::WITHERED->valueInt():
  114. // 枯萎期没有结束时间
  115. return null;
  116. default:
  117. return null;
  118. }
  119. }
  120. }