自选项目实验优化

This commit is contained in:
2026-08-11 09:29:44 +08:00 Unverified
parent 9b32ee916c
commit e69ab6f580
11 changed files with 7578 additions and 67 deletions
@@ -263,6 +263,7 @@ public sealed class ExamPublishJobProcessor(
var projects = await db.ExperimentProjects
.Include(x => x.Sessions)
.ThenInclude(x => x.Instructors)
.Include(x => x.TeachingTask)
.ThenInclude(x => x!.Course)
.Where(x => ids.Contains(x.Id))
@@ -282,6 +283,14 @@ public sealed class ExamPublishJobProcessor(
if (project.Sessions.Any(x => x.Status == ExperimentSessionStatus.Scheduled &&
(x.SessionDate < project.StartDate || x.SessionDate > project.EndDate)))
throw new ExamPublishValidationException($"“{project.Name}”存在不在开放日期范围内的实验场次。");
if (project.ArrangementMode == ExperimentArrangementMode.SelfScheduled &&
(project.SelectionStartsAt is null || project.SelectionEndsAt is null ||
project.SelectionStartsAt >= project.SelectionEndsAt))
throw new ExamPublishValidationException($"“{project.Name}”未设置有效的选课时间范围。");
if (project.ArrangementMode == ExperimentArrangementMode.SelfScheduled &&
project.Sessions.Any(x => x.Status == ExperimentSessionStatus.Scheduled &&
x.Instructors.Count == 0))
throw new ExamPublishValidationException($"“{project.Name}”存在未指定指导老师的实验场次。");
}
job.CurrentStep = "正在发布实验项目";
@@ -51,6 +51,8 @@ public sealed class AppDbContext(DbContextOptions<AppDbContext> options)
Set<ClassroomReservation>();
public DbSet<ExperimentProject> ExperimentProjects => Set<ExperimentProject>();
public DbSet<ExperimentSession> ExperimentSessions => Set<ExperimentSession>();
public DbSet<ExperimentSessionInstructor> ExperimentSessionInstructors =>
Set<ExperimentSessionInstructor>();
public DbSet<ExperimentBooking> ExperimentBookings => Set<ExperimentBooking>();
public DbSet<ExperimentGradeSheet> ExperimentGradeSheets =>
Set<ExperimentGradeSheet>();
@@ -690,6 +692,8 @@ public sealed class AppDbContext(DbContextOptions<AppDbContext> options)
entity.Property(x => x.Name).HasMaxLength(120);
entity.Property(x => x.Description).HasMaxLength(1000);
entity.Property(x => x.Requirements).HasMaxLength(1000);
entity.Property(x => x.SelectionStartsAt).HasConversion<UtcDateTimeConverter>();
entity.Property(x => x.SelectionEndsAt).HasConversion<UtcDateTimeConverter>();
// 集中安排会为同一教学任务的每一条实验课表记录生成项目;
// 自行安排仍由控制器保持“教学任务 + 编码”唯一。
entity.HasIndex(x => new
@@ -805,6 +809,19 @@ public sealed class AppDbContext(DbContextOptions<AppDbContext> options)
.OnDelete(DeleteBehavior.Restrict);
});
builder.Entity<ExperimentSessionInstructor>(entity =>
{
entity.HasIndex(x => new { x.ExperimentSessionId, x.TeacherId })
.IsUnique();
entity.HasIndex(x => x.TeacherId);
entity.HasOne(x => x.ExperimentSession).WithMany(x => x.Instructors)
.HasForeignKey(x => x.ExperimentSessionId)
.OnDelete(DeleteBehavior.Cascade);
entity.HasOne(x => x.Teacher).WithMany()
.HasForeignKey(x => x.TeacherId)
.OnDelete(DeleteBehavior.Restrict);
});
builder.Entity<ExperimentCourseGrade>(entity =>
{
entity.Property(x => x.WeightedAverageScore).HasPrecision(5, 1);
@@ -104,6 +104,8 @@ public sealed class DevelopmentSqliteMigrator(
"20260809_54_experiment_course_grades";
private const string NotificationInboxIndexesMigration =
"20260810_55_notification_inbox_indexes";
private const string SelfScheduledExperimentSelectionMigration =
"20260811_56_self_scheduled_experiment_selection";
public async Task MigrateAsync(CancellationToken cancellationToken = default)
{
@@ -737,6 +739,10 @@ public sealed class DevelopmentSqliteMigrator(
NotificationInboxIndexesMigration,
NotificationInboxIndexesStatements,
cancellationToken);
await ApplyMigrationAsync(
SelfScheduledExperimentSelectionMigration,
SelfScheduledExperimentSelectionStatements,
cancellationToken);
}
private async Task ApplyMigrationAsync(
@@ -3129,6 +3135,27 @@ public sealed class DevelopmentSqliteMigrator(
"""
];
private static readonly string[] SelfScheduledExperimentSelectionStatements =
[
"ALTER TABLE \"ExperimentProjects\" ADD COLUMN \"SelectionStartsAt\" TEXT NULL;",
"ALTER TABLE \"ExperimentProjects\" ADD COLUMN \"SelectionEndsAt\" TEXT NULL;",
"""
CREATE TABLE "ExperimentSessionInstructors" (
"Id" TEXT NOT NULL CONSTRAINT "PK_ExperimentSessionInstructors" PRIMARY KEY,
"ExperimentSessionId" TEXT NOT NULL,
"TeacherId" TEXT NOT NULL,
"CreatedAt" TEXT NOT NULL,
"UpdatedAt" TEXT NOT NULL,
CONSTRAINT "FK_ExperimentSessionInstructors_ExperimentSessions_ExperimentSessionId"
FOREIGN KEY ("ExperimentSessionId") REFERENCES "ExperimentSessions" ("Id") ON DELETE CASCADE,
CONSTRAINT "FK_ExperimentSessionInstructors_Teachers_TeacherId"
FOREIGN KEY ("TeacherId") REFERENCES "Teachers" ("Id") ON DELETE RESTRICT
);
""",
"CREATE UNIQUE INDEX \"IX_ExperimentSessionInstructors_ExperimentSessionId_TeacherId\" ON \"ExperimentSessionInstructors\" (\"ExperimentSessionId\", \"TeacherId\");",
"CREATE INDEX \"IX_ExperimentSessionInstructors_TeacherId\" ON \"ExperimentSessionInstructors\" (\"TeacherId\");"
];
private static readonly string[] ReusableCourseGroupsStatements =
[
"""
@@ -0,0 +1,81 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
{
/// <inheritdoc />
public partial class AddSelfScheduledExperimentSelectionWindowsAndInstructors : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "SelectionEndsAt",
table: "ExperimentProjects",
type: "datetime(6)",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "SelectionStartsAt",
table: "ExperimentProjects",
type: "datetime(6)",
nullable: true);
migrationBuilder.CreateTable(
name: "ExperimentSessionInstructors",
columns: table => new
{
Id = table.Column<Guid>(type: "char(36)", nullable: false),
ExperimentSessionId = table.Column<Guid>(type: "char(36)", nullable: false),
TeacherId = table.Column<Guid>(type: "char(36)", nullable: false),
CreatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false),
UpdatedAt = table.Column<DateTime>(type: "datetime(6)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ExperimentSessionInstructors", x => x.Id);
table.ForeignKey(
name: "FK_ExperimentSessionInstructors_ExperimentSessions_ExperimentSe~",
column: x => x.ExperimentSessionId,
principalTable: "ExperimentSessions",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ExperimentSessionInstructors_Teachers_TeacherId",
column: x => x.TeacherId,
principalTable: "Teachers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
})
.Annotation("MySQL:Charset", "utf8mb4");
migrationBuilder.CreateIndex(
name: "IX_ExperimentSessionInstructors_ExperimentSessionId_TeacherId",
table: "ExperimentSessionInstructors",
columns: new[] { "ExperimentSessionId", "TeacherId" },
unique: true);
migrationBuilder.CreateIndex(
name: "IX_ExperimentSessionInstructors_TeacherId",
table: "ExperimentSessionInstructors",
column: "TeacherId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ExperimentSessionInstructors");
migrationBuilder.DropColumn(
name: "SelectionEndsAt",
table: "ExperimentProjects");
migrationBuilder.DropColumn(
name: "SelectionStartsAt",
table: "ExperimentProjects");
}
}
}
@@ -2520,6 +2520,12 @@ namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
b.Property<int?>("ScheduleWeek")
.HasColumnType("int");
b.Property<DateTime?>("SelectionEndsAt")
.HasColumnType("datetime(6)");
b.Property<DateTime?>("SelectionStartsAt")
.HasColumnType("datetime(6)");
b.Property<DateTime>("StartDate")
.HasColumnType("date");
@@ -2596,6 +2602,34 @@ namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
b.ToTable("ExperimentSessions");
});
modelBuilder.Entity("Jiaowu.Api.Domain.Academic.ExperimentSessionInstructor", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("char(36)");
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime(6)");
b.Property<Guid>("ExperimentSessionId")
.HasColumnType("char(36)");
b.Property<Guid>("TeacherId")
.HasColumnType("char(36)");
b.Property<DateTime>("UpdatedAt")
.HasColumnType("datetime(6)");
b.HasKey("Id");
b.HasIndex("TeacherId");
b.HasIndex("ExperimentSessionId", "TeacherId")
.IsUnique();
b.ToTable("ExperimentSessionInstructors");
});
modelBuilder.Entity("Jiaowu.Api.Domain.Academic.GradeItem", b =>
{
b.Property<Guid>("Id")
@@ -5970,6 +6004,25 @@ namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
b.Navigation("ExperimentProject");
});
modelBuilder.Entity("Jiaowu.Api.Domain.Academic.ExperimentSessionInstructor", b =>
{
b.HasOne("Jiaowu.Api.Domain.Academic.ExperimentSession", "ExperimentSession")
.WithMany("Instructors")
.HasForeignKey("ExperimentSessionId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Jiaowu.Api.Domain.Academic.Teacher", "Teacher")
.WithMany()
.HasForeignKey("TeacherId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("ExperimentSession");
b.Navigation("Teacher");
});
modelBuilder.Entity("Jiaowu.Api.Domain.Academic.GradeItem", b =>
{
b.HasOne("Jiaowu.Api.Domain.Academic.GradeSheet", "GradeSheet")
@@ -6843,6 +6896,8 @@ namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
modelBuilder.Entity("Jiaowu.Api.Domain.Academic.ExperimentSession", b =>
{
b.Navigation("Bookings");
b.Navigation("Instructors");
});
modelBuilder.Entity("Jiaowu.Api.Domain.Academic.GradeItem", b =>