“教学班成绩分析”页面新增“定时刷新设置”,支持启停、刷新间隔、单次处理上限、查看上次/下次扫描时间。
配置保存在独立数据库表 CourseGradeStatisticsRefreshSettings,不是修改 appsettings.json。 后台每 10 秒读取配置,仅到期扫描;成绩录入、导入、审批时不再立即创建统计任务。 扫描发现过期数据后,仍通过持久任务、Outbox 和 RabbitMQ 执行;Redis统计缓存由处理任务统一刷新。
This commit is contained in:
@@ -251,13 +251,6 @@ public sealed class ApprovalsController(AppDbContext db, ICurrentUserDataScope s
|
||||
// Auto-apply: update grade record
|
||||
gm.GradeRecord!.TotalScore = gm.RequestedScore;
|
||||
gm.GradeRecord.GradePoint = GradeCalculator.CalculateGradePoint(gm.RequestedScore);
|
||||
var statisticsJob = new CourseGradeStatisticsRefreshJob
|
||||
{
|
||||
GradeSheetId = gm.GradeRecord.GradeSheetId
|
||||
};
|
||||
db.CourseGradeStatisticsRefreshJobs.Add(statisticsJob);
|
||||
db.BackgroundJobOutboxMessages.Add(BackgroundJobOutboxMessage.Create(
|
||||
BackgroundJobKind.CourseGradeStatisticsRefresh, statisticsJob.Id));
|
||||
await db.SaveChangesAsync(ct);
|
||||
await NotificationService.SendAsync(
|
||||
db,
|
||||
|
||||
@@ -25,6 +25,69 @@ public sealed class GradeAnalyticsController(
|
||||
SystemRoles.CollegeAdmin + "," +
|
||||
SystemRoles.Leader + "," +
|
||||
SystemRoles.Teacher;
|
||||
private const string ScheduleManagers =
|
||||
SystemRoles.SuperAdmin + "," + SystemRoles.AcademicAdmin;
|
||||
|
||||
[HttpGet("refresh-schedule")]
|
||||
[Authorize(Roles = ScheduleManagers)]
|
||||
public async Task<ActionResult> GetRefreshSchedule(CancellationToken cancellationToken)
|
||||
{
|
||||
var setting = await db.CourseGradeStatisticsRefreshSettings.AsNoTracking()
|
||||
.SingleOrDefaultAsync(
|
||||
x => x.Key == CourseGradeStatisticsRefreshSetting.DefaultKey,
|
||||
cancellationToken);
|
||||
var defaults = new CourseGradeStatisticsRefreshSetting();
|
||||
var enabled = setting?.IsEnabled ?? defaults.IsEnabled;
|
||||
var intervalSeconds = setting?.IntervalSeconds ?? defaults.IntervalSeconds;
|
||||
var batchSize = setting?.BatchSize ?? defaults.BatchSize;
|
||||
var lastRunAt = setting?.LastRunAt;
|
||||
return Ok(new
|
||||
{
|
||||
IsEnabled = enabled,
|
||||
IntervalSeconds = intervalSeconds,
|
||||
BatchSize = batchSize,
|
||||
LastRunAt = lastRunAt,
|
||||
NextRunAt = enabled && lastRunAt.HasValue
|
||||
? lastRunAt.Value.AddSeconds(intervalSeconds)
|
||||
: null as DateTime?
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPut("refresh-schedule")]
|
||||
[Authorize(Roles = ScheduleManagers)]
|
||||
public async Task<ActionResult> SaveRefreshSchedule(
|
||||
SaveGradeStatisticsRefreshScheduleRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (request.IntervalSeconds is < 10 or > 86400)
|
||||
return BadRequest(new ProblemDetails
|
||||
{
|
||||
Title = "刷新间隔应在 10 秒到 24 小时之间。",
|
||||
Status = StatusCodes.Status400BadRequest
|
||||
});
|
||||
if (request.BatchSize is < 1 or > 5000)
|
||||
return BadRequest(new ProblemDetails
|
||||
{
|
||||
Title = "单次刷新批量应在 1 到 5000 之间。",
|
||||
Status = StatusCodes.Status400BadRequest
|
||||
});
|
||||
|
||||
var setting = await db.CourseGradeStatisticsRefreshSettings
|
||||
.SingleOrDefaultAsync(
|
||||
x => x.Key == CourseGradeStatisticsRefreshSetting.DefaultKey,
|
||||
cancellationToken);
|
||||
if (setting is null)
|
||||
{
|
||||
setting = new CourseGradeStatisticsRefreshSetting();
|
||||
db.CourseGradeStatisticsRefreshSettings.Add(setting);
|
||||
}
|
||||
|
||||
setting.IsEnabled = request.IsEnabled;
|
||||
setting.IntervalSeconds = request.IntervalSeconds;
|
||||
setting.BatchSize = request.BatchSize;
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("teaching-classes")]
|
||||
public async Task<ActionResult> GetTeachingClasses(
|
||||
@@ -526,3 +589,8 @@ public sealed class GradeAnalyticsController(
|
||||
IReadOnlyList<HistoricalComparison> History,
|
||||
ComparisonDelta? UniversityDelta);
|
||||
}
|
||||
|
||||
public sealed record SaveGradeStatisticsRefreshScheduleRequest(
|
||||
bool IsEnabled,
|
||||
int IntervalSeconds,
|
||||
int BatchSize);
|
||||
|
||||
@@ -508,7 +508,6 @@ public sealed class GradesController(
|
||||
}
|
||||
targetItem.SourceType = GradeItemSourceType.ExperimentSummary;
|
||||
targetItem.SourceSnapshotAt = DateTime.UtcNow;
|
||||
QueueCourseStatisticsRefresh(sheet.Id);
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
return Ok(new
|
||||
{
|
||||
@@ -668,7 +667,6 @@ public sealed class GradesController(
|
||||
return ConflictProblem("只有审核通过的成绩单可以发布。");
|
||||
sheet.Status = GradeSheetStatus.Published;
|
||||
sheet.PublishedAt = DateTime.UtcNow;
|
||||
QueueCourseStatisticsRefresh(sheet.Id);
|
||||
|
||||
// The grade sheet roster is authoritative at publication time. This also
|
||||
// covers students added through approved roster corrections.
|
||||
@@ -918,7 +916,6 @@ public sealed class GradesController(
|
||||
if (errors.Count > 0)
|
||||
return ImportValidationProblem(errors);
|
||||
|
||||
QueueCourseStatisticsRefresh(sheet.Id);
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
return Ok(new { updated, total = rows.Count });
|
||||
}
|
||||
@@ -1161,7 +1158,6 @@ public sealed class GradesController(
|
||||
{
|
||||
try
|
||||
{
|
||||
QueueCourseStatisticsRefresh(id);
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
return created ? Created(string.Empty, new { id }) : NoContent();
|
||||
}
|
||||
@@ -1171,14 +1167,6 @@ public sealed class GradesController(
|
||||
}
|
||||
}
|
||||
|
||||
private void QueueCourseStatisticsRefresh(Guid gradeSheetId)
|
||||
{
|
||||
var job = new CourseGradeStatisticsRefreshJob { GradeSheetId = gradeSheetId };
|
||||
db.CourseGradeStatisticsRefreshJobs.Add(job);
|
||||
db.BackgroundJobOutboxMessages.Add(BackgroundJobOutboxMessage.Create(
|
||||
BackgroundJobKind.CourseGradeStatisticsRefresh, job.Id));
|
||||
}
|
||||
|
||||
private ActionResult ConflictProblem(string detail) =>
|
||||
Conflict(new ProblemDetails
|
||||
{
|
||||
|
||||
@@ -21,32 +21,45 @@ public sealed class WarningsController(AppDbContext db, ICurrentUserDataScope sc
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<ActionResult> GetRules(Guid academicTermId, CancellationToken ct) =>
|
||||
Ok(await db.WarningRules.AsNoTracking().Where(x => x.AcademicTermId == academicTermId)
|
||||
.OrderBy(x => x.Type).Select(x => new { x.Id, x.Type, x.Name, x.Threshold, x.IsEnabled, x.NotifyStudent, x.NotifyCounselor, x.Description, x.AutoCheckEnabled, CheckDayOfWeek = x.CheckDayOfWeek ?? 0, x.CheckHour, x.CheckMinute, x.LastCheckAt })
|
||||
.OrderBy(x => x.Type).Select(x => new { x.Id, Type = (int)x.Type, x.Name, x.Threshold, x.IsEnabled, x.NotifyStudent, x.NotifyCounselor, x.Description, x.AutoCheckEnabled, CheckDayOfWeek = x.CheckDayOfWeek ?? 0, x.CheckHour, x.CheckMinute, x.LastCheckAt })
|
||||
.ToListAsync(ct));
|
||||
|
||||
[HttpPut("rules")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<ActionResult> SaveRules(Guid academicTermId, List<WarningRuleDto> rules, CancellationToken ct)
|
||||
{
|
||||
if (rules.GroupBy(x => x.Type).Any(group => group.Count() > 1))
|
||||
return BadRequest(new ProblemDetails { Title = "预警类型不能重复。", Status = StatusCodes.Status400BadRequest });
|
||||
if (rules.Any(x => x.CheckDayOfWeek is < 0 or > 7 || x.CheckHour is < 0 or > 23 || x.CheckMinute is < 0 or > 59))
|
||||
return BadRequest(new ProblemDetails { Title = "自动检测时间无效。", Status = StatusCodes.Status400BadRequest });
|
||||
|
||||
var existing = await db.WarningRules.Where(x => x.AcademicTermId == academicTermId).ToListAsync(ct);
|
||||
db.WarningRules.RemoveRange(existing);
|
||||
var incomingTypes = rules.Select(x => x.Type).ToHashSet();
|
||||
db.WarningRules.RemoveRange(existing.Where(x => !incomingTypes.Contains(x.Type)));
|
||||
foreach (var r in rules)
|
||||
{
|
||||
db.WarningRules.Add(new WarningRule
|
||||
var entity = existing.FirstOrDefault(x => x.Type == r.Type);
|
||||
if (entity is null)
|
||||
{
|
||||
AcademicTermId = academicTermId,
|
||||
Type = r.Type,
|
||||
Name = r.Name.Trim(),
|
||||
Threshold = r.Threshold,
|
||||
IsEnabled = r.IsEnabled,
|
||||
NotifyStudent = r.NotifyStudent,
|
||||
NotifyCounselor = r.NotifyCounselor,
|
||||
Description = r.Description?.Trim(),
|
||||
AutoCheckEnabled = r.AutoCheckEnabled,
|
||||
CheckDayOfWeek = r.CheckDayOfWeek == 0 ? null : r.CheckDayOfWeek,
|
||||
CheckHour = r.CheckHour,
|
||||
CheckMinute = r.CheckMinute
|
||||
});
|
||||
entity = new WarningRule
|
||||
{
|
||||
AcademicTermId = academicTermId,
|
||||
Type = r.Type,
|
||||
Name = r.Name.Trim()
|
||||
};
|
||||
db.WarningRules.Add(entity);
|
||||
}
|
||||
|
||||
entity.Name = r.Name.Trim();
|
||||
entity.Threshold = r.Threshold;
|
||||
entity.IsEnabled = r.IsEnabled;
|
||||
entity.NotifyStudent = r.NotifyStudent;
|
||||
entity.NotifyCounselor = r.NotifyCounselor;
|
||||
entity.Description = r.Description?.Trim();
|
||||
entity.AutoCheckEnabled = r.AutoCheckEnabled;
|
||||
entity.CheckDayOfWeek = r.CheckDayOfWeek == 0 ? null : r.CheckDayOfWeek;
|
||||
entity.CheckHour = r.CheckHour;
|
||||
entity.CheckMinute = r.CheckMinute;
|
||||
}
|
||||
await db.SaveChangesAsync(ct);
|
||||
return NoContent();
|
||||
@@ -120,7 +133,7 @@ public sealed class WarningsController(AppDbContext db, ICurrentUserDataScope sc
|
||||
}
|
||||
if (academicTermId.HasValue) q = q.Where(x => x.AcademicTermId == academicTermId);
|
||||
if (type.HasValue) q = q.Where(x => x.Type == type);
|
||||
return Ok(await q.OrderByDescending(x => x.CreatedAt).Select(x => new { x.Id, x.StudentId, StudentName = x.Student!.Name, StudentNumber = x.Student.StudentNumber, ClassName = x.Student.AdministrativeClass!.Name, x.Type, x.Status, x.TriggerValue, x.Detail, x.AcknowledgedAt, x.AcknowledgeComment, x.CreatedAt }).ToListAsync(ct));
|
||||
return Ok(await q.OrderByDescending(x => x.CreatedAt).Select(x => new { x.Id, x.StudentId, StudentName = x.Student!.Name, StudentNumber = x.Student.StudentNumber, ClassName = x.Student.AdministrativeClass!.Name, Type = (int)x.Type, Status = (int)x.Status, x.TriggerValue, x.Detail, x.AcknowledgedAt, x.AcknowledgeComment, x.CreatedAt }).ToListAsync(ct));
|
||||
}
|
||||
|
||||
// ═══════════ Student ═══════════
|
||||
@@ -131,7 +144,7 @@ public sealed class WarningsController(AppDbContext db, ICurrentUserDataScope sc
|
||||
var sid = await GetStudentIdAsync(ct);
|
||||
if (sid is null) return StudentNotFound();
|
||||
return Ok(await db.WarningRecords.AsNoTracking().Where(x => x.StudentId == sid).OrderByDescending(x => x.CreatedAt)
|
||||
.Select(x => new { x.Id, x.Type, x.Status, x.TriggerValue, x.Detail, x.AcknowledgedAt, x.AcknowledgeComment, x.CreatedAt, TermName = x.AcademicTermId })
|
||||
.Select(x => new { x.Id, Type = (int)x.Type, Status = (int)x.Status, x.TriggerValue, x.Detail, x.AcknowledgedAt, x.AcknowledgeComment, x.CreatedAt, TermName = x.AcademicTermId })
|
||||
.ToListAsync(ct));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user