Explorar el Código

农场作物增加土地等级字段功能

- 为 farm_crops 表增加 land_level 字段,记录种植时的土地等级
- 更新 FarmCrop 模型,增加 land_level 字段定义和 fillable 配置
- 修改种植逻辑,在 CropLogic::plantCrop 中记录当前土地等级
- 更新产出计算逻辑,使用作物记录的 land_level 而不是当前土地等级
- 修改所有作物日志记录,使用 crop->land_level 字段确保一致性
- 更新后台管理界面,增加土地等级字段的显示、编辑和筛选功能
- 数据库迁移:更新现有 28 条作物记录的土地等级数据

功能说明:
- 种植时记录土地等级,后续确认作物产出使用该字段的等级
- 避免土地升级后影响已种植作物的产出计算
- 保证作物产出计算的一致性和准确性
AI Assistant hace 6 meses
padre
commit
055c2268f1

+ 15 - 0
app/Module/Farm/AdminControllers/FarmCropController.php

@@ -50,6 +50,11 @@ class FarmCropController extends AdminController
             $grid->column('land_id', '土地ID')->sortable();
             $helper->columnUserId();
             $grid->column('seed_id', '种子ID')->sortable();
+            $grid->column('land_level', '土地等级')->sortable()->display(function ($value) {
+                $landType = \App\Module\Farm\Models\FarmLandType::find($value);
+                $typeName = $landType ? $landType->name : '未知';
+                return "<span class='badge badge-info'>{$typeName}({$value})</span>";
+            });
             $grid->column('plant_time', '种植时间')->sortable();
             $helper->columnUseingEnmu('growth_stage', \App\Module\Farm\Enums\GROWTH_STAGE::class,'生长阶段');
 
@@ -192,6 +197,11 @@ class FarmCropController extends AdminController
             $show->field('land_id', '土地ID');
             $helper->fieldUserId('user_id', '用户ID');
             $show->field('seed_id', '种子ID');
+            $show->field('land_level', '土地等级')->as(function ($value) {
+                $landType = \App\Module\Farm\Models\FarmLandType::find($value);
+                $typeName = $landType ? $landType->name : '未知';
+                return "<span class='badge badge-info'>{$typeName}({$value})</span>";
+            });
             $show->field('plant_time', '种植时间');
             $helper->fieldGrowthStage('growth_stage', '生长阶段');
             $show->field('stage_end_time', '阶段结束时间');
@@ -253,6 +263,11 @@ class FarmCropController extends AdminController
 
             // 获取所有种子选项
             $form->display('seed_id', '种子ID');
+
+            // 土地等级字段
+            $form->select('land_level', '土地等级')
+                ->options(\App\Module\Farm\Models\FarmLandType::pluck('name', 'id')->toArray())
+                ->help('种植时记录的土地等级,用于产出计算');
             $form->datetime('plant_time', '种植时间')->required();
             $helper->selectOptionCast('growth_stage', '生长阶段');
             $form->datetime('stage_end_time', '阶段结束时间');

+ 20 - 0
app/Module/Farm/Databases/Migrations/add_land_level_to_farm_crops.sql

@@ -0,0 +1,20 @@
+-- ******************************************************************
+-- 为 farm_crops 表增加土地等级字段
+-- 执行时间:2025年06月22日
+-- 说明:增加 land_level 字段用于记录种植时的土地等级,后续确认作物产出使用该字段的等级
+-- ******************************************************************
+
+-- 为 farm_crops 表增加 land_level 字段
+ALTER TABLE `kku_farm_crops` 
+ADD COLUMN `land_level` tinyint unsigned NOT NULL DEFAULT 1 COMMENT '种植时的土地等级,用于产出计算' AFTER `seed_id`;
+
+-- 添加索引以提高查询性能
+ALTER TABLE `kku_farm_crops` 
+ADD INDEX `idx_land_level` (`land_level`);
+
+-- 更新现有数据:将当前土地的等级设置为作物记录的土地等级
+-- 这里假设土地等级就是土地类型,如果有其他逻辑需要调整
+UPDATE `kku_farm_crops` c 
+INNER JOIN `kku_farm_land_users` l ON c.land_id = l.id 
+SET c.land_level = l.land_type 
+WHERE c.land_level = 1;

+ 10 - 8
app/Module/Farm/Logics/CropLogic.php

@@ -142,6 +142,7 @@ class CropLogic
             $crop->land_id                  = $landId;
             $crop->user_id                  = $userId;
             $crop->seed_id                  = $seedId;
