队列异步执行,拆分查询消除笛卡尔积。
修改的文件(共 13 个)
┌───────────────────────────────────────────────────────────────┬──────────────────────────────────────────────────┐
│ 文件 │ 变更 │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ Domain/Academic/ExamEntities.cs │ 新增 ExamPublishJob 实体 + ExamPublishJobStatus │
│ │ / ExamPublishJobKind 枚举 │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ Domain/System/BackgroundJobOutboxMessage.cs │ BackgroundJobKind 新增 ExamPublish = 6 │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ Infrastructure/BackgroundJobs/BackgroundJobOptions.cs │ 新增 ExamPublishConcurrency 配置项 │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ Infrastructure/BackgroundJobs/BackgroundJobRunner.cs │ RunAsync 和 MarkJobRetryLimitExceeded 添加 │
│ │ ExamPublish 分支 │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ Infrastructure/BackgroundJobs/RabbitMqBackgroundJobs.cs │ JobKinds 数组和 RoutingKey 添加 ExamPublish → │
│ │ "exam.publish" │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ Infrastructure/BackgroundJobs/BackgroundJobOutboxPublisher.cs │ 启动恢复逻辑添加 ExamPublishJobs │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ Infrastructure/Persistence/AppDbContext.cs │ 新增 ExamPublishJobs DbSet │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ Controllers/OperationsController.cs │ CountFailedJobsAsync / GetFailedJobs │
│ │ 添加考试发布失败统计和筛选 │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ Program.cs │ 校验 ExamPublishConcurrency + 注册 │
│ │ ExamPublishJobProcessor │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ Infrastructure/Exams/ExamPublishJobs.cs │ 新文件 — │
│ │ ExamPublishJobProcessor,拆分查询校验后发布 │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ Controllers/ExamsController.cs │ Publish 改为创建后台任务 + 202 返回;新增 GET │
│ │ publish-jobs/{id} / GET plans/{id}/publish-job │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ Controllers/MakeupExamsController.cs │ 同上改造 │
├───────────────────────────────────────────────────────────────┼──────────────────────────────────────────────────┤
│ tests/.../TeachingWorkflowRosterTests.cs │ 更新测试适配新的异步发布模式 │
└───────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────┘
笛卡尔积消除
之前:一个 Include 链拉全部 → EF Core 生成 Sessions × Invigilators × RoomLinks × Seats 笛卡尔积
之后:
- 场次计数:db.ExamSessions.CountAsync(无 JOIN)
- 场次摘要:Select new { Id, ClassroomId, InvigilatorCount, RoomLinkCount }(只查所需列)
- 容量超限:db.ExamRooms.Select(r => new { SeatCount = r.Seats.Count, Capacity })(单表 JOIN)
- 课程冲突:db.ExamRoomSessions.Where(link => ...CourseId != link.ExamRoom!.CourseId)(独立查询)
- 每个查询只做自己需要的 JOIN,互不干扰
测试结果
213 通过,0 失败,0 跳过
配置方式
- BackgroundJobs__Transport=RabbitMq → 走 RabbitMQ 队列 jiaowu.background-jobs.exam.publish
- BackgroundJobs__Transport=InMemory(默认) → 走内存 Channel
- BackgroundJobs__ExamPublishConcurrency=1(默认,可调 1-16)
363 lines
13 KiB
C#
363 lines
13 KiB
C#
using ClosedXML.Excel;
|
|
using Jiaowu.Api.Controllers;
|
|
using Jiaowu.Api.Domain.Academic;
|
|
using Jiaowu.Api.Domain.Identity;
|
|
using Jiaowu.Api.Infrastructure.Auth;
|
|
using Jiaowu.Api.Infrastructure.Caching;
|
|
using Jiaowu.Api.Infrastructure.Exams;
|
|
using Jiaowu.Api.Infrastructure.Persistence;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Data.Sqlite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
|
|
namespace Jiaowu.Api.Tests;
|
|
|
|
public sealed class TeachingWorkflowRosterTests
|
|
{
|
|
[Fact]
|
|
public async Task ClassAssignedStudents_AppearAcrossTeachingAndExamWorkflows()
|
|
{
|
|
await using var connection = new SqliteConnection("Data Source=:memory:");
|
|
await connection.OpenAsync();
|
|
var options = new DbContextOptionsBuilder<AppDbContext>()
|
|
.UseSqlite(connection)
|
|
.Options;
|
|
await using var db = new AppDbContext(options);
|
|
await db.Database.EnsureCreatedAsync();
|
|
|
|
var teacherUserId = Guid.NewGuid();
|
|
var studentUserId = Guid.NewGuid();
|
|
var teacherUser = new ApplicationUser
|
|
{
|
|
Id = teacherUserId,
|
|
UserName = "T001",
|
|
NormalizedUserName = "T001",
|
|
DisplayName = "张老师"
|
|
};
|
|
var studentUser = new ApplicationUser
|
|
{
|
|
Id = studentUserId,
|
|
UserName = "202601001",
|
|
NormalizedUserName = "202601001",
|
|
DisplayName = "周同学"
|
|
};
|
|
var campus = new Campus { Code = "MAIN", Name = "主校区" };
|
|
var college = new College
|
|
{
|
|
Code = "CS",
|
|
Name = "计算机学院",
|
|
CampusId = campus.Id
|
|
};
|
|
var building = new Building
|
|
{
|
|
Code = "B01",
|
|
Name = "第一教学楼",
|
|
CampusId = campus.Id
|
|
};
|
|
var classroom = new Classroom
|
|
{
|
|
Code = "B01-101",
|
|
Name = "101",
|
|
BuildingId = building.Id,
|
|
Capacity = 60
|
|
};
|
|
var major = new Major
|
|
{
|
|
Code = "080901",
|
|
Name = "计算机科学与技术",
|
|
CollegeId = college.Id,
|
|
DegreeType = "工学学士"
|
|
};
|
|
var administrativeClass = new AdministrativeClass
|
|
{
|
|
Code = "CS2026-01",
|
|
Name = "计科 2026-1 班",
|
|
MajorId = major.Id,
|
|
Grade = 2026
|
|
};
|
|
var student = new Student
|
|
{
|
|
StudentNumber = "202601001",
|
|
Name = "周同学",
|
|
AdministrativeClassId = administrativeClass.Id,
|
|
EnrollmentYear = 2026,
|
|
EnrollmentDate = new DateOnly(2026, 9, 1),
|
|
UserId = studentUserId
|
|
};
|
|
var teacher = new Teacher
|
|
{
|
|
TeacherNumber = "T001",
|
|
Name = "张老师",
|
|
CollegeId = college.Id,
|
|
UserId = teacherUserId
|
|
};
|
|
var course = new Course
|
|
{
|
|
Code = "CS101",
|
|
Name = "程序设计基础",
|
|
CollegeId = college.Id,
|
|
Credits = 4,
|
|
TotalHours = 64,
|
|
LectureHours = 48,
|
|
PracticeHours = 16,
|
|
Nature = CourseNature.MajorRequired,
|
|
AssessmentMethod = AssessmentMethod.Examination
|
|
};
|
|
var term = new AcademicTerm
|
|
{
|
|
Code = "2026-1",
|
|
Name = "2026—2027 学年第一学期",
|
|
AcademicYear = "2026-2027",
|
|
Season = TermSeason.Autumn,
|
|
StartDate = new DateOnly(2026, 9, 1),
|
|
EndDate = new DateOnly(2027, 1, 20)
|
|
};
|
|
var task = new TeachingTask
|
|
{
|
|
TaskNumber = "2026-1-CS101-01",
|
|
Name = "程序设计基础教学班",
|
|
AcademicTermId = term.Id,
|
|
CourseId = course.Id,
|
|
Capacity = 60,
|
|
Status = TeachingTaskStatus.Published,
|
|
Teachers =
|
|
[
|
|
new TeachingTaskTeacher
|
|
{
|
|
TeacherId = teacher.Id,
|
|
IsPrimary = true
|
|
}
|
|
],
|
|
Classes =
|
|
[
|
|
new TeachingTaskClass
|
|
{
|
|
AdministrativeClassId = administrativeClass.Id
|
|
}
|
|
]
|
|
};
|
|
db.AddRange(
|
|
teacherUser,
|
|
studentUser,
|
|
campus,
|
|
college,
|
|
building,
|
|
classroom,
|
|
major,
|
|
administrativeClass,
|
|
student,
|
|
teacher,
|
|
course,
|
|
term,
|
|
task);
|
|
await db.SaveChangesAsync();
|
|
|
|
var examSession = new ExamSession
|
|
{
|
|
TeachingTaskId = task.Id,
|
|
ExamDate = new DateOnly(2027, 1, 8),
|
|
StartPeriod = 1,
|
|
PeriodCount = 2,
|
|
StartsAt = new DateTime(2027, 1, 8, 8, 0, 0, DateTimeKind.Utc),
|
|
EndsAt = new DateTime(2027, 1, 8, 9, 50, 0, DateTimeKind.Utc),
|
|
RequiredInvigilatorCount = 1
|
|
};
|
|
var examPlan = new ExamPlan
|
|
{
|
|
AcademicTermId = term.Id,
|
|
Name = "期末考试",
|
|
Status = ExamPlanStatus.Draft,
|
|
Sessions = [examSession],
|
|
Rooms =
|
|
[
|
|
new ExamRoomAssignment
|
|
{
|
|
CourseId = course.Id,
|
|
ClassroomId = classroom.Id,
|
|
ExamDate = new DateOnly(2027, 1, 8),
|
|
StartPeriod = 1,
|
|
PeriodCount = 2,
|
|
StartsAt = new DateTime(2027, 1, 8, 8, 0, 0, DateTimeKind.Utc),
|
|
EndsAt = new DateTime(2027, 1, 8, 9, 50, 0, DateTimeKind.Utc),
|
|
RequiredInvigilatorCount = 1,
|
|
SessionLinks =
|
|
[
|
|
new ExamRoomSession
|
|
{
|
|
ExamSessionId = examSession.Id
|
|
}
|
|
],
|
|
Seats =
|
|
[
|
|
new ExamSeatAssignment
|
|
{
|
|
ExamSessionId = examSession.Id,
|
|
StudentId = student.Id,
|
|
SeatNumber = 1
|
|
}
|
|
],
|
|
Invigilators =
|
|
[
|
|
new ExamRoomInvigilator
|
|
{
|
|
TeacherId = teacher.Id
|
|
}
|
|
]
|
|
}
|
|
]
|
|
};
|
|
db.ExamPlans.Add(examPlan);
|
|
await db.SaveChangesAsync();
|
|
|
|
var scope = new TeacherDataScope(teacherUserId);
|
|
var attendance = new AttendanceController(db, scope);
|
|
var attendanceResult = await attendance.CreateSheet(
|
|
new AttendanceSheetRequest(
|
|
task.Id,
|
|
"第1周点名",
|
|
new DateTime(2026, 9, 3),
|
|
null,
|
|
AttendanceCheckInMethod.Manual,
|
|
null,
|
|
null,
|
|
null,
|
|
null),
|
|
CancellationToken.None);
|
|
Assert.IsType<CreatedResult>(attendanceResult);
|
|
Assert.Equal(1, await db.AttendanceRecords.CountAsync());
|
|
|
|
var grades = new GradesController(db, scope);
|
|
var gradeResult = await grades.CreateSheet(
|
|
new GradeSheetRequest(task.Id, 40, 60, []),
|
|
CancellationToken.None);
|
|
Assert.IsType<CreatedResult>(gradeResult);
|
|
Assert.Equal(1, await db.GradeRecords.CountAsync());
|
|
|
|
var selections = new CourseSelectionsController(db, scope);
|
|
Assert.Single(ReadItems(await selections.GetMyTeachingTasks(
|
|
term.Id,
|
|
CancellationToken.None)));
|
|
var roster = Assert.IsType<OkObjectResult>(
|
|
await selections.GetTeachingTaskRoster(task.Id, CancellationToken.None));
|
|
Assert.Equal(1, ReadIntProperty(roster.Value!, "StudentCount"));
|
|
|
|
var adjustments = new CourseAdjustmentsController(db, scope);
|
|
Assert.Single(ReadItems(await adjustments.GetTaskOptions(
|
|
term.Id,
|
|
CancellationToken.None)));
|
|
|
|
var managerScope = new ManagerDataScope();
|
|
var exams = new ExamsController(
|
|
db,
|
|
managerScope,
|
|
NoOpAppCache.Instance);
|
|
var publishResult = await exams.Publish(examPlan.Id, CancellationToken.None);
|
|
var acceptedPublish = Assert.IsType<AcceptedAtActionResult>(publishResult);
|
|
var publishJobId = (Guid)acceptedPublish.Value!
|
|
.GetType().GetProperty("jobId")!.GetValue(acceptedPublish.Value)!;
|
|
var publishProcessor = new ExamPublishJobProcessor(
|
|
db,
|
|
NoOpAppCache.Instance,
|
|
NullLogger<ExamPublishJobProcessor>.Instance);
|
|
await publishProcessor.ProcessAsync(publishJobId, CancellationToken.None);
|
|
|
|
var planResult = Assert.IsType<OkObjectResult>(
|
|
await exams.GetPlan(
|
|
examPlan.Id,
|
|
cancellationToken: CancellationToken.None));
|
|
var planSessions = ReadEnumerableProperty(planResult.Value!, "Sessions");
|
|
Assert.Equal(1, ReadIntProperty(planSessions.Single(), "StudentCount"));
|
|
var returnedRoom = Assert.Single(
|
|
ReadEnumerableProperty(planSessions.Single(), "ExamRooms"));
|
|
Assert.Equal(
|
|
classroom.Id,
|
|
returnedRoom.GetType().GetProperty("ClassroomId")!.GetValue(returnedRoom));
|
|
Assert.Equal(1, ReadIntProperty(returnedRoom, "SeatCount"));
|
|
Assert.Equal(1, ReadIntProperty(returnedRoom, "TotalSeatCount"));
|
|
Assert.False(
|
|
(bool)returnedRoom.GetType().GetProperty("IsMixed")!
|
|
.GetValue(returnedRoom)!);
|
|
Assert.Equal(
|
|
[teacher.Id],
|
|
ReadEnumerableProperty(returnedRoom, "InvigilatorIds").Cast<Guid>());
|
|
Assert.Equal(
|
|
[teacher.Name],
|
|
ReadEnumerableProperty(returnedRoom, "InvigilatorNames").Cast<string>());
|
|
|
|
var examSessionId = examPlan.Sessions.Single().Id;
|
|
var rosterResult = Assert.IsType<OkObjectResult>(
|
|
await exams.GetRoster(examSessionId, CancellationToken.None));
|
|
Assert.Single(ReadEnumerableProperty(rosterResult.Value!, "Students"));
|
|
|
|
var studentExams = new ExamsController(
|
|
db,
|
|
new StudentDataScope(studentUserId),
|
|
NoOpAppCache.Instance);
|
|
Assert.Single(ReadItems(
|
|
await studentExams.GetMySchedule(CancellationToken.None)));
|
|
var teacherExams = new ExamsController(
|
|
db,
|
|
scope,
|
|
NoOpAppCache.Instance);
|
|
Assert.Single(ReadItems(
|
|
await teacherExams.GetMySchedule(CancellationToken.None)));
|
|
|
|
var exportResult = Assert.IsType<FileContentResult>(
|
|
await exams.ExportSignInSheets(examPlan.Id, CancellationToken.None));
|
|
using var workbookStream = new MemoryStream(exportResult.FileContents);
|
|
using var workbook = new XLWorkbook(workbookStream);
|
|
Assert.Equal(2, workbook.Worksheets.Count);
|
|
var signInSheet = workbook.Worksheets.Single(x => x.Name != "考场汇总");
|
|
Assert.Equal("202601001", signInSheet.Cell("C8").GetString());
|
|
Assert.Equal("周同学", signInSheet.Cell("D8").GetString());
|
|
}
|
|
|
|
private static List<object> ReadItems(ActionResult result)
|
|
{
|
|
var ok = Assert.IsType<OkObjectResult>(result);
|
|
return Assert.IsAssignableFrom<System.Collections.IEnumerable>(ok.Value)
|
|
.Cast<object>()
|
|
.ToList();
|
|
}
|
|
|
|
private static int ReadIntProperty(object value, string name) =>
|
|
(int)value.GetType().GetProperty(name)!.GetValue(value)!;
|
|
|
|
private static List<object> ReadEnumerableProperty(object value, string name) =>
|
|
Assert.IsAssignableFrom<System.Collections.IEnumerable>(
|
|
value.GetType().GetProperty(name)!.GetValue(value))
|
|
.Cast<object>()
|
|
.ToList();
|
|
|
|
private sealed class TeacherDataScope(Guid userId) : ICurrentUserDataScope
|
|
{
|
|
public CurrentUserScope Current { get; } = new(
|
|
userId,
|
|
"张老师",
|
|
null,
|
|
DataScope.Self,
|
|
new HashSet<string>([SystemRoles.Teacher]));
|
|
}
|
|
|
|
private sealed class StudentDataScope(Guid userId) : ICurrentUserDataScope
|
|
{
|
|
public CurrentUserScope Current { get; } = new(
|
|
userId,
|
|
"周同学",
|
|
null,
|
|
DataScope.Self,
|
|
new HashSet<string>([SystemRoles.Student]));
|
|
}
|
|
|
|
private sealed class ManagerDataScope : ICurrentUserDataScope
|
|
{
|
|
public CurrentUserScope Current { get; } = new(
|
|
Guid.NewGuid(),
|
|
"考试管理员",
|
|
null,
|
|
DataScope.All,
|
|
new HashSet<string>([SystemRoles.AcademicAdmin]));
|
|
}
|
|
}
|