InsertTransferAdminMenuCommand.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. 'title' => '统计图表',
  46. 'icon' => 'fa-line-chart',
  47. 'uri' => 'transfer-metrics',
  48. 'order' => 30,
  49. ],
  50. ],
  51. ];
  52. /**
  53. * 执行命令
  54. */
  55. public function handle()
  56. {
  57. $this->info('开始配置Transfer模块后台管理菜单...');
  58. // 检查父菜单是否存在
  59. if (!$this->checkParentMenu()) {
  60. $this->error("外接管理父菜单 (ID: {$this->externalManagementParentId}) 不存在");
  61. return 1;
  62. }
  63. // 检查是否强制重新创建
  64. $force = $this->option('force');
  65. // 检查菜单是否已存在
  66. $existingMenu = DB::table('admin_menu')
  67. ->where('title', $this->menuStructure['title'])
  68. ->where('parent_id', $this->externalManagementParentId)
  69. ->first();
  70. if ($existingMenu && !$force) {
  71. $this->warn('Transfer模块菜单已存在,使用 --force 参数强制重新创建');
  72. return 0;
  73. }
  74. if ($existingMenu && $force) {
  75. $this->info('删除现有菜单...');
  76. $this->deleteExistingMenus();
  77. }
  78. // 创建菜单
  79. $this->createMenus();
  80. $this->info('Transfer模块后台管理菜单配置完成!');
  81. return 0;
  82. }
  83. /**
  84. * 检查父菜单是否存在
  85. */
  86. protected function checkParentMenu(): bool
  87. {
  88. return DB::table('admin_menu')
  89. ->where('id', $this->externalManagementParentId)
  90. ->exists();
  91. }
  92. /**
  93. * 删除现有菜单
  94. */
  95. protected function deleteExistingMenus(): void
  96. {
  97. // 查找现有的Transfer模块菜单
  98. $transferMenu = DB::table('admin_menu')
  99. ->where('title', $this->menuStructure['title'])
  100. ->where('parent_id', $this->externalManagementParentId)
  101. ->first();
  102. if ($transferMenu) {
  103. // 删除子菜单
  104. $deletedChildren = DB::table('admin_menu')
  105. ->where('parent_id', $transferMenu->id)
  106. ->delete();
  107. // 删除主菜单
  108. DB::table('admin_menu')
  109. ->where('id', $transferMenu->id)
  110. ->delete();
  111. $this->info("删除了Transfer模块菜单及其 {$deletedChildren} 个子菜单");
  112. }
  113. }
  114. /**
  115. * 创建菜单
  116. */
  117. protected function createMenus(): void
  118. {
  119. DB::transaction(function () {
  120. // 创建Transfer模块主菜单
  121. $transferMenuId = DB::table('admin_menu')->insertGetId([
  122. 'parent_id' => $this->externalManagementParentId,
  123. 'order' => $this->menuStructure['order'],
  124. 'title' => $this->menuStructure['title'],
  125. 'icon' => $this->menuStructure['icon'],
  126. 'uri' => '',
  127. 'show' => 1,
  128. 'created_at' => now(),
  129. 'updated_at' => now(),
  130. ]);
  131. $this->info("✓ 创建模块菜单: {$this->menuStructure['title']} (ID: {$transferMenuId})");
  132. // 创建子菜单
  133. foreach ($this->menuStructure['children'] as $child) {
  134. $childMenuId = DB::table('admin_menu')->insertGetId([
  135. 'parent_id' => $transferMenuId,
  136. 'order' => $child['order'],
  137. 'title' => $child['title'],
  138. 'icon' => $child['icon'],
  139. 'uri' => $child['uri'],
  140. 'show' => 1,
  141. 'created_at' => now(),
  142. 'updated_at' => now(),
  143. ]);
  144. $this->info(" ├── 创建子菜单: {$child['title']} (ID: {$childMenuId}) -> {$child['uri']}");
  145. }
  146. });
  147. }
  148. /**
  149. * 显示当前菜单结构
  150. */
  151. protected function showCurrentStructure(): void
  152. {
  153. $this->info('当前外接管理菜单结构:');
  154. $parentMenu = DB::table('admin_menu')
  155. ->where('id', $this->externalManagementParentId)
  156. ->first();
  157. if ($parentMenu) {
  158. $this->line("├── {$parentMenu->title} (ID: {$parentMenu->id})");
  159. $children = DB::table('admin_menu')
  160. ->where('parent_id', $this->externalManagementParentId)
  161. ->orderBy('order')
  162. ->get();
  163. foreach ($children as $child) {
  164. $this->line("│ ├── {$child->title} (ID: {$child->id}) -> {$child->uri}");
  165. $grandChildren = DB::table('admin_menu')
  166. ->where('parent_id', $child->id)
  167. ->orderBy('order')
  168. ->get();
  169. foreach ($grandChildren as $grandChild) {
  170. $this->line("│ │ ├── {$grandChild->title} (ID: {$grandChild->id}) -> {$grandChild->uri}");
  171. }
  172. }
  173. }
  174. }
  175. }