Răsfoiți Sursa

refactor(farm): 优化作物模型和逻辑

- 更新 GROWTH_STAGE 枚举类,改进 canUseFertilizer 方法参数类型
- 优化 CropLogic 中的 useFertilizer 方法,增加类型提示和代码注释
- 重构 FarmCrop 模型类,调整属性注释和类型转换设置
notfff 7 luni în urmă
părinte
comite
f044f844e4

+ 2 - 2
app/Module/Farm/Enums/GROWTH_STAGE.php

@@ -72,9 +72,9 @@ enum GROWTH_STAGE: int
      * @param int $stage
      * @return bool
      */
-    public static function canUseFertilizer(int $stage): bool
+    public static function canUseFertilizer(GROWTH_STAGE $stage): bool
     {
-        return in_array($stage, [ self::SEED->value, self::SPROUT->value, self::GROWTH->value ]);
+        return in_array($stage->value(), [ self::SEED->value, self::SPROUT->value, self::GROWTH->value ]);
     }
 
     /**

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

@@ -302,6 +302,9 @@ class CropLogic
             }
 
             // 获取作物信息
+            /**
+             * @var FarmCrop $crop
+             */
             $crop = FarmCrop::where('land_id', $landId)->first();
 
             if (!$crop) {
@@ -325,6 +328,7 @@ class CropLogic
             if ($crop->stage_end_time) {
                 $currentTime = now();
                 $endTime = $crop->stage_end_time;
+//                dd($endTime);
                 $remainingTime = $currentTime->diffInSeconds($endTime, false);
 
                 if ($remainingTime > 0) {

+ 24 - 29
app/Module/Farm/Models/FarmCrop.php

@@ -9,23 +9,25 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
 /**
  * 作物信息模型
  *
- * field start 
- * @property  int  $id  主键ID
- * @property  int  $land_id  土地ID
- * @property  int  $user_id  用户ID
- * @property  int  $seed_id  种子ID
- * @property  string  $plant_time  种植时间
- * @property  int  $growth_stage  生长阶段:1种子期,2发芽期,3生长期,4成熟期,5枯萎期
- * @property  string  $stage_start_time  当前阶段结束时间
- * @property  string  $stage_end_time  当前阶段结束时间
- * @property  array  $disasters  灾害情况
- * @property  bool  $fertilized  当前阶段是否已使用化肥
- * @property  \Carbon\Carbon  $created_at  创建时间
- * @property  \Carbon\Carbon  $updated_at  更新时间
+ * field start
+ *
+ * @property  int $id  主键ID
+ * @property  int $land_id  土地ID
+ * @property  int $user_id  用户ID
+ * @property  int $seed_id  种子ID
+ * @property  string $plant_time  种植时间
+ * @property  int $growth_stage  生长阶段:1种子期,2发芽期,3生长期,4成熟期,5枯萎期
+ * @property  string $stage_start_time  当前阶段结束时间
+ * @property  string $stage_end_time  当前阶段结束时间
+ * @property  array $disasters  灾害情况
+ * @property  bool $fertilized  当前阶段是否已使用化肥
+ * @property  \Carbon\Carbon $created_at  创建时间
+ * @property  \Carbon\Carbon $updated_at  更新时间
  * field end
  */
 class FarmCrop extends Model
 {
+
     /**
      * 与模型关联的表名
      *
@@ -50,28 +52,20 @@ class FarmCrop extends Model
         'fertilized',
     ];
 
-    /**
-     * 应该被转换为日期的属性
-     *
-     * @var array
-     */
-    protected $dates = [
-        'plant_time',
-        'stage_start_time',
-        'stage_end_time',
-        'created_at',
-        'updated_at',
-    ];
-
+    
     /**
      * 应该被转换为原生类型的属性
      *
      * @var array
      */
     protected $casts = [
-        'disasters' => 'json',
-        'growth_stage'=>GROWTH_STAGE::class,
-        'fertilized' => 'boolean',
+        'disasters'        => 'json',
+        'growth_stage'     => GROWTH_STAGE::class,
+        'fertilized'       => 'boolean',
+        'plant_time'       => 'datetime',
+        'stage_start_time' => 'datetime',
+        'stage_end_time'   => 'datetime',
+
     ];
 
     /**
@@ -93,4 +87,5 @@ class FarmCrop extends Model
     {
         return $this->belongsTo(FarmSeed::class, 'seed_id', 'id');
     }
+
 }