修复“其他考试”成绩链路

This commit is contained in:
biss committed 2026-10-06 21:07:46 +08:00
1 parent ce2f91c36f
commit a5ef6d7b8e
3 files changed
+132 -21

No files matched your search

@@ -97,15 +97,35 @@ public sealed class OtherExamsController(AppDbContext db, ICurrentUserDataScope
[HttpGet("batches/{id:guid}")]
[Authorize(Roles = Managers)]
public async Task<ActionResult> GetBatch(Guid id, CancellationToken ct)
public async Task<ActionResult> GetBatch(Guid id, string? cursor = null, int pageSize = 30, CancellationToken ct = default)
{
var batch = await db.OtherExamBatches.AsNoTracking().Where(x => x.Id == id)
.Select(x => new { x.Id, x.ExamCode, x.Name, x.Organizer, x.ExamDate, x.MetricKind, x.MaxScore, x.LevelOptions, x.Status, x.PublicationCount, x.PublishedAt }).FirstOrDefaultAsync(ct);
if (batch is null) return NotFound();
var results = await db.OtherExamResults.AsNoTracking().Where(x => x.OtherExamBatchId == id)
pageSize = Math.Clamp(pageSize, 10, 100);
if (!CursorToken.TryDecode<OtherExamResultCursor>(cursor, out var after)) return BadRequest("无效的游标。");
var source = db.OtherExamResults.AsNoTracking().Where(x => x.OtherExamBatchId == id);
var total = await source.CountAsync(ct);
if (after is not null)
{
source = source.Where(x => string.Compare(x.Student!.StudentNumber, after.StudentNumber) > 0 ||
(x.Student!.StudentNumber == after.StudentNumber && x.Id.CompareTo(after.Id) > 0));
}
var rows = await source
.OrderBy(x => x.Student!.StudentNumber)
.ThenBy(x => x.Id)
.Take(pageSize + 1)
.Select(x => new { x.Id, x.StudentId, StudentNumber = x.Student!.StudentNumber, StudentName = x.Student.Name, CollegeName = x.Student.AdministrativeClass!.Major!.College!.Name, ClassName = x.Student.AdministrativeClass.Name, x.AttemptNumber, x.Score, x.Level, x.IsPassed, x.Notes }).ToListAsync(ct);
return Ok(new { Batch = batch, Results = results });
var hasMore = rows.Count > pageSize;
var items = rows.Take(pageSize).Cast<object>().ToArray();
var last = rows.Take(pageSize).LastOrDefault();
return Ok(new
{
Batch = batch,
Results = new CursorPagedResult<object>(items,
hasMore && last is not null ? CursorToken.Encode(new OtherExamResultCursor(last.StudentNumber, last.Id)) : null,
hasMore, pageSize, total)
});
}
[HttpPut("batches/{id:guid}/results")]
@@ -114,7 +134,7 @@ public sealed class OtherExamsController(AppDbContext db, ICurrentUserDataScope
{
var batch = await db.OtherExamBatches.Include(x => x.Results).FirstOrDefaultAsync(x => x.Id == id, ct);
if (batch is null) return NotFound();
var result = await ReplaceResultsAsync(batch, request.Results, ct);
var result = await MergeResultsAsync(batch, request.Results, ct);
return result is null ? Ok(new { updated = request.Results.Count }) : result;
}
@@ -153,8 +173,8 @@ public sealed class OtherExamsController(AppDbContext db, ICurrentUserDataScope
inputs.Add(new OtherExamResultRequest(number, score, level, passed, Normalize(row["备注"])));
}
if (errors.Count > 0) return ImportValidationProblem(errors);
var result = await ReplaceResultsAsync(batch, inputs, ct);
return result ?? Ok(new { updated = inputs.Count });
var result = await MergeResultsAsync(batch, inputs, ct);
return result ?? Ok(new { imported = inputs.Count });
}
[HttpPost("batches/{id:guid}/publish")]
@@ -179,7 +199,7 @@ public sealed class OtherExamsController(AppDbContext db, ICurrentUserDataScope
if (studentId is null) return ConflictProblem("当前账号未关联有效学生档案。");
var history = await db.OtherExamResults.AsNoTracking().Where(x => x.StudentId == studentId && x.OtherExamBatch!.Status == OtherExamBatchStatus.Published)
.OrderByDescending(x => x.OtherExamBatch!.ExamDate).ThenByDescending(x => x.AttemptNumber)
.Select(x => new { x.Id, ExamCode = x.OtherExamBatch!.ExamCode ?? x.OtherExamBatch.Name, BatchId = x.OtherExamBatchId, ExamName = x.OtherExamBatch.Name, x.OtherExamBatch.ExamDate, x.OtherExamBatch.MetricKind, x.OtherExamBatch.MaxScore, x.OtherExamBatch.LevelOptions, x.AttemptNumber, x.Score, x.Level, x.IsPassed, x.OtherExamBatch.PublishedAt }).ToListAsync(ct);
.Select(x => new { x.Id, ExamCode = x.OtherExamBatch!.ExamCode ?? x.OtherExamBatch.Name, BatchId = x.OtherExamBatchId, ExamName = x.OtherExamBatch.Name, x.OtherExamBatch.ExamDate, x.OtherExamBatch.MetricKind, x.OtherExamBatch.MaxScore, x.OtherExamBatch.LevelOptions, x.AttemptNumber, x.Score, x.Level, x.IsPassed, x.Notes, x.OtherExamBatch.PublishedAt }).ToListAsync(ct);
var best = history.GroupBy(x => x.ExamCode).Select(g => g.OrderByDescending(x => Rank(x.MetricKind, x.Score, x.Level, x.IsPassed, x.LevelOptions)).ThenByDescending(x => x.ExamDate).First()).ToList();
return Ok(new { Best = best, History = history });
}
@@ -205,7 +225,6 @@ public sealed class OtherExamsController(AppDbContext db, ICurrentUserDataScope
return await db.ExecuteInRetriableTransactionAsync<ActionResult?>(async transaction =>
{
db.OtherExamResults.RemoveRange(batch.Results);
batch.Status = OtherExamBatchStatus.Draft;
await db.SaveChangesAsync(ct);
var replacementResults = inputs.Select(x =>
@@ -229,6 +248,53 @@ public sealed class OtherExamsController(AppDbContext db, ICurrentUserDataScope
}, ct);
}
private async Task<ActionResult?> MergeResultsAsync(OtherExamBatch batch, IReadOnlyList<OtherExamResultRequest> inputs, CancellationToken ct)
{
var numbers = inputs.Select(x => x.StudentNumber.Trim()).ToList();
if (numbers.Count != numbers.Distinct(StringComparer.OrdinalIgnoreCase).Count()) return ValidationProblem("同一导入文件中学生不能重复出现。");
var studentRows = await db.Students.Where(x => numbers.Contains(x.StudentNumber)).ToListAsync(ct);
var students = studentRows.ToDictionary(x => x.StudentNumber, StringComparer.OrdinalIgnoreCase);
if (students.Count != numbers.Count) return ValidationProblem("存在不存在的学号,请先检查学生档案。");
foreach (var item in inputs)
{
var error = ValidateResult(batch, item.Score, item.Level, item.IsPassed, item.Notes);
if (error is not null) return ValidationProblem(error);
}
var existing = batch.Results.ToDictionary(x => x.StudentId);
var newStudentIds = students.Values.Where(x => !existing.ContainsKey(x.Id)).Select(x => x.Id).ToList();
var beforeCount = await db.OtherExamResults.AsNoTracking()
.Where(x => x.OtherExamBatchId != batch.Id && newStudentIds.Contains(x.StudentId) &&
(x.OtherExamBatch!.ExamCode == batch.ExamCode || (x.OtherExamBatch.ExamCode == null && batch.ExamCode == null && x.OtherExamBatch.Name == batch.Name)) &&
(x.OtherExamBatch.ExamDate < batch.ExamDate || (x.OtherExamBatch.ExamDate == batch.ExamDate && x.OtherExamBatch.CreatedAt < batch.CreatedAt)))
.GroupBy(x => x.StudentId).Select(x => new { StudentId = x.Key, Count = x.Count() }).ToDictionaryAsync(x => x.StudentId, x => x.Count, ct);
return await db.ExecuteInRetriableTransactionAsync<ActionResult?>(async transaction =>
{
foreach (var item in inputs)
{
var student = students[item.StudentNumber.Trim()];
if (existing.TryGetValue(student.Id, out var result))
{
result.Score = item.Score;
result.Level = Normalize(item.Level);
result.IsPassed = item.IsPassed;
result.Notes = Normalize(item.Notes);
continue;
}
db.OtherExamResults.Add(new OtherExamResult
{
OtherExamBatchId = batch.Id, StudentId = student.Id,
AttemptNumber = beforeCount.GetValueOrDefault(student.Id) + 1,
Score = item.Score, Level = Normalize(item.Level), IsPassed = item.IsPassed, Notes = Normalize(item.Notes)
});
}
await db.SaveChangesAsync(ct);
await transaction.CommitAsync(ct);
return null;
}, ct);
}
private static string[] HeadersFor(OtherExamBatch batch) => batch.MetricKind switch
{
OtherExamMetricKind.Score => ["学号", "成绩", "备注"],
@@ -287,3 +353,4 @@ public sealed record CreateOtherExamRequest([Required] string ExamCode, [Require
public sealed record ReplaceOtherExamResultsRequest(List<OtherExamResultRequest> Results);
public sealed record OtherExamResultRequest([Required] string StudentNumber, decimal? Score, string? Level, bool? IsPassed, string? Notes);
public sealed record OtherExamBatchCursor(DateOnly ExamDate, DateTime CreatedAt, Guid Id);
public sealed record OtherExamResultCursor(string StudentNumber, Guid Id);
@@ -66,13 +66,23 @@ public sealed class OtherExamsControllerTests
EnrollmentDate = new DateOnly(2026, 9, 1),
Status = StudentStatus.Active
};
var anotherStudent = new Student
{
StudentNumber = "202600002",
Name = "另一测试学生",
AdministrativeClassId = administrativeClass.Id,
EnrollmentYear = 2026,
EnrollmentDate = new DateOnly(2026, 9, 1),
Status = StudentStatus.Active
};
var batch = new OtherExamBatch
{
ExamCode = "CET4",
Name = "大学英语四级",
ExamDate = new DateOnly(2026, 6, 1),
MetricKind = OtherExamMetricKind.Score,
MaxScore = 710
MaxScore = 710,
Status = OtherExamBatchStatus.Published
};
var oldResult = new OtherExamResult
{
@@ -81,7 +91,14 @@ public sealed class OtherExamsControllerTests
Score = 500,
AttemptNumber = 1
};
db.AddRange(college, major, administrativeClass, student, batch, oldResult);
var anotherOldResult = new OtherExamResult
{
OtherExamBatchId = batch.Id,
StudentId = anotherStudent.Id,
Score = 520,
AttemptNumber = 1
};
db.AddRange(college, major, administrativeClass, student, anotherStudent, batch, oldResult, anotherOldResult);
await db.SaveChangesAsync();
var controller = new OtherExamsController(db, new TestDataScope(college.Id));
@@ -91,10 +108,13 @@ public sealed class OtherExamsControllerTests
Assert.IsType<OkObjectResult>(result);
db.ChangeTracker.Clear();
var saved = await db.OtherExamResults.SingleAsync(x => x.OtherExamBatchId == batch.Id);
var saved = await db.OtherExamResults.SingleAsync(x => x.StudentId == student.Id);
Assert.Equal(610.5m, saved.Score);
Assert.Equal("已复核", saved.Notes);
Assert.Equal(1, saved.AttemptNumber);
Assert.Equal(2, await db.OtherExamResults.CountAsync(x => x.OtherExamBatchId == batch.Id));
Assert.Equal(520m, (await db.OtherExamResults.SingleAsync(x => x.StudentId == anotherStudent.Id)).Score);
Assert.Equal(OtherExamBatchStatus.Published, (await db.OtherExamBatches.SingleAsync()).Status);
}
[Fact]
+34 -10
View File
@@ -23,6 +23,12 @@ const nextBatchCursor = ref<string>()
const batchHasMore = ref(false)
const selected = ref<any>(null)
const results = ref<ResultRow[]>([])
const resultPage = ref(1)
const resultPageSize = 30
const resultCursors = ref<string[]>([])
const nextResultCursor = ref<string>()
const resultHasMore = ref(false)
const resultTotal = ref(0)
const mine = reactive({ best: [] as any[], history: [] as any[] })
const dialog = ref(false)
const publishDialog = ref(false)
@@ -73,15 +79,33 @@ async function previousBatchPage() {
batchPage.value -= 1
await load()
}
async function openBatch(row: any) {
async function openBatch(row: any, resetPage = true) {
if (resetPage) {
resultPage.value = 1
resultCursors.value = []
nextResultCursor.value = undefined
}
try {
const data = (await http.get(`/other-exams/batches/${row.id}`)).data
const data = (await http.get(`/other-exams/batches/${row.id}`, { params: { cursor: resultCursors.value.at(-1), pageSize: resultPageSize } })).data
const metricKind = Number(data.batch.metricKind ?? 3)
data.batch.metricKind = [1, 2, 3].includes(metricKind) ? metricKind : 3
selected.value = data.batch
results.value = data.results.map((item: any) => ({ ...item, score: item.score ?? null, level: item.level ?? '', notes: item.notes ?? '' }))
results.value = data.results.items.map((item: any) => ({ ...item, score: item.score ?? null, level: item.level ?? '', notes: item.notes ?? '' }))
resultTotal.value = data.results.total
resultHasMore.value = data.results.hasMore
nextResultCursor.value = data.results.nextCursor
} catch (error) { ElMessage.error(apiErrorMessage(error)) }
}
async function nextResultPage() {
if (!selected.value || !nextResultCursor.value || loading.value) return
resultCursors.value.push(nextResultCursor.value); resultPage.value += 1
await openBatch(selected.value, false)
}
async function previousResultPage() {
if (!selected.value || !resultCursors.value.length || loading.value) return
resultCursors.value.pop(); resultPage.value -= 1
await openBatch(selected.value, false)
}
function addResult() { results.value.push({ studentNumber: '', studentName: '', collegeName: '', className: '', score: null, level: '', isPassed: null, notes: '' }) }
async function lookupStudent(row: ResultRow | any) {
row.studentName = ''; row.collegeName = ''; row.className = ''
@@ -95,7 +119,7 @@ async function saveResults() {
saving.value = true
try {
await http.put(`/other-exams/batches/${selected.value.id}/results`, { results: results.value.map(row => ({ studentNumber: row.studentNumber, score: isScoreMetric.value ? row.score : null, level: isLevelMetric.value ? row.level : null, isPassed: isPassMetric.value ? row.isPassed : null, notes: row.notes })) })
ElMessage.success('成绩已保存,参加次数由系统自动计算'); await openBatch(selected.value); await load()
ElMessage.success('本页成绩已保存,参加次数由系统自动计算'); await openBatch(selected.value, false); await load()
} catch (error) { ElMessage.error(apiErrorMessage(error)) } finally { saving.value = false }
}
async function publish() {
@@ -126,11 +150,11 @@ async function importExcel(event: Event) {
const input = event.target as HTMLInputElement; const file = input.files?.[0]; input.value = ''
if (!file) return
const data = new FormData(); data.append('file', file)
try { await http.post(`/other-exams/batches/${selected.value.id}/import`, data, { headers: { 'Content-Type': 'multipart/form-data' } }); ElMessage.success('其他考试成绩导入成功'); await openBatch(selected.value); await load() }
try { await http.post(`/other-exams/batches/${selected.value.id}/import`, data, { headers: { 'Content-Type': 'multipart/form-data' } }); ElMessage.success('其他考试成绩导入成功,未在导入文件中的原有记录已保留'); await openBatch(selected.value, false); await load() }
catch (error) { ElMessage.error(apiErrorMessage(error)) }
}
function metricName(kind: number | string) { const value = Number(kind); return value === 1 ? '合格制' : value === 2 ? '等级制' : '分数制' }
function displayResult(row: any) { return row.metricKind === 3 ? `${row.score} / ${row.maxScore}` : row.metricKind === 2 ? row.level : row.isPassed ? '合格' : '不合格' }
function displayResult(row: any) { return Number(row.metricKind) === 3 ? `${row.score} / ${row.maxScore}` : Number(row.metricKind) === 2 ? row.level : row.isPassed ? '合格' : '不合格' }
onMounted(load)
</script>
@@ -138,12 +162,12 @@ onMounted(load)
<div class="other-exams-page">
<section class="exam-hero"><div><span class="kicker">考试成绩</span><h1>其他考试成绩</h1><p>{{ isStudent ? '你的证书、等级考试与校外考试,按考试归档,最优结果一目了然。' : '用考试编码归并同一考试,按场次维护成绩,系统自动记录每位学生的参加次数。' }}</p></div><el-button v-if="!isStudent" type="primary" :icon="Plus" @click="dialog = true">新建考试场次</el-button></section>
<template v-if="isStudent">
<section class="result-board"><div class="board-title"><div><span class="kicker">成绩汇总</span><h2>我的最优结果</h2></div><span class="board-note">同一考试编码下自动比较</span></div><el-table :data="mine.best" v-loading="loading"><el-table-column prop="examName" label="考试" min-width="180"/><el-table-column prop="examCode" label="考试编码" width="130"/><el-table-column prop="examDate" label="考试日期" width="120"/><el-table-column label="最优结果" min-width="150"><template #default="{ row }"><strong class="best-value">{{ displayResult(row) }}</strong></template></el-table-column><el-table-column prop="attemptNumber" label="参加次数" width="100"/></el-table><el-empty v-if="!loading && !mine.best.length" description="暂无已发布的其他考试成绩"/></section>
<section class="result-board history-board"><div class="board-title"><div><span class="kicker">历史记录</span><h2>历史成绩</h2></div><span class="board-note">每次发布的结果都会保留</span></div><el-table :data="mine.history" v-loading="loading"><el-table-column prop="examName" label="考试" min-width="180"/><el-table-column prop="examCode" label="考试编码" width="130"/><el-table-column prop="examDate" label="考试日期" width="120"/><el-table-column prop="attemptNumber" label="第几次参加" width="110"/><el-table-column label="结果" min-width="140"><template #default="{ row }">{{ displayResult(row) }}</template></el-table-column><el-table-column prop="publishedAt" label="发布时间" width="170"/></el-table></section>
<section class="result-board"><div class="board-title"><div><span class="kicker">成绩汇总</span><h2>我的最优结果</h2></div><span class="board-note">同一考试编码下自动比较</span></div><el-table :data="mine.best" v-loading="loading"><el-table-column prop="examName" label="考试" min-width="180"/><el-table-column prop="examCode" label="考试编码" width="130"/><el-table-column prop="examDate" label="考试日期" width="120"/><el-table-column label="最优结果" min-width="150"><template #default="{ row }"><strong class="best-value">{{ displayResult(row) }}</strong></template></el-table-column><el-table-column prop="notes" label="备注" min-width="200"><template #default="{ row }">{{ row.notes || '—' }}</template></el-table-column><el-table-column prop="attemptNumber" label="参加次数" width="100"/></el-table><el-empty v-if="!loading && !mine.best.length" description="暂无已发布的其他考试成绩"/></section>
<section class="result-board history-board"><div class="board-title"><div><span class="kicker">历史记录</span><h2>历史成绩</h2></div><span class="board-note">每次发布的结果都会保留</span></div><el-table :data="mine.history" v-loading="loading"><el-table-column prop="examName" label="考试" min-width="180"/><el-table-column prop="examCode" label="考试编码" width="130"/><el-table-column prop="examDate" label="考试日期" width="120"/><el-table-column prop="attemptNumber" label="第几次参加" width="110"/><el-table-column label="结果" min-width="140"><template #default="{ row }">{{ displayResult(row) }}</template></el-table-column><el-table-column prop="notes" label="备注" min-width="200"><template #default="{ row }">{{ row.notes || '—' }}</template></el-table-column><el-table-column prop="publishedAt" label="发布时间" width="170"/></el-table></section>
</template>
<template v-else>
<section class="batch-panel"><div class="panel-heading"><div><span class="kicker">考试场次</span><h2>考试场次</h2></div><span>点击场次进入成绩录入</span></div><div class="batch-filters"><el-input v-model="batchKeyword" clearable placeholder="考试编码、名称或组织方" @keyup.enter="load(true)" @clear="load(true)"/><el-select v-model="batchStatus" clearable placeholder="全部状态" @change="load(true)"><el-option label="草稿" :value="1"/><el-option label="已发布" :value="2"/></el-select><el-select v-model="batchMetricKind" clearable placeholder="全部方式" @change="load(true)"><el-option label="合格制" :value="1"/><el-option label="等级制" :value="2"/><el-option label="分数制" :value="3"/></el-select><el-date-picker v-model="batchDateRange" type="daterange" value-format="YYYY-MM-DD" start-placeholder="开始日期" end-placeholder="结束日期" @change="load(true)"/><el-button :icon="Search" @click="load(true)">查询</el-button><el-button :icon="Refresh" @click="() => load()">刷新</el-button></div><el-table :data="batches" v-loading="loading" @row-click="openBatch"><el-table-column prop="examCode" label="考试编码" width="140"/><el-table-column prop="name" label="考试名称" min-width="190"/><el-table-column prop="examDate" label="考试日期" width="120"/><el-table-column label="评价方式" width="100"><template #default="{ row }">{{ metricName(row.metricKind) }}</template></el-table-column><el-table-column prop="resultCount" label="已录入" width="90"/><el-table-column label="状态" width="120"><template #default="{ row }"><el-tag :type="row.status === 2 ? 'success' : 'info'">{{ row.status === 2 ? `已发布 ${row.publicationCount} 次` : '草稿' }}</el-tag></template></el-table-column></el-table><div v-if="batchCursors.length || batchHasMore" class="cursor-pager"><el-button size="small" :disabled="!batchCursors.length || loading" @click="previousBatchPage">上一批</el-button><span>第 {{ batchPage }} 批</span><el-button size="small" type="primary" :disabled="!batchHasMore || loading" @click="nextBatchPage">下一批</el-button></div></section>
<section v-if="selected" class="editor-panel"><div class="editor-heading"><div><span class="kicker">{{ selected.examCode }}</span><h2>{{ selected.name }}</h2><p>{{ selected.organizer || '未填写组织方' }} · {{ selected.examDate }} · {{ metricName(selected.metricKind) }}{{ selected.metricKind === 3 ? ` · 满分 ${selected.maxScore}` : '' }}</p></div><div class="editor-actions"><el-button :icon="Download" @click="downloadTemplate">下载导入模板</el-button><label class="upload-button"><Upload />批量导入<input type="file" accept=".xlsx,.xls" @change="importExcel" /></label><el-button :icon="Plus" @click="addResult">新增一行</el-button><el-button type="primary" :loading="saving" @click="saveResults">保存记录</el-button><el-button type="success" :icon="Promotion" @click="publish">发布</el-button></div></div><div class="editor-hint">学号输入完成后离开输入框,系统自动检索姓名、学院和班级;参加次数不需要填写,由系统按考试编码和考试日期自动计算。</div><el-table :data="results" class="score-table"><el-table-column label="学号" min-width="150" fixed><template #default="{ row }"><el-input v-model="row.studentNumber" placeholder="输入学号" @blur="lookupStudent(row)" /></template></el-table-column><el-table-column label="姓名" width="110"><template #default="{ row }"><span :class="{ 'unresolved': row.studentNumber && !row.studentName }">{{ row.studentName || '待检索' }}</span></template></el-table-column><el-table-column label="学院" min-width="150"><template #default="{ row }">{{ row.collegeName || '—' }}</template></el-table-column><el-table-column label="班级" min-width="150"><template #default="{ row }">{{ row.className || '—' }}</template></el-table-column><el-table-column v-if="selected.metricKind === 3" label="成绩" width="150"><template #default="{ row }"><el-input-number v-model="row.score" :min="0" :max="selected.maxScore" :precision="2" controls-position="right" placeholder="请输入分数" /></template></el-table-column><el-table-column v-if="selected.metricKind === 2" label="等级" width="150"><template #default="{ row }"><el-select v-model="row.level" placeholder="选择等级"><el-option v-for="level in (selected.levelOptions || '').split(',').filter(Boolean)" :key="level" :label="level" :value="level" /></el-select></template></el-table-column><el-table-column v-if="selected.metricKind === 1" label="考试结果" width="150"><template #default="{ row }"><el-select v-model="row.isPassed" placeholder="选择结果"><el-option label="合格" :value="true"/><el-option label="不合格" :value="false"/></el-select></template></el-table-column><el-table-column label="参加次数" width="100"><template #default="{ row }"><span class="auto-attempt">{{ row.attemptNumber || '自动' }}</span></template></el-table-column><el-table-column label="备注" min-width="180"><template #default="{ row }"><el-input v-model="row.notes" placeholder="可选" /></template></el-table-column></el-table><el-empty v-if="!results.length" description="还没有成绩记录,点击“新增一行”或使用批量导入" /></section>
<section class="batch-panel"><div class="panel-heading"><div><span class="kicker">考试场次</span><h2>考试场次</h2></div><span>点击场次进入成绩录入</span></div><div class="batch-filters"><el-input v-model="batchKeyword" clearable placeholder="考试编码、名称或组织方" @keyup.enter="load(true)" @clear="load(true)"/><el-select v-model="batchStatus" clearable placeholder="全部状态" @change="load(true)"><el-option label="草稿" :value="1"/><el-option label="已发布" :value="2"/></el-select><el-select v-model="batchMetricKind" clearable placeholder="全部方式" @change="load(true)"><el-option label="合格制" :value="1"/><el-option label="等级制" :value="2"/><el-option label="分数制" :value="3"/></el-select><el-date-picker v-model="batchDateRange" type="daterange" value-format="YYYY-MM-DD" start-placeholder="开始日期" end-placeholder="结束日期" @change="load(true)"/><el-button :icon="Search" @click="load(true)">查询</el-button><el-button :icon="Refresh" @click="() => load()">刷新</el-button></div><el-table :data="batches" v-loading="loading" @row-click="row => openBatch(row)"><el-table-column prop="examCode" label="考试编码" width="140"/><el-table-column prop="name" label="考试名称" min-width="190"/><el-table-column prop="examDate" label="考试日期" width="120"/><el-table-column label="评价方式" width="100"><template #default="{ row }">{{ metricName(row.metricKind) }}</template></el-table-column><el-table-column prop="resultCount" label="已录入" width="90"/><el-table-column label="状态" width="120"><template #default="{ row }"><el-tag :type="row.status === 2 ? 'success' : 'info'">{{ row.status === 2 ? `已发布 ${row.publicationCount} 次` : '草稿' }}</el-tag></template></el-table-column></el-table><div v-if="batchCursors.length || batchHasMore" class="cursor-pager"><el-button size="small" :disabled="!batchCursors.length || loading" @click="previousBatchPage">上一批</el-button><span>第 {{ batchPage }} 批</span><el-button size="small" type="primary" :disabled="!batchHasMore || loading" @click="nextBatchPage">下一批</el-button></div></section>
<section v-if="selected" class="editor-panel"><div class="editor-heading"><div><span class="kicker">{{ selected.examCode }}</span><h2>{{ selected.name }}</h2><p>{{ selected.organizer || '未填写组织方' }} · {{ selected.examDate }} · {{ metricName(selected.metricKind) }}{{ selected.metricKind === 3 ? ` · 满分 ${selected.maxScore}` : '' }}</p></div><div class="editor-actions"><el-button :icon="Download" @click="downloadTemplate">下载导入模板</el-button><label class="upload-button"><Upload />批量导入<input type="file" accept=".xlsx,.xls" @change="importExcel" /></label><el-button :icon="Plus" @click="addResult">新增一行</el-button><el-button type="primary" :loading="saving" @click="saveResults">保存记录</el-button><el-button type="success" :icon="Promotion" @click="publish">发布</el-button></div></div><div class="editor-hint">成绩按页加载;保存和导入按学号新增或更新,未出现在本页或导入文件中的原有记录会保留。学号输入完成后离开输入框,系统自动检索姓名、学院和班级;参加次数由系统自动计算。</div><el-table :data="results" class="score-table"><el-table-column label="学号" min-width="150" fixed><template #default="{ row }"><el-input v-model="row.studentNumber" placeholder="输入学号" @blur="lookupStudent(row)" /></template></el-table-column><el-table-column label="姓名" width="110"><template #default="{ row }"><span :class="{ 'unresolved': row.studentNumber && !row.studentName }">{{ row.studentName || '待检索' }}</span></template></el-table-column><el-table-column label="学院" min-width="150"><template #default="{ row }">{{ row.collegeName || '—' }}</template></el-table-column><el-table-column label="班级" min-width="150"><template #default="{ row }">{{ row.className || '—' }}</template></el-table-column><el-table-column v-if="selected.metricKind === 3" label="成绩" width="150"><template #default="{ row }"><el-input-number v-model="row.score" :min="0" :max="selected.maxScore" :precision="2" controls-position="right" placeholder="请输入分数" /></template></el-table-column><el-table-column v-if="selected.metricKind === 2" label="等级" width="150"><template #default="{ row }"><el-select v-model="row.level" placeholder="选择等级"><el-option v-for="level in (selected.levelOptions || '').split(',').filter(Boolean)" :key="level" :label="level" :value="level" /></el-select></template></el-table-column><el-table-column v-if="selected.metricKind === 1" label="考试结果" width="150"><template #default="{ row }"><el-select v-model="row.isPassed" placeholder="选择结果"><el-option label="合格" :value="true"/><el-option label="不合格" :value="false"/></el-select></template></el-table-column><el-table-column label="参加次数" width="100"><template #default="{ row }"><span class="auto-attempt">{{ row.attemptNumber || '自动' }}</span></template></el-table-column><el-table-column label="备注" min-width="180"><template #default="{ row }"><el-input v-model="row.notes" placeholder="可选" /></template></el-table-column></el-table><div v-if="resultCursors.length || resultHasMore" class="cursor-pager"><span>共 {{ resultTotal }} 条</span><el-button size="small" :disabled="!resultCursors.length || loading" @click="previousResultPage">上一批</el-button><span>第 {{ resultPage }} 批</span><el-button size="small" type="primary" :disabled="!resultHasMore || loading" @click="nextResultPage">下一批</el-button></div><el-empty v-if="!results.length" description="还没有成绩记录,点击“新增一行”或使用批量导入" /></section>
</template>
<el-dialog v-model="dialog" title="新建其他考试场次" width="600px"><el-form label-width="100px"><el-form-item label="考试编码" required><el-input v-model="form.examCode" placeholder="如 CET4、IELTS、计算机二级;同一考试始终使用相同编码" /></el-form-item><el-form-item label="考试名称" required><el-input v-model="form.name" placeholder="如 大学英语四级" /></el-form-item><el-form-item label="组织方"><el-input v-model="form.organizer" /></el-form-item><el-form-item label="考试日期"><el-date-picker v-model="form.examDate" type="date" value-format="YYYY-MM-DD" /></el-form-item><el-form-item label="评价方式"><el-select v-model="form.metricKind"><el-option label="分数制" :value="3"/><el-option label="等级制" :value="2"/><el-option label="合格/不合格" :value="1"/></el-select></el-form-item><el-form-item v-if="form.metricKind === 3" label="满分"><el-input-number v-model="form.maxScore" :min="1" /></el-form-item><el-form-item v-if="form.metricKind === 2" label="等级顺序"><el-input v-model="form.levelOptions" placeholder="按最优到最差填写,如 A+,A,B,C,D" /></el-form-item></el-form><template #footer><el-button @click="dialog = false">取消</el-button><el-button type="primary" @click="createBatch">建立场次</el-button></template></el-dialog>
<el-dialog v-model="publishDialog" title="发布其他考试成绩" width="460px" :close-on-click-modal="false"><div class="publish-confirm"><strong>确认发布“{{ selected?.name }}”成绩?</strong><p>发布后学生可以查看本批次成绩;如需更正,可以修改后再次发布。</p></div><template #footer><el-button @click="publishDialog = false">取消</el-button><el-button type="success" :loading="saving" @click="confirmPublish">确认发布</el-button></template></el-dialog>