| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace App\Module\Transfer\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- /**
- * Transfer模块后台菜单配置命令
- *
- * 用于配置Transfer模块的后台管理菜单
- */
- class InsertTransferAdminMenuCommand extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'transfer:insert-admin-menu {--force : 强制重新创建菜单}';
- /**
- * 命令描述
- */
- protected $description = '配置Transfer模块后台管理菜单';
- /**
- * 外接管理父菜单ID
- */
- protected int $externalManagementParentId = 533;
- /**
- * 菜单配置
- */
- protected array $menuStructure = [
- 'title' => 'Transfer模块',
- 'icon' => 'fa-exchange',
- 'order' => 30,
- 'children' => [
- [
- 'title' => '应用管理',
- 'icon' => 'fa-cogs',
- 'uri' => 'transfer/apps',
- 'order' => 10,
- ],
- [
- 'title' => '订单管理',
- 'icon' => 'fa-list-alt',
- 'uri' => 'transfer/orders',
- 'order' => 20,
- ],
- [
- 'title' => '统计图表',
- 'icon' => 'fa-line-chart',
- 'uri' => 'transfer-metrics',
- 'order' => 30,
- ],
- ],
- ];
- /**
- * 执行命令
- */
- public function handle()
- {
- $this->info('开始配置Transfer模块后台管理菜单...');
-
- // 检查父菜单是否存在
- if (!$this->checkParentMenu()) {
- $this->error("外接管理父菜单 (ID: {$this->externalManagementParentId}) 不存在");
- return 1;
- }
- // 检查是否强制重新创建
- $force = $this->option('force');
-
- // 检查菜单是否已存在
- $existingMenu = DB::table('admin_menu')
- ->where('title', $this->menuStructure['title'])
- ->where('parent_id', $this->externalManagementParentId)
- ->first();
-
- if ($existingMenu && !$force) {
- $this->warn('Transfer模块菜单已存在,使用 --force 参数强制重新创建');
- return 0;
- }
-
- if ($existingMenu && $force) {
- $this->info('删除现有菜单...');
- $this->deleteExistingMenus();
- }
-
- // 创建菜单
- $this->createMenus();
-
- $this->info('Transfer模块后台管理菜单配置完成!');
- return 0;
- }
- /**
- * 检查父菜单是否存在
- */
- protected function checkParentMenu(): bool
- {
- return DB::table('admin_menu')
- ->where('id', $this->externalManagementParentId)
- ->exists();
- }
- /**
- * 删除现有菜单
- */
- protected function deleteExistingMenus(): void
- {
- // 查找现有的Transfer模块菜单
- $transferMenu = DB::table('admin_menu')
- ->where('title', $this->menuStructure['title'])
- ->where('parent_id', $this->externalManagementParentId)
- ->first();
- if ($transferMenu) {
- // 删除子菜单
- $deletedChildren = DB::table('admin_menu')
- ->where('parent_id', $transferMenu->id)
- ->delete();
- // 删除主菜单
- DB::table('admin_menu')
- ->where('id', $transferMenu->id)
- ->delete();
-
- $this->info("删除了Transfer模块菜单及其 {$deletedChildren} 个子菜单");
- }
- }
- /**
- * 创建菜单
- */
- protected function createMenus(): void
- {
- DB::transaction(function () {
- // 创建Transfer模块主菜单
- $transferMenuId = DB::table('admin_menu')->insertGetId([
- 'parent_id' => $this->externalManagementParentId,
- 'order' => $this->menuStructure['order'],
- 'title' => $this->menuStructure['title'],
- 'icon' => $this->menuStructure['icon'],
- 'uri' => '',
- 'show' => 1,
- 'created_at' => now(),
- 'updated_at' => now(),
- ]);
- $this->info("✓ 创建模块菜单: {$this->menuStructure['title']} (ID: {$transferMenuId})");
- // 创建子菜单
- foreach ($this->menuStructure['children'] as $child) {
- $childMenuId = DB::table('admin_menu')->insertGetId([
- 'parent_id' => $transferMenuId,
- 'order' => $child['order'],
- 'title' => $child['title'],
- 'icon' => $child['icon'],
- 'uri' => $child['uri'],
- 'show' => 1,
- 'created_at' => now(),
- 'updated_at' => now(),
- ]);
- $this->info(" ├── 创建子菜单: {$child['title']} (ID: {$childMenuId}) -> {$child['uri']}");
- }
- });
- }
- /**
- * 显示当前菜单结构
- */
- protected function showCurrentStructure(): void
- {
- $this->info('当前外接管理菜单结构:');
-
- $parentMenu = DB::table('admin_menu')
- ->where('id', $this->externalManagementParentId)
- ->first();
- if ($parentMenu) {
- $this->line("├── {$parentMenu->title} (ID: {$parentMenu->id})");
- $children = DB::table('admin_menu')
- ->where('parent_id', $this->externalManagementParentId)
- ->orderBy('order')
- ->get();
- foreach ($children as $child) {
- $this->line("│ ├── {$child->title} (ID: {$child->id}) -> {$child->uri}");
- $grandChildren = DB::table('admin_menu')
- ->where('parent_id', $child->id)
- ->orderBy('order')
- ->get();
- foreach ($grandChildren as $grandChild) {
- $this->line("│ │ ├── {$grandChild->title} (ID: {$grandChild->id}) -> {$grandChild->uri}");
- }
- }
- }
- }
- }
|