默认 Redis 缓存 3 分钟、本地缓存 30 秒,可通过环境变量调整。 缓存键包含数据权限范围、有效学院和全部筛选条件,避免跨学院串数据。 Excel 导出保持实时查询,不使用摘要缓存。 无需数据库迁移。
103 lines
5.0 KiB
C#
103 lines
5.0 KiB
C#
using System.Text.Json;
|
|
using Jiaowu.Api.Domain.Academic;
|
|
using Jiaowu.Api.Infrastructure.Caching;
|
|
using Jiaowu.Api.Infrastructure.Persistence;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Jiaowu.Api.Controllers;
|
|
|
|
[ApiController]
|
|
[Authorize]
|
|
[Route("api/dashboard")]
|
|
public sealed class DashboardController(
|
|
AppDbContext db,
|
|
IAppCache appCache,
|
|
IOptions<JsonOptions> jsonOptions) : ControllerBase
|
|
{
|
|
[HttpGet]
|
|
public async Task<ActionResult<object>> Get(CancellationToken cancellationToken)
|
|
{
|
|
var response = await appCache.GetOrCreateAsync(
|
|
AppCacheKeys.Dashboard,
|
|
LoadAsync,
|
|
AppCacheProfile.Analytics,
|
|
[AppCacheTags.Analytics],
|
|
cancellationToken);
|
|
return response;
|
|
}
|
|
|
|
private async Task<JsonElement> LoadAsync(CancellationToken cancellationToken)
|
|
{
|
|
var currentTerm = await db.AcademicTerms
|
|
.AsNoTracking()
|
|
.Where(x => x.IsCurrent)
|
|
.Select(x => new { x.Id, x.Name, x.StartDate, x.EndDate })
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
|
|
return JsonSerializer.SerializeToElement(
|
|
new
|
|
{
|
|
CurrentTerm = currentTerm,
|
|
Counts = new
|
|
{
|
|
Campuses = await db.Campuses.CountAsync(cancellationToken),
|
|
Colleges = await db.Colleges.CountAsync(cancellationToken),
|
|
Majors = await db.Majors.CountAsync(cancellationToken),
|
|
Classes = await db.AdministrativeClasses.CountAsync(cancellationToken),
|
|
Classrooms = await db.Classrooms.CountAsync(cancellationToken),
|
|
Teachers = await db.Teachers.CountAsync(cancellationToken),
|
|
Students = await db.Students.CountAsync(cancellationToken),
|
|
Courses = await db.Courses.CountAsync(cancellationToken),
|
|
CurriculumPlans = await db.CurriculumPlans.CountAsync(cancellationToken),
|
|
TeachingTasks = await db.TeachingTasks.CountAsync(cancellationToken),
|
|
SchedulePlans = await db.SchedulePlans.CountAsync(cancellationToken),
|
|
CourseSelectionRounds = await db.CourseSelectionRounds
|
|
.CountAsync(cancellationToken),
|
|
CourseSelectionOfferings = await db.CourseSelectionOfferings
|
|
.CountAsync(cancellationToken),
|
|
CourseEnrollments = await db.CourseEnrollments
|
|
.CountAsync(
|
|
x => x.Status == CourseEnrollmentStatus.Enrolled,
|
|
cancellationToken),
|
|
GradeSheets = await db.GradeSheets.CountAsync(cancellationToken),
|
|
PublishedGradeSheets = await db.GradeSheets.CountAsync(
|
|
x => x.Status == GradeSheetStatus.Published,
|
|
cancellationToken),
|
|
GradeRecords = await db.GradeRecords.CountAsync(cancellationToken),
|
|
ExamPlans = await db.ExamPlans.CountAsync(cancellationToken),
|
|
ExamSessions = await db.ExamSessions.CountAsync(cancellationToken),
|
|
StudentStatusChanges = await db.StudentStatusChanges
|
|
.CountAsync(cancellationToken),
|
|
PendingStudentStatusChanges = await db.StudentStatusChanges.CountAsync(
|
|
x => x.State == StudentStatusChangeState.Submitted ||
|
|
x.State == StudentStatusChangeState.CounselorApproved ||
|
|
x.State == StudentStatusChangeState.CollegeApproved,
|
|
cancellationToken),
|
|
GraduationAuditBatches = await db.GraduationAuditBatches
|
|
.CountAsync(cancellationToken),
|
|
PublishedGraduationAuditBatches = await db.GraduationAuditBatches
|
|
.CountAsync(
|
|
x => x.Status == GraduationAuditBatchStatus.Published,
|
|
cancellationToken),
|
|
DegreeAwardBatches = await db.DegreeAwardBatches
|
|
.CountAsync(cancellationToken),
|
|
PublishedDegreeAwardBatches = await db.DegreeAwardBatches
|
|
.CountAsync(
|
|
x => x.Status == DegreeAwardBatchStatus.Published,
|
|
cancellationToken),
|
|
GraduationClearanceBatches = await db.GraduationClearanceBatches
|
|
.CountAsync(cancellationToken),
|
|
OpenGraduationClearanceBatches = await db.GraduationClearanceBatches
|
|
.CountAsync(
|
|
x => x.Status == GraduationClearanceBatchStatus.Open,
|
|
cancellationToken),
|
|
Users = await db.Users.CountAsync(cancellationToken)
|
|
}
|
|
},
|
|
jsonOptions.Value.JsonSerializerOptions);
|
|
}
|
|
}
|