UpdateCropGrowthCommand.php 4.7 KB

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