InsertTransferAdminMenuCommand.php 6.3 KB

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