Files
ai-showcase-platform/scripts/insert-test-team-members.js

83 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const mysql = require('mysql2/promise');
async function insertTeamMembers() {
const connection = await mysql.createConnection({
host: 'mysql.theaken.com',
port: 33306,
user: 'AI_Platform',
password: 'Aa123456',
database: 'db_AI_Platform',
charset: 'utf8mb4',
timezone: '+08:00'
});
try {
console.log('🔍 開始插入團隊成員測試數據...');
// 查看現有團隊
const [teams] = await connection.execute('SELECT id, name, leader_id FROM teams WHERE is_active = 1 LIMIT 5');
console.log('現有團隊:', teams);
// 查看現有用戶
const [users] = await connection.execute('SELECT id, name FROM users WHERE status = "active" LIMIT 5');
console.log('現有用戶:', users);
if (teams.length > 0 && users.length > 0) {
// 為每個團隊插入成員
for (let i = 0; i < Math.min(teams.length, 3); i++) {
const team = teams[i];
const teamId = team.id;
// 插入隊長(使用團隊的 leader_id
await connection.execute(
'INSERT IGNORE INTO team_members (id, team_id, user_id, role, joined_at) VALUES (?, ?, ?, ?, NOW())',
[`tm_${Date.now()}_${i}_1`, teamId, team.leader_id, '隊長']
);
// 插入成員(使用其他用戶)
for (let j = 1; j < Math.min(users.length, 3); j++) {
if (users[j].id !== team.leader_id) {
await connection.execute(
'INSERT IGNORE INTO team_members (id, team_id, user_id, role, joined_at) VALUES (?, ?, ?, ?, NOW())',
[`tm_${Date.now()}_${i}_${j+1}`, teamId, users[j].id, '成員']
);
}
}
console.log(`✅ 團隊 ${team.name} 成員插入成功`);
}
}
// 驗證結果
const [members] = await connection.execute(`
SELECT tm.*, t.name as team_name, u.name as user_name
FROM team_members tm
LEFT JOIN teams t ON tm.team_id = t.id
LEFT JOIN users u ON tm.user_id = u.id
ORDER BY tm.team_id
`);
console.log('📊 團隊成員統計:', members);
// 檢查團隊成員數量
const [counts] = await connection.execute(`
SELECT
t.id,
t.name as team_name,
COUNT(tm.id) as member_count
FROM teams t
LEFT JOIN team_members tm ON t.id = tm.team_id
WHERE t.is_active = 1
GROUP BY t.id, t.name
ORDER BY t.name
`);
console.log('📈 團隊成員數量:', counts);
} catch (error) {
console.error('❌ 錯誤:', error);
} finally {
await connection.end();
}
}
insertTeamMembers();