InsertFarmConfigAdminMenu.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Module\Farm\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. /**
  6. * 添加农场配置管理菜单命令
  7. */
  8. class InsertFarmConfigAdminMenu extends Command
  9. {
  10. /**
  11. * 命令签名
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'farm:insert-config-menu {--force : 强制重新创建菜单}';
  16. /**
  17. * 命令描述
  18. *
  19. * @var string
  20. */
  21. protected $description = '添加农场配置管理菜单到后台管理系统';
  22. /**
  23. * 农场配置父菜单ID
  24. *
  25. * @var int
  26. */
  27. protected $farmConfigParentId = 264;
  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->farmConfigParentId}) 不存在");
  39. return 1;
  40. }
  41. // 检查是否强制重新创建
  42. if ($this->option('force')) {
  43. $this->deleteExistingMenu();
  44. }
  45. // 创建农场配置管理菜单
  46. $this->createFarmConfigMenu();
  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. $parentMenu = DB::table('kku_admin_menu')
  62. ->where('id', $this->farmConfigParentId)
  63. ->first();
  64. if ($parentMenu) {
  65. $this->info("找到农场配置父菜单: {$parentMenu->title} (ID: {$parentMenu->id})");
  66. return true;
  67. }
  68. return false;
  69. }
  70. /**
  71. * 删除已存在的菜单
  72. *
  73. * @return void
  74. */
  75. protected function deleteExistingMenu(): void
  76. {
  77. $existingMenu = DB::table('kku_admin_menu')
  78. ->where('parent_id', $this->farmConfigParentId)
  79. ->where('uri', 'farm-configs')
  80. ->first();
  81. if ($existingMenu) {
  82. DB::table('kku_admin_menu')->where('id', $existingMenu->id)->delete();
  83. $this->info("删除已存在的农场配置管理菜单 (ID: {$existingMenu->id})");
  84. }
  85. }
  86. /**
  87. * 创建农场配置管理菜单
  88. *
  89. * @return void
  90. */
  91. protected function createFarmConfigMenu(): void
  92. {
  93. // 检查菜单是否已存在
  94. $existingMenu = DB::table('kku_admin_menu')
  95. ->where('parent_id', $this->farmConfigParentId)
  96. ->where('uri', 'farm-configs')
  97. ->first();
  98. if ($existingMenu && !$this->option('force')) {
  99. $this->info("农场配置管理菜单已存在 (ID: {$existingMenu->id})");
  100. return;
  101. }
  102. // 获取农场配置菜单下的最大order值
  103. $maxOrder = DB::table('kku_admin_menu')
  104. ->where('parent_id', $this->farmConfigParentId)
  105. ->max('order');
  106. $nextOrder = $maxOrder ? $maxOrder + 1 : 1;
  107. // 创建农场配置管理菜单
  108. $menuId = DB::table('kku_admin_menu')->insertGetId([
  109. 'parent_id' => $this->farmConfigParentId,
  110. 'order' => $nextOrder,
  111. 'title' => '农场配置管理',
  112. 'icon' => 'fa-cogs',
  113. 'uri' => 'farm-configs',
  114. 'show' => 1,
  115. 'extension' => '',
  116. 'created_at' => now(),
  117. 'updated_at' => now(),
  118. ]);
  119. $this->info("创建农场配置管理菜单成功 (ID: {$menuId})");
  120. $this->info("菜单标题: 农场配置管理");
  121. $this->info("菜单URI: farm-configs");
  122. $this->info("菜单图标: fa-cogs");
  123. $this->info("菜单顺序: {$nextOrder}");
  124. }
  125. }