+            $crop->land_level               = $land->land_type; // 记录种植时的土地等级
             $crop->plant_time               = now();
             $crop->growth_stage             = GROWTH_STAGE::SEED;
             $crop->stage_start_time         = now(); // 设置当前阶段开始时间
@@ -336,7 +337,7 @@ class CropLogic
                 'amount' => $outputAmount,
                 'harvest_log_id' => $harvestLog->id,
                 'growth_stage' => GROWTH_STAGE::MATURE->value,
-                'land_type' => $land->land_type ?? 1,
+                'land_type' => $crop->land_level,
                 'old_stage' => $oldStage->value,
                 'new_stage' => GROWTH_STAGE::WITHERED->value,
                 'land_status_before' => $land->status,
@@ -666,7 +667,7 @@ class CropLogic
                 'disaster_type' => $disasterType,
                 'disaster_info' => $disasterInfo,
                 'growth_stage' => $crop->growth_stage->value,
-                'land_type' => $land->land_type ?? 1,
+                'land_type' => $crop->land_level,
                 'has_other_active_disasters' => $hasActiveDisaster,
                 'old_land_status' => $oldLandStatus,
                 'new_land_status' => $land->status,
@@ -999,7 +1000,7 @@ class CropLogic
                             'crop_id'              => $crop->id,
                             'user_id'              => $crop->user_id,
                             'seed_id'              => $seed->id,
-                            'land_type'            => $land->land_type,
+                            'land_type'            => $crop->land_level,
                             'final_output_item_id' => $selectedOutput['item_id']
                         ]);
 
@@ -1007,7 +1008,7 @@ class CropLogic
                         FarmCropLog::logFruitConfirmed($crop->user_id, $crop->land_id, $crop->id, $seed->id, [
                             'final_output_item_id' => $selectedOutput['item_id'],
                             'growth_stage' => $newStage,
-                            'land_type' => $land->land_type,
+                            'land_type' => $crop->land_level,
                             'is_mystery_seed' => true,
                             'selected_output' => $selectedOutput,
                             'land_effect_applied' => true
@@ -1028,7 +1029,7 @@ class CropLogic
                         FarmCropLog::logFruitConfirmed($crop->user_id, $crop->land_id, $crop->id, $seed->id, [
                             'final_output_item_id' => $outputInfo['item_id'],
                             'growth_stage' => $newStage,
-                            'land_type' => $crop->land->land_type ?? 1,
+                            'land_type' => $crop->land_level,
                             'is_mystery_seed' => false,
                             'output_info' => $outputInfo
                         ]);
@@ -1376,8 +1377,9 @@ class CropLogic
             // 3. 计算基础产量(随机值)
             $baseAmount = mt_rand($minBaseAmount, $maxBaseAmount);
 
-            // 3. 获取土地的产量加成
-            $landOutputBonus = $land->landType->output_bonus ?? 0;
+            // 3. 获取土地的产量加成(使用种植时记录的土地等级)
+            $landType = \App\Module\Farm\Models\FarmLandType::find($crop->land_level);
+            $landOutputBonus = $landType ? $landType->output_bonus : 0;
 
             // 4. 获取房屋的产量加成
             $houseConfig = $farmUser->houseConfig;
@@ -1468,7 +1470,7 @@ class CropLogic
                 'disaster_max_amount' => $outputInfo['disaster_max_amount'] ?? 2000,
                 'global_max_output' => $globalMaxOutput,
                 'growth_stage' => GROWTH_STAGE::MATURE->value,
-                'land_type' => $crop->land->land_type ?? 1,
+                'land_type' => $crop->land_level,
                 'final_output_item_id' => $crop->final_output_item_id
             ]);
 

+ 2 - 0
app/Module/Farm/Models/FarmCrop.php

@@ -15,6 +15,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
  * @property  int  $land_id  土地ID
  * @property  int  $user_id  用户ID
  * @property  int  $seed_id  种子ID
+ * @property  int  $land_level  种植时的土地等级
  * @property  \Carbon\Carbon  $plant_time  种植时间
  * @property  \App\Module\Farm\Enums\GROWTH_STAGE  $growth_stage  生长阶段:1种子期,2发芽期,3生长期,4成熟期,5枯萎期
  * @property  \Carbon\Carbon  $stage_start_time  当前阶段结束时间
@@ -52,6 +53,7 @@ class FarmCrop extends Model
         'land_id',
         'user_id',
         'seed_id',
+        'land_level',
         'plant_time',
         'growth_stage',
         'stage_start_time',