student leaves
This commit is contained in:
1 parent
9fa1ea4d65
commit
7244f7469e
10 files changed
+22108
-9417
No files matched your search
@@ -0,0 +1,86 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Jiaowu.Api.Domain.Academic;
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
using Jiaowu.Api.Infrastructure.Auth;
|
||||
using Jiaowu.Api.Infrastructure.Persistence;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Jiaowu.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
[Route("api/student-leaves")]
|
||||
public sealed class StudentLeavesController(AppDbContext db, ICurrentUserDataScope scope) : ControllerBase
|
||||
{
|
||||
private const string Managers = SystemRoles.SuperAdmin + "," + SystemRoles.AcademicAdmin + "," + SystemRoles.CollegeAdmin;
|
||||
|
||||
[HttpGet("mine")]
|
||||
[Authorize(Roles = SystemRoles.Student)]
|
||||
public async Task<IActionResult> Mine(CancellationToken ct) => Ok(await db.StudentLeaves.AsNoTracking()
|
||||
.Where(x => x.Student!.UserId == scope.Current.UserId)
|
||||
.OrderByDescending(x => x.CreatedAt).Take(200)
|
||||
.Select(x => new { x.Id, x.Type, x.StartAt, x.EndAt, x.Reason, x.Status, x.CreatedAt, x.ReviewedAt, x.ReviewComment })
|
||||
.ToListAsync(ct));
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Roles = SystemRoles.Student)]
|
||||
public async Task<IActionResult> Create(CreateStudentLeaveRequest request, CancellationToken ct)
|
||||
{
|
||||
if (!Enum.IsDefined(request.Type) || request.StartAt == default || request.EndAt == default || request.EndAt <= request.StartAt || string.IsNullOrWhiteSpace(request.Reason))
|
||||
return BadRequest(new { message = "请填写有效的请假类型、起止时间和事由。" });
|
||||
var studentId = await db.Students.Where(x => x.UserId == scope.Current.UserId).Select(x => (Guid?)x.Id).FirstOrDefaultAsync(ct);
|
||||
if (studentId is null) return Forbid();
|
||||
var leave = new StudentLeave { StudentId = studentId.Value, Type = request.Type, StartAt = request.StartAt.UtcDateTime,
|
||||
EndAt = request.EndAt.UtcDateTime, Reason = request.Reason.Trim() };
|
||||
db.StudentLeaves.Add(leave);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return Ok(new { leave.Id });
|
||||
}
|
||||
|
||||
[HttpGet("pending")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<IActionResult> Pending(CancellationToken ct) => Ok(await Scoped()
|
||||
.Where(x => x.Status == StudentLeaveStatus.Submitted)
|
||||
.OrderBy(x => x.CreatedAt).Take(200)
|
||||
.Select(x => new { x.Id, StudentName = x.Student!.Name, x.Student.StudentNumber, x.Type, x.StartAt, x.EndAt, x.Reason, x.CreatedAt })
|
||||
.ToListAsync(ct));
|
||||
|
||||
[HttpPost("{id:guid}/review")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<IActionResult> Review(Guid id, ReviewStudentLeaveRequest request, CancellationToken ct)
|
||||
{
|
||||
if (!request.Approved && string.IsNullOrWhiteSpace(request.Comment))
|
||||
return BadRequest(new { message = "驳回时请填写审核意见。" });
|
||||
var now = DateTime.UtcNow;
|
||||
var status = request.Approved ? StudentLeaveStatus.Approved : StudentLeaveStatus.Rejected;
|
||||
var comment = request.Comment?.Trim();
|
||||
var updated = await Scoped().Where(x => x.Id == id && x.Status == StudentLeaveStatus.Submitted)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(x => x.Status, status).SetProperty(x => x.ReviewComment, comment)
|
||||
.SetProperty(x => x.ReviewedAt, now).SetProperty(x => x.ReviewedByUserId, scope.Current.UserId)
|
||||
.SetProperty(x => x.UpdatedAt, now), ct);
|
||||
return updated == 1 ? NoContent() : Conflict(new { message = "申请不存在、无权审核或已处理。" });
|
||||
}
|
||||
|
||||
[HttpPost("{id:guid}/withdraw")]
|
||||
[Authorize(Roles = SystemRoles.Student)]
|
||||
public async Task<IActionResult> Withdraw(Guid id, CancellationToken ct)
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var updated = await db.StudentLeaves.Where(x => x.Id == id && x.Student!.UserId == scope.Current.UserId && x.Status == StudentLeaveStatus.Submitted)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(x => x.Status, StudentLeaveStatus.Withdrawn).SetProperty(x => x.UpdatedAt, now), ct);
|
||||
return updated == 1 ? NoContent() : Conflict(new { message = "申请不存在或已处理。" });
|
||||
}
|
||||
|
||||
private IQueryable<StudentLeave> Scoped()
|
||||
{
|
||||
var query = db.StudentLeaves.AsNoTracking();
|
||||
if (scope.Current.Scope == DataScope.All) return query;
|
||||
var collegeId = scope.Current.RestrictedCollegeId;
|
||||
return collegeId.HasValue ? query.Where(x => x.Student!.AdministrativeClass!.Major!.CollegeId == collegeId) : query.Where(x => false);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record CreateStudentLeaveRequest(StudentLeaveType Type, DateTimeOffset StartAt, DateTimeOffset EndAt, [Required, MaxLength(500)] string Reason);
|
||||
public sealed record ReviewStudentLeaveRequest(bool Approved, [MaxLength(500)] string? Comment);
|
||||
@@ -2,6 +2,23 @@ using Jiaowu.Api.Domain.Common;
|
||||
|
||||
namespace Jiaowu.Api.Domain.Academic;
|
||||
|
||||
public sealed class StudentLeave : EntityBase
|
||||
{
|
||||
public Guid StudentId { get; set; }
|
||||
public Student? Student { get; set; }
|
||||
public StudentLeaveType Type { get; set; }
|
||||
public DateTime StartAt { get; set; }
|
||||
public DateTime EndAt { get; set; }
|
||||
public required string Reason { get; set; }
|
||||
public StudentLeaveStatus Status { get; set; } = StudentLeaveStatus.Submitted;
|
||||
public string? ReviewComment { get; set; }
|
||||
public Guid? ReviewedByUserId { get; set; }
|
||||
public DateTime? ReviewedAt { get; set; }
|
||||
}
|
||||
|
||||
public enum StudentLeaveType { Sick = 1, Personal = 2, Official = 3, Bereavement = 4, Other = 5 }
|
||||
public enum StudentLeaveStatus { Submitted = 1, Approved = 2, Rejected = 3, Withdrawn = 4 }
|
||||
|
||||
// ═══════════════ 免修 ═══════════════
|
||||
public sealed class CourseExemption : EntityBase
|
||||
{
|
||||
|
||||
@@ -178,6 +178,7 @@ public sealed class AppDbContext(DbContextOptions<AppDbContext> options)
|
||||
public DbSet<DeferredExam> DeferredExams => Set<DeferredExam>();
|
||||
public DbSet<GradeModification> GradeModifications => Set<GradeModification>();
|
||||
public DbSet<CourseSubstitution> CourseSubstitutions => Set<CourseSubstitution>();
|
||||
public DbSet<StudentLeave> StudentLeaves => Set<StudentLeave>();
|
||||
public DbSet<GradeAppeal> GradeAppeals => Set<GradeAppeal>();
|
||||
public DbSet<TeachingMonitoringSetting> TeachingMonitoringSettings =>
|
||||
Set<TeachingMonitoringSetting>();
|
||||
@@ -2216,6 +2217,15 @@ public sealed class AppDbContext(DbContextOptions<AppDbContext> options)
|
||||
.HasForeignKey(x => x.EvaluationDimensionId).OnDelete(DeleteBehavior.Restrict);
|
||||
});
|
||||
|
||||
builder.Entity<StudentLeave>(entity =>
|
||||
{
|
||||
entity.Property(x => x.Reason).HasMaxLength(500).IsRequired();
|
||||
entity.Property(x => x.ReviewComment).HasMaxLength(500);
|
||||
entity.HasIndex(x => new { x.StudentId, x.CreatedAt });
|
||||
entity.HasIndex(x => new { x.Status, x.CreatedAt });
|
||||
entity.HasOne(x => x.Student).WithMany().HasForeignKey(x => x.StudentId).OnDelete(DeleteBehavior.Restrict);
|
||||
});
|
||||
|
||||
builder.Entity<CourseSubstitution>(entity =>
|
||||
{
|
||||
entity.Property(x => x.ReviewerRoleSnapshot).HasMaxLength(32);
|
||||
|
||||
Loaded 3 of 10 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user