/* 成绩管理:按学号查成绩,再做增删改
成绩模块的五个接口都用业务学号 stu_no,参数字段是 query 不是 JSON body,
这里通过 ScoreApi 调用,不再关心细节 */
window.PAGES.push({
key: 'scores',
title: '成绩管理',
sub: '按学生维护每次考核的成绩',
group: '数据管理',
icon: 'score',
component: {
name: 'ScoresPage',
template: `
刷新
+ 新增成绩
{{ currentName }} 的成绩
学号 {{ stuNo }} · 共 {{ rows.length }} 次考核
· 平均 {{ avgScore }} 分
{{ row.score }}
不及格
编辑
删除
取消
保存
`,
setup() {
const loading = ref(false);
const saving = ref(false);
const rows = ref([]);
const students = ref([]);
const stuNo = ref(null);
const dialog = ref(false);
const editing = ref(null);
const formRef = ref(null);
const form = ref({ stu_no: null, exam_order: 1, score: 80 });
const rules = {
exam_order: [{ required: true, message: '请填写考核序次', trigger: 'blur' }],
score: [{ required: true, message: '请填写成绩', trigger: 'blur' }],
};
const currentName = computed(() => {
const s = students.value.find(x => x.stu_no === stuNo.value);
return s ? (s.stu_name || '未命名') : '';
});
const avgScore = computed(() => {
if (!rows.value.length) return 0;
const sum = rows.value.reduce((a, r) => a + Number(r.score || 0), 0);
return Math.round((sum / rows.value.length) * 10) / 10;
});
const load = async () => {
if (!stuNo.value) { rows.value = []; return; }
loading.value = true;
try {
rows.value = await ScoreApi.listByStuNo(stuNo.value);
rows.value.sort((a, b) => a.exam_order - b.exam_order);
} catch (e) { toastErr(e); }
finally { loading.value = false; }
};
const onPickStudent = () => { load(); };
const openCreate = () => {
editing.value = null;
// 序次默认接着最后一条往后排,省得每次手填
const next = rows.value.length
? Math.max.apply(null, rows.value.map(r => r.exam_order)) + 1 : 1;
form.value = { stu_no: stuNo.value, exam_order: next, score: 80 };
dialog.value = true;
};
const openEdit = (row) => {
editing.value = { stu_no: row.stu_no, exam_order: row.exam_order };
form.value = { stu_no: row.stu_no, exam_order: row.exam_order, score: row.score };
dialog.value = true;
};
const submit = async () => {
if (!formRef.value) return;
try { await formRef.value.validate(); } catch (e) { return; }
saving.value = true;
try {
if (editing.value) {
await ScoreApi.update(form.value.stu_no, form.value.exam_order, form.value.score);
toastOk('修改成功');
} else {
await ScoreApi.add(form.value.stu_no, form.value.exam_order, form.value.score);
toastOk('新增成功');
}
dialog.value = false;
await load();
} catch (e) { toastErr(e); }
finally { saving.value = false; }
};
const remove = async (row) => {
try {
await ElementPlus.ElMessageBox.confirm(
`确定删除第 ${row.exam_order} 次考核的成绩(${row.score} 分)吗?`,
'删除确认', { type: 'warning', confirmButtonText: '确定删除', cancelButtonText: '取消' });
} catch (e) { return; }
try {
await ScoreApi.del(row.stu_no || stuNo.value, row.exam_order);
toastOk('删除成功');
await load();
} catch (e) { toastErr(e); }
};
onMounted(async () => {
try { students.value = await Cache.loadStudents(); }
catch (e) { toastErr(e); }
});
return { loading, saving, rows, students, stuNo, dialog, editing, formRef, form, rules,
currentName, avgScore, load, onPickStudent, openCreate, openEdit, submit, remove,
studentLabel };
},
},
});