Customize dormitory data exports
This commit is contained in:
1 parent
99e33f9a48
commit
1f48660d49
5 files changed
+61
-26
No files matched your search
@@ -255,11 +255,16 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
return File(bytes, ExcelWorkbookHelper.ContentType, DownloadFileName.Template("宿舍卫生成绩", ".xlsx", batch.Name));
|
||||
}
|
||||
|
||||
private static readonly string[] ExportHeaders = ["宿舍楼", "房间号", "楼层", .. ImportHeaders.Skip(2), "总分", "等级"];
|
||||
[HttpGet("export-columns")]
|
||||
[Authorize(Roles = Viewers)]
|
||||
public IActionResult ExportColumns() => Ok(ExportHeaders);
|
||||
|
||||
[HttpGet("batches/{id:guid}/export.xlsx")]
|
||||
[Authorize(Roles = Viewers)]
|
||||
[ProducesResponseType<FileContentResult>(StatusCodes.Status200OK, ExcelWorkbookHelper.ContentType)]
|
||||
public async Task<IActionResult> Export(Guid id, CancellationToken ct,
|
||||
Guid? buildingId = null, int? floor = null)
|
||||
Guid? buildingId = null, int? floor = null, string? columns = null, string? format = null)
|
||||
{
|
||||
var batch = await db.DormitoryHygieneBatches.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id, ct);
|
||||
if (batch is null) return NotFound();
|
||||
@@ -287,11 +292,13 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
x.score.Issues,
|
||||
x.score.Notes
|
||||
}).ToListAsync(ct);
|
||||
string[] headers = ["宿舍楼", "房间号", "楼层", .. ImportHeaders.Skip(2), "总分", "等级"];
|
||||
var headers = ExportHeaders;
|
||||
var rows = data.Select(x => (IReadOnlyList<object?>)
|
||||
[x.BuildingName, x.RoomNumber, x.Floor, x.FloorScore, x.BeddingScore, x.BelongingsScore,
|
||||
x.FacilitiesScore, x.SafetyScore, x.IsViolation ? "是" : "否", x.Issues, x.Notes,
|
||||
x.TotalScore, GradeName(x.Grade)]).ToList();
|
||||
if (columns is not null || format is not null)
|
||||
return TabularExport.Create(this, "宿舍卫生成绩", headers, rows, columns, format);
|
||||
var bytes = ExcelWorkbookHelper.Create("宿舍卫生成绩", headers, rows);
|
||||
return File(bytes, ExcelWorkbookHelper.ContentType, DownloadFileName.Export("宿舍卫生成绩", ".xlsx", batch.Name));
|
||||
}
|
||||
|
||||
@@ -231,10 +231,14 @@ public sealed class DormitoryServicesController(
|
||||
return File(bytes, ExcelWorkbookHelper.ContentType, DownloadFileName.Template("宿舍基础数据与入住关系", ".xlsx"));
|
||||
}
|
||||
|
||||
[HttpGet("excel/export-columns")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public IActionResult ExportColumns() => Ok(ExcelHeaders);
|
||||
|
||||
[HttpGet("excel/export.xlsx")]
|
||||
[Authorize(Roles = Managers)]
|
||||
[ProducesResponseType<FileContentResult>(StatusCodes.Status200OK, ExcelWorkbookHelper.ContentType)]
|
||||
public async Task<IActionResult> ExportExcel(CancellationToken ct)
|
||||
public async Task<IActionResult> ExportExcel(CancellationToken ct, string? columns = null, string? format = null)
|
||||
{
|
||||
var rows = await db.DormitoryBeds.AsNoTracking().OrderBy(x => x.DormitoryRoom!.DormitoryBuilding!.Code)
|
||||
.ThenBy(x => x.DormitoryRoom!.RoomNumber).ThenBy(x => x.BedNumber)
|
||||
@@ -248,6 +252,8 @@ public sealed class DormitoryServicesController(
|
||||
x.BedNumber, x.IsEnabled ? "启用" : "停用", x.Occupancy != null ? x.Occupancy.Student!.StudentNumber : null,
|
||||
x.Occupancy != null ? x.Occupancy.CheckInDate : null, x.DormitoryRoom.Notes
|
||||
}).ToListAsync(ct);
|
||||
if (columns is not null || format is not null)
|
||||
return TabularExport.Create(this, "宿舍基础数据", ExcelHeaders, rows.Cast<IReadOnlyList<object?>>().ToList(), columns, format);
|
||||
var bytes = ExcelWorkbookHelper.Create("宿舍基础数据", ExcelHeaders, rows);
|
||||
return File(bytes, ExcelWorkbookHelper.ContentType, DownloadFileName.Export("宿舍基础数据与入住关系", ".xlsx"));
|
||||
}
|
||||
|
||||
@@ -202,6 +202,14 @@ public sealed class DormitoryHygieneControllerTests
|
||||
Assert.Equal("总分", sheet.Cell("L1").GetString());
|
||||
Assert.Equal(90, sheet.Cell("L2").GetDouble());
|
||||
Assert.Equal("优秀", sheet.Cell("M2").GetString());
|
||||
var headers = Assert.IsType<string[]>(Assert.IsType<OkObjectResult>(controller.ExportColumns()).Value);
|
||||
Assert.Equal("总分", headers[11]);
|
||||
var custom = Assert.IsType<FileContentResult>(await controller.Export(
|
||||
fixture.Batch.Id, CancellationToken.None, columns: "11,0", format: "xlsx"));
|
||||
using var selected = new XLWorkbook(new MemoryStream(custom.FileContents));
|
||||
Assert.Equal("总分", selected.Worksheet(1).Cell(1, 1).GetString());
|
||||
Assert.Equal(90, selected.Worksheet(1).Cell(2, 1).GetDouble());
|
||||
Assert.Equal("宿舍楼", selected.Worksheet(1).Cell(1, 2).GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Loaded 3 of 5 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user