// 检查表结构脚本
// 查看4个核心模块的表结构

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

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

async function checkTableStructure() {
  let connection;
  
  try {
    console.log('🔌 连接数据库...');
    connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功！');
    
    console.log('\n🔍 检查表结构...');
    
    // 检查Admin模块表结构
    await checkAdminTables(connection);
    
    // 检查Member模块表结构
    await checkMemberTables(connection);
    
    // 检查RBAC模块表结构
    await checkRbacTables(connection);
    
    // 检查Auth模块表结构
    await checkAuthTables(connection);
    
  } catch (error) {
    console.error('❌ 检查失败:', error.message);
  } finally {
    if (connection) {
      await connection.end();
      console.log('🔌 数据库连接已关闭');
    }
  }
}

async function checkAdminTables(connection) {
  console.log('\n📊 Admin模块表结构:');
  
  try {
    // 检查sys_user表
    console.log('  👥 sys_user表:');
    const [userFields] = await connection.execute('DESCRIBE sys_user');
    userFields.forEach(field => {
      console.log(`    - ${field.Field}: ${field.Type} ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Default ? `DEFAULT ${field.Default}` : ''} ${field.Comment ? `COMMENT '${field.Comment}'` : ''}`);
    });
    
    // 检查sys_user_role表
    console.log('  🔐 sys_user_role表:');
    const [roleFields] = await connection.execute('DESCRIBE sys_user_role');
    roleFields.forEach(field => {
      console.log(`    - ${field.Field}: ${field.Type} ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Default ? `DEFAULT ${field.Default}` : ''} ${field.Comment ? `COMMENT '${field.Comment}'` : ''}`);
    });
    
    // 检查sys_user_log表
    console.log('  📝 sys_user_log表:');
    const [logFields] = await connection.execute('DESCRIBE sys_user_log');
    logFields.forEach(field => {
      console.log(`    - ${field.Field}: ${field.Type} ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Default ? `DEFAULT ${field.Default}` : ''} ${field.Comment ? `COMMENT '${field.Comment}'` : ''}`);
    });
    
  } catch (error) {
    console.error(`  ❌ Admin模块检查失败: ${error.message}`);
  }
}

async function checkMemberTables(connection) {
  console.log('\n👥 Member模块表结构:');
  
  try {
    // 检查member表
    console.log('  👤 member表:');
    const [memberFields] = await connection.execute('DESCRIBE member');
    memberFields.forEach(field => {
      console.log(`    - ${field.Field}: ${field.Type} ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Default ? `DEFAULT ${field.Default}` : ''} ${field.Comment ? `COMMENT '${field.Comment}'` : ''}`);
    });
    
    // 检查member_level表
    console.log('  ⭐ member_level表:');
    const [levelFields] = await connection.execute('DESCRIBE member_level');
    levelFields.forEach(field => {
      console.log(`    - ${field.Field}: ${field.Type} ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Default ? `DEFAULT ${field.Default}` : ''} ${field.Comment ? `COMMENT '${field.Comment}'` : ''}`);
    });
    
    // 检查member_address表
    console.log('  🏠 member_address表:');
    const [addressFields] = await connection.execute('DESCRIBE member_address');
    addressFields.forEach(field => {
      console.log(`    - ${field.Field}: ${field.Type} ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Default ? `DEFAULT ${field.Default}` : ''} ${field.Comment ? `COMMENT '${field.Comment}'` : ''}`);
    });
    
  } catch (error) {
    console.error(`  ❌ Member模块检查失败: ${error.message}`);
  }
}

async function checkRbacTables(connection) {
  console.log('\n🔐 RBAC模块表结构:');
  
  try {
    // 检查sys_role表
    console.log('  🎭 sys_role表:');
    const [roleFields] = await connection.execute('DESCRIBE sys_role');
    roleFields.forEach(field => {
      console.log(`    - ${field.Field}: ${field.Type} ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Default ? `DEFAULT ${field.Default}` : ''} ${field.Comment ? `COMMENT '${field.Comment}'` : ''}`);
    });
    
    // 检查sys_menu表
    console.log('  📋 sys_menu表:');
    const [menuFields] = await connection.execute('DESCRIBE sys_menu');
    menuFields.forEach(field => {
      console.log(`    - ${field.Field}: ${field.Type} ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Default ? `DEFAULT ${field.Default}` : ''} ${field.Comment ? `COMMENT '${field.Comment}'` : ''}`);
    });
    
  } catch (error) {
    console.error(`  ❌ RBAC模块检查失败: ${error.message}`);
  }
}

async function checkAuthTables(connection) {
  console.log('\n🔑 Auth模块表结构:');
  
  try {
    // 检查auth_token表
    const [tables] = await connection.execute("SHOW TABLES LIKE 'auth_token'");
    
    if (tables.length > 0) {
      console.log('  🎫 auth_token表:');
      const [tokenFields] = await connection.execute('DESCRIBE auth_token');
      tokenFields.forEach(field => {
        console.log(`    - ${field.Field}: ${field.Type} ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Default ? `DEFAULT ${field.Default}` : ''} ${field.Comment ? `COMMENT '${field.Comment}'` : ''}`);
      });
    } else {
      console.log('  ⚠️  auth_token表不存在');
    }
    
  } catch (error) {
    console.error(`  ❌ Auth模块检查失败: ${error.message}`);
  }
}

// 运行检查
checkTableStructure(); 