110 lines
6.4 KiB
C#
110 lines
6.4 KiB
C#
using Jiaowu.Api.Domain.Academic;
|
|
using Jiaowu.Api.Infrastructure.Persistence;
|
|
using Jiaowu.Api.Infrastructure.Timetables;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Jiaowu.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[AllowAnonymous]
|
|
[Route("api/venue-displays")]
|
|
public sealed class VenueDisplaysController(
|
|
AppDbContext db,
|
|
TimetableDataService timetableDataService,
|
|
ClassroomReservationAvailabilityService availabilityService) : ControllerBase
|
|
{
|
|
[HttpGet("classrooms/{classroomId:guid}")]
|
|
public async Task<ActionResult> Classroom(Guid classroomId, CancellationToken cancellationToken)
|
|
{
|
|
var term = await CurrentTermAsync(cancellationToken);
|
|
var classroom = await db.Classrooms.AsNoTracking()
|
|
.Where(x => x.Id == classroomId && x.IsEnabled && x.Building!.IsEnabled)
|
|
.Select(x => new { x.Id, x.Name, x.Code, x.BuildingId, CampusId = x.Building!.CampusId, BuildingName = x.Building.Name, CampusName = x.Building.Campus!.Name })
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
if (classroom is null) return NotFound();
|
|
if (term is null) return Ok(new { Classroom = classroom, Today = DateOnly.FromDateTime(DateTime.Now), Entries = Array.Empty<object>() });
|
|
|
|
var sheet = await timetableDataService.BuildAsync(TimetableResourceType.Classroom, classroomId,
|
|
term.Id, null, false, null, null, cancellationToken);
|
|
var today = DateOnly.FromDateTime(DateTime.Now);
|
|
var (week, day) = ClassroomReservationAvailabilityService.ResolveTeachingWeek(term, today);
|
|
var entries = sheet?.Entries
|
|
.Where(x => x.DayOfWeek == day && x.StartWeek <= week && x.EndWeek >= week &&
|
|
(x.WeekPattern == WeekPattern.All || x.WeekPattern == WeekPattern.Odd && week % 2 == 1 || x.WeekPattern == WeekPattern.Even && week % 2 == 0))
|
|
.Select(x => new VenueDisplayClassroomEntry(
|
|
x.CourseName,
|
|
x.TeacherNames,
|
|
x.ClassNames,
|
|
x.StartPeriod,
|
|
x.PeriodCount,
|
|
false))
|
|
.ToList() ?? [];
|
|
var reservations = await db.ClassroomReservations.AsNoTracking()
|
|
.Where(x => x.AcademicTermId == term.Id &&
|
|
x.ClassroomId == classroomId &&
|
|
x.Status == ClassroomReservationStatus.Approved &&
|
|
x.ReservationDate == today)
|
|
.OrderBy(x => x.StartPeriod)
|
|
.Select(x => new VenueDisplayClassroomEntry(
|
|
"教室借用",
|
|
new[] { x.ApplicantName },
|
|
new[] { x.Purpose },
|
|
x.StartPeriod,
|
|
x.PeriodCount,
|
|
true))
|
|
.ToListAsync(cancellationToken);
|
|
entries.AddRange(reservations);
|
|
entries.Sort((left, right) => left.StartPeriod.CompareTo(right.StartPeriod));
|
|
var announcements = await GetAnnouncementsAsync(classroom.CampusId, classroom.BuildingId, classroom.Id, cancellationToken);
|
|
return Ok(new { Classroom = classroom, Term = term.Name, Today = today, Week = week, Slots = sheet?.Slots ?? [], Entries = entries, Announcements = announcements });
|
|
}
|
|
|
|
[HttpGet("buildings/{buildingId:guid}")]
|
|
public async Task<ActionResult> Building(Guid buildingId, CancellationToken cancellationToken)
|
|
{
|
|
var term = await CurrentTermAsync(cancellationToken);
|
|
var building = await db.Buildings.AsNoTracking().Where(x => x.Id == buildingId && x.IsEnabled)
|
|
.Select(x => new { x.Id, x.Name, x.Code, x.CampusId, CampusName = x.Campus!.Name }).FirstOrDefaultAsync(cancellationToken);
|
|
if (building is null) return NotFound();
|
|
if (term is null) return Ok(new { Building = building, Today = DateOnly.FromDateTime(DateTime.Now), Slots = Array.Empty<object>(), Rooms = Array.Empty<object>() });
|
|
|
|
var today = DateOnly.FromDateTime(DateTime.Now);
|
|
var slots = await db.ScheduleTimeSlots.AsNoTracking().Where(x => x.AcademicTermId == term.Id && x.IsEnabled)
|
|
.OrderBy(x => x.PeriodNumber).Select(x => new { x.PeriodNumber, x.Name, x.StartsAt, x.EndsAt }).ToListAsync(cancellationToken);
|
|
var rooms = await db.Classrooms.AsNoTracking().Where(x => x.BuildingId == buildingId && x.IsEnabled)
|
|
.OrderBy(x => x.Code).Select(x => new { x.Id, x.Code, x.Name, x.Capacity }).ToListAsync(cancellationToken);
|
|
var occupied = await availabilityService.GetOccupiedClassroomIdsByPeriodAsync(
|
|
term, today, slots.Select(slot => slot.PeriodNumber).ToArray(), cancellationToken);
|
|
var announcements = await GetAnnouncementsAsync(building.CampusId, building.Id, null, cancellationToken);
|
|
return Ok(new { Building = building, Term = term.Name, Today = today, Slots = slots, Rooms = rooms.Select(room => new { room.Code, room.Name, room.Capacity, FreePeriods = slots.Where(slot => !occupied[slot.PeriodNumber].Contains(room.Id)).Select(slot => slot.PeriodNumber) }), Announcements = announcements });
|
|
}
|
|
|
|
private Task<List<object>> GetAnnouncementsAsync(Guid campusId, Guid buildingId, Guid? classroomId, CancellationToken cancellationToken)
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
return db.VenueDisplayAnnouncements.AsNoTracking()
|
|
.Where(x => x.IsPublished && (!x.StartsAt.HasValue || x.StartsAt <= now) && (!x.EndsAt.HasValue || x.EndsAt > now) &&
|
|
(x.Scope == VenueDisplayAnnouncementScope.School ||
|
|
x.Scope == VenueDisplayAnnouncementScope.Campus && x.TargetId == campusId ||
|
|
x.Scope == VenueDisplayAnnouncementScope.Building && x.TargetId == buildingId ||
|
|
classroomId.HasValue && x.Scope == VenueDisplayAnnouncementScope.Classroom && x.TargetId == classroomId))
|
|
.OrderByDescending(x => x.IsPinned).ThenByDescending(x => x.IsUrgent).ThenByDescending(x => x.Priority).ThenByDescending(x => x.UpdatedAt)
|
|
.Take(8)
|
|
.Select(x => (object)new { x.Id, x.Title, x.Content, x.IsPinned, x.IsUrgent })
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
private Task<AcademicTerm?> CurrentTermAsync(CancellationToken cancellationToken) => db.AcademicTerms.AsNoTracking()
|
|
.Where(x => x.IsEnabled && x.IsCurrent).FirstOrDefaultAsync(cancellationToken);
|
|
}
|
|
|
|
public sealed record VenueDisplayClassroomEntry(
|
|
string CourseName,
|
|
IEnumerable<string> TeacherNames,
|
|
IEnumerable<string> ClassNames,
|
|
int StartPeriod,
|
|
int PeriodCount,
|
|
bool IsReservation);
|