教学点名强化
This commit is contained in:
@@ -23,6 +23,7 @@ public sealed class AttendanceController(
|
||||
SystemRoles.SuperAdmin + "," +
|
||||
SystemRoles.AcademicAdmin + "," +
|
||||
SystemRoles.CollegeAdmin + "," +
|
||||
SystemRoles.Counselor + "," +
|
||||
SystemRoles.Teacher;
|
||||
|
||||
[HttpGet("my-tasks")]
|
||||
@@ -299,6 +300,220 @@ public sealed class AttendanceController(
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
// ═══════════════ Student endpoints ═══════════════
|
||||
|
||||
[HttpGet("my-records")]
|
||||
[Authorize(Roles = SystemRoles.Student)]
|
||||
public async Task<ActionResult> GetMyRecords(
|
||||
Guid? academicTermId,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var userId = currentUserDataScope.Current.UserId;
|
||||
var studentId = await db.Students
|
||||
.Where(s => s.UserId == userId)
|
||||
.Select(s => (Guid?)s.Id)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
if (!studentId.HasValue)
|
||||
return ConflictProblem("当前账号未关联学生档案。");
|
||||
|
||||
var source = db.AttendanceRecords.AsNoTracking()
|
||||
.Where(r => r.StudentId == studentId.Value &&
|
||||
r.AttendanceSheet!.Status == AttendanceSheetStatus.Submitted);
|
||||
if (academicTermId.HasValue)
|
||||
source = source.Where(r =>
|
||||
r.AttendanceSheet!.TeachingTask!.AcademicTermId == academicTermId);
|
||||
|
||||
return Ok(await source
|
||||
.OrderByDescending(r => r.AttendanceSheet!.AttendanceDate)
|
||||
.Select(r => new
|
||||
{
|
||||
r.AttendanceSheetId,
|
||||
SheetName = r.AttendanceSheet!.Name,
|
||||
r.AttendanceSheet.AttendanceDate,
|
||||
TaskNumber = r.AttendanceSheet.TeachingTask!.TaskNumber,
|
||||
CourseCode = r.AttendanceSheet.TeachingTask.Course!.Code,
|
||||
CourseName = r.AttendanceSheet.TeachingTask.Course.Name,
|
||||
TeacherNames = r.AttendanceSheet.TeachingTask.Teachers
|
||||
.OrderByDescending(t => t.IsPrimary)
|
||||
.Select(t => t.Teacher!.Name),
|
||||
r.Status,
|
||||
r.Notes,
|
||||
r.AppealStatus,
|
||||
r.AppealReason,
|
||||
r.AppealSubmittedAt,
|
||||
r.AppealReviewComment,
|
||||
r.AppealReviewedAt
|
||||
})
|
||||
.ToListAsync(cancellationToken));
|
||||
}
|
||||
|
||||
[HttpPost("records/appeal")]
|
||||
[Authorize(Roles = SystemRoles.Student)]
|
||||
public async Task<ActionResult> SubmitAppeal(
|
||||
AttendanceAppealRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var userId = currentUserDataScope.Current.UserId;
|
||||
var studentId = await db.Students
|
||||
.Where(s => s.UserId == userId)
|
||||
.Select(s => (Guid?)s.Id)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
if (!studentId.HasValue)
|
||||
return ConflictProblem("当前账号未关联学生档案。");
|
||||
|
||||
var record = await db.AttendanceRecords
|
||||
.FirstOrDefaultAsync(r =>
|
||||
r.AttendanceSheetId == request.AttendanceSheetId &&
|
||||
r.StudentId == studentId.Value,
|
||||
cancellationToken);
|
||||
if (record is null) return NotFound();
|
||||
if (record.AppealStatus == AttendanceAppealStatus.Pending)
|
||||
return ConflictProblem("已有申诉正在处理中。");
|
||||
if (record.AppealStatus == AttendanceAppealStatus.Approved)
|
||||
return ConflictProblem("该考勤记录申诉已通过。");
|
||||
|
||||
record.AppealStatus = AttendanceAppealStatus.Pending;
|
||||
record.AppealReason = request.Reason.Trim();
|
||||
record.AppealSubmittedAt = DateTime.UtcNow;
|
||||
record.AppealReviewComment = null;
|
||||
record.AppealReviewedAt = null;
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// Notify the course teacher
|
||||
var teacherUserIds = await db.TeachingTaskTeachers
|
||||
.Where(x => x.TeachingTaskId == record.AttendanceSheet!.TeachingTaskId)
|
||||
.Select(x => x.Teacher!.UserId)
|
||||
.Where(id => id != null)
|
||||
.Select(id => id!.Value)
|
||||
.ToListAsync(cancellationToken);
|
||||
if (teacherUserIds.Count > 0)
|
||||
{
|
||||
var studentName = await db.Students
|
||||
.Where(s => s.Id == studentId.Value)
|
||||
.Select(s => s.Name)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
var courseName = record.AttendanceSheet!.TeachingTask?.Course?.Name ?? "";
|
||||
await NotificationService.SendToUserIdsAsync(db, teacherUserIds,
|
||||
"考勤申诉待处理",
|
||||
$"学生 {studentName} 对《{courseName}》考勤记录提出申诉。",
|
||||
"/teacher-attendance", cancellationToken);
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
// ═══════════════ Counselor endpoints ═══════════════
|
||||
|
||||
[HttpGet("counselor-records")]
|
||||
[Authorize(Roles = SystemRoles.Counselor)]
|
||||
public async Task<ActionResult> GetCounselorRecords(
|
||||
Guid? academicTermId,
|
||||
Guid? classId,
|
||||
bool? withAppeal,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var userId = currentUserDataScope.Current.UserId;
|
||||
var classIds = await db.AdministrativeClasses
|
||||
.Where(c => c.CounselorUserId == userId)
|
||||
.Select(c => c.Id)
|
||||
.ToListAsync(cancellationToken);
|
||||
if (classIds.Count == 0)
|
||||
return ConflictProblem("当前账号未关联任何班级。");
|
||||
|
||||
if (classId.HasValue && !classIds.Contains(classId.Value))
|
||||
return ConflictProblem("您不是该班级的辅导员。");
|
||||
|
||||
var targetClassIds = classId.HasValue
|
||||
? [classId.Value]
|
||||
: classIds;
|
||||
|
||||
var source = db.AttendanceRecords.AsNoTracking()
|
||||
.Where(r =>
|
||||
r.AttendanceSheet!.Status == AttendanceSheetStatus.Submitted &&
|
||||
r.Student!.AdministrativeClassId != null &&
|
||||
targetClassIds.Contains(r.Student.AdministrativeClassId));
|
||||
if (academicTermId.HasValue)
|
||||
source = source.Where(r =>
|
||||
r.AttendanceSheet!.TeachingTask!.AcademicTermId == academicTermId);
|
||||
if (withAppeal == true)
|
||||
source = source.Where(r => r.AppealStatus == AttendanceAppealStatus.Pending);
|
||||
|
||||
return Ok(await source
|
||||
.OrderByDescending(r => r.AttendanceSheet!.AttendanceDate)
|
||||
.Select(r => new
|
||||
{
|
||||
r.AttendanceSheetId,
|
||||
SheetName = r.AttendanceSheet!.Name,
|
||||
r.AttendanceSheet.AttendanceDate,
|
||||
TaskNumber = r.AttendanceSheet.TeachingTask!.TaskNumber,
|
||||
CourseCode = r.AttendanceSheet.TeachingTask.Course!.Code,
|
||||
CourseName = r.AttendanceSheet.TeachingTask.Course.Name,
|
||||
StudentNumber = r.Student!.StudentNumber,
|
||||
StudentName = r.Student.Name,
|
||||
ClassName = r.Student.AdministrativeClass!.Name,
|
||||
r.Status,
|
||||
r.Notes,
|
||||
r.AppealStatus,
|
||||
r.AppealReason,
|
||||
r.AppealSubmittedAt,
|
||||
r.AppealReviewComment
|
||||
})
|
||||
.ToListAsync(cancellationToken));
|
||||
}
|
||||
|
||||
[HttpPost("appeals/{attendanceSheetId:guid}/{studentId:guid}/review")]
|
||||
[Authorize(Roles = AttendanceRoles)]
|
||||
public async Task<ActionResult> ReviewAppeal(
|
||||
Guid attendanceSheetId,
|
||||
Guid studentId,
|
||||
AttendanceAppealReviewRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var record = await db.AttendanceRecords
|
||||
.Include(r => r.AttendanceSheet)
|
||||
.ThenInclude(s => s!.TeachingTask)
|
||||
.ThenInclude(t => t!.Teachers)
|
||||
.FirstOrDefaultAsync(r =>
|
||||
r.AttendanceSheetId == attendanceSheetId &&
|
||||
r.StudentId == studentId,
|
||||
cancellationToken);
|
||||
if (record is null) return NotFound();
|
||||
if (!CanManageSheet(record.AttendanceSheet!))
|
||||
return Forbid();
|
||||
if (record.AppealStatus != AttendanceAppealStatus.Pending)
|
||||
return ConflictProblem("该申诉不在待处理状态。");
|
||||
|
||||
record.AppealStatus = request.Approve
|
||||
? AttendanceAppealStatus.Approved
|
||||
: AttendanceAppealStatus.Rejected;
|
||||
record.AppealReviewComment = request.Comment?.Trim();
|
||||
record.AppealReviewedAt = DateTime.UtcNow;
|
||||
|
||||
// Update record status on approved appeal
|
||||
if (request.Approve)
|
||||
record.Status = AttendanceStatus.Excused;
|
||||
|
||||
await db.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// Notify student
|
||||
var studentUserId = await db.Students
|
||||
.Where(s => s.Id == studentId)
|
||||
.Select(s => s.UserId)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
if (studentUserId.HasValue)
|
||||
{
|
||||
var result = request.Approve ? "已通过" : "已驳回";
|
||||
await NotificationService.SendAsync(db, studentUserId.Value,
|
||||
$"考勤申诉{result}",
|
||||
request.Comment is not null
|
||||
? $"您的考勤申诉{result}。意见:{request.Comment}"
|
||||
: $"您的考勤申诉{result}。",
|
||||
null, cancellationToken);
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpDelete("sheets/{id:guid}")]
|
||||
public async Task<ActionResult> Delete(Guid id, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -320,6 +535,14 @@ public sealed class AttendanceController(
|
||||
if (scope.Scope == DataScope.All) return source;
|
||||
if (scope.Scope == DataScope.College)
|
||||
return source.Where(x => x.Course!.CollegeId == scope.RestrictedCollegeId);
|
||||
if (scope.IsInRole(SystemRoles.Counselor))
|
||||
{
|
||||
var collegeIds = db.AdministrativeClasses
|
||||
.Where(c => c.CounselorUserId == scope.UserId)
|
||||
.Select(c => c.Major!.CollegeId)
|
||||
.Distinct();
|
||||
return source.Where(x => collegeIds.Contains(x.Course!.CollegeId));
|
||||
}
|
||||
if (scope.IsInRole(SystemRoles.Teacher))
|
||||
return source.Where(x =>
|
||||
x.Teachers.Any(item => item.Teacher!.UserId == scope.UserId));
|
||||
@@ -357,3 +580,11 @@ public sealed record AttendanceRecordRequest(
|
||||
Guid StudentId,
|
||||
AttendanceStatus Status,
|
||||
[MaxLength(300)] string? Notes);
|
||||
|
||||
public sealed record AttendanceAppealRequest(
|
||||
Guid AttendanceSheetId,
|
||||
[Required, MaxLength(500)] string Reason);
|
||||
|
||||
public sealed record AttendanceAppealReviewRequest(
|
||||
bool Approve,
|
||||
[MaxLength(300)] string? Comment);
|
||||
|
||||
@@ -21,6 +21,7 @@ public sealed class GradesController(
|
||||
SystemRoles.SuperAdmin + "," +
|
||||
SystemRoles.AcademicAdmin + "," +
|
||||
SystemRoles.CollegeAdmin + "," +
|
||||
SystemRoles.Counselor + "," +
|
||||
SystemRoles.Teacher;
|
||||
|
||||
private const string Reviewers =
|
||||
@@ -587,6 +588,15 @@ public sealed class GradesController(
|
||||
if (scope.Scope == DataScope.All) return source;
|
||||
if (scope.Scope == DataScope.College)
|
||||
return source.Where(x => x.Course!.CollegeId == scope.RestrictedCollegeId);
|
||||
if (scope.IsInRole(SystemRoles.Counselor))
|
||||
{
|
||||
// Counselor sees grades for courses in their managed classes' college
|
||||
var collegeIds = db.AdministrativeClasses
|
||||
.Where(c => c.CounselorUserId == scope.UserId)
|
||||
.Select(c => c.Major!.CollegeId)
|
||||
.Distinct();
|
||||
return source.Where(x => collegeIds.Contains(x.Course!.CollegeId));
|
||||
}
|
||||
if (scope.IsInRole(SystemRoles.Teacher))
|
||||
return source.Where(x =>
|
||||
x.Teachers.Any(item => item.Teacher!.UserId == scope.UserId));
|
||||
|
||||
@@ -22,6 +22,11 @@ public sealed class AttendanceRecord
|
||||
public Student? Student { get; set; }
|
||||
public AttendanceStatus Status { get; set; } = AttendanceStatus.Present;
|
||||
public string? Notes { get; set; }
|
||||
public AttendanceAppealStatus AppealStatus { get; set; } = AttendanceAppealStatus.None;
|
||||
public string? AppealReason { get; set; }
|
||||
public DateTime? AppealSubmittedAt { get; set; }
|
||||
public string? AppealReviewComment { get; set; }
|
||||
public DateTime? AppealReviewedAt { get; set; }
|
||||
}
|
||||
|
||||
public enum AttendanceSheetStatus
|
||||
@@ -38,3 +43,11 @@ public enum AttendanceStatus
|
||||
Leave = 4,
|
||||
Excused = 5
|
||||
}
|
||||
|
||||
public enum AttendanceAppealStatus
|
||||
{
|
||||
None = 0,
|
||||
Pending = 1,
|
||||
Approved = 2,
|
||||
Rejected = 3
|
||||
}
|
||||
|
||||
@@ -543,6 +543,9 @@ public sealed class AppDbContext(DbContextOptions<AppDbContext> options)
|
||||
{
|
||||
entity.HasKey(x => new { x.AttendanceSheetId, x.StudentId });
|
||||
entity.Property(x => x.Notes).HasMaxLength(300);
|
||||
entity.Property(x => x.AppealReason).HasMaxLength(500);
|
||||
entity.Property(x => x.AppealReviewComment).HasMaxLength(300);
|
||||
entity.HasIndex(x => x.AppealStatus);
|
||||
entity.HasOne(x => x.AttendanceSheet)
|
||||
.WithMany(x => x.Records)
|
||||
.HasForeignKey(x => x.AttendanceSheetId)
|
||||
|
||||
@@ -38,6 +38,8 @@ public sealed class DevelopmentSqliteMigrator(
|
||||
"20260725_21_retake_enrollment";
|
||||
private const string CourseAdjustmentsMigration =
|
||||
"20260725_22_course_adjustments";
|
||||
private const string AttendanceAppealMigration =
|
||||
"20260725_23_attendance_appeal";
|
||||
|
||||
public async Task MigrateAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -255,6 +257,19 @@ public sealed class DevelopmentSqliteMigrator(
|
||||
CourseAdjustmentsMigration,
|
||||
courseAdjustmentsExist ? [] : CourseAdjustmentsStatements,
|
||||
cancellationToken);
|
||||
|
||||
var attendanceAppealExists = await db.Database
|
||||
.SqlQueryRaw<int>(
|
||||
"""
|
||||
SELECT COUNT(*) AS "Value"
|
||||
FROM pragma_table_info('AttendanceRecords')
|
||||
WHERE name = 'AppealStatus'
|
||||
""")
|
||||
.AnyAsync(value => value > 0, cancellationToken);
|
||||
await ApplyMigrationAsync(
|
||||
AttendanceAppealMigration,
|
||||
attendanceAppealExists ? [] : AttendanceAppealStatements,
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
private async Task ApplyMigrationAsync(
|
||||
@@ -1553,4 +1568,14 @@ public sealed class DevelopmentSqliteMigrator(
|
||||
"""CREATE INDEX "IX_Notifications_UserId_IsRead" ON "Notifications" ("UserId", "IsRead");""",
|
||||
"""CREATE INDEX "IX_Notifications_CreatedAt" ON "Notifications" ("CreatedAt");"""
|
||||
];
|
||||
|
||||
private static readonly string[] AttendanceAppealStatements =
|
||||
[
|
||||
"""ALTER TABLE "AttendanceRecords" ADD COLUMN "AppealStatus" INTEGER NOT NULL DEFAULT 0;""",
|
||||
"""ALTER TABLE "AttendanceRecords" ADD COLUMN "AppealReason" TEXT NULL;""",
|
||||
"""ALTER TABLE "AttendanceRecords" ADD COLUMN "AppealSubmittedAt" TEXT NULL;""",
|
||||
"""ALTER TABLE "AttendanceRecords" ADD COLUMN "AppealReviewComment" TEXT NULL;""",
|
||||
"""ALTER TABLE "AttendanceRecords" ADD COLUMN "AppealReviewedAt" TEXT NULL;""",
|
||||
"""CREATE INDEX IF NOT EXISTS "IX_AttendanceRecords_AppealStatus" ON "AttendanceRecords" ("AppealStatus");"""
|
||||
];
|
||||
}
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Jiaowu.Api.Infrastructure.Persistence.Migrations.MySql
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AttendanceAppeal : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "AppealStatus",
|
||||
table: "AttendanceRecords",
|
||||
type: "int",
|
||||
nullable: false,
|
||||
defaultValue: 0);
|
||||
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "AppealReason",
|
||||
table: "AttendanceRecords",
|
||||
type: "varchar(500)",
|
||||
maxLength: 500,
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.AddColumn<DateTime>(
|
||||
name: "AppealSubmittedAt",
|
||||
table: "AttendanceRecords",
|
||||
type: "datetime(6)",
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "AppealReviewComment",
|
||||
table: "AttendanceRecords",
|
||||
type: "varchar(300)",
|
||||
maxLength: 300,
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.AddColumn<DateTime>(
|
||||
name: "AppealReviewedAt",
|
||||
table: "AttendanceRecords",
|
||||
type: "datetime(6)",
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_AttendanceRecords_AppealStatus",
|
||||
table: "AttendanceRecords",
|
||||
column: "AppealStatus");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_AttendanceRecords_AppealStatus",
|
||||
table: "AttendanceRecords");
|
||||
|
||||
migrationBuilder.DropColumn(name: "AppealReviewedAt", table: "AttendanceRecords");
|
||||
migrationBuilder.DropColumn(name: "AppealReviewComment", table: "AttendanceRecords");
|
||||
migrationBuilder.DropColumn(name: "AppealSubmittedAt", table: "AttendanceRecords");
|
||||
migrationBuilder.DropColumn(name: "AppealReason", table: "AttendanceRecords");
|
||||
migrationBuilder.DropColumn(name: "AppealStatus", table: "AttendanceRecords");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user