| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Dcat\Admin\Models\Menu as AdminMenu;
- /**
- * 添加宠物激活技能菜单命令
- */
- class AddPetActiveSkillMenu extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'pet:add-active-skill-menu {--force : 强制重新创建菜单}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '添加宠物激活技能菜单到后台管理系统';
- /**
- * 宠物管理父菜单ID
- *
- * @var int
- */
- protected $petParentId = 494;
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- try {
- // 检查父菜单是否存在
- if (!$this->checkParentMenu()) {
- $this->error("宠物管理父菜单 (ID: {$this->petParentId}) 不存在");
- return 1;
- }
- // 检查是否强制重新创建
- if ($this->option('force')) {
- $this->deleteExistingMenu();
- }
- // 创建宠物激活技能菜单
- $this->createPetActiveSkillMenu();
- $this->info('宠物激活技能菜单添加成功!');
- return 0;
- } catch (\Exception $e) {
- $this->error('添加宠物激活技能菜单失败: ' . $e->getMessage());
- return 1;
- }
- }
- /**
- * 检查父菜单是否存在
- *
- * @return bool
- */
- protected function checkParentMenu(): bool
- {
- return AdminMenu::where('id', $this->petParentId)->exists();
- }
- /**
- * 删除已存在的菜单
- *
- * @return void
- */
- protected function deleteExistingMenu(): void
- {
- $existingMenu = AdminMenu::where('title', '宠物激活技能')
- ->where('parent_id', $this->petParentId)
- ->first();
- if ($existingMenu) {
- $existingMenu->delete();
- $this->info("✓ 删除已存在的菜单: {$existingMenu->title}, ID: {$existingMenu->id}");
- }
- }
- /**
- * 创建宠物激活技能菜单
- *
- * @return void
- */
- protected function createPetActiveSkillMenu(): void
- {
- // 获取当前最大order值
- $maxOrder = AdminMenu::where('parent_id', $this->petParentId)->max('order');
- $nextOrder = $maxOrder ? $maxOrder + 1 : 202;
- // 创建宠物激活技能菜单
- $activeSkillMenu = AdminMenu::firstOrCreate(
- [
- 'title' => '宠物激活技能',
- 'parent_id' => $this->petParentId
- ],
- [
- 'uri' => 'pet-active-skills',
- 'icon' => 'fa-magic',
- 'order' => $nextOrder,
- 'show' => 1,
- 'extension' => '',
- 'created_at' => now(),
- 'updated_at' => now()
- ]
- );
- $this->info("✓ 添加宠物激活技能菜单: {$activeSkillMenu->title}, ID: {$activeSkillMenu->id}, Order: {$activeSkillMenu->order}");
- }
- }
|