// 查询userlogin数据库中的personnel表,获取所有采购员数据 require('dotenv').config({ path: 'd:\\xt\\mian_ly\\server-example\\.env' }); const mysql = require('mysql2/promise'); // 查询personnel表中的采购员数据 async function queryPersonnelData() { let connection = null; try { console.log('连接到userlogin数据库...'); connection = await mysql.createConnection({ host: '1.95.162.61', // 直接使用正确的主机地址 user: process.env.DB_USER || 'root', password: process.env.DB_PASSWORD || 'schl@2025', // 直接使用默认密码 database: 'userlogin', port: process.env.DB_PORT || 3306, timezone: '+08:00' // 设置时区为UTC+8 }); console.log('数据库连接成功\n'); // 首先查询personnel表结构 console.log('=== personnel表结构 ==='); const [columns] = await connection.execute( 'SHOW COLUMNS FROM personnel' ); console.log('字段列表:', columns.map(col => col.Field).join(', ')); console.log(); // 查询projectName为采购员的数据 console.log('=== 所有采购员数据 ==='); const [personnelData] = await connection.execute( 'SELECT * FROM personnel WHERE projectName = ?', ['采购员'] ); console.log(`找到 ${personnelData.length} 名采购员记录`); console.log('\n采购员数据详情:'); // 格式化输出数据 personnelData.forEach((person, index) => { console.log(`\n${index + 1}. 采购员信息:`); console.log(` - ID: ${person.id || 'N/A'}`); console.log(` - 用户ID: ${person.userId || 'N/A'}`); console.log(` - 姓名: ${person.name || 'N/A'}`); console.log(` - 别名: ${person.alias || 'N/A'}`); console.log(` - 电话号码: ${person.phoneNumber || 'N/A'}`); console.log(` - 公司: ${person.managercompany || 'N/A'}`); console.log(` - 部门: ${person.managerdepartment || 'N/A'}`); console.log(` - 角色: ${person.role || 'N/A'}`); }); // 输出JSON格式,便于复制使用 console.log('\n=== JSON格式数据 (用于客服页面) ==='); const customerServiceData = personnelData.map((person, index) => ({ id: index + 1, managerId: person.userId || `PM${String(index + 1).padStart(3, '0')}`, managercompany: person.managercompany || '未知公司', managerdepartment: person.managerdepartment || '采购部', organization: person.organization || '采购组', projectName: person.role || '采购员', name: person.name || '未知', alias: person.alias || person.name || '未知', phoneNumber: person.phoneNumber || '', avatarUrl: '', score: 990 + (index % 10), isOnline: index % 4 !== 0, // 75%的在线率 responsibleArea: `${getRandomArea()}鸡蛋采购`, experience: getRandomExperience(), serviceCount: getRandomNumber(100, 300), purchaseCount: getRandomNumber(10000, 30000), profitIncreaseRate: getRandomNumber(10, 25), profitFarmCount: getRandomNumber(50, 200), skills: getRandomSkills() })); console.log(JSON.stringify(customerServiceData, null, 2)); return customerServiceData; } catch (error) { console.error('查询过程中发生错误:', error); console.error('错误详情:', error.stack); return null; } finally { if (connection) { await connection.end(); console.log('\n数据库连接已关闭'); } } } // 辅助函数:生成随机区域 function getRandomArea() { const areas = ['华北区', '华东区', '华南区', '全国', '西南区', '西北区', '东北区']; return areas[Math.floor(Math.random() * areas.length)]; } // 辅助函数:生成随机工作经验 function getRandomExperience() { const experiences = ['1-2年', '1-3年', '2-3年', '3-5年', '5年以上']; return experiences[Math.floor(Math.random() * experiences.length)]; } // 辅助函数:生成随机数字 function getRandomNumber(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } // 辅助函数:生成随机技能 function getRandomSkills() { const allSkills = ['渠道拓展', '供应商维护', '质量把控', '精准把控市场价格', '谈判技巧', '库存管理']; const skillCount = Math.floor(Math.random() * 3) + 2; // 2-4个技能 const selectedSkills = []; while (selectedSkills.length < skillCount) { const skill = allSkills[Math.floor(Math.random() * allSkills.length)]; if (!selectedSkills.includes(skill)) { selectedSkills.push(skill); } } return selectedSkills; } // 运行查询 queryPersonnelData();