题库导入功能完成,考试计划功能完成。
This commit is contained in:
@@ -40,9 +40,27 @@ export interface TaskReport {
|
||||
}>;
|
||||
}
|
||||
|
||||
export interface ActiveTaskStat {
|
||||
taskId: string;
|
||||
taskName: string;
|
||||
subjectName: string;
|
||||
totalUsers: number;
|
||||
completedUsers: number;
|
||||
completionRate: number;
|
||||
passRate: number;
|
||||
excellentRate: number;
|
||||
startAt: string;
|
||||
endAt: string;
|
||||
}
|
||||
|
||||
export class ExamTaskModel {
|
||||
static async findAll(): Promise<TaskWithSubject[]> {
|
||||
const sql = `
|
||||
static async findAll(): Promise<(TaskWithSubject & {
|
||||
completedUsers: number;
|
||||
passRate: number;
|
||||
excellentRate: number;
|
||||
})[]> {
|
||||
// 1. 先获取所有任务的基本信息
|
||||
const baseTasks = await all(`
|
||||
SELECT
|
||||
t.id,
|
||||
t.name,
|
||||
@@ -51,14 +69,112 @@ export class ExamTaskModel {
|
||||
t.end_at as endAt,
|
||||
t.created_at as createdAt,
|
||||
s.name as subjectName,
|
||||
COUNT(DISTINCT etu.user_id) as userCount
|
||||
COUNT(DISTINCT etu.user_id) as userCount,
|
||||
s.total_score as totalScore
|
||||
FROM exam_tasks t
|
||||
JOIN exam_subjects s ON t.subject_id = s.id
|
||||
LEFT JOIN exam_task_users etu ON t.id = etu.task_id
|
||||
GROUP BY t.id
|
||||
ORDER BY t.created_at DESC
|
||||
`;
|
||||
return query(sql);
|
||||
`);
|
||||
|
||||
// 2. 为每个任务计算完成人数、合格率和优秀率
|
||||
const tasksWithStats: any[] = [];
|
||||
for (const task of baseTasks) {
|
||||
// 获取该任务的详细报表数据
|
||||
const report = await this.getReport(task.id);
|
||||
|
||||
// 计算合格率(得分率60%以上)
|
||||
const passingUsers = report.details.filter((d: any) => {
|
||||
if (d.score === null) return false;
|
||||
return (d.score / task.totalScore) >= 0.6;
|
||||
}).length;
|
||||
|
||||
const passRate = report.totalUsers > 0
|
||||
? Math.round((passingUsers / report.totalUsers) * 100)
|
||||
: 0;
|
||||
|
||||
// 计算优秀率(得分率80%以上)
|
||||
const excellentUsers = report.details.filter((d: any) => {
|
||||
if (d.score === null) return false;
|
||||
return (d.score / task.totalScore) >= 0.8;
|
||||
}).length;
|
||||
|
||||
const excellentRate = report.totalUsers > 0
|
||||
? Math.round((excellentUsers / report.totalUsers) * 100)
|
||||
: 0;
|
||||
|
||||
tasksWithStats.push({
|
||||
...task,
|
||||
completedUsers: report.completedUsers,
|
||||
passRate,
|
||||
excellentRate
|
||||
});
|
||||
}
|
||||
|
||||
return tasksWithStats;
|
||||
}
|
||||
|
||||
static async getActiveTasksWithStats(): Promise<ActiveTaskStat[]> {
|
||||
const now = new Date().toISOString();
|
||||
|
||||
// 1. 获取当前时间有效的任务,包括开始和结束时间
|
||||
const activeTasks = await all(`
|
||||
SELECT
|
||||
t.id, t.name as taskName, s.name as subjectName, s.total_score as totalScore,
|
||||
t.start_at as startAt, t.end_at as endAt
|
||||
FROM exam_tasks t
|
||||
JOIN exam_subjects s ON t.subject_id = s.id
|
||||
WHERE t.start_at <= ? AND t.end_at >= ?
|
||||
ORDER BY t.created_at DESC
|
||||
`, [now, now]);
|
||||
|
||||
const stats: ActiveTaskStat[] = [];
|
||||
|
||||
for (const task of activeTasks) {
|
||||
// 2. 获取每个任务的详细报告数据
|
||||
const report = await this.getReport(task.id);
|
||||
|
||||
// 3. 计算完成率
|
||||
const completionRate = report.totalUsers > 0
|
||||
? Math.round((report.completedUsers / report.totalUsers) * 100)
|
||||
: 0;
|
||||
|
||||
// 4. 计算合格率(得分率60%以上)
|
||||
const passingUsers = report.details.filter(d => {
|
||||
if (d.score === null) return false;
|
||||
return (d.score / task.totalScore) >= 0.6;
|
||||
}).length;
|
||||
|
||||
const passRate = report.totalUsers > 0
|
||||
? Math.round((passingUsers / report.totalUsers) * 100)
|
||||
: 0;
|
||||
|
||||
// 5. 计算优秀率(得分率80%以上)
|
||||
const excellentUsers = report.details.filter(d => {
|
||||
if (d.score === null) return false;
|
||||
return (d.score / task.totalScore) >= 0.8;
|
||||
}).length;
|
||||
|
||||
const excellentRate = report.totalUsers > 0
|
||||
? Math.round((excellentUsers / report.totalUsers) * 100)
|
||||
: 0;
|
||||
|
||||
stats.push({
|
||||
taskId: task.id,
|
||||
taskName: task.taskName,
|
||||
subjectName: task.subjectName,
|
||||
totalUsers: report.totalUsers,
|
||||
completedUsers: report.completedUsers,
|
||||
completionRate,
|
||||
passRate,
|
||||
excellentRate,
|
||||
startAt: task.startAt,
|
||||
endAt: task.endAt
|
||||
});
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
static async findById(id: string): Promise<ExamTask | null> {
|
||||
@@ -140,6 +256,16 @@ export class ExamTaskModel {
|
||||
await run(`DELETE FROM exam_tasks WHERE id = ?`, [id]);
|
||||
}
|
||||
|
||||
static async getTaskUsers(taskId: string): Promise<string[]> {
|
||||
const rows = await all(`
|
||||
SELECT user_id as userId
|
||||
FROM exam_task_users
|
||||
WHERE task_id = ?
|
||||
`, [taskId]);
|
||||
|
||||
return rows.map(row => row.userId);
|
||||
}
|
||||
|
||||
static async getReport(taskId: string): Promise<TaskReport> {
|
||||
const task = await this.findById(taskId);
|
||||
if (!task) throw new Error('任务不存在');
|
||||
@@ -209,23 +335,141 @@ export class ExamTaskModel {
|
||||
if (!subject) throw new Error('科目不存在');
|
||||
|
||||
const { QuestionModel } = await import('./question');
|
||||
const questions: Awaited<ReturnType<typeof QuestionModel.getRandomQuestions>> = [];
|
||||
|
||||
for (const [type, ratio] of Object.entries(subject.typeRatios)) {
|
||||
if (ratio <= 0) continue;
|
||||
const typeScore = Math.floor((ratio / 100) * subject.totalScore);
|
||||
const avgScore = 10;
|
||||
const count = Math.max(1, Math.round(typeScore / avgScore));
|
||||
|
||||
const categories = Object.entries(subject.categoryRatios)
|
||||
.filter(([, r]) => r > 0)
|
||||
.map(([c]) => c);
|
||||
|
||||
const qs = await QuestionModel.getRandomQuestions(type as any, count, categories);
|
||||
questions.push(...qs);
|
||||
let questions: Awaited<ReturnType<typeof QuestionModel.getRandomQuestions>> = [];
|
||||
|
||||
// 构建包含所有类别的数组,根据比重重复对应次数
|
||||
const allCategories: string[] = [];
|
||||
for (const [category, catRatio] of Object.entries(subject.categoryRatios)) {
|
||||
if (catRatio > 0) {
|
||||
// 根据比重计算该类别应占的总题目数比例
|
||||
const count = Math.round((catRatio / 100) * 100); // 放大100倍避免小数问题
|
||||
for (let i = 0; i < count; i++) {
|
||||
allCategories.push(category);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 确保总题目数至少为1
|
||||
if (allCategories.length === 0) {
|
||||
allCategories.push('通用');
|
||||
}
|
||||
|
||||
// 按题型分配题目
|
||||
for (const [type, typeRatio] of Object.entries(subject.typeRatios)) {
|
||||
if (typeRatio <= 0) continue;
|
||||
|
||||
// 计算该题型应占的总分
|
||||
const targetTypeScore = Math.round((typeRatio / 100) * subject.totalScore);
|
||||
let currentTypeScore = 0;
|
||||
let typeQuestions: Awaited<ReturnType<typeof QuestionModel.getRandomQuestions>> = [];
|
||||
|
||||
// 尝试获取足够分数的题目
|
||||
while (currentTypeScore < targetTypeScore) {
|
||||
// 随机选择一个类别
|
||||
const randomCategory = allCategories[Math.floor(Math.random() * allCategories.length)];
|
||||
|
||||
// 获取该类型和类别的随机题目
|
||||
const availableQuestions = await QuestionModel.getRandomQuestions(
|
||||
type as any,
|
||||
10, // 一次获取多个,提高效率
|
||||
[randomCategory]
|
||||
);
|
||||
|
||||
if (availableQuestions.length === 0) {
|
||||
break; // 该类型/类别没有题目,跳过
|
||||
}
|
||||
|
||||
// 过滤掉已选题目
|
||||
const availableUnselected = availableQuestions.filter(q =>
|
||||
!questions.some(selected => selected.id === q.id)
|
||||
);
|
||||
|
||||
if (availableUnselected.length === 0) {
|
||||
break; // 没有可用的新题目了
|
||||
}
|
||||
|
||||
// 选择分数最接近剩余需求的题目
|
||||
const remainingForType = targetTypeScore - currentTypeScore;
|
||||
const selectedQuestion = availableUnselected.reduce((prev, curr) => {
|
||||
const prevDiff = Math.abs(remainingForType - prev.score);
|
||||
const currDiff = Math.abs(remainingForType - curr.score);
|
||||
return currDiff < prevDiff ? curr : prev;
|
||||
});
|
||||
|
||||
// 添加到题型题目列表
|
||||
typeQuestions.push(selectedQuestion);
|
||||
currentTypeScore += selectedQuestion.score;
|
||||
|
||||
// 防止无限循环
|
||||
if (typeQuestions.length > 100) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
questions.push(...typeQuestions);
|
||||
}
|
||||
|
||||
// 如果总分不足,尝试补充题目
|
||||
let totalScore = questions.reduce((sum, q) => sum + q.score, 0);
|
||||
while (totalScore < subject.totalScore) {
|
||||
// 获取所有类型的随机题目
|
||||
const allTypes = Object.keys(subject.typeRatios).filter(type => subject.typeRatios[type] > 0);
|
||||
if (allTypes.length === 0) break;
|
||||
|
||||
const randomType = allTypes[Math.floor(Math.random() * allTypes.length)];
|
||||
const availableQuestions = await QuestionModel.getRandomQuestions(
|
||||
randomType as any,
|
||||
10,
|
||||
allCategories
|
||||
);
|
||||
|
||||
if (availableQuestions.length === 0) break;
|
||||
|
||||
// 过滤掉已选题目
|
||||
const availableUnselected = availableQuestions.filter(q =>
|
||||
!questions.some(selected => selected.id === q.id)
|
||||
);
|
||||
|
||||
if (availableUnselected.length === 0) break;
|
||||
|
||||
// 选择分数最接近剩余需求的题目
|
||||
const remainingScore = subject.totalScore - totalScore;
|
||||
const selectedQuestion = availableUnselected.reduce((prev, curr) => {
|
||||
const prevDiff = Math.abs(remainingScore - prev.score);
|
||||
const currDiff = Math.abs(remainingScore - curr.score);
|
||||
return currDiff < prevDiff ? curr : prev;
|
||||
});
|
||||
|
||||
questions.push(selectedQuestion);
|
||||
totalScore += selectedQuestion.score;
|
||||
|
||||
// 防止无限循环
|
||||
if (questions.length > 200) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果总分超过,尝试移除一些题目
|
||||
while (totalScore > subject.totalScore) {
|
||||
// 找到最接近剩余差值的题目
|
||||
const excessScore = totalScore - subject.totalScore;
|
||||
let closestIndex = -1;
|
||||
let closestDiff = Infinity;
|
||||
|
||||
for (let i = 0; i < questions.length; i++) {
|
||||
const diff = Math.abs(questions[i].score - excessScore);
|
||||
if (diff < closestDiff) {
|
||||
closestDiff = diff;
|
||||
closestIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (closestIndex === -1) break;
|
||||
|
||||
// 移除该题目
|
||||
totalScore -= questions[closestIndex].score;
|
||||
questions.splice(closestIndex, 1);
|
||||
}
|
||||
|
||||
const totalScore = questions.reduce((sum, q) => sum + q.score, 0);
|
||||
|
||||
return {
|
||||
questions,
|
||||
@@ -241,9 +485,9 @@ export class ExamTaskModel {
|
||||
FROM exam_tasks t
|
||||
INNER JOIN exam_task_users tu ON t.id = tu.task_id
|
||||
INNER JOIN exam_subjects s ON t.subject_id = s.id
|
||||
WHERE tu.user_id = ? AND t.start_at <= ? AND t.end_at >= ?
|
||||
WHERE tu.user_id = ? AND t.start_at <= ?
|
||||
ORDER BY t.start_at DESC
|
||||
`, [userId, now, now]);
|
||||
`, [userId, now]);
|
||||
|
||||
return rows.map(row => ({
|
||||
id: row.id,
|
||||
|
||||
Reference in New Issue
Block a user