140 lines
4.8 KiB
JavaScript
140 lines
4.8 KiB
JavaScript
|
|
import fs from 'fs';
|
|||
|
|
import path from 'path';
|
|||
|
|
import { fileURLToPath } from 'url';
|
|||
|
|
|
|||
|
|
const __filename = fileURLToPath(import.meta.url);
|
|||
|
|
const __dirname = path.dirname(__filename);
|
|||
|
|
|
|||
|
|
// 读取财经商贸个人档案数据
|
|||
|
|
const financeProfileData = JSON.parse(
|
|||
|
|
fs.readFileSync(path.join(__dirname, '网页未导入数据/财经商贸产业/财经商贸个人档案.json'), 'utf-8')
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// 读取头像列表
|
|||
|
|
const avatarList = JSON.parse(
|
|||
|
|
fs.readFileSync(path.join(__dirname, '网页未导入数据/头像列表.json'), 'utf-8')
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// 按学分排序
|
|||
|
|
const sortedStudents = [...financeProfileData].sort((a, b) => {
|
|||
|
|
return parseInt(b["学分"]) - parseInt(a["学分"]);
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 重新分配正确的排名
|
|||
|
|
sortedStudents.forEach((student, index) => {
|
|||
|
|
student.correctRank = index + 1;
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 为学生分配头像
|
|||
|
|
const studentAvatars = {};
|
|||
|
|
sortedStudents.forEach((student, index) => {
|
|||
|
|
studentAvatars[student["学员名称"]] = avatarList[index % avatarList.length].url;
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 找到罗浩宇
|
|||
|
|
const luoHaoyu = sortedStudents.find(s => s["学员名称"] === "罗浩宇");
|
|||
|
|
|
|||
|
|
// 创建完整的班级排名数据
|
|||
|
|
const fullClassRanking = sortedStudents.map((student) => {
|
|||
|
|
return {
|
|||
|
|
id: student.correctRank,
|
|||
|
|
name: student["学员名称"],
|
|||
|
|
studentName: student["学员名称"], // 兼容性字段
|
|||
|
|
score: parseInt(student["学分"]),
|
|||
|
|
rank: student.correctRank,
|
|||
|
|
studentId: student["学号"],
|
|||
|
|
school: student["学校名称"],
|
|||
|
|
major: student["专业名称"],
|
|||
|
|
mbti: student["MBTI"],
|
|||
|
|
verticalDirection: student["垂直方向"],
|
|||
|
|
avatar: studentAvatars[student["学员名称"]],
|
|||
|
|
isCurrentUser: student["学员名称"] === "罗浩宇"
|
|||
|
|
};
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
// 读取mockData.js文件
|
|||
|
|
const mockDataPath = path.join(__dirname, 'src/data/mockData.js');
|
|||
|
|
let mockDataContent = fs.readFileSync(mockDataPath, 'utf-8');
|
|||
|
|
|
|||
|
|
// 备份原文件
|
|||
|
|
const backupPath = path.join(__dirname, 'src/data', `mockData.js.backup_${new Date().toISOString().replace(/[:.]/g, '-')}`);
|
|||
|
|
fs.copyFileSync(mockDataPath, backupPath);
|
|||
|
|
console.log(`备份已创建: ${backupPath}`);
|
|||
|
|
|
|||
|
|
// 1. 更新classRanking数组(所有10名学生)
|
|||
|
|
const classRankingStr = ` classRanking: ${JSON.stringify(fullClassRanking, null, 4).replace(/\n/g, '\n ')},`;
|
|||
|
|
|
|||
|
|
// 替换classRanking数组
|
|||
|
|
mockDataContent = mockDataContent.replace(
|
|||
|
|
/classRanking:\s*\[[^\]]*\],/s,
|
|||
|
|
classRankingStr
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// 2. 在mockData根级别添加ranking对象(在classRanking后面)
|
|||
|
|
const rankingObject = {
|
|||
|
|
myRank: {
|
|||
|
|
rank: luoHaoyu?.correctRank || 6,
|
|||
|
|
score: parseInt(luoHaoyu?.["学分"] || "84"),
|
|||
|
|
totalStudents: sortedStudents.length,
|
|||
|
|
trend: "stable",
|
|||
|
|
change: 0,
|
|||
|
|
},
|
|||
|
|
classInfo: {
|
|||
|
|
className: "财经商贸班",
|
|||
|
|
totalStudents: sortedStudents.length,
|
|||
|
|
averageScore: Math.round(
|
|||
|
|
sortedStudents.reduce((sum, s) => sum + parseInt(s["学分"]), 0) / sortedStudents.length
|
|||
|
|
),
|
|||
|
|
},
|
|||
|
|
topStudents: fullClassRanking,
|
|||
|
|
rankings: fullClassRanking
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// 查找是否已存在根级别的ranking
|
|||
|
|
const hasRootRanking = /^\s{2}ranking:\s*\{/m.test(mockDataContent);
|
|||
|
|
|
|||
|
|
if (!hasRootRanking) {
|
|||
|
|
// 在classRanking后添加ranking对象
|
|||
|
|
const rankingStr = `
|
|||
|
|
// 排名数据
|
|||
|
|
ranking: ${JSON.stringify(rankingObject, null, 4).replace(/\n/g, '\n ')},`;
|
|||
|
|
|
|||
|
|
// 在classRanking后面插入ranking
|
|||
|
|
mockDataContent = mockDataContent.replace(
|
|||
|
|
/(classRanking:\s*\[[^\]]*\],)/s,
|
|||
|
|
`$1${rankingStr}`
|
|||
|
|
);
|
|||
|
|
console.log('添加了根级别的ranking对象');
|
|||
|
|
} else {
|
|||
|
|
console.log('根级别ranking对象已存在,跳过添加');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 3. 确保文件末尾有正确的export语句
|
|||
|
|
if (!mockDataContent.includes('export default mockData')) {
|
|||
|
|
mockDataContent += '\nexport default mockData;\n';
|
|||
|
|
console.log('添加了export语句');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 4. 在文件末尾(export之前)添加dashboardStatistics.ranking引用
|
|||
|
|
if (!mockDataContent.includes('mockData.dashboardStatistics.ranking')) {
|
|||
|
|
const exportIndex = mockDataContent.lastIndexOf('export default mockData');
|
|||
|
|
if (exportIndex > -1) {
|
|||
|
|
const insertContent = '\n// 添加ranking到dashboardStatistics\nmockData.dashboardStatistics.ranking = mockData.ranking;\n\n';
|
|||
|
|
mockDataContent = mockDataContent.slice(0, exportIndex) + insertContent + mockDataContent.slice(exportIndex);
|
|||
|
|
} else {
|
|||
|
|
// 如果没有export语句,直接在末尾添加
|
|||
|
|
mockDataContent += '\n// 添加ranking到dashboardStatistics\nmockData.dashboardStatistics.ranking = mockData.ranking;\n\nexport default mockData;\n';
|
|||
|
|
}
|
|||
|
|
console.log('添加了dashboardStatistics.ranking引用');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 写入更新后的文件
|
|||
|
|
fs.writeFileSync(mockDataPath, mockDataContent, 'utf-8');
|
|||
|
|
|
|||
|
|
console.log(`\n✅ 成功修复班级排名数据`);
|
|||
|
|
console.log(`- 班级总人数:${sortedStudents.length}人`);
|
|||
|
|
console.log(`- 罗浩宇排名:第${luoHaoyu?.correctRank}名(${luoHaoyu?.["学分"]}分)`);
|
|||
|
|
console.log('\n前10名学生:');
|
|||
|
|
fullClassRanking.forEach(s => {
|
|||
|
|
console.log(` ${s.rank}. ${s.name} - ${s.score}分`);
|
|||
|
|
});
|