/** * 添加"价格套餐管理"菜单项到用户管理菜单下 * 执行 SQL 插入操作 */ const Framework = require('../framework/node-core-framework.js'); const frameworkConfig = require('../config/framework.config.js'); async function addPricingPlansMenu() { console.log('🔄 开始添加"价格套餐管理"菜单项...\n'); try { // 初始化框架 console.log('正在初始化框架...'); const framework = await Framework.init(frameworkConfig); const models = Framework.getModels(); if (!models) { throw new Error('无法获取模型列表'); } // 从任意模型获取 sequelize 实例 const Sequelize = require('sequelize'); const firstModel = Object.values(models)[0]; if (!firstModel || !firstModel.sequelize) { throw new Error('无法获取数据库连接'); } const sequelize = firstModel.sequelize; // 查找用户管理菜单的ID const [userMenu] = await sequelize.query( `SELECT id FROM sys_menu WHERE name = '用户管理' AND parent_id = 0 AND is_delete = 0 LIMIT 1`, { type: Sequelize.QueryTypes.SELECT } ); let parentId = 120; // 默认 fallback 值 if (userMenu && userMenu.id) { parentId = userMenu.id; console.log(`找到用户管理菜单,ID: ${parentId}`); } else { console.log(`未找到用户管理菜单,使用默认 parent_id: ${parentId}`); } // 检查是否已存在 const [existing] = await sequelize.query( `SELECT id, name FROM sys_menu WHERE path = 'pricing_plans' AND is_delete = 0`, { type: Sequelize.QueryTypes.SELECT } ); if (existing) { console.log(`⚠️ 菜单项已存在 (ID: ${existing.id}, 名称: ${existing.name})`); console.log('✅ 无需重复添加\n'); return; } // 获取最大排序值 const [maxSort] = await sequelize.query( `SELECT MAX(sort) as maxSort FROM sys_menu WHERE parent_id = ${parentId} AND is_delete = 0`, { type: Sequelize.QueryTypes.SELECT } ); const nextSort = (maxSort && maxSort.maxSort ? maxSort.maxSort : 0) + 1; // 执行插入 await sequelize.query( `INSERT INTO sys_menu ( name, parent_id, model_id, form_id, icon, path, component, api_path, is_show_menu, is_show, type, sort, create_time, last_modify_time, is_delete ) VALUES ( '价格套餐管理', ${parentId}, 0, 0, 'md-pricetags', 'pricing_plans', 'system/pricing_plans.vue', 'system/pricing_plans_server.js', 1, 1, '页面', ${nextSort}, NOW(), NOW(), 0 )`, { type: Sequelize.QueryTypes.INSERT } ); console.log('✅ "价格套餐管理"菜单项添加成功!\n'); // 验证插入结果 const [menu] = await sequelize.query( `SELECT id, name, parent_id, path, component, api_path, sort FROM sys_menu WHERE path = 'pricing_plans' AND is_delete = 0`, { type: Sequelize.QueryTypes.SELECT } ); if (menu) { console.log('📋 菜单项详情:'); console.log(` ID: ${menu.id}`); console.log(` 名称: ${menu.name}`); console.log(` 父菜单ID: ${menu.parent_id}`); console.log(` 路由路径: ${menu.path}`); console.log(` 组件路径: ${menu.component}`); console.log(` API路径: ${menu.api_path}`); console.log(` 排序: ${menu.sort}\n`); } } catch (error) { console.error('❌ 添加失败:', error.message); console.error('\n详细错误:', error); throw error; } } // 执行添加 addPricingPlansMenu() .then(() => { console.log('✨ 操作完成!'); process.exit(0); }) .catch(error => { console.error('\n💥 执行失败:', error); process.exit(1); });