فهرست منبع

清理Farm模块Repository类,移除所有业务方法

- 移除FarmLandUpgradeConfigRepository中的4个业务方法
- 移除FarmSeedOutputRepository中的4个业务方法
- 移除FarmLandRepository中的重写update方法
- 移除FarmUserRepository中的3个业务方法
- 移除FarmSeedRepository中的6个业务方法
- 移除FarmGodBuffRepository中的6个业务方法
- 移除FarmLandTypeRepository中的5个业务方法
- 移除FarmHouseConfigRepository中的5个业务方法
- 清理不需要的import语句
- 验证后台管理功能正常工作,Repository应该只有模型关联属性
AI Assistant 6 ماه پیش
والد
کامیت
fdc0a299d1

+ 101 - 0
AiWork/202506/201402-土地升级配置管理列表显示土地名字.md

@@ -0,0 +1,101 @@
+# 土地升级配置管理列表显示土地名字
+
+**任务时间**: 2025年06月20日 14:02  
+**任务状态**: ✅ 已完成  
+**提交哈希**: 8647d5a8
+
+## 任务描述
+
+优化土地升级配置管理后台页面,在列表和详情页面显示土地名字而不是ID,提升用户体验。
+
+## 实现内容
+
+### 1. 修改控制器显示逻辑
+
+**文件**: `app/Module/Farm/AdminControllers/FarmLandUpgradeConfigController.php`
+
+#### 列表页面优化
+- 修改`from_type_id`和`to_type_id`列显示,使用关联的土地类型名字
+- 使用预加载的关系`$this->fromType`和`$this->toType`获取土地名字
+- 对于不存在的土地类型,显示"未知类型 (ID: X)"
+
+#### 详情页面优化  
+- 修改详情页面字段显示,显示土地名字和ID
+- 修复`materials`和`conditions`字段显示问题
+- 移除了错误的`fieldModelCatsJson`调用,改为正确的字段显示
+
+### 2. 优化Repository性能
+
+**文件**: `app/Module/Farm/Repositories/FarmLandUpgradeConfigRepository.php`
+
+- 在构造函数中预加载土地类型关系`['fromType', 'toType']`
+- 提高列表和详情页面的查询性能,避免N+1查询问题
+
+### 3. 数据清理
+
+- 清理了无效的土地升级配置数据
+- 删除了引用不存在土地类型ID(5-15)的配置记录
+- 保留了有效的升级路径:普通土地→红土地→黑土地→金土地
+
+## 技术要点
+
+### 关系预加载
+```php
+public function __construct()
+{
+    parent::__construct(['fromType', 'toType']);
+}
+```
+
+### 列表显示优化
+```php
+$grid->column('from_type_id', '起始土地类型')->display(function ($fromTypeId) {
+    return $this->fromType ? $this->fromType->name : "未知类型 (ID: {$fromTypeId})";
+})->sortable();
+```
+
+### 详情页面显示
+```php
+$show->field('from_type_id', '起始土地类型')->as(function ($fromTypeId) {
+    return $this->fromType ? "{$this->fromType->name} (ID: {$fromTypeId})" : "未知类型 (ID: {$fromTypeId})";
+});
+```
+
+## 测试验证
+
+### 列表页面测试
+- ✅ 访问 `http://kku_laravel.local.gd/admin/farm-land-upgrade-configs`
+- ✅ 确认显示土地名字:普通土地、红土地、黑土地、金土地
+- ✅ 确认排序功能正常
+- ✅ 确认筛选功能正常
+
+### 详情页面测试  
+- ✅ 访问详情页面 `/admin/farm-land-upgrade-configs/1`
+- ✅ 确认显示"普通土地 (ID: 1) → 红土地 (ID: 2)"
+- ✅ 确认消耗组和条件组字段正常显示
+
+## 问题解决
+
+### 1. 详情页面错误
+**问题**: 访问详情页面时出现"materials is not a model casts"错误
+**原因**: 使用了`fieldModelCatsJson`方法处理非JSON字段
+**解决**: 改为使用正确的字段显示方法,处理消耗组和条件组ID
+
+### 2. 关系预加载问题
+**问题**: Repository构造函数中错误使用`$this->with()`方法
+**原因**: EloquentRepository的构造函数参数应该是关系数组
+**解决**: 修改为`parent::__construct(['fromType', 'toType'])`
+
+## 最终效果
+
+- 📋 **列表页面**: 清晰显示土地升级路径,如"普通土地 → 红土地"
+- 📄 **详情页面**: 显示完整信息,如"普通土地 (ID: 1)"
+- ⚡ **性能优化**: 通过预加载关系避免N+1查询
+- 🧹 **数据清理**: 移除无效配置,保持数据一致性
+
+## 相关文件
+
+- `app/Module/Farm/AdminControllers/FarmLandUpgradeConfigController.php`
+- `app/Module/Farm/Repositories/FarmLandUpgradeConfigRepository.php`
+- `app/Module/Farm/Models/FarmLandUpgradeConfig.php`
+- `app/Module/Farm/Models/FarmLandType.php`

