// 修复后的测试数据插入脚本
// 根据真实表结构插入测试数据

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

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

async function insertTestData() {
  let connection;
  
  try {
    console.log('🔌 连接数据库...');
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功！');
    
    console.log('\n📊 开始插入测试数据...');
    
    // 插入Member模块数据
    await insertMemberData(connection);
    
    // 插入RBAC模块数据
    await insertRbacData(connection);
    
    console.log('\n🎉 测试数据插入完成！');
    
  } catch (error) {
    console.error('❌ 插入失败:', error.message);
  } finally {
    if (connection) {
      await connection.end();
      console.log('🔌 数据库连接已关闭');
    }
  }
}

async function insertMemberData(connection) {
  console.log('\n👥 插入Member模块数据...');
  
  try {
    // 插入会员等级 - 根据真实表结构
    console.log('  ⭐ 插入会员等级...');
    await connection.execute(`
      INSERT INTO member_level (level_id, site_id, level_name, growth, remark, status, create_time, update_time, level_benefits, level_gifts) 
      VALUES 
      (1, 0, '普通会员', 0, '新注册用户', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), '基础权益', '欢迎礼包'),
      (2, 0, 'VIP会员', 1000, '消费满1000元', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 'VIP专享权益', 'VIP礼包'),
      (3, 0, '钻石会员', 5000, '消费满5000元', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), '钻石专享权益', '钻石礼包')
    `);
    console.log('    ✅ 会员等级插入成功');
    
    // 插入会员用户 - 根据真实表结构
    console.log('  👤 插入会员用户...');
    await connection.execute(`
      INSERT INTO member (member_no, pid, site_id, username, mobile, password, nickname, headimg, member_level, member_label, wx_openid, weapp_openid, wx_unionid, ali_openid, douyin_openid, register_channel, register_type, login_ip, login_type, login_channel, login_count, login_time, create_time, last_visit_time, last_consum_time, sex, status, birthday, id_card, point, point_get, balance, balance_get, money, money_get, money_cash_outing, growth, growth_get, commission, commission_get, commission_cash_outing, is_member, member_time, is_del, province_id, city_id, district_id, address, location, remark, delete_time, update_time) 
      VALUES 
      ('M001', 0, 0, 'member', '13800138000', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', '测试会员', '', 1, 'VIP', '', '', '', '', '', 'H5', 'password', '127.0.0.1', 'h5', '', 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0, 1, 1, '', '', 100, 100, 1000.00, 1000.00, 500.00, 500.00, 0.00, 50, 50, 0.00, 0.00, 0.00, 1, UNIX_TIMESTAMP(), 0, 0, 0, 0, '', '', '', 0, UNIX_TIMESTAMP()),
      ('M002', 0, 0, 'testmember', '13800138001', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', '普通会员', '', 0, '普通', '', '', '', '', '', 'H5', 'password', '127.0.0.1', 'h5', '', 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0, 1, 1, '', '', 50, 50, 500.00, 500.00, 200.00, 200.00, 0.00, 20, 20, 0.00, 0.00, 0.00, 0, 0, 0, 0, 0, 0, '', '', '', 0, UNIX_TIMESTAMP())
    `);
    console.log('    ✅ 会员用户插入成功');
    
    // 插入会员地址 - 根据真实表结构
    console.log('  🏠 插入会员地址...');
    await connection.execute(`
      INSERT INTO member_address (id, member_id, site_id, name, mobile, province_id, city_id, district_id, address, address_name, update_time) 
      VALUES 
      (1, 1, 0, '张三', '13800138000', 110000, 110100, 110101, '朝阳区建国路88号', '家', UNIX_TIMESTAMP()),
      (2, 1, 0, '张三', '13800138000', 110000, 110100, 110102, '西城区西单大街1号', '公司', UNIX_TIMESTAMP())
    `);
    console.log('    ✅ 会员地址插入成功');
    
  } catch (error) {
    console.error(`  ❌ Member模块数据插入失败: ${error.message}`);
  }
}

async function insertRbacData(connection) {
  console.log('\n🔐 插入RBAC模块数据...');
  
  try {
    // 插入角色
    console.log('  🎭 插入系统角色...');
    await connection.execute(`
      INSERT INTO sys_role (role_id, site_id, role_name, rules, status, create_time, update_time) 
      VALUES 
      (1, 0, '超级管理员', '1,2,3,4,5,6,7,8,9,10', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
      (2, 0, '运营管理员', '1,2,3,4,5', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
      (3, 0, '内容管理员', '1,2,3', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP())
    `);
    console.log('    ✅ 系统角色插入成功');
    
    // 插入菜单
    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', ''),
      (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', '会员管理', '会员', 'member', '', 0, 'user', '', '/member', 'member/index', '', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', '')
    `);
    console.log('    ✅ 系统菜单插入成功');
    
  } catch (error) {
    console.error(`  ❌ RBAC模块数据插入失败: ${error.message}`);
  }
}

// 运行脚本
insertTestData(); 