// 详细数据验证脚本
// 检查4个核心模块的数据插入情况

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

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

async function verifyAllData() {
  let connection;
  
  try {
    console.log('🔌 连接数据库...');
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功！');
    
    console.log('\n🔍 开始验证所有模块数据...');
    
    // 验证Admin模块
    await verifyAdminModule(connection);
    
    // 验证Member模块
    await verifyMemberModule(connection);
    
    // 验证RBAC模块
    await verifyRbacModule(connection);
    
    // 验证Auth模块
    await verifyAuthModule(connection);
    
    // 显示总体统计
    await showOverallStats(connection);
    
  } catch (error) {
    console.error('❌ 验证失败:', error.message);
  } finally {
    if (connection) {
      await connection.end();
      console.log('🔌 数据库连接已关闭');
    }
  }
}

async function verifyAdminModule(connection) {
  console.log('\n📊 Admin模块验证:');
  
  try {
    // 检查sys_user表
    const [users] = await connection.execute('SELECT COUNT(*) as count FROM sys_user WHERE is_del = 0');
    console.log(`  👥 管理员用户: ${users[0].count} 条`);
    
    if (users[0].count > 0) {
      const [userList] = await connection.execute('SELECT uid, username, real_name, status FROM sys_user WHERE is_del = 0 LIMIT 5');
      userList.forEach(user => {
        console.log(`    - ID:${user.uid}, 用户名:${user.username}, 姓名:${user.real_name}, 状态:${user.status}`);
      });
    }
    
    // 检查sys_user_role表
    const [userRoles] = await connection.execute('SELECT COUNT(*) as count FROM sys_user_role WHERE delete_time = 0');
    console.log(`  🔐 用户角色关联: ${userRoles[0].count} 条`);
    
    // 检查sys_user_log表
    const [userLogs] = await connection.execute('SELECT COUNT(*) as count FROM sys_user_log');
    console.log(`  📝 操作日志: ${userLogs[0].count} 条`);
    
  } catch (error) {
    console.error(`  ❌ Admin模块验证失败: ${error.message}`);
  }
}

async function verifyMemberModule(connection) {
  console.log('\n👥 Member模块验证:');
  
  try {
    // 检查member表
    const [members] = await connection.execute('SELECT COUNT(*) as count FROM member WHERE is_del = 0');
    console.log(`  👤 会员用户: ${members[0].count} 条`);
    
    if (members[0].count > 0) {
      const [memberList] = await connection.execute('SELECT member_id, username, nickname, mobile, status FROM member WHERE is_del = 0 LIMIT 5');
      memberList.forEach(member => {
        console.log(`    - ID:${member.member_id}, 用户名:${member.username}, 昵称:${member.nickname}, 手机:${member.mobile}, 状态:${member.status}`);
      });
    }
    
    // 检查member_address表
    const [addresses] = await connection.execute('SELECT COUNT(*) as count FROM member_address');
    console.log(`  🏠 会员地址: ${addresses[0].count} 条`);
    
    // 检查member_level表
    const [levels] = await connection.execute('SELECT COUNT(*) as count FROM member_level');
    console.log(`  ⭐ 会员等级: ${levels[0].count} 条`);
    
    if (levels[0].count > 0) {
      const [levelList] = await connection.execute('SELECT level_id, level_name, level_weight, status FROM member_level LIMIT 5');
      levelList.forEach(level => {
        console.log(`    - ID:${level.level_id}, 等级:${level.level_name}, 权重:${level.level_weight}, 状态:${level.status}`);
      });
    }
    
  } catch (error) {
    console.error(`  ❌ Member模块验证失败: ${error.message}`);
  }
}

async function verifyRbacModule(connection) {
  console.log('\n🔐 RBAC模块验证:');
  
  try {
    // 检查sys_role表
    const [roles] = await connection.execute('SELECT COUNT(*) as count FROM sys_role');
    console.log(`  🎭 系统角色: ${roles[0].count} 条`);
    
    if (roles[0].count > 0) {
      const [roleList] = await connection.execute('SELECT role_id, role_name, status FROM sys_role LIMIT 5');
      roleList.forEach(role => {
        console.log(`    - ID:${role.role_id}, 角色:${role.role_name}, 状态:${role.status}`);
      });
    }
    
    // 检查sys_menu表
    const [menus] = await connection.execute('SELECT COUNT(*) as count FROM sys_menu');
    console.log(`  📋 系统菜单: ${menus[0].count} 条`);
    
    if (menus[0].count > 0) {
      const [menuList] = await connection.execute('SELECT id, menu_name, menu_key, parent_key, status FROM sys_menu LIMIT 5');
      menuList.forEach(menu => {
        console.log(`    - ID:${menu.id}, 菜单:${menu.menu_name}, 标识:${menu.menu_key}, 父级:${menu.parent_key || '无'}, 状态:${menu.status}`);
      });
    }
    
  } catch (error) {
    console.error(`  ❌ RBAC模块验证失败: ${error.message}`);
  }
}

async function verifyAuthModule(connection) {
  console.log('\n🔑 Auth模块验证:');
  
  try {
    // 检查auth_token表是否存在
    const [tables] = await connection.execute("SHOW TABLES LIKE 'auth_token'");
    
    if (tables.length > 0) {
      const [tokens] = await connection.execute('SELECT COUNT(*) as count FROM auth_token WHERE is_revoked = 0');
      console.log(`  🎫 认证Token: ${tokens[0].count} 条`);
      
      if (tokens[0].count > 0) {
        const [tokenList] = await connection.execute('SELECT id, user_id, user_type, expires_at FROM auth_token WHERE is_revoked = 0 LIMIT 5');
        tokenList.forEach(token => {
          console.log(`    - ID:${token.id}, 用户ID:${token.user_id}, 类型:${token.user_type}, 过期:${token.expires_at}`);
        });
      }
    } else {
      console.log(`  ⚠️  auth_token表不存在`);
    }
    
  } catch (error) {
    console.error(`  ❌ Auth模块验证失败: ${error.message}`);
  }
}

async function showOverallStats(connection) {
  console.log('\n📊 总体数据统计:');
  
  try {
    const [adminCount] = await connection.execute('SELECT COUNT(*) as count FROM sys_user WHERE is_del = 0');
    const [memberCount] = await connection.execute('SELECT COUNT(*) as count FROM member WHERE is_del = 0');
    const [roleCount] = await connection.execute('SELECT COUNT(*) as count FROM sys_role');
    const [menuCount] = await connection.execute('SELECT COUNT(*) as count FROM sys_menu');
    
    console.log(`  👥 管理员: ${adminCount[0].count} 人`);
    console.log(`  👤 会员: ${memberCount[0].count} 人`);
    console.log(`  🎭 角色: ${roleCount[0].count} 个`);
    console.log(`  📋 菜单: ${menuCount[0].count} 个`);
    
    const total = adminCount[0].count + memberCount[0].count + roleCount[0].count + menuCount[0].count;
    console.log(`  📈 总计: ${total} 条记录`);
    
    if (total > 0) {
      console.log('\n🎉 数据验证完成！4个核心模块已准备就绪！');
    } else {
      console.log('\n⚠️  数据为空，需要重新运行测试数据脚本');
    }
    
  } catch (error) {
    console.error(`  ❌ 统计失败: ${error.message}`);
  }
}

// 运行验证
verifyAllData(); 