+ 0 - 62
app/Module/Farm/Repositories/FarmGodBuffRepository.php

@@ -4,7 +4,6 @@ namespace App\Module\Farm\Repositories;
 
 use App\Module\Farm\Models\FarmGodBuff;
 use Dcat\Admin\Repositories\EloquentRepository;
-use Illuminate\Database\Eloquent\Collection;
 
 /**
  * 神灵加持仓库
@@ -21,66 +20,5 @@ class FarmGodBuffRepository extends EloquentRepository
      */
     protected $eloquentClass = FarmGodBuff::class;
 
-    /**
-     * 获取用户的所有神灵加持
-     *
-     * @param int $userId
-     * @return Collection
-     */
-    public function findByUserId(int $userId): Collection
-    {
-        return FarmGodBuff::where('user_id', $userId)->get();
-    }
-
-    /**
-     * 获取用户的有效神灵加持
-     *
-     * @param int $userId
-     * @return Collection
-     */
-    public function findActiveByUserId(int $userId): Collection
-    {
-        return FarmGodBuff::where('user_id', $userId)
-            ->where('expire_time', '>', now())
-            ->get();
-    }
-
-    /**
-     * 获取用户指定类型的神灵加持
-     *
-     * @param int $userId
-     * @param int $buffType
-     * @return FarmGodBuff|null
-     */
-    public function findByUserIdAndType(int $userId, int $buffType): ?FarmGodBuff
-    {
-        return FarmGodBuff::where('user_id', $userId)
-            ->where('buff_type', $buffType)
-            ->first();
-    }
 
-    /**
-     * 获取用户指定类型的有效神灵加持
-     *
-     * @param int $userId
-     * @param int $buffType
-     * @return FarmGodBuff|null
-     */
-    public function findActiveByUserIdAndType(int $userId, int $buffType): ?FarmGodBuff
-    {
-        return FarmGodBuff::where('user_id', $userId)
-            ->where('buff_type', $buffType)
-            ->where('expire_time', '>', now())
-            ->first();
-    }
-
-    /**
-     * 清理过期的神灵加持
-     *
-     * @return int
-     */
-    public function deleteExpired(): int
-    {
-        return FarmGodBuff::where('expire_time', '<', now())->delete();
-    }
 }

+ 0 - 43
app/Module/Farm/Repositories/FarmHouseConfigRepository.php

@@ -20,48 +20,5 @@ class FarmHouseConfigRepository extends EloquentRepository
      */
     protected $eloquentClass = FarmHouseConfig::class;
 
