dongasai 6 bulan lalu
induk
melakukan
2d70c5c552

+ 1 - 0
AiWork/记忆习惯.md

@@ -118,6 +118,7 @@
 
 ## 项目相关运行和访问
 - 项目已经使用Docker运行,访问地址:http://kku_laravel.local.gd
+- 用户偏好在game模块创建清理命令,能够安全地清理用户数据、日志等信息,包含完整的安全机制和确认流程
 - 用户已创建ThirdParty模块,专门处理接入第三方服务的需求,与OpenAPI模块互补(OpenAPI提供API给别人,ThirdParty使用别人的API)
 - 系统包含35个模块,Promotionurs模块已被移除,创建了新的UrsPromotion模块来替代
 - UrsPromotion模块是专门为URS业务场景设计的推广系统,与Promotion模块完全独立

+ 242 - 0
app/Module/Farm/AdminControllers/FarmConfigController.php

@@ -0,0 +1,242 @@
+<?php
+
+namespace App\Module\Farm\AdminControllers;
+
+
+use App\Module\Farm\Models\FarmConfig;
+use App\Module\Farm\Repositories\FarmConfigRepository;
+use App\Module\Farm\Services\FarmConfigService;
+use App\Module\Game\AdminControllers\LazyRenderable\GameRewardGroupLazyRenderable;
+use Dcat\Admin\Form;
+use Dcat\Admin\Grid;
+use Dcat\Admin\Show;
+use Dcat\Admin\Http\Controllers\AdminController;
+
+/**
+ * 农场配置管理控制器
+ *
+ * 路由: /admin/farm-configs
+ * 清除缓存路由: POST /admin/farm-configs/clear-cache
+ */
+class FarmConfigController extends AdminController
+{
+    /**
+     * 页面标题
+     *
+     * @var string
+     */
+    protected $title = '农场配置管理';
+
+    /**
+     * 页面描述
+     *
+     * @var string
+     */
+    protected $description = '管理农场相关的配置参数';
+
+    /**
+     * 构建表格
+     *
+     * @return Grid
+     */
+    protected function grid()
+    {
+        return Grid::make(new FarmConfigRepository(), function (Grid $grid) {
+            $grid->column('id', 'ID')->sortable();
+            $grid->column('config_key', '配置键')->copyable();
+            $grid->column('config_name', '配置名称');
+            $grid->column('config_value', '配置值')->display(function ($value) {
+                if (strlen($value) > 50) {
+                    return substr($value, 0, 50) . '...';
+                }
+                return $value;
+            });
+            $grid->column('config_type', '配置类型')->using([
+                'string' => '字符串',
+                'integer' => '整数',
+                'float' => '浮点数',
+                'boolean' => '布尔值',
+                'json' => 'JSON对象',
+            ])->label([
+                'string' => 'primary',
+                'integer' => 'success',
+                'float' => 'warning',
+                'boolean' => 'info',
+                'json' => 'danger',
+            ]);
+            $grid->column('is_active', '状态')->switch();
+            $grid->column('description', '描述')->limit(30);
+            $grid->column('updated_at', '更新时间')->sortable();
+
+            // 禁用创建按钮(配置项通过数据库初始化)
+            $grid->disableCreateButton();
+
+            // 禁用删除操作
+            $grid->disableDeleteButton();
+
+            // 添加清除缓存工具
+            $grid->tools(function (Grid\Tools $tools) {
+                $tools->append('<a href="javascript:void(0)" class="btn btn-sm btn-outline-primary" onclick="clearFarmConfigCache()">清除缓存</a>');
+            });
+
+            // 添加JavaScript
+            admin_script('
+                function clearFarmConfigCache() {
+                    $.post("' . admin_url('farm-configs/clear-cache') . '", {
+                        _token: "' . csrf_token() . '"
+                    }).done(function(data) {
+                        if (data.status) {
+                            Dcat.success(data.message || "缓存清除成功");
+                        } else {
+                            Dcat.error(data.message || "缓存清除失败");
+                        }
+                    }).fail(function() {
+                        Dcat.error("请求失败");
+                    });
+                }
+            ');
+
+            $grid->filter(function (Grid\Filter $filter) {
+                $filter->like('config_key', '配置键');
+                $filter->like('config_name', '配置名称');
+                $filter->equal('config_type', '配置类型')->select([
+                    'string' => '字符串',
+                    'integer' => '整数',
+                    'float' => '浮点数',
+                    'boolean' => '布尔值',
+                    'json' => 'JSON对象',
+                ]);
+                $filter->equal('is_active', '状态')->select([
+                    1 => '启用',
+                    0 => '禁用',
+                ]);
+            });
+        });
+    }
+
+    /**
+     * 构建详情页
+     *
+     * @param mixed $id
+     * @return Show
+     */
+    protected function detail($id)
+    {
+        return Show::make($id, new FarmConfigRepository(), function (Show $show) {
+            $show->field('id', 'ID');
+            $show->field('config_key', '配置键');
+            $show->field('config_name', '配置名称');
+            $show->field('config_value', '配置值');
+            $show->field('config_type', '配置类型')->using([
+                'string' => '字符串',
+                'integer' => '整数',
+                'float' => '浮点数',
+                'boolean' => '布尔值',
+                'json' => 'JSON对象',
+            ]);
+            $show->field('description', '配置描述');
+            $show->field('default_value', '默认值');
+            $show->field('is_active', '启用状态')->using([
+                1 => '启用',
+                0 => '禁用',
+            ]);
+            $show->field('created_at', '创建时间');
+            $show->field('updated_at', '更新时间');
+
+            // 显示类型转换后的值
+            $show->field('typed_value', '类型转换后的值')->as(function () {
+                return json_encode($this->getTypedValue(), JSON_UNESCAPED_UNICODE);
+            });
+        });
+    }
+
+    /**
+     * 构建表单
+     *
+     * @return Form
+     */
+    protected function form()
+    {
+        return Form::make(new FarmConfigRepository(), function (Form $form) {
+
+            $form->display('id', 'ID');
+            $form->display('config_key', '配置键');
+            $form->display('config_name', '配置名称');
+
+            // 根据配置键显示不同的输入控件
+            if ($form->model() && $form->model()->config_key === 'farm_init_reward_group_id') {
+                // 农场初始化奖励组ID - 使用数字输入框
+                $form->number('config_value', '配置值')
+                    ->min(0)
+                    ->help('选择农场初始化时发放的奖励组ID,设置为0表示不发放奖励');
+            } else {
+                // 其他配置项的通用处理
+                $configType = $form->model()->config_type ?? 'string';
+
+                switch ($configType) {
+                    case 'integer':
+                        $form->number('config_value', '配置值');
+                        break;
+                    case 'float':
+                        $form->number('config_value', '配置值')->decimal(2);
+                        break;
+                    case 'boolean':
+                        $form->switch('config_value', '配置值');
+                        break;
+                    case 'json':
+                        $form->textarea('config_value', '配置值')->help('请输入有效的JSON格式');
+                        break;
+                    case 'string':
+                    default:
+                        $form->text('config_value', '配置值');
+                        break;
+                }
+            }
+
+            $form->display('config_type_name', '配置类型');
+            $form->display('description', '配置描述');
+            $form->display('default_value', '默认值');
+            $form->switch('is_active', '启用状态')->default(1);
+
+            $form->display('created_at', '创建时间');
+            $form->display('updated_at', '更新时间');
+
+            // 保存后清除缓存
+            $form->saved(function (Form $form) {
+                FarmConfigService::clearCache();
+            });
+        });
+    }
+
+    /**
+     * 清除配置缓存
+     *
+     * @return \Illuminate\Http\JsonResponse
+     */
+    public function clearCache()
+    {
+        try {
+            FarmConfigService::clearCache();
+            return response()->json([
+                'status' => true,
+                'message' => '农场配置缓存清除成功'
+            ]);
+        } catch (\Exception $e) {
+            return response()->json([
+                'status' => false,
+                'message' => '缓存清除失败:' . $e->getMessage()
+            ]);
+        }
+    }
+
+    /**
+     * 注册自定义路由
+     *
+     * @return void
+     */
+    public static function routes()
+    {
+        // 清除缓存路由
+        admin_route('farm-configs/clear-cache', [static::class, 'clearCache'], ['POST']);
+    }
+}

+ 4 - 0
app/Module/Farm/Databases/GenerateSql/README.md

@@ -4,3 +4,7 @@
 
 此目录下的SQL文件由系统自动生成,用于记录数据库表结构。
 如需修改表结构,请修改对应的模型文件,然后重新运行生成命令。
+
+## 新增文件
+
+- `create_farm_configs_table.sql` - 农场配置表创建脚本(农场配置功能)

+ 20 - 0
app/Module/Farm/Databases/GenerateSql/create_farm_configs_table.sql

@@ -0,0 +1,20 @@
+-- 农场配置表
+CREATE TABLE `kku_farm_configs` (
+  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
+  `config_key` varchar(100) NOT NULL COMMENT '配置键',
+  `config_name` varchar(200) NOT NULL COMMENT '配置名称',
+  `config_value` text COMMENT '配置值',
+  `config_type` varchar(50) NOT NULL DEFAULT 'string' COMMENT '配置类型:string,integer,float,boolean,json',
+  `description` text COMMENT '配置描述',
+  `default_value` text COMMENT '默认值',
+  `is_active` tinyint(1) NOT NULL DEFAULT 1 COMMENT '是否启用:0否,1是',
+  `created_at` timestamp NULL DEFAULT NULL COMMENT '创建时间',
+  `updated_at` timestamp NULL DEFAULT NULL COMMENT '更新时间',
+  PRIMARY KEY (`id`),
+  UNIQUE KEY `uk_config_key` (`config_key`),
+  KEY `idx_is_active` (`is_active`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='农场配置表';
+
+-- 插入初始化奖励组ID配置项
+INSERT INTO `kku_farm_configs` (`config_key`, `config_name`, `config_value`, `config_type`, `description`, `default_value`, `is_active`, `created_at`, `updated_at`) VALUES
+('farm_init_reward_group_id', '农场初始化奖励组ID', '0', 'integer', '用户首次创建农场时发放的奖励组ID,0表示不发放奖励', '0', 1, NOW(), NOW());

+ 50 - 2
app/Module/Farm/Docs/DEV.md

@@ -1,3 +1,51 @@
-# 
+# 清理命令开发任务
 
-FarmCrop 增加了  $stage_start_time 当前阶段开始时间 / $stage_end_time 当前阶段结束时间,对其进行维护,在种植时/状态变化时进行维护 
+## 任务描述
+开发一个清理命令,这是测试数据清理,用于清除所有的模块的运行数据(配置数据不清理)
+
+## 实现方案
+
+### 1. 命令设计
+- 命令名称:`farm:clean-test-data`
+- 支持参数:
+  - `--dry-run`: 预览模式,只显示将要清理的数据,不实际执行
+  - `--force`: 强制执行,跳过确认提示
+  - `--module=`: 指定清理特定模块(可选)
+  - `--user-id=`: 指定清理特定用户的数据(可选)
+
+### 2. 清理范围
+#### 用户运行数据
+- 农场数据:用户农场、土地、作物、神灵加持
+- 物品数据:用户物品、物品实例
+- 资金数据:用户资金、积分
+- 宠物数据:用户宠物、技能、战队
+- 任务数据:用户任务进度
+- 用户信息:用户基础信息、时间记录
+
+#### 日志数据
+- 各模块的操作日志
+- 交易记录
+- 系统请求日志
+
+#### 临时数据
+- 缓存数据
+- 会话数据
+- 队列数据
+
+### 3. 安全机制
+- 多重确认机制
+- 预览模式
+- 分批处理
+- 错误回滚
+- 详细日志记录
+
+### 4. 保留数据
+- 所有配置表
+- 基础数据表(种子、物品、宠物配置等)
+- 系统配置
+
+## 状态
+- [x] 需求分析
+- [ ] 命令实现
+- [ ] 测试验证
+- [ ] 文档更新

+ 94 - 0
app/Module/Farm/Listeners/FarmInitRewardListener.php

@@ -0,0 +1,94 @@
+<?php
+
+namespace App\Module\Farm\Listeners;
+
+use App\Module\Farm\Events\FarmCreatedEvent;
+use App\Module\Farm\Services\FarmConfigService;
+use App\Module\Game\Services\RewardService;
+use Illuminate\Support\Facades\Log;
+
+/**
+ * 农场初始化奖励监听器
+ * 
+ * 监听农场创建事件,发放初始化奖励
+ */
+class FarmInitRewardListener
+{
+    /**
+     * 处理农场创建事件
+     *
+     * @param FarmCreatedEvent $event
+     * @return void
+     */
+    public function handle(FarmCreatedEvent $event)
+    {
+        try {
+            $userId = $event->userId;
+            $farmUser = $event->farmUser;
+
+            // 获取农场初始化奖励组ID
+            $rewardGroupId = FarmConfigService::getInitRewardGroupId();
+
+            // 如果奖励组ID为0,表示不发放奖励
+            if ($rewardGroupId <= 0) {
+                Log::info('农场初始化奖励未配置,跳过奖励发放', [
+                    'user_id' => $userId,
+                    'farm_id' => $farmUser->id,
+                    'reward_group_id' => $rewardGroupId
+                ]);
+                return;
+            }
+
+            // 检查奖励组是否存在
+            if (!RewardService::rewardGroupExists($rewardGroupId)) {
+                Log::warning('农场初始化奖励组不存在', [
+                    'user_id' => $userId,
+                    'farm_id' => $farmUser->id,
+                    'reward_group_id' => $rewardGroupId
+                ]);
+                return;
+            }
+
+            // 发放初始化奖励
+            $result = RewardService::grantReward(
+                $userId,
+                $rewardGroupId,
+                'farm_init', // 来源类型:农场初始化
+                $farmUser->id // 来源ID:农场ID
+            );
+
+            if ($result->success) {
+                Log::info('农场初始化奖励发放成功', [
+                    'user_id' => $userId,
+                    'farm_id' => $farmUser->id,
+                    'reward_group_id' => $rewardGroupId,
+                    'reward_items_count' => count($result->items)
+                ]);
+
+                // 记录详细的奖励信息
+                foreach ($result->items as $item) {
+                    Log::debug('农场初始化奖励详情', [
+                        'user_id' => $userId,
+                        'reward_type' => $item->rewardType,
+                        'target_id' => $item->targetId,
+                        'quantity' => $item->quantity
+                    ]);
+                }
+            } else {
+                Log::error('农场初始化奖励发放失败', [
+                    'user_id' => $userId,
+                    'farm_id' => $farmUser->id,
+                    'reward_group_id' => $rewardGroupId,
+                    'error_message' => $result->errorMessage
+                ]);
+            }
+        } catch (\Exception $e) {
+            Log::error('农场初始化奖励监听器处理失败', [
+                'user_id' => $event->userId,
+                'farm_id' => $event->farmUser->id,
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+        }
+    }
+}

+ 192 - 0
app/Module/Farm/Logics/FarmConfigLogic.php

@@ -0,0 +1,192 @@
+<?php
+
+namespace App\Module\Farm\Logics;
+
+use App\Module\Farm\Models\FarmConfig;
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Log;
+
+/**
+ * 农场配置逻辑类
+ */
+class FarmConfigLogic
+{
+    /**
+     * 缓存前缀
+     *
+     * @var string
+     */
+    private $cachePrefix = 'farm_config:';
+
+    /**
+     * 缓存时间(秒)
+     *
+     * @var int
+     */
+    private $cacheTime = 3600; // 1小时
+
+    /**
+     * 获取配置值
+     *
+     * @param string $configKey 配置键
+     * @param mixed $defaultValue 默认值
+     * @return mixed
+     */
+    public function getConfigValue(string $configKey, $defaultValue = null)
+    {
+        try {
+            // 尝试从缓存获取
+            $cacheKey = $this->cachePrefix . $configKey;
+            $cachedValue = Cache::get($cacheKey);
+
+            if ($cachedValue !== null) {
+                return $cachedValue;
+            }
+
+            // 从数据库获取
+            $config = FarmConfig::where('config_key', $configKey)
+                ->where('is_active', true)
+                ->first();
+
+            if (!$config) {
+                // 配置不存在,返回默认值
+                Cache::put($cacheKey, $defaultValue, $this->cacheTime);
+                return $defaultValue;
+            }
+
+            $value = $config->getTypedValue();
+
+            // 缓存配置值
+            Cache::put($cacheKey, $value, $this->cacheTime);
+
+            return $value;
+        } catch (\Exception $e) {
+            Log::error('获取农场配置失败', [
+                'config_key' => $configKey,
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+
+            return $defaultValue;
+        }
+    }
+
+    /**
+     * 设置配置值
+     *
+     * @param string $configKey 配置键
+     * @param mixed $value 配置值
+     * @return bool
+     */
+    public function setConfigValue(string $configKey, $value): bool
+    {
+        try {
+            $config = FarmConfig::where('config_key', $configKey)->first();
+
+            if (!$config) {
+                Log::warning('尝试设置不存在的农场配置', [
+                    'config_key' => $configKey,
+                    'value' => $value
+                ]);
+                return false;
+            }
+
+            $config->setTypedValue($value);
+            $config->save();
+
+            // 清除缓存
+            $cacheKey = $this->cachePrefix . $configKey;
+            Cache::forget($cacheKey);
+
+            Log::info('农场配置更新成功', [
+                'config_key' => $configKey,
+                'old_value' => $config->getOriginal('config_value'),
+                'new_value' => $config->config_value
+            ]);
+
+            return true;
+        } catch (\Exception $e) {
+            Log::error('设置农场配置失败', [
+                'config_key' => $configKey,
+                'value' => $value,
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+
+            return false;
+        }
+    }
+
+    /**
+     * 获取所有配置
+     *
+     * @return array
+     */
+    public function getAllConfigs(): array
+    {
+        try {
+            $configs = FarmConfig::where('is_active', true)
+                ->orderBy('config_key')
+                ->get();
+
+            $result = [];
+            foreach ($configs as $config) {
+                $result[$config->config_key] = $config->getTypedValue();
+            }
+
+            return $result;
+        } catch (\Exception $e) {
+            Log::error('获取所有农场配置失败', [
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+
+            return [];
+        }
+    }
+
+    /**
+     * 清除所有配置缓存
+     *
+     * @return void
+     */
+    public function clearCache(): void
+    {
+        try {
+            $configs = FarmConfig::pluck('config_key');
+
+            foreach ($configs as $configKey) {
+                $cacheKey = $this->cachePrefix . $configKey;
+                Cache::forget($cacheKey);
+            }
+
+            Log::info('农场配置缓存清除成功');
+        } catch (\Exception $e) {
+            Log::error('清除农场配置缓存失败', [
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+        }
+    }
+
+    /**
+     * 获取农场初始化奖励组ID
+     *
+     * @return int
+     */
+    public function getInitRewardGroupId(): int
+    {
+        return (int) $this->getConfigValue('farm_init_reward_group_id', 0);
+    }
+
+    /**
+     * 设置农场初始化奖励组ID
+     *
+     * @param int $groupId 奖励组ID
+     * @return bool
+     */
+    public function setInitRewardGroupId(int $groupId): bool
+    {
+        return $this->setConfigValue('farm_init_reward_group_id', $groupId);
+    }
+}

+ 129 - 0
app/Module/Farm/Models/FarmConfig.php

@@ -0,0 +1,129 @@
+<?php
+
+namespace App\Module\Farm\Models;
+
+use UCore\ModelCore;
+
+/**
+ * 农场配置模型
+ * field start 
+ * @property  int  $id  主键ID
+ * @property  string  $config_key  配置键
+ * @property  string  $config_name  配置名称
+ * @property  string  $config_value  配置值
+ * @property  string  $config_type  配置类型:string,integer,float,boolean,json
+ * @property  string  $description  配置描述
+ * @property  string  $default_value  默认值
+ * @property  bool  $is_active  是否启用:0否,1是
+ * @property  \Carbon\Carbon  $created_at  创建时间
+ * @property  \Carbon\Carbon  $updated_at  更新时间
+ * field end
+ */
+class FarmConfig extends ModelCore
+{
+    /**
+     * 与模型关联的表名
+     *
+     * @var string
+     */
+    protected $table = 'farm_configs';
+
+    // attrlist start 
+    protected $fillable = [
+        'id',
+        'config_key',
+        'config_name',
+        'config_value',
+        'config_type',
+        'description',
+        'default_value',
+        'is_active',
+    ];
+    // attrlist end
+
+    /**
+     * 应该被转换为原生类型的属性
+     *
+     * @var array
+     */
+    protected $casts = [
+        'is_active' => 'boolean',
+    ];
+
+    /**
+     * 获取配置值(根据类型转换)
+     *
+     * @return mixed
+     */
+    public function getTypedValue()
+    {
+        $value = $this->config_value ?? $this->default_value;
+
+        if ($value === null) {
+            return null;
+        }
+
+        switch ($this->config_type) {
+            case 'integer':
+                return (int) $value;
+            case 'float':
+                return (float) $value;
+            case 'boolean':
+                return in_array(strtolower($value), ['1', 'true', 'yes', 'on']);
+            case 'json':
+                return json_decode($value, true);
+            case 'string':
+            default:
+                return (string) $value;
+        }
+    }
+
+    /**
+     * 设置配置值(根据类型转换)
+     *
+     * @param mixed $value
+     * @return void
+     */
+    public function setTypedValue($value): void
+    {
+        switch ($this->config_type) {
+            case 'json':
+                $this->config_value = json_encode($value);
+                break;
+            case 'boolean':
+                $this->config_value = $value ? '1' : '0';
+                break;
+            default:
+                $this->config_value = (string) $value;
+                break;
+        }
+    }
+
+    /**
+     * 获取配置类型的中文名称
+     *
+     * @return string
+     */
+    public function getConfigTypeNameAttribute(): string
+    {
+        $types = [
+            'string' => '字符串',
+            'integer' => '整数',
+            'float' => '浮点数',
+            'boolean' => '布尔值',
+            'json' => 'JSON对象',
+        ];
+
+        return $types[$this->config_type] ?? '未知';
+    }
+
+    /**
+     * 获取启用状态的中文名称
+     *
+     * @return string
+     */
+    public function getIsActiveNameAttribute(): string
+    {
+        return $this->is_active ? '启用' : '禁用';
+    }
+}

+ 33 - 1
app/Module/Farm/Providers/FarmServiceProvider.php

@@ -5,8 +5,10 @@ namespace App\Module\Farm\Providers;
 use App\Module\Farm\Commands;
 use App\Module\AppGame\Events\LoginSuccessEvent;
 use App\Module\Farm\Events\CropGrowthStageChangedEvent;
+use App\Module\Farm\Events\FarmCreatedEvent;
 use App\Module\Farm\Events\HouseUpgradedEvent;
 use App\Module\Farm\Listeners\AddLandAfterHouseUpgradeListener;
+use App\Module\Farm\Listeners\FarmInitRewardListener;
 use App\Module\Farm\Listeners\GenerateDisasterListener;
 use App\Module\Farm\Listeners\LoginSuccessListener;
 use App\Module\Farm\Listeners\UpdateCropStatusListener;
@@ -32,9 +34,14 @@ class FarmServiceProvider extends ServiceProvider
         HouseUpgradedEvent::class => [
             AddLandAfterHouseUpgradeListener::class,
         ],
+
         LoginSuccessEvent::class => [
             LoginSuccessListener::class,
         ],
+
+        FarmCreatedEvent::class => [
+            FarmInitRewardListener::class,
+        ],
     ];
 
     /**
@@ -57,7 +64,8 @@ class FarmServiceProvider extends ServiceProvider
             Commands\GenerateFarmSeedConfigJson::class,
             Commands\MigrateLandUpgradeMaterialsToConsumeGroupsCommand::class,
             Commands\MigrateLandUpgradeConditionsToConditionGroupsCommand::class,
-            Commands\InitializeUserLandsCommand::class
+            Commands\InitializeUserLandsCommand::class,
+            Commands\TestFarmConfigCommand::class
         ]);
 
 
@@ -79,6 +87,9 @@ class FarmServiceProvider extends ServiceProvider
             }
         }
 
+        // 注册农场配置管理路由
+        $this->registerAdminRoutes();
+
         // 注册定时任务监听器
 //        $this->app->booted(function () {
 //            $schedule = $this->app->make(\Illuminate\Console\Scheduling\Schedule::class);
@@ -94,4 +105,25 @@ class FarmServiceProvider extends ServiceProvider
 //            $schedule->command('farm:rebuild-cache')->dailyAt('05:00');
 //        });
     }
+
+    /**
+     * 注册后台管理路由
+     *
+     * @return void
+     */
+    protected function registerAdminRoutes()
+    {
+        $attributes = [
+            'prefix' => config('admin.route.prefix'),
+            'middleware' => config('admin.route.middleware'),
+        ];
+
+        app('router')->group($attributes, function ($router) {
+            // 农场配置管理路由
+            $router->resource('farm-configs', \App\Module\Farm\AdminControllers\FarmConfigController::class);
+
+            // 清除缓存路由
+            $router->post('farm-configs/clear-cache', [\App\Module\Farm\AdminControllers\FarmConfigController::class, 'clearCache']);
+        });
+    }
 }

+ 19 - 0
app/Module/Farm/Repositories/FarmConfigRepository.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Module\Farm\Repositories;
+
+use App\Module\Farm\Models\FarmConfig;
+use Dcat\Admin\Repositories\EloquentRepository;
+
+/**
+ * 农场配置仓库
+ */
+class FarmConfigRepository extends EloquentRepository
+{
+    /**
+     * 模型类名
+     *
+     * @var string
+     */
+    protected $eloquentClass = FarmConfig::class;
+}

+ 139 - 0
app/Module/Farm/Services/FarmConfigService.php

@@ -0,0 +1,139 @@
+<?php
+
+namespace App\Module\Farm\Services;
+
+use App\Module\Farm\Logics\FarmConfigLogic;
+use Illuminate\Support\Facades\Log;
+
+/**
+ * 农场配置服务类
+ */
+class FarmConfigService
+{
+    /**
+     * 获取配置值
+     *
+     * @param string $configKey 配置键
+     * @param mixed $defaultValue 默认值
+     * @return mixed
+     */
+    public static function getConfigValue(string $configKey, $defaultValue = null)
+    {
+        try {
+            $logic = new FarmConfigLogic();
+            return $logic->getConfigValue($configKey, $defaultValue);
+        } catch (\Exception $e) {
+            Log::error('获取农场配置失败', [
+                'config_key' => $configKey,
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+
+            return $defaultValue;
+        }
+    }
+
+    /**
+     * 设置配置值
+     *
+     * @param string $configKey 配置键
+     * @param mixed $value 配置值
+     * @return bool
+     */
+    public static function setConfigValue(string $configKey, $value): bool
+    {
+        try {
+            $logic = new FarmConfigLogic();
+            return $logic->setConfigValue($configKey, $value);
+        } catch (\Exception $e) {
+            Log::error('设置农场配置失败', [
+                'config_key' => $configKey,
+                'value' => $value,
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+
+            return false;
+        }
+    }
+
+    /**
+     * 获取所有配置
+     *
+     * @return array
+     */
+    public static function getAllConfigs(): array
+    {
+        try {
+            $logic = new FarmConfigLogic();
+            return $logic->getAllConfigs();
+        } catch (\Exception $e) {
+            Log::error('获取所有农场配置失败', [
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+
+            return [];
+        }
+    }
+
+    /**
+     * 清除所有配置缓存
+     *
+     * @return void
+     */
+    public static function clearCache(): void
+    {
+        try {
+            $logic = new FarmConfigLogic();
+            $logic->clearCache();
+        } catch (\Exception $e) {
+            Log::error('清除农场配置缓存失败', [
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+        }
+    }
+
+    /**
+     * 获取农场初始化奖励组ID
+     *
+     * @return int
+     */
+    public static function getInitRewardGroupId(): int
+    {
+        try {
+            $logic = new FarmConfigLogic();
+            return $logic->getInitRewardGroupId();
+        } catch (\Exception $e) {
+            Log::error('获取农场初始化奖励组ID失败', [
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+
+            return 0;
+        }
+    }
+
+    /**
+     * 设置农场初始化奖励组ID
+     *
+     * @param int $groupId 奖励组ID
+     * @return bool
+     */
+    public static function setInitRewardGroupId(int $groupId): bool
+    {
+        try {
+            $logic = new FarmConfigLogic();
+            return $logic->setInitRewardGroupId($groupId);
+        } catch (\Exception $e) {
+            Log::error('设置农场初始化奖励组ID失败', [
+                'group_id' => $groupId,
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+
+            return false;
+        }
+    }
+}