| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Dcat\Admin\Models\Menu as AdminMenu;
- /**
- * 添加宠物技能处理日志菜单命令
- */
- class AddPetSkillProcessLogMenu extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'pet:add-skill-process-log-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->createPetSkillProcessLogMenu();
- $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 createPetSkillProcessLogMenu(): void
- {
- // 获取当前最大order值
- $maxOrder = AdminMenu::where('parent_id', $this->petParentId)->max('order');
- $nextOrder = $maxOrder ? $maxOrder + 1 : 79;
- // 创建宠物技能处理日志菜单
- $skillProcessLogMenu = AdminMenu::firstOrCreate(
- [
- 'title' => '宠物技能处理日志',
- 'parent_id' => $this->petParentId
- ],
- [
- 'uri' => 'pet-skill-process-logs',
- 'icon' => 'fa-list-alt',
- 'order' => $nextOrder,
- 'show' => 1,
- 'extension' => '',
- 'created_at' => now(),
- 'updated_at' => now()
- ]
- );
- $this->info("✓ 添加宠物技能处理日志菜单: {$skillProcessLogMenu->title}, ID: {$skillProcessLogMenu->id}, Order: {$skillProcessLogMenu->order}");
- }
- /**
- * 显示当前宠物管理菜单结构
- *
- * @return void
- */
- protected function showCurrentMenuStructure(): void
- {
- $this->info('当前宠物管理菜单结构:');
-
- $menus = AdminMenu::where('parent_id', $this->petParentId)
- ->orderBy('order')
- ->get(['id', 'title', 'uri', 'order', 'show']);
- $this->table(
- ['ID', '标题', 'URI', '排序', '显示'],
- $menus->map(function ($menu) {
- return [
- $menu->id,
- $menu->title,
- $menu->uri,
- $menu->order,
- $menu->show ? '是' : '否'
- ];
- })->toArray()
- );
- }
- }
|