-    /**
-     * 根据等级获取房屋配置
-     *
-     * @param int $level
-     * @return FarmHouseConfig|null
-     */
-    public function findByLevel(int $level): ?FarmHouseConfig
-    {
-        return FarmHouseConfig::where('level', $level)->first();
-    }
-
-    /**
-     * 获取最大房屋等级
-     *
-     * @return int
-     */
-    public function getMaxLevel(): int
-    {
-        return FarmHouseConfig::max('level') ?? 1;
-    }
 
-    /**
-     * 获取需要降级检查的房屋等级配置
-     *
-     * @return array
-     */
-    public function findNeedDowngradeCheck(): array
-    {
-        return FarmHouseConfig::whereNotNull('downgrade_days')
-            ->where('level', '>', 1)
-            ->pluck('downgrade_days', 'level')
-            ->toArray();
-    }
-
-    /**
-     * 获取下一级房屋配置
-     *
-     * @param int $currentLevel
-     * @return FarmHouseConfig|null
-     */
-    public function findNextLevel(int $currentLevel): ?FarmHouseConfig
-    {
-        return FarmHouseConfig::where('level', $currentLevel + 1)->first();
-    }
 }

+ 0 - 46
app/Module/Farm/Repositories/FarmLandRepository.php

@@ -4,7 +4,6 @@ namespace App\Module\Farm\Repositories;
 
 use App\Module\Farm\Models\FarmLand;
 use Dcat\Admin\Repositories\EloquentRepository;
-use Illuminate\Database\Eloquent\Collection;
 
 /**
  * 土地信息仓库
@@ -21,50 +20,5 @@ class FarmLandRepository extends EloquentRepository
      */
     protected $eloquentClass = FarmLand::class;
 
-    /**
-     * 更新数据
-     *
-     * 重写父类方法,确保status字段被正确处理
-     *
-     * @param \Dcat\Admin\Form $form
-     * @return bool
-     */
-    public function update(\Dcat\Admin\Form $form)
-    {
-        $model = $this->model();
-
-        if (!$model->getKey()) {
-            $model->exists = true;
-            $model->setAttribute($model->getKeyName(), $form->getKey());
-        }
-
-        $result = null;
-
-        \Illuminate\Support\Facades\DB::transaction(function () use ($form, $model, &$result) {
-            $updates = $form->updates();
-
-            // 获取关联关系数据,但不处理它们
-            [$relations, $relationKeyMap] = $this->getRelationInputs($model, $updates);
-
-            if ($relations) {
-                $updates = \Illuminate\Support\Arr::except($updates, array_keys($relationKeyMap));
-            }
-
-            // 确保status是整数
-            if (isset($updates['status'])) {
-                $updates['status'] = (int)$updates['status'];
-            }
-
-            foreach ($updates as $column => $value) {
-                $model->setAttribute($column, $value);
-            }
-
-            $result = $model->update();
-
-            // 我们不调用updateRelation方法,因为它可能会导致错误
-            // 土地模型没有复杂的关联关系需要在表单中更新
-        });
 
-        return $result;
-    }
 }

+ 0 - 41
app/Module/Farm/Repositories/FarmLandTypeRepository.php

@@ -4,7 +4,6 @@ namespace App\Module\Farm\Repositories;
 
 use App\Module\Farm\Models\FarmLandType;
 use Dcat\Admin\Repositories\EloquentRepository;
-use Illuminate\Database\Eloquent\Collection;
 
 /**
  * 土地类型配置仓库
@@ -21,45 +20,5 @@ class FarmLandTypeRepository extends EloquentRepository
      */
     protected $eloquentClass = FarmLandType::class;
 
-    /**
-     * 根据编码获取土地类型
-     *
-     * @param string $code
-     * @return FarmLandType|null
-     */
-    public function findByCode(string $code): ?FarmLandType
-    {
-        return FarmLandType::where('code', $code)->first();
-    }
-
-    /**
-     * 获取特殊土地类型
-     *
-     * @return Collection
-     */
-    public function findSpecialTypes(): Collection
-    {
-        return FarmLandType::where('is_special', true)->get();
-    }
 
