feat: add filters to exam sign-in exports

This commit is contained in:
biss committed 2026-10-26 11:58:50 +08:00
1 parent 8ff0951255
commit 58c1a6c425
10 files changed
+11455 -7

No files matched your search

@@ -1156,17 +1156,34 @@ public sealed class ExamsController(
[Authorize(Roles = Managers)]
public async Task<ActionResult> StartSignInExport(
Guid id,
ExamSignInExportRequest? request,
CancellationToken cancellationToken)
{
request ??= new ExamSignInExportRequest();
if (request.StartDate.HasValue && request.EndDate.HasValue &&
request.StartDate > request.EndDate)
{
return ValidationProblem("开始日期不能晚于结束日期。");
}
var planExists = await db.ExamPlans.AsNoTracking()
.AnyAsync(x => x.Id == id, cancellationToken);
if (!planExists) return NotFound();
var userId = currentUserDataScope.Current.UserId;
var filter = new ExamSignInExportFilter(
request.StartDate,
request.EndDate,
request.BuildingId,
request.ClassroomId,
Normalize(request.Keyword));
var job = new ExamSignInExportJob
{
PlanId = id,
RequestedByUserId = userId == Guid.Empty ? null : userId,
FilterJson = filter.HasConditions
? System.Text.Json.JsonSerializer.Serialize(filter)
: null,
CurrentStep = "等待后台生成"
};
db.ExamSignInExportJobs.Add(job);
@@ -1774,6 +1791,13 @@ public sealed record ExamAutoArrangeRequest(
bool AssignClassrooms = true,
bool AssignInvigilators = true);
public sealed record ExamSignInExportRequest(
DateOnly? StartDate = null,
DateOnly? EndDate = null,
Guid? BuildingId = null,
Guid? ClassroomId = null,
[MaxLength(100)] string? Keyword = null);
public sealed record ExamScheduleResponse(
Guid Id,
string PlanName,
@@ -8,6 +8,7 @@ public sealed class ExamSignInExportJob : EntityBase
public ExamSignInExportJobStatus Status { get; set; } =
ExamSignInExportJobStatus.Queued;
public Guid? RequestedByUserId { get; set; }
public string? FilterJson { get; set; }
public string? FileName { get; set; }
public byte[]? FileBytes { get; set; }
public int FileSize { get; set; }
@@ -3,6 +3,7 @@ using Jiaowu.Api.Infrastructure.Persistence;
using Jiaowu.Api.Infrastructure.Files;
using Jiaowu.Api.Infrastructure.Teaching;
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
namespace Jiaowu.Api.Infrastructure.Exams;
@@ -44,7 +45,9 @@ public sealed class ExamSignInExportJobProcessor(
return;
}
var rooms = await db.ExamRooms.AsNoTracking()
var filter = ExamSignInExportFilter.Parse(job.FilterJson);
var roomQuery = db.ExamRooms.AsNoTracking()
.Include(x => x.Course)
.Include(x => x.Classroom)
.ThenInclude(x => x!.Building)
@@ -56,7 +59,29 @@ public sealed class ExamSignInExportJobProcessor(
.Include(x => x.Seats)
.ThenInclude(x => x.Student)
.ThenInclude(x => x!.AdministrativeClass)
.Where(x => x.ExamPlanId == job.PlanId)
.Where(x => x.ExamPlanId == job.PlanId);
if (filter.StartDate.HasValue)
roomQuery = roomQuery.Where(x => x.ExamDate >= filter.StartDate.Value);
if (filter.EndDate.HasValue)
roomQuery = roomQuery.Where(x => x.ExamDate <= filter.EndDate.Value);
if (filter.BuildingId.HasValue)
roomQuery = roomQuery.Where(
x => x.Classroom!.BuildingId == filter.BuildingId.Value);
if (filter.ClassroomId.HasValue)
roomQuery = roomQuery.Where(
x => x.ClassroomId == filter.ClassroomId.Value);
if (filter.Keyword is not null)
{
roomQuery = roomQuery.Where(x =>
x.Course!.Code.Contains(filter.Keyword) ||
x.Course.Name.Contains(filter.Keyword) ||
x.SessionLinks.Any(link =>
link.ExamSession!.TeachingTask!.TaskNumber.Contains(filter.Keyword) ||
link.ExamSession.TeachingTask.Course!.Code.Contains(filter.Keyword) ||
link.ExamSession.TeachingTask.Course.Name.Contains(filter.Keyword)));
}
var rooms = await roomQuery
.OrderBy(x => x.ExamDate)
.ThenBy(x => x.StartPeriod)
.ThenBy(x => x.Classroom!.Building!.Name)
@@ -64,9 +89,31 @@ public sealed class ExamSignInExportJobProcessor(
.AsSplitQuery()
.ToListAsync(stoppingToken);
var legacySessions = await db.ExamSessions.AsNoTracking()
var legacySessionQuery = db.ExamSessions.AsNoTracking()
.Where(x => x.ExamPlanId == job.PlanId)
.Where(x => !x.RoomLinks.Any())
.Where(x => !x.RoomLinks.Any());
if (filter.StartDate.HasValue)
legacySessionQuery = legacySessionQuery.Where(
x => x.ExamDate >= filter.StartDate.Value);
if (filter.EndDate.HasValue)
legacySessionQuery = legacySessionQuery.Where(
x => x.ExamDate <= filter.EndDate.Value);
if (filter.BuildingId.HasValue)
legacySessionQuery = legacySessionQuery.Where(
x => x.Classroom != null &&
x.Classroom.BuildingId == filter.BuildingId.Value);
if (filter.ClassroomId.HasValue)
legacySessionQuery = legacySessionQuery.Where(
x => x.ClassroomId == filter.ClassroomId.Value);
if (filter.Keyword is not null)
{
legacySessionQuery = legacySessionQuery.Where(x =>
x.TeachingTask!.TaskNumber.Contains(filter.Keyword) ||
x.TeachingTask.Course!.Code.Contains(filter.Keyword) ||
x.TeachingTask.Course.Name.Contains(filter.Keyword));
}
var legacySessions = await legacySessionQuery
.OrderBy(x => x.ExamDate)
.ThenBy(x => x.StartPeriod)
.Select(session => new
@@ -93,7 +140,11 @@ public sealed class ExamSignInExportJobProcessor(
if (rooms.Count == 0 && legacySessions.Count == 0)
{
await MarkFailedAsync(jobId, "当前考试计划没有可导出的考试场次。");
await MarkFailedAsync(
jobId,
filter.HasConditions
? "没有符合筛选条件的考试场次。"
: "当前考试计划没有可导出的考试场次。");
return;
}
@@ -211,3 +262,30 @@ public sealed class ExamSignInExportJobProcessor(
return normalized.Length > 100 ? normalized[..100] : normalized;
}
}
public sealed record ExamSignInExportFilter(
DateOnly? StartDate,
DateOnly? EndDate,
Guid? BuildingId,
Guid? ClassroomId,
string? Keyword)
{
public bool HasConditions =>
StartDate.HasValue || EndDate.HasValue || BuildingId.HasValue ||
ClassroomId.HasValue || Keyword is not null;
public static ExamSignInExportFilter Parse(string? json)
{
if (string.IsNullOrWhiteSpace(json))
return new(null, null, null, null, null);
var filter = JsonSerializer.Deserialize<ExamSignInExportFilter>(json)
?? new(null, null, null, null, null);
return filter with
{
Keyword = string.IsNullOrWhiteSpace(filter.Keyword)
? null
: filter.Keyword.Trim()
};
}
}
Loaded 3 of 10 files, more files were not shown because too many files have changed in this diff. Show more