| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace App\Module\Farm\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- /**
- * 添加农场配置管理菜单命令
- */
- class InsertFarmConfigAdminMenu extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'farm:insert-config-menu {--force : 强制重新创建菜单}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '添加农场配置管理菜单到后台管理系统';
- /**
- * 农场配置父菜单ID
- *
- * @var int
- */
- protected $farmConfigParentId = 264;
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- try {
- // 检查父菜单是否存在
- if (!$this->checkParentMenu()) {
- $this->error("农场配置父菜单 (ID: {$this->farmConfigParentId}) 不存在");
- return 1;
- }
- // 检查是否强制重新创建
- if ($this->option('force')) {
- $this->deleteExistingMenu();
- }
- // 创建农场配置管理菜单
- $this->createFarmConfigMenu();
- $this->info('农场配置管理菜单添加成功!');
- return 0;
- } catch (\Exception $e) {
- $this->error('添加农场配置管理菜单失败: ' . $e->getMessage());
- return 1;
- }
- }
- /**
- * 检查父菜单是否存在
- *
- * @return bool
- */
- protected function checkParentMenu(): bool
- {
- $parentMenu = DB::table('kku_admin_menu')
- ->where('id', $this->farmConfigParentId)
- ->first();
- if ($parentMenu) {
- $this->info("找到农场配置父菜单: {$parentMenu->title} (ID: {$parentMenu->id})");
- return true;
- }
- return false;
- }
- /**
- * 删除已存在的菜单
- *
- * @return void
- */
- protected function deleteExistingMenu(): void
- {
- $existingMenu = DB::table('kku_admin_menu')
- ->where('parent_id', $this->farmConfigParentId)
- ->where('uri', 'farm-configs')
- ->first();
- if ($existingMenu) {
- DB::table('kku_admin_menu')->where('id', $existingMenu->id)->delete();
- $this->info("删除已存在的农场配置管理菜单 (ID: {$existingMenu->id})");
- }
- }
- /**
- * 创建农场配置管理菜单
- *
- * @return void
- */
- protected function createFarmConfigMenu(): void
- {
- // 检查菜单是否已存在
- $existingMenu = DB::table('kku_admin_menu')
- ->where('parent_id', $this->farmConfigParentId)
- ->where('uri', 'farm-configs')
- ->first();
- if ($existingMenu && !$this->option('force')) {
- $this->info("农场配置管理菜单已存在 (ID: {$existingMenu->id})");
- return;
- }
- // 获取农场配置菜单下的最大order值
- $maxOrder = DB::table('kku_admin_menu')
- ->where('parent_id', $this->farmConfigParentId)
- ->max('order');
- $nextOrder = $maxOrder ? $maxOrder + 1 : 1;
- // 创建农场配置管理菜单
- $menuId = DB::table('kku_admin_menu')->insertGetId([
- 'parent_id' => $this->farmConfigParentId,
- 'order' => $nextOrder,
- 'title' => '农场配置管理',
- 'icon' => 'fa-cogs',
- 'uri' => 'farm-configs',
- 'show' => 1,
- 'extension' => '',
- 'created_at' => now(),
- 'updated_at' => now(),
- ]);
- $this->info("创建农场配置管理菜单成功 (ID: {$menuId})");
- $this->info("菜单标题: 农场配置管理");
- $this->info("菜单URI: farm-configs");
- $this->info("菜单图标: fa-cogs");
- $this->info("菜单顺序: {$nextOrder}");
- }
- }
|