增强筛选

This commit is contained in:
2026-08-11 16:34:52 +08:00 Unverified
parent b08b4cc4df
commit eb20211f0e
5 changed files with 253 additions and 18 deletions
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Jiaowu.Api.Contracts;
using Jiaowu.Api.Domain.Academic;
using Jiaowu.Api.Domain.Identity;
using Jiaowu.Api.Infrastructure.Auth;
@@ -20,10 +21,38 @@ public sealed class OtherExamsController(AppDbContext db, ICurrentUserDataScope
[HttpGet("batches")]
[Authorize(Roles = Managers)]
public async Task<ActionResult> GetBatches(CancellationToken ct) => Ok(await db.OtherExamBatches
.AsNoTracking().OrderByDescending(x => x.ExamDate).ThenByDescending(x => x.CreatedAt)
public async Task<ActionResult> GetBatches(
string? keyword = null,
OtherExamBatchStatus? status = null,
OtherExamMetricKind? metricKind = null,
DateOnly? examDateFrom = null,
DateOnly? examDateTo = null,
int page = 1,
int pageSize = 20,
CancellationToken ct = default)
{
page = Math.Max(1, page);
pageSize = Math.Clamp(pageSize, 10, 100);
var source = db.OtherExamBatches.AsNoTracking();
if (!string.IsNullOrWhiteSpace(keyword))
{
keyword = keyword.Trim();
source = source.Where(x =>
(x.ExamCode != null && x.ExamCode.Contains(keyword)) ||
x.Name.Contains(keyword) ||
(x.Organizer != null && x.Organizer.Contains(keyword)));
}
if (status.HasValue) source = source.Where(x => x.Status == status.Value);
if (metricKind.HasValue) source = source.Where(x => x.MetricKind == metricKind.Value);
if (examDateFrom.HasValue) source = source.Where(x => x.ExamDate >= examDateFrom.Value);
if (examDateTo.HasValue) source = source.Where(x => x.ExamDate <= examDateTo.Value);
var total = await source.CountAsync(ct);
var items = await source.OrderByDescending(x => x.ExamDate).ThenByDescending(x => x.CreatedAt)
.Skip((page - 1) * pageSize).Take(pageSize)
.Select(x => new { x.Id, x.ExamCode, x.Name, x.Organizer, x.ExamDate, x.MetricKind, x.MaxScore, x.LevelOptions, x.Status, x.PublicationCount, x.PublishedAt, ResultCount = x.Results.Count })
.ToListAsync(ct));
.ToListAsync(ct);
return Ok(new PagedResult<object>(items, total, page, pageSize));
}
[HttpPost("batches")]
[Authorize(Roles = Managers)]