宿舍模块优化

This commit is contained in:
biss committed 2026-10-20 20:58:49 +08:00
1 parent bd7c586c5a
commit 4360280aee
13 files changed
+10277 -23

No files matched your search

@@ -1,4 +1,5 @@
using System.Text.Json;
using ClosedXML.Excel;
using Jiaowu.Api.Controllers;
using Jiaowu.Api.Domain.Academic;
using Jiaowu.Api.Domain.Identity;
@@ -69,6 +70,42 @@ public sealed class DormitoryHygieneControllerTests
Assert.DoesNotContain(draft.Id.ToString(), json);
}
[Fact]
public async Task Mine_MatchesScoresToActualStayWindowsAcrossTransfer()
{
await using var fixture = await Fixture.CreateAsync();
var targetRoom = new DormitoryRoom { DormitoryBuildingId = fixture.Room.DormitoryBuildingId, RoomNumber = "102", Floor = 1 };
var targetBed = new DormitoryBed { DormitoryRoomId = targetRoom.Id, BedNumber = "1" };
var occupancy = await fixture.Db.DormitoryOccupancies.SingleAsync(x => x.StudentId == fixture.Student.Id);
var originalBedId = occupancy.DormitoryBedId;
occupancy.DormitoryBedId = targetBed.Id;
occupancy.CheckInDate = new DateOnly(2026, 10, 10);
fixture.Db.AddRange(targetRoom, targetBed,
new DormitoryStayHistory { StudentId = fixture.Student.Id, ToBedId = originalBedId, Action = DormitoryStayAction.CheckIn,
EffectiveDate = new DateOnly(2026, 9, 1), OperatedByUserId = fixture.AllScope.Current.UserId },
new DormitoryStayHistory { StudentId = fixture.Student.Id, FromBedId = originalBedId, ToBedId = targetBed.Id,
Action = DormitoryStayAction.Transfer, EffectiveDate = new DateOnly(2026, 10, 10), OperatedByUserId = fixture.AllScope.Current.UserId });
var batches = new[]
{
new DormitoryHygieneBatch { Name = "原宿舍入住期间", InspectionDate = new DateOnly(2026, 9, 15), Status = DormitoryHygieneBatchStatus.Published },
new DormitoryHygieneBatch { Name = "原宿舍迁出之后", InspectionDate = new DateOnly(2026, 10, 15), Status = DormitoryHygieneBatchStatus.Published },
new DormitoryHygieneBatch { Name = "新宿舍入住之前", InspectionDate = new DateOnly(2026, 9, 20), Status = DormitoryHygieneBatchStatus.Published },
new DormitoryHygieneBatch { Name = "新宿舍入住之后", InspectionDate = new DateOnly(2026, 10, 16), Status = DormitoryHygieneBatchStatus.Published }
};
fixture.Db.AddRange(batches);
fixture.Db.AddRange(ScoreFor(batches[0].Id, fixture.Room.Id, 90), ScoreFor(batches[1].Id, fixture.Room.Id, 80),
ScoreFor(batches[2].Id, targetRoom.Id, 70), ScoreFor(batches[3].Id, targetRoom.Id, 95));
await fixture.Db.SaveChangesAsync();
var result = Assert.IsType<OkObjectResult>(await new DormitoryHygieneController(fixture.Db, fixture.StudentScope).Mine(CancellationToken.None));
var json = JsonSerializer.Serialize(result.Value);
Assert.Contains(batches[0].Id.ToString(), json);
Assert.Contains(batches[3].Id.ToString(), json);
Assert.DoesNotContain(batches[1].Id.ToString(), json);
Assert.DoesNotContain(batches[2].Id.ToString(), json);
}
[Fact]
public async Task Publish_RejectsBatchWithoutScores()
{
@@ -81,6 +118,25 @@ public sealed class DormitoryHygieneControllerTests
Assert.Contains("没有宿舍成绩", Assert.IsType<ProblemDetails>(conflict.Value).Detail);
}
[Fact]
public async Task Export_ReturnsCurrentBatchScoresWorkbook()
{
await using var fixture = await Fixture.CreateAsync();
fixture.Db.Add(ScoreFor(fixture.Batch.Id, fixture.Room.Id, 90));
await fixture.Db.SaveChangesAsync();
var controller = new DormitoryHygieneController(fixture.Db, fixture.AllScope);
var result = Assert.IsType<FileContentResult>(await controller.Export(fixture.Batch.Id, CancellationToken.None));
using var workbook = new XLWorkbook(new MemoryStream(result.FileContents));
var sheet = workbook.Worksheet("宿舍卫生成绩");
Assert.Equal("宿舍楼", sheet.Cell("A1").GetString());
Assert.Equal("楼层", sheet.Cell("C1").GetString());
Assert.Equal("总分", sheet.Cell("K1").GetString());
Assert.Equal(90, sheet.Cell("K2").GetDouble());
Assert.Equal("优秀", sheet.Cell("L2").GetString());
}
private static DormitoryHygieneScore ScoreFor(Guid batchId, Guid roomId, decimal total) => new()
{
DormitoryHygieneBatchId = batchId,
@@ -99,18 +155,20 @@ public sealed class DormitoryHygieneControllerTests
private readonly SqliteConnection connection;
public AppDbContext Db { get; }
public DormitoryRoom Room { get; }
public Student Student { get; }
public DormitoryRoom? OtherRoom { get; }
public DormitoryHygieneBatch Batch { get; }
public ICurrentUserDataScope AllScope { get; }
public ICurrentUserDataScope CollegeScope { get; }
public ICurrentUserDataScope StudentScope { get; }
private Fixture(SqliteConnection connection, AppDbContext db, DormitoryRoom room,
private Fixture(SqliteConnection connection, AppDbContext db, DormitoryRoom room, Student student,
DormitoryRoom? otherRoom, DormitoryHygieneBatch batch, Guid collegeId, Guid userId)
{
this.connection = connection;
Db = db;
Room = room;
Student = student;
OtherRoom = otherRoom;
Batch = batch;
AllScope = new TestScope(Guid.NewGuid(), null, DataScope.All, SystemRoles.DormitoryAdmin);
@@ -137,7 +195,7 @@ public sealed class DormitoryHygieneControllerTests
DormitoryRoomId = room.Id,
BedNumber = number.ToString()
}).ToArray();
var occupancy = new DormitoryOccupancy { DormitoryBedId = beds[0].Id, StudentId = student.Id };
var occupancy = new DormitoryOccupancy { DormitoryBedId = beds[0].Id, StudentId = student.Id, CheckInDate = new DateOnly(2026, 9, 1) };
var batch = new DormitoryHygieneBatch { Name = "第七周检查", InspectionDate = new DateOnly(2026, 10, 16) };
db.AddRange(user, college, major, classRoom, student, building, room, occupancy, batch);
db.AddRange(beds);
@@ -156,11 +214,11 @@ public sealed class DormitoryHygieneControllerTests
BedNumber = number.ToString()
}).ToArray();
db.AddRange(otherCollege, otherMajor, otherClass, otherStudent, otherBuilding, otherRoom,
new DormitoryOccupancy { DormitoryBedId = otherBeds[0].Id, StudentId = otherStudent.Id });
new DormitoryOccupancy { DormitoryBedId = otherBeds[0].Id, StudentId = otherStudent.Id, CheckInDate = new DateOnly(2026, 9, 1) });
db.AddRange(otherBeds);
}
await db.SaveChangesAsync();
return new Fixture(connection, db, room, otherRoom, batch, college.Id, userId);
return new Fixture(connection, db, room, student, otherRoom, batch, college.Id, userId);
}
public async ValueTask DisposeAsync()
@@ -1,10 +1,12 @@
using System.Text.Json;
using ClosedXML.Excel;
using Jiaowu.Api.Controllers;
using Jiaowu.Api.Domain.Academic;
using Jiaowu.Api.Domain.Identity;
using Jiaowu.Api.Infrastructure.Auth;
using Jiaowu.Api.Infrastructure.Persistence;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
@@ -90,6 +92,38 @@ public sealed class DormitoryServicesControllerTests
Assert.Equal(DormitoryStayAction.Checkout, (await fixture.Db.DormitoryStayHistories.SingleAsync()).Action);
}
[Fact]
public async Task ExcelImport_MergesRoomBedAndAssignsStudent_AndExportReturnsWorkbook()
{
await using var fixture = await Fixture.CreateAsync();
var controller = new DormitoryServicesController(fixture.Db, fixture.ManagerScope);
using var workbook = new XLWorkbook();
var sheet = workbook.Worksheets.Add("宿舍基础数据");
string[] headers = ["楼栋编码", "楼栋名称", "校区编码", "楼层数", "楼栋住宿性别", "楼栋状态",
"房间号", "所在楼层", "房间住宿性别", "房间状态", "床位号", "床位状态", "入住学生学号", "入住日期", "备注"];
for (var column = 0; column < headers.Length; column++) sheet.Cell(1, column + 1).Value = headers[column];
object[] values = ["MY", "梅园", "", 6, "男生", "启用", "103", 1, "男生", "启用", "1", "启用",
fixture.UnassignedStudent.StudentNumber, "2026-10-20", "批量导入"];
for (var column = 0; column < values.Length; column++) sheet.Cell(2, column + 1).Value = values[column].ToString();
await using var stream = new MemoryStream();
workbook.SaveAs(stream); stream.Position = 0;
var file = new FormFile(stream, 0, stream.Length, "file", "宿舍导入.xlsx");
Assert.IsType<OkObjectResult>(await controller.ImportExcel(file, CancellationToken.None));
fixture.Db.ChangeTracker.Clear();
var occupancy = await fixture.Db.DormitoryOccupancies.Include(x => x.DormitoryBed)!.ThenInclude(x => x!.DormitoryRoom)
.SingleAsync(x => x.StudentId == fixture.UnassignedStudent.Id);
Assert.Equal("103", occupancy.DormitoryBed!.DormitoryRoom!.RoomNumber);
Assert.Equal("1", occupancy.DormitoryBed.BedNumber);
Assert.Equal(new DateOnly(2026, 10, 20), occupancy.CheckInDate);
var export = Assert.IsType<FileContentResult>(await controller.ExportExcel(CancellationToken.None));
Assert.Equal("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", export.ContentType);
using var exported = new XLWorkbook(new MemoryStream(export.FileContents));
Assert.Equal("宿舍基础数据", exported.Worksheet(1).Name);
Assert.True(exported.Worksheet(1).RowsUsed().Count() >= 5);
}
private sealed class Fixture : IAsyncDisposable
{
private readonly SqliteConnection connection;