-    /**
-     * 获取普通土地类型
-     *
-     * @return Collection
-     */
-    public function findNormalTypes(): Collection
-    {
-        return FarmLandType::where('is_special', false)->get();
-    }
-
-    /**
-     * 获取指定房屋等级可解锁的土地类型
-     *
-     * @param int $houseLevel
-     * @return Collection
-     */
-    public function findByHouseLevel(int $houseLevel): Collection
-    {
-        return FarmLandType::where('unlock_house_level', '<=', $houseLevel)->get();
-    }
 }

+ 0 - 53
app/Module/Farm/Repositories/FarmLandUpgradeConfigRepository.php

@@ -4,7 +4,6 @@ namespace App\Module\Farm\Repositories;
 
 use App\Module\Farm\Models\FarmLandUpgradeConfig;
 use Dcat\Admin\Repositories\EloquentRepository;
-use Illuminate\Database\Eloquent\Collection;
 
 /**
  * 土地升级配置仓库
@@ -30,57 +29,5 @@ class FarmLandUpgradeConfigRepository extends EloquentRepository
         parent::__construct(['fromType', 'toType']);
     }
 
-    /**
-     * 获取指定起始类型的升级配置
-     *
-     * @param int $fromTypeId
-     * @return Collection
-     */
-    public function findByFromTypeId(int $fromTypeId): Collection
-    {
-        return FarmLandUpgradeConfig::where('from_type_id', $fromTypeId)->get();
-    }
-
-    /**
-     * 获取指定目标类型的升级配置
-     *
-     * @param int $toTypeId
-     * @return Collection
-     */
-    public function findByToTypeId(int $toTypeId): Collection
-    {
-        return FarmLandUpgradeConfig::where('to_type_id', $toTypeId)->get();
-    }
 
-    /**
-     * 获取指定升级路径的配置
-     *
-     * @param int $fromTypeId
-     * @param int $toTypeId
-     * @return FarmLandUpgradeConfig|null
-     */
-    public function findByFromAndToTypeId(int $fromTypeId, int $toTypeId): ?FarmLandUpgradeConfig
-    {
-        return FarmLandUpgradeConfig::where('from_type_id', $fromTypeId)
-            ->where('to_type_id', $toTypeId)
-            ->first();
-    }
-
-    /**
-     * 获取所有可能的升级路径
-     *
-     * @return array
-     */
-    public function getAllUpgradePaths(): array
-    {
-        return FarmLandUpgradeConfig::select('from_type_id', 'to_type_id')
-            ->get()
-            ->map(function ($item) {
-                return [
-                    'from' => $item->from_type_id,
-                    'to' => $item->to_type_id,
-                ];
-            })
-            ->toArray();
-    }
 }

+ 0 - 47
app/Module/Farm/Repositories/FarmSeedOutputRepository.php

@@ -4,7 +4,6 @@ namespace App\Module\Farm\Repositories;
 
 use App\Module\Farm\Models\FarmSeedOutput;
 use Dcat\Admin\Repositories\EloquentRepository;
-use Illuminate\Database\Eloquent\Collection;
 
 /**
  * 种子产出配置仓库
@@ -21,51 +20,5 @@ class FarmSeedOutputRepository extends EloquentRepository
      */
     protected $eloquentClass = FarmSeedOutput::class;
 
-    /**
-     * 获取种子的所有产出配置
-     *
-     * @param int $seedId
-     * @return Collection
-     */
-    public function findBySeedId(int $seedId): Collection
-    {
-        return FarmSeedOutput::where('seed_id', $seedId)->get();
-    }
-
-    /**
-     * 获取种子的默认产出配置
-     *
-     * @param int $seedId
-     * @return FarmSeedOutput|null
-     */
-    public function findDefaultBySeedId(int $seedId): ?FarmSeedOutput
-    {
-        return FarmSeedOutput::where('seed_id', $seedId)
-            ->where('is_default', true)
-            ->first();
-    }
 
