Просмотр исходного кода

修复施肥失败问题:解决Carbon对象修改和枚举类型处理

- 修复CropLogic::useFertilizer中Carbon对象直接修改问题,使用copy()方法创建副本
- 添加剩余时间检查,处理作物已到达或超过结束时间的情况
- 修复calculateNextStage方法中枚举类型处理,统一值获取方式
- 添加详细的警告日志记录,便于问题排查
- 提高施肥功能的稳定性和可靠性
notfff 8 месяцев назад
Родитель
Сommit
41d7c9048c
1 измененных файлов с 14 добавлено и 4 удалено
  1. 14 4
      app/Module/Farm/Logics/CropLogic.php

+ 14 - 4
app/Module/Farm/Logics/CropLogic.php

@@ -368,13 +368,14 @@ class CropLogic
             if ($crop->stage_end_time) {
                 $currentTime = now();
                 $endTime = $crop->stage_end_time;
-//                dd($endTime);
                 $remainingTime = $currentTime->diffInSeconds($endTime, false);
 
                 if ($remainingTime > 0) {
                     // 确保减少的时间不超过剩余时间
                     $reducedTime = min($crop_growth_time, $remainingTime);
-                    $crop->stage_end_time = $endTime->subSeconds($reducedTime);
+                    // 使用copy()方法创建副本,避免修改原始对象
+                    $newEndTime = $endTime->copy()->subSeconds($reducedTime);
+                    $crop->stage_end_time = $newEndTime;
 
                     Log::info('化肥减少生长时间', [
                         'crop_id' => $crop->id,
@@ -383,6 +384,13 @@ class CropLogic
                         'new_end_time' => $crop->stage_end_time->toDateTimeString(),
                         'stage_start_time' => $crop->stage_start_time->toDateTimeString()
                     ]);
+                } else {
+                    Log::warning('作物已经到达或超过结束时间,无法减少生长时间', [
+                        'crop_id' => $crop->id,
+                        'current_time' => $currentTime->toDateTimeString(),
+                        'stage_end_time' => $endTime->toDateTimeString(),
+                        'remaining_time' => $remainingTime
+                    ]);
                 }
             }
 
@@ -684,7 +692,8 @@ class CropLogic
         $currentStage = $crop->growth_stage;
 
         // 如果当前是成熟期,且超过一定时间,则进入枯萎期
-        if ($currentStage === GROWTH_STAGE::MATURE->value) {
+        $currentStageValue = is_object($currentStage) ? $currentStage->value : $currentStage;
+        if ($currentStageValue === GROWTH_STAGE::MATURE->value) {
             // 成熟期持续时间,默认为24小时
             $matureDuration = 24 * 60 * 60;
 
@@ -709,7 +718,8 @@ class CropLogic
         ];
 
         // 确保返回整数值
-        return $stageMap[$currentStage->value()] ?? GROWTH_STAGE::WITHERED->value;
+        $currentStageValue = is_object($currentStage) ? $currentStage->value : $currentStage;
+        return $stageMap[$currentStageValue] ?? GROWTH_STAGE::WITHERED->value;
     }
 
     /**