InsertTransferAdminMenuCommand.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace App\Module\Transfer\Commands;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Facades\DB;
  5. /**
  6. * Transfer模块后台菜单配置命令
  7. *
  8. * 用于配置Transfer模块的后台管理菜单
  9. */
  10. class InsertTransferAdminMenuCommand extends Command
  11. {
  12. /**
  13. * 命令签名
  14. */
  15. protected $signature = 'transfer:insert-admin-menu {--force : 强制重新创建菜单}';
  16. /**
  17. * 命令描述
  18. */
  19. protected $description = '配置Transfer模块后台管理菜单';
  20. /**
  21. * 外接管理父菜单ID
  22. */
  23. protected int $externalManagementParentId = 533;
  24. /**
  25. * 菜单配置
  26. */
  27. protected array $menuStructure = [
  28. 'title' => 'Transfer模块',
  29. 'icon' => 'fa-exchange',
  30. 'order' => 30,
  31. 'children' => [
  32. [
  33. 'title' => '应用管理',
  34. 'icon' => 'fa-cogs',
  35. 'uri' => 'transfer/apps',
  36. 'order' => 10,
  37. ],
  38. [
  39. 'title' => '订单管理',
  40. 'icon' => 'fa-list-alt',
  41. 'uri' => 'transfer/orders',
  42. 'order' => 20,
  43. ],
  44. ],
  45. ];
  46. /**
  47. * 执行命令
  48. */
  49. public function handle()
  50. {
  51. $this->info('开始配置Transfer模块后台管理菜单...');
  52. // 检查父菜单是否存在
  53. if (!$this->checkParentMenu()) {
  54. $this->error("外接管理父菜单 (ID: {$this->externalManagementParentId}) 不存在");
  55. return 1;
  56. }
  57. // 检查是否强制重新创建
  58. $force = $this->option('force');
  59. // 检查菜单是否已存在
  60. $existingMenu = DB::table('admin_menu')
  61. ->where('title', $this->menuStructure['title'])
  62. ->where('parent_id', $this->externalManagementParentId)
  63. ->first();
  64. if ($existingMenu && !$force) {
  65. $this->warn('Transfer模块菜单已存在,使用 --force 参数强制重新创建');
  66. return 0;
  67. }
  68. if ($existingMenu && $force) {
  69. $this->info('删除现有菜单...');
  70. $this->deleteExistingMenus();
  71. }
  72. // 创建菜单
  73. $this->createMenus();
  74. $this->info('Transfer模块后台管理菜单配置完成!');
  75. return 0;
  76. }
  77. /**
  78. * 检查父菜单是否存在
  79. */
  80. protected function checkParentMenu(): bool
  81. {
  82. return DB::table('admin_menu')
  83. ->where('id', $this->externalManagementParentId)
  84. ->exists();
  85. }
  86. /**
  87. * 删除现有菜单
  88. */
  89. protected function deleteExistingMenus(): void
  90. {
  91. // 查找现有的Transfer模块菜单
  92. $transferMenu = DB::table('admin_menu')
  93. ->where('title', $this->menuStructure['title'])
  94. ->where('parent_id', $this->externalManagementParentId)
  95. ->first();
  96. if ($transferMenu) {
  97. // 删除子菜单
  98. $deletedChildren = DB::table('admin_menu')
  99. ->where('parent_id', $transferMenu->id)
  100. ->delete();
  101. // 删除主菜单
  102. DB::table('admin_menu')
  103. ->where('id', $transferMenu->id)
  104. ->delete();
  105. $this->info("删除了Transfer模块菜单及其 {$deletedChildren} 个子菜单");
  106. }
  107. }
  108. /**
  109. * 创建菜单
  110. */
  111. protected function createMenus(): void
  112. {
  113. DB::transaction(function () {
  114. // 创建Transfer模块主菜单
  115. $transferMenuId = DB::table('admin_menu')->insertGetId([
  116. 'parent_id' => $this->externalManagementParentId,
  117. 'order' => $this->menuStructure['order'],
  118. 'title' => $this->menuStructure['title'],
  119. 'icon' => $this->menuStructure['icon'],
  120. 'uri' => '',
  121. 'show' => 1,
  122. 'created_at' => now(),
  123. 'updated_at' => now(),
  124. ]);
  125. $this->info("✓ 创建模块菜单: {$this->menuStructure['title']} (ID: {$transferMenuId})");
  126. // 创建子菜单
  127. foreach ($this->menuStructure['children'] as $child) {
  128. $childMenuId = DB::table('admin_menu')->insertGetId([
  129. 'parent_id' => $transferMenuId,
  130. 'order' => $child['order'],
  131. 'title' => $child['title'],
  132. 'icon' => $child['icon'],
  133. 'uri' => $child['uri'],
  134. 'show' => 1,
  135. 'created_at' => now(),
  136. 'updated_at' => now(),
  137. ]);
  138. $this->info(" ├── 创建子菜单: {$child['title']} (ID: {$childMenuId}) -> {$child['uri']}");
  139. }
  140. });
  141. }
  142. /**
  143. * 显示当前菜单结构
  144. */
  145. protected function showCurrentStructure(): void
  146. {
  147. $this->info('当前外接管理菜单结构:');
  148. $parentMenu = DB::table('admin_menu')
  149. ->where('id', $this->externalManagementParentId)
  150. ->first();
  151. if ($parentMenu) {
  152. $this->line("├── {$parentMenu->title} (ID: {$parentMenu->id})");
  153. $children = DB::table('admin_menu')
  154. ->where('parent_id', $this->externalManagementParentId)
  155. ->orderBy('order')
  156. ->get();
  157. foreach ($children as $child) {
  158. $this->line("│ ├── {$child->title} (ID: {$child->id}) -> {$child->uri}");
  159. $grandChildren = DB::table('admin_menu')
  160. ->where('parent_id', $child->id)
  161. ->orderBy('order')
  162. ->get();
  163. foreach ($grandChildren as $grandChild) {
  164. $this->line("│ │ ├── {$grandChild->title} (ID: {$grandChild->id}) -> {$grandChild->uri}");
  165. }
  166. }
  167. }
  168. }
  169. }