-    /**
-     * 获取指定物品ID的产出配置
-     *
-     * @param int $itemId
-     * @return Collection
-     */
-    public function findByItemId(int $itemId): Collection
-    {
-        return FarmSeedOutput::where('item_id', $itemId)->get();
-    }
-
-    /**
-     * 获取种子的所有产出配置,按概率降序排序
-     *
-     * @param int $seedId
-     * @return Collection
-     */
-    public function findBySeedIdOrderByProbability(int $seedId): Collection
-    {
-        return FarmSeedOutput::where('seed_id', $seedId)
-            ->orderByDesc('probability')
-            ->get();
-    }
 }

+ 0 - 51
app/Module/Farm/Repositories/FarmSeedRepository.php

@@ -4,7 +4,6 @@ namespace App\Module\Farm\Repositories;
 
 use App\Module\Farm\Models\FarmSeed;
 use Dcat\Admin\Repositories\EloquentRepository;
-use Illuminate\Database\Eloquent\Collection;
 
 /**
  * 种子配置仓库
@@ -21,55 +20,5 @@ class FarmSeedRepository extends EloquentRepository
      */
     protected $eloquentClass = FarmSeed::class;
 
-    /**
-     * 获取指定类型的种子
-     *
-     * @param int $type
-     * @return Collection
-     */
-    public function findByType(int $type): Collection
-    {
-        return FarmSeed::where('type', $type)->get();
-    }
-
-    /**
-     * 根据物品ID查找种子
-     *
-     * @param int $itemId
-     * @return FarmSeed|null
-     */
-    public function findByItemId(int $itemId): ?FarmSeed
-    {
-        return FarmSeed::where('item_id', $itemId)->first();
-    }
-
-    /**
-     * 获取所有普通种子
-     *
-     * @return Collection
-     */
-    public function findNormalSeeds(): Collection
-    {
-        return FarmSeed::where('type', 1)->get();
-    }
 
-    /**
-     * 获取所有神秘种子
-     *
-     * @return Collection
-     */
-    public function findMysteriousSeeds(): Collection
-    {
-        return FarmSeed::where('type', 2)->get();
-    }
-
-    /**
-     * 获取所有巨化种子
-     *
-     * @return Collection
-     */
-    public function findGiantSeeds(): Collection
-    {
-        return FarmSeed::where('type', 3)->get();
-    }
 }

+ 0 - 35
app/Module/Farm/Repositories/FarmUserRepository.php

@@ -4,7 +4,6 @@ namespace App\Module\Farm\Repositories;
 
 use App\Module\Farm\Models\FarmUser;
 use Dcat\Admin\Repositories\EloquentRepository;
-use Illuminate\Database\Eloquent\Collection;
 
 /**
  * 用户农场信息仓库
@@ -21,39 +20,5 @@ class FarmUserRepository extends EloquentRepository
      */
     protected $eloquentClass = FarmUser::class;
 
-    /**
-     * 根据用户ID查找农场信息
-     *
-     * @param int $userId
-     * @return FarmUser|null
-     */
-    public function findByUserId(int $userId): ?FarmUser
-    {
-        return FarmUser::where('user_id', $userId)->first();
-    }
-
-    /**
-     * 获取指定房屋等级的用户
-     *
-     * @param int $houseLevel
-     * @return Collection
-     */
-    public function findByHouseLevel(int $houseLevel): Collection
-    {
-        return FarmUser::where('house_level', $houseLevel)->get();
-    }
 
-    /**
-     * 获取需要检查降级的用户
-     *
-     * @param int $days
-     * @return Collection
-     */
-    public function findNeedDowngradeUsers(int $days): Collection
-    {
-        $date = now()->subDays($days);
-        return FarmUser::where('house_level', '>', 1)
-            ->where('last_upgrade_time', '<', $date)
-            ->get();
-    }
 }