AddPetSkillProcessLogMenu.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Dcat\Admin\Models\Menu as AdminMenu;
  5. /**
  6. * 添加宠物技能处理日志菜单命令
  7. */
  8. class AddPetSkillProcessLogMenu extends Command
  9. {
  10. /**
  11. * 命令签名
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'pet:add-skill-process-log-menu {--force : 强制重新创建菜单}';
  16. /**
  17. * 命令描述
  18. *
  19. * @var string
  20. */
  21. protected $description = '添加宠物技能处理日志菜单到后台管理系统';
  22. /**
  23. * 宠物管理父菜单ID
  24. *
  25. * @var int
  26. */
  27. protected $petParentId = 494;
  28. /**
  29. * 执行命令
  30. *
  31. * @return int
  32. */
  33. public function handle(): int
  34. {
  35. try {
  36. // 检查父菜单是否存在
  37. if (!$this->checkParentMenu()) {
  38. $this->error("宠物管理父菜单 (ID: {$this->petParentId}) 不存在");
  39. return 1;
  40. }
  41. // 检查是否强制重新创建
  42. if ($this->option('force')) {
  43. $this->deleteExistingMenu();
  44. }
  45. // 创建宠物技能处理日志菜单
  46. $this->createPetSkillProcessLogMenu();
  47. $this->info('宠物技能处理日志菜单添加成功!');
  48. return 0;
  49. } catch (\Exception $e) {
  50. $this->error('添加宠物技能处理日志菜单失败: ' . $e->getMessage());
  51. return 1;
  52. }
  53. }
  54. /**
  55. * 检查父菜单是否存在
  56. *
  57. * @return bool
  58. */
  59. protected function checkParentMenu(): bool
  60. {
  61. return AdminMenu::where('id', $this->petParentId)->exists();
  62. }
  63. /**
  64. * 删除已存在的菜单
  65. *
  66. * @return void
  67. */
  68. protected function deleteExistingMenu(): void
  69. {
  70. $existingMenu = AdminMenu::where('title', '宠物技能处理日志')
  71. ->where('parent_id', $this->petParentId)
  72. ->first();
  73. if ($existingMenu) {
  74. $existingMenu->delete();
  75. $this->info("✓ 删除已存在的菜单: {$existingMenu->title}, ID: {$existingMenu->id}");
  76. }
  77. }
  78. /**
  79. * 创建宠物技能处理日志菜单
  80. *
  81. * @return void
  82. */
  83. protected function createPetSkillProcessLogMenu(): void
  84. {
  85. // 获取当前最大order值
  86. $maxOrder = AdminMenu::where('parent_id', $this->petParentId)->max('order');
  87. $nextOrder = $maxOrder ? $maxOrder + 1 : 79;
  88. // 创建宠物技能处理日志菜单
  89. $skillProcessLogMenu = AdminMenu::firstOrCreate(
  90. [
  91. 'title' => '宠物技能处理日志',
  92. 'parent_id' => $this->petParentId
  93. ],
  94. [
  95. 'uri' => 'pet-skill-process-logs',
  96. 'icon' => 'fa-list-alt',
  97. 'order' => $nextOrder,
  98. 'show' => 1,
  99. 'extension' => '',
  100. 'created_at' => now(),
  101. 'updated_at' => now()
  102. ]
  103. );
  104. $this->info("✓ 添加宠物技能处理日志菜单: {$skillProcessLogMenu->title}, ID: {$skillProcessLogMenu->id}, Order: {$skillProcessLogMenu->order}");
  105. }
  106. /**
  107. * 显示当前宠物管理菜单结构
  108. *
  109. * @return void
  110. */
  111. protected function showCurrentMenuStructure(): void
  112. {
  113. $this->info('当前宠物管理菜单结构:');
  114. $menus = AdminMenu::where('parent_id', $this->petParentId)
  115. ->orderBy('order')
  116. ->get(['id', 'title', 'uri', 'order', 'show']);
  117. $this->table(
  118. ['ID', '标题', 'URI', '排序', '显示'],
  119. $menus->map(function ($menu) {
  120. return [
  121. $menu->id,
  122. $menu->title,
  123. $menu->uri,
  124. $menu->order,
  125. $menu->show ? '是' : '否'
  126. ];
  127. })->toArray()
  128. );
  129. }
  130. }