// 补充菜单数据脚本
// 完善RBAC权限系统的菜单结构

const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
  host: 'localhost',
  port: 3306,
  user: 'wwjcloud',
  password: 'wwjcloud',
  database: 'wwjcloud'
};

async function insertMenuData() {
  let connection;
  
  try {
    console.log('🔌 连接数据库...');
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功！');
    
    console.log('\n📋 开始插入菜单数据...');
    
    // 插入完整的菜单结构
    await insertCompleteMenuStructure(connection);
    
    console.log('\n🎉 菜单数据插入完成！');
    
  } catch (error) {
    console.error('❌ 插入失败:', error.message);
  } finally {
    if (connection) {
      await connection.end();
      console.log('🔌 数据库连接已关闭');
    }
  }
}

async function insertCompleteMenuStructure(connection) {
  try {
    console.log('  🏗️  插入完整菜单结构...');
    
    // 清空现有菜单数据
    await connection.execute('DELETE FROM sys_menu WHERE id > 0');
    console.log('    ✅ 清空现有菜单数据');
    
    // 插入系统管理菜单
    await connection.execute(`
      INSERT INTO sys_menu (id, app_type, menu_name, menu_short_name, menu_key, parent_key, menu_type, icon, api_url, router_path, view_path, methods, sort, status, is_show, create_time, delete_time, addon, source, menu_attr, parent_select_key) 
      VALUES 
      (1, 'admin', '系统管理', '系统', 'system', '', 0, 'setting', '', '/system', 'system/index', '', 1, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'system'),
      (2, 'admin', '用户管理', '用户', 'user', 'system', 1, 'user', '/adminapi/admin', '/system/user', 'system/user/index', 'GET,POST,PUT,DELETE', 1, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'system'),
      (3, 'admin', '角色管理', '角色', 'role', 'system', 1, 'team', '/adminapi/role', '/system/role', 'system/role/index', 'GET,POST,PUT,DELETE', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'system'),
      (4, 'admin', '菜单管理', '菜单', 'menu', 'system', 1, 'menu', '/adminapi/menu', '/system/menu', 'system/menu/index', 'GET,POST,PUT,DELETE', 3, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'system'),
      (5, 'admin', '操作日志', '日志', 'log', 'system', 1, 'file-text', '/adminapi/log', '/system/log', 'system/log/index', 'GET', 4, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'system')
    `);
    console.log('    ✅ 系统管理菜单插入成功');
    
    // 插入会员管理菜单
    await connection.execute(`
      INSERT INTO sys_menu (id, app_type, menu_name, menu_short_name, menu_key, parent_key, menu_type, icon, api_url, router_path, view_path, methods, sort, status, is_show, create_time, delete_time, addon, source, menu_attr, parent_select_key) 
      VALUES 
      (6, 'admin', '会员管理', '会员', 'member', '', 0, 'user', '', '/member', 'member/index', '', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', ''),
      (7, 'admin', '会员列表', '列表', 'member_list', 'member', 1, 'table', '/adminapi/member', '/member/list', 'member/list/index', 'GET,POST,PUT,DELETE', 1, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'member'),
      (8, 'admin', '会员等级', '等级', 'member_level', 'member', 1, 'star', '/adminapi/member-level', '/member/level', 'member/level/index', 'GET,POST,PUT,DELETE', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'member'),
      (9, 'admin', '会员地址', '地址', 'member_address', 'member', 1, 'environment', '/adminapi/member-address', '/member/address', 'member/address/index', 'GET,POST,PUT,DELETE', 3, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'member')
    `);
    console.log('    ✅ 会员管理菜单插入成功');
    
    // 插入财务管理菜单
    await connection.execute(`
      INSERT INTO sys_menu (id, app_type, menu_name, menu_short_name, menu_key, parent_key, menu_type, icon, api_url, router_path, view_path, methods, sort, status, is_show, create_time, delete_time, addon, source, menu_attr, parent_select_key) 
      VALUES 
      (10, 'admin', '财务管理', '财务', 'finance', '', 0, 'money-collect', '', '/finance', 'finance/index', '', 3, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', ''),
      (11, 'admin', '收入统计', '收入', 'income', 'finance', 1, 'rise', '/adminapi/finance/income', '/finance/income', 'finance/income/index', 'GET', 1, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'finance'),
      (12, 'admin', '支出统计', '支出', 'expense', 'finance', 1, 'fall', '/adminapi/finance/expense', '/finance/expense', 'finance/expense/index', 'GET', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'finance'),
      (13, 'admin', '资金流水', '流水', 'cash_flow', 'finance', 1, 'transaction', '/adminapi/finance/cash-flow', '/finance/cash-flow', 'finance/cash-flow/index', 'GET', 3, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'finance')
    `);
    console.log('    ✅ 财务管理菜单插入成功');
    
    // 插入内容管理菜单
    await connection.execute(`
      INSERT INTO sys_menu (id, app_type, menu_name, menu_short_name, menu_key, parent_key, menu_type, icon, api_url, router_path, view_path, methods, sort, status, is_show, create_time, delete_time, addon, source, menu_attr, parent_select_key) 
      VALUES 
      (14, 'admin', '内容管理', '内容', 'content', '', 0, 'file-text', '', '/content', 'content/index', '', 4, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', ''),
      (15, 'admin', '文章管理', '文章', 'article', 'content', 1, 'file', '/adminapi/content/article', '/content/article', 'content/article/index', 'GET,POST,PUT,DELETE', 1, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'content'),
      (16, 'admin', '分类管理', '分类', 'category', 'content', 1, 'folder', '/adminapi/content/category', '/content/category', 'content/category/index', 'GET,POST,PUT,DELETE', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'content'),
      (17, 'admin', '标签管理', '标签', 'tag', 'content', 1, 'tags', '/adminapi/content/tag', '/content/tag', 'content/tag/index', 'GET,POST,PUT,DELETE', 3, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'content')
    `);
    console.log('    ✅ 内容管理菜单插入成功');
    
    // 插入系统设置菜单
    await connection.execute(`
      INSERT INTO sys_menu (id, app_type, menu_name, menu_short_name, menu_key, parent_key, menu_type, icon, api_url, router_path, view_path, methods, sort, status, is_show, create_time, delete_time, addon, source, menu_attr, parent_select_key) 
      VALUES 
      (18, 'admin', '系统设置', '设置', 'settings', '', 0, 'tool', '', '/settings', 'settings/index', '', 5, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', ''),
      (19, 'admin', '站点设置', '站点', 'site_settings', 'settings', 1, 'global', '/adminapi/settings/site', '/settings/site', 'settings/site/index', 'GET,POST', 1, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'settings'),
      (20, 'admin', '邮件设置', '邮件', 'email_settings', 'settings', 1, 'mail', '/adminapi/settings/email', '/settings/email', 'settings/email/index', 'GET,POST', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'settings'),
      (21, 'admin', '支付设置', '支付', 'payment_settings', 'settings', 1, 'credit-card', '/adminapi/settings/payment', '/settings/payment', 'settings/payment/index', 'GET,POST', 3, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'settings')
    `);
    console.log('    ✅ 系统设置菜单插入成功');
    
    // 更新角色权限
    await updateRolePermissions(connection);
    
  } catch (error) {
    console.error(`  ❌ 菜单数据插入失败: ${error.message}`);
  }
}

async function updateRolePermissions(connection) {
  try {
    console.log('  🔐 更新角色权限...');
    
    // 超级管理员拥有所有菜单权限
    await connection.execute(`
      UPDATE sys_role SET rules = '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21' WHERE role_id = 1
    `);
    
    // 运营管理员拥有会员和财务权限
    await connection.execute(`
      UPDATE sys_role SET rules = '6,7,8,9,10,11,12,13' WHERE role_id = 2
    `);
    
    // 内容管理员拥有内容管理权限
    await connection.execute(`
      UPDATE sys_role SET rules = '14,15,16,17' WHERE role_id = 3
    `);
    
    console.log('    ✅ 角色权限更新成功');
    
  } catch (error) {
    console.error(`  ❌ 角色权限更新失败: ${error.message}`);
  }
}

// 运行脚本
insertMenuData(); 