diff --git a/src/Jiaowu.Api/Controllers/StudentDisciplinesController.cs b/src/Jiaowu.Api/Controllers/StudentDisciplinesController.cs index 6ad754f..2189551 100644 --- a/src/Jiaowu.Api/Controllers/StudentDisciplinesController.cs +++ b/src/Jiaowu.Api/Controllers/StudentDisciplinesController.cs @@ -81,14 +81,19 @@ public sealed class StudentDisciplinesController(AppDbContext db, ICurrentUserDa return Ok(new { items, total, page, pageSize }); } + private static readonly string[] ExportHeaders = ["学号", "姓名", "班级", "处分类型", "状态", "决定日期", "生效日期", "文号", "决定机构"]; + [HttpGet("export-columns")] + public IActionResult ExportColumns() => Ok(ExportHeaders); + [HttpGet("export")] - public async Task Export(string? keyword, StudentDisciplineState? state, string? disciplineType, DateOnly? dateFrom, DateOnly? dateTo, CancellationToken token) + public async Task Export(string? keyword, StudentDisciplineState? state, string? disciplineType, DateOnly? dateFrom, DateOnly? dateTo, CancellationToken token, string? columns = null, string? format = null) { var query = Filter(keyword, state, disciplineType, dateFrom, dateTo); var items = await query.OrderByDescending(x => x.CreatedAt).ThenByDescending(x => x.Id).Take(10001).Select(x => new { x.Student!.StudentNumber, x.Student.Name, ClassName = x.Student.AdministrativeClass!.Name, x.DisciplineType, x.State, x.DecisionDate, x.EffectiveDate, x.DecisionNumber, x.DecisionAuthority }).ToListAsync(token); if (items.Count > 10000) return BadRequest("结果超过 10000 条,请缩小查询范围。"); - var rows = items.Select(x => (IReadOnlyList)new object?[] { x.StudentNumber, x.Name, x.ClassName, x.DisciplineType, StateLabel(x.State), x.DecisionDate.ToString("yyyy-MM-dd"), x.EffectiveDate.ToString("yyyy-MM-dd"), x.DecisionNumber, x.DecisionAuthority }); - return File(ExcelWorkbookHelper.Create("处分记录", ["学号", "姓名", "班级", "处分类型", "状态", "决定日期", "生效日期", "文号", "决定机构"], rows), ExcelWorkbookHelper.ContentType, "处分记录.xlsx"); + var rows = items.Select(x => (IReadOnlyList)new object?[] { x.StudentNumber, x.Name, x.ClassName, x.DisciplineType, StateLabel(x.State), x.DecisionDate.ToString("yyyy-MM-dd"), x.EffectiveDate.ToString("yyyy-MM-dd"), x.DecisionNumber, x.DecisionAuthority }).ToList(); + if (columns is not null || format is not null) return TabularExport.Create(this, "处分记录", ExportHeaders, rows, columns, format); + return File(ExcelWorkbookHelper.Create("处分记录", ExportHeaders, rows), ExcelWorkbookHelper.ContentType, "处分记录.xlsx"); } private static string StateLabel(StudentDisciplineState state) => state switch { StudentDisciplineState.Draft => "草稿", StudentDisciplineState.Submitted => "待院系审核", StudentDisciplineState.CollegeApproved => "待校级审核", StudentDisciplineState.Effective => "已批准", StudentDisciplineState.Rejected => "已驳回", StudentDisciplineState.Withdrawn => "已撤回", StudentDisciplineState.Lifted => "已解除", StudentDisciplineState.Revoked => "已撤销", _ => "未知" }; diff --git a/tests/Jiaowu.Api.Tests/StudentDisciplinesControllerTests.cs b/tests/Jiaowu.Api.Tests/StudentDisciplinesControllerTests.cs index dbd9b93..636c8c2 100644 --- a/tests/Jiaowu.Api.Tests/StudentDisciplinesControllerTests.cs +++ b/tests/Jiaowu.Api.Tests/StudentDisciplinesControllerTests.cs @@ -52,6 +52,12 @@ public sealed class StudentDisciplinesControllerTests using var stream = new MemoryStream(export.FileContents); using var workbook = new ClosedXML.Excel.XLWorkbook(stream); Assert.Equal(1, workbook.Worksheet(1).LastRowUsed()!.RowNumber()); + var custom = Assert.IsType(await school.Export(null, null, null, null, null, default, "1,0", "xlsx")); + using var selected = new ClosedXML.Excel.XLWorkbook(new MemoryStream(custom.FileContents)); + Assert.Equal("姓名", selected.Worksheet(1).Cell(1, 1).GetString()); + Assert.Equal(student.Name, selected.Worksheet(1).Cell(2, 1).GetString()); + var restricted = Assert.IsType(await otherCollege.Export(null, null, null, null, null, default, "1,0", "txt")); + Assert.DoesNotContain(student.StudentNumber, System.Text.Encoding.UTF8.GetString(restricted.FileContents)); Assert.IsType(await school.SetTypes(["警告", "严重警告"], default)); Assert.IsType(await school.SetTypes(["严重警告"], default)); Assert.False((await db.StudentDisciplineTypes.SingleAsync(x => x.Name == "警告")).IsEnabled); diff --git a/web/src/views/StudentDisciplinesView.vue b/web/src/views/StudentDisciplinesView.vue index 56ce79a..044c031 100644 --- a/web/src/views/StudentDisciplinesView.vue +++ b/web/src/views/StudentDisciplinesView.vue @@ -1,4 +1,6 @@