宿舍相关
This commit is contained in:
1 parent
908a174020
commit
92e9f7c8f1
21 files changed
+21179
-47
No files matched your search
@@ -1,6 +1,7 @@
|
||||
using System.Globalization;
|
||||
using Jiaowu.Api.Domain.Academic;
|
||||
using Jiaowu.Api.Domain.Identity;
|
||||
using Jiaowu.Api.Domain.System;
|
||||
using Jiaowu.Api.Infrastructure.Auth;
|
||||
using Jiaowu.Api.Infrastructure.Excel;
|
||||
using Jiaowu.Api.Infrastructure.Files;
|
||||
@@ -20,7 +21,7 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
private const string Viewers = Managers + "," + SystemRoles.CollegeAdmin + "," + SystemRoles.Counselor;
|
||||
private static readonly string[] ImportHeaders =
|
||||
[
|
||||
"宿舍楼", "房间号", "地面卫生", "床铺整洁", "物品摆放", "设施卫生", "安全规范", "问题说明", "备注"
|
||||
"宿舍楼", "房间号", "地面卫生", "床铺整洁", "物品摆放", "设施卫生", "安全规范", "是否违纪", "问题说明", "备注"
|
||||
];
|
||||
|
||||
[HttpGet("rooms")]
|
||||
@@ -79,6 +80,7 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
{
|
||||
x.Id, x.Name, x.InspectionDate, x.Inspector, x.Status, x.PublicationCount, x.PublishedAt,
|
||||
ScoreCount = x.Scores.Count(s => rooms.Any(r => r.Id == s.DormitoryRoomId)),
|
||||
ViolationCount = x.Scores.Count(s => s.IsViolation && rooms.Any(r => r.Id == s.DormitoryRoomId)),
|
||||
AverageScore = x.Scores.Where(s => rooms.Any(r => r.Id == s.DormitoryRoomId))
|
||||
.Select(s => (decimal?)s.TotalScore).Average()
|
||||
}).ToListAsync(ct);
|
||||
@@ -98,6 +100,39 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
return Ok(new { batch.Id });
|
||||
}
|
||||
|
||||
[HttpPut("batches/{id:guid}")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<ActionResult> UpdateBatch(Guid id, CreateDormitoryHygieneBatchRequest request,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var batch = await db.DormitoryHygieneBatches.FirstOrDefaultAsync(x => x.Id == id, ct);
|
||||
if (batch is null) return NotFound();
|
||||
var name = Normalize(request.Name);
|
||||
if (name is null || name.Length > 150) return ValidationProblem("批次名称不能为空且不能超过 150 个字符。");
|
||||
if (Normalize(request.Inspector)?.Length > 100) return ValidationProblem("检查人员不能超过 100 个字符。");
|
||||
batch.Name = name;
|
||||
batch.InspectionDate = request.InspectionDate;
|
||||
batch.Inspector = Normalize(request.Inspector);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return Ok(new { batch.Id, batch.Status });
|
||||
}
|
||||
|
||||
[HttpDelete("batches/{id:guid}")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<ActionResult> DeleteBatch(Guid id, CancellationToken ct)
|
||||
{
|
||||
var batch = await db.DormitoryHygieneBatches
|
||||
.Include(x => x.Scores)
|
||||
.FirstOrDefaultAsync(x => x.Id == id, ct);
|
||||
if (batch is null) return NotFound();
|
||||
if (batch.Status == DormitoryHygieneBatchStatus.Published)
|
||||
return ConflictProblem("已发布的检查批次不能删除,可修改批次信息或成绩后重新发布。");
|
||||
var scoreCount = batch.Scores.Count;
|
||||
db.DormitoryHygieneBatches.Remove(batch);
|
||||
await db.SaveChangesAsync(ct);
|
||||
return Ok(new { DeletedScoreCount = scoreCount });
|
||||
}
|
||||
|
||||
[HttpGet("batches/{id:guid}/scores")]
|
||||
[Authorize(Roles = Viewers)]
|
||||
public async Task<ActionResult> GetScores(Guid id, string? keyword = null, Guid? buildingId = null, int? floor = null,
|
||||
@@ -117,7 +152,8 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
{
|
||||
TotalCount = x.Count(), AverageScore = Math.Round(x.Average(v => v.score.TotalScore), 1),
|
||||
ExcellentCount = x.Count(v => v.score.Grade == DormitoryHygieneGrade.Excellent),
|
||||
UnqualifiedCount = x.Count(v => v.score.Grade == DormitoryHygieneGrade.Unqualified)
|
||||
UnqualifiedCount = x.Count(v => v.score.Grade == DormitoryHygieneGrade.Unqualified),
|
||||
ViolationCount = x.Count(v => v.score.IsViolation)
|
||||
}).FirstOrDefaultAsync(ct);
|
||||
var source = all;
|
||||
if (buildingId.HasValue) source = source.Where(x => x.room.DormitoryBuildingId == buildingId.Value);
|
||||
@@ -128,7 +164,8 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
source = source.Where(x => x.room.DormitoryBuilding!.Name.Contains(keyword) || x.room.RoomNumber.Contains(keyword));
|
||||
}
|
||||
if (grade.HasValue) source = source.Where(x => x.score.Grade == grade.Value);
|
||||
if (attentionOnly) source = source.Where(x => x.score.Grade == DormitoryHygieneGrade.Unqualified || x.score.Issues != null);
|
||||
if (attentionOnly) source = source.Where(x => x.score.Grade == DormitoryHygieneGrade.Unqualified ||
|
||||
x.score.IsViolation || x.score.Issues != null);
|
||||
var total = await source.CountAsync(ct);
|
||||
var items = await source.OrderBy(x => x.room.DormitoryBuilding!.Code).ThenBy(x => x.room.RoomNumber)
|
||||
.Skip((page - 1) * pageSize).Take(pageSize)
|
||||
@@ -138,12 +175,12 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
BuildingName = x.room.DormitoryBuilding!.Name, x.room.RoomNumber, x.room.Floor,
|
||||
OccupantCount = x.room.Beds.Count(b => b.Occupancy != null),
|
||||
x.score.FloorScore, x.score.BeddingScore, x.score.BelongingsScore, x.score.FacilitiesScore,
|
||||
x.score.SafetyScore, x.score.TotalScore, x.score.Grade, x.score.Issues, x.score.Notes
|
||||
x.score.SafetyScore, x.score.TotalScore, x.score.Grade, x.score.IsViolation, x.score.Issues, x.score.Notes
|
||||
}).ToListAsync(ct);
|
||||
return Ok(new
|
||||
{
|
||||
Batch = new { batch.Id, batch.Name, batch.InspectionDate, batch.Inspector, batch.Status, batch.PublicationCount, batch.PublishedAt },
|
||||
Summary = summary ?? new { TotalCount = 0, AverageScore = 0m, ExcellentCount = 0, UnqualifiedCount = 0 },
|
||||
Summary = summary ?? new { TotalCount = 0, AverageScore = 0m, ExcellentCount = 0, UnqualifiedCount = 0, ViolationCount = 0 },
|
||||
Scores = new { Items = items, Page = page, PageSize = pageSize, Total = total }
|
||||
});
|
||||
}
|
||||
@@ -166,7 +203,7 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
var batch = await db.DormitoryHygieneBatches.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id, ct);
|
||||
if (batch is null) return NotFound();
|
||||
var bytes = ExcelWorkbookHelper.Create("宿舍卫生成绩导入", ImportHeaders, [],
|
||||
["每项满分 20 分,总分由系统自动计算。", "宿舍楼与房间号必须和宿舍档案完全一致。"]);
|
||||
["每项满分 20 分,总分由系统自动计算。", "宿舍楼与房间号必须和宿舍档案完全一致。", "是否违纪填写“是”或“否”,留空按“否”处理。"]);
|
||||
return File(bytes, ExcelWorkbookHelper.ContentType, DownloadFileName.Template("宿舍卫生成绩", ".xlsx", batch.Name));
|
||||
}
|
||||
|
||||
@@ -186,12 +223,13 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
BuildingName = room.DormitoryBuilding!.Name, room.RoomNumber, room.Floor,
|
||||
score.FloorScore, score.BeddingScore, score.BelongingsScore,
|
||||
score.FacilitiesScore, score.SafetyScore, score.TotalScore,
|
||||
score.Grade, score.Issues, score.Notes
|
||||
score.Grade, score.IsViolation, score.Issues, score.Notes
|
||||
}).ToListAsync(ct);
|
||||
string[] headers = ["宿舍楼", "房间号", "楼层", .. ImportHeaders.Skip(2), "总分", "等级"];
|
||||
var rows = data.Select(x => (IReadOnlyList<object?>)
|
||||
[x.BuildingName, x.RoomNumber, x.Floor, x.FloorScore, x.BeddingScore, x.BelongingsScore,
|
||||
x.FacilitiesScore, x.SafetyScore, x.Issues, x.Notes, x.TotalScore, GradeName(x.Grade)]).ToList();
|
||||
x.FacilitiesScore, x.SafetyScore, x.IsViolation ? "是" : "否", x.Issues, x.Notes,
|
||||
x.TotalScore, GradeName(x.Grade)]).ToList();
|
||||
var bytes = ExcelWorkbookHelper.Create("宿舍卫生成绩", headers, rows);
|
||||
return File(bytes, ExcelWorkbookHelper.ContentType, DownloadFileName.Export("宿舍卫生成绩", ".xlsx", batch.Name));
|
||||
}
|
||||
@@ -223,7 +261,8 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
ParseScore(row["物品摆放"], "物品摆放", row.RowNumber, errors),
|
||||
ParseScore(row["设施卫生"], "设施卫生", row.RowNumber, errors),
|
||||
ParseScore(row["安全规范"], "安全规范", row.RowNumber, errors),
|
||||
Normalize(row["问题说明"]), Normalize(row["备注"])));
|
||||
Normalize(row["问题说明"]), Normalize(row["备注"]),
|
||||
ParseViolation(row["是否违纪"], row.RowNumber, errors)));
|
||||
}
|
||||
if (errors.Count > 0) return ImportValidationProblem(errors);
|
||||
var error = await UpsertScoresAsync(batch, inputs, ct);
|
||||
@@ -244,6 +283,133 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
return Ok(new { batch.PublicationCount, batch.PublishedAt });
|
||||
}
|
||||
|
||||
[HttpGet("calculations/options/{academicTermId:guid}")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<ActionResult> GetCalculationOptions(Guid academicTermId, CancellationToken ct)
|
||||
{
|
||||
var term = await db.AcademicTerms.AsNoTracking()
|
||||
.Where(x => x.Id == academicTermId)
|
||||
.Select(x => new { x.Id, x.Name, x.StartDate, x.EndDate, x.IsCurrent })
|
||||
.FirstOrDefaultAsync(ct);
|
||||
if (term is null) return NotFound();
|
||||
var batches = await db.DormitoryHygieneBatches.AsNoTracking()
|
||||
.Where(x => x.Status == DormitoryHygieneBatchStatus.Published &&
|
||||
x.InspectionDate >= term.StartDate && x.InspectionDate <= term.EndDate)
|
||||
.OrderBy(x => x.InspectionDate).ThenBy(x => x.Name)
|
||||
.Select(x => new
|
||||
{
|
||||
x.Id, x.Name, x.InspectionDate, x.Inspector,
|
||||
ScoreCount = x.Scores.Count,
|
||||
AverageScore = x.Scores.Select(s => (decimal?)s.TotalScore).Average(),
|
||||
ViolationCount = x.Scores.Count(s => s.IsViolation)
|
||||
}).ToListAsync(ct);
|
||||
return Ok(new { Term = term, Batches = batches });
|
||||
}
|
||||
|
||||
[HttpGet("calculations")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<ActionResult> GetCalculations(Guid? academicTermId = null, int page = 1, int pageSize = 20,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
page = Math.Max(page, 1);
|
||||
pageSize = Math.Clamp(pageSize, 10, 100);
|
||||
var source = db.DormitoryHygieneCalculations.AsNoTracking();
|
||||
if (academicTermId.HasValue) source = source.Where(x => x.AcademicTermId == academicTermId.Value);
|
||||
var total = await source.CountAsync(ct);
|
||||
var items = await source.OrderByDescending(x => x.CreatedAt)
|
||||
.Skip((page - 1) * pageSize).Take(pageSize)
|
||||
.Select(x => new
|
||||
{
|
||||
x.Id, x.AcademicTermId, TermName = x.AcademicTerm!.Name, x.Status,
|
||||
x.SelectedBatchCount, x.StudentCount, x.StudentWithoutScoreCount,
|
||||
x.StartedAt, x.CompletedAt, x.ErrorMessage, x.CreatedAt,
|
||||
CreatedBy = x.CreatedByUser!.DisplayName,
|
||||
BatchNames = x.SelectedBatches.OrderBy(b => b.DormitoryHygieneBatch!.InspectionDate)
|
||||
.Select(b => b.DormitoryHygieneBatch!.Name).ToList()
|
||||
}).ToListAsync(ct);
|
||||
return Ok(new { Items = items, Page = page, PageSize = pageSize, Total = total });
|
||||
}
|
||||
|
||||
[HttpGet("calculations/{id:guid}")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<ActionResult> GetCalculation(Guid id, string? keyword = null, int page = 1, int pageSize = 50,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
var calculation = await db.DormitoryHygieneCalculations.AsNoTracking()
|
||||
.Where(x => x.Id == id)
|
||||
.Select(x => new
|
||||
{
|
||||
x.Id, x.AcademicTermId, TermName = x.AcademicTerm!.Name, x.Status,
|
||||
x.SelectedBatchCount, x.StudentCount, x.StudentWithoutScoreCount,
|
||||
x.StartedAt, x.CompletedAt, x.ErrorMessage, x.CreatedAt,
|
||||
Batches = x.SelectedBatches.OrderBy(b => b.DormitoryHygieneBatch!.InspectionDate).Select(b => new
|
||||
{
|
||||
b.DormitoryHygieneBatchId, b.DormitoryHygieneBatch!.Name,
|
||||
b.DormitoryHygieneBatch.InspectionDate
|
||||
}).ToList()
|
||||
}).FirstOrDefaultAsync(ct);
|
||||
if (calculation is null) return NotFound();
|
||||
page = Math.Max(page, 1);
|
||||
pageSize = Math.Clamp(pageSize, 10, 200);
|
||||
var source = db.DormitoryHygieneStudentSummaries.AsNoTracking()
|
||||
.Where(x => x.DormitoryHygieneCalculationId == id);
|
||||
if (!string.IsNullOrWhiteSpace(keyword))
|
||||
{
|
||||
keyword = keyword.Trim();
|
||||
source = source.Where(x => x.Student!.StudentNumber.Contains(keyword) || x.Student.Name.Contains(keyword));
|
||||
}
|
||||
var total = await source.CountAsync(ct);
|
||||
var items = await source.OrderBy(x => x.Student!.StudentNumber)
|
||||
.Skip((page - 1) * pageSize).Take(pageSize)
|
||||
.Select(x => new
|
||||
{
|
||||
x.StudentId, x.Student!.StudentNumber, x.Student.Name,
|
||||
ClassName = x.Student.AdministrativeClass!.Name,
|
||||
MajorName = x.Student.AdministrativeClass.Major!.Name,
|
||||
x.AverageScore, x.ExpectedBatchCount, x.IncludedScoreCount,
|
||||
x.MissingScoreCount, x.ViolationCount
|
||||
}).ToListAsync(ct);
|
||||
return Ok(new { Calculation = calculation, Results = new { Items = items, Page = page, PageSize = pageSize, Total = total } });
|
||||
}
|
||||
|
||||
[HttpPost("calculations")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<ActionResult> CreateCalculation(CreateDormitoryHygieneCalculationRequest request,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var batchIds = request.BatchIds.Distinct().ToArray();
|
||||
if (batchIds.Length == 0 || batchIds.Length > 500)
|
||||
return ValidationProblem("请选择 1 到 500 个检查批次。");
|
||||
var term = await db.AcademicTerms.AsNoTracking().FirstOrDefaultAsync(x => x.Id == request.AcademicTermId, ct);
|
||||
if (term is null) return ValidationProblem("所选学期不存在。");
|
||||
var validBatchCount = await db.DormitoryHygieneBatches.CountAsync(x => batchIds.Contains(x.Id) &&
|
||||
x.Status == DormitoryHygieneBatchStatus.Published &&
|
||||
x.InspectionDate >= term.StartDate && x.InspectionDate <= term.EndDate, ct);
|
||||
if (validBatchCount != batchIds.Length)
|
||||
return ValidationProblem("只能选择目标学期日期范围内的已发布检查批次。");
|
||||
var unfinished = await db.DormitoryHygieneCalculations.AnyAsync(x => x.AcademicTermId == request.AcademicTermId &&
|
||||
(x.Status == DormitoryHygieneCalculationStatus.Queued || x.Status == DormitoryHygieneCalculationStatus.Running), ct);
|
||||
if (unfinished) return ConflictProblem("该学期已有正在排队或计算中的汇总任务,请等待任务完成。");
|
||||
|
||||
var calculation = new DormitoryHygieneCalculation
|
||||
{
|
||||
AcademicTermId = request.AcademicTermId,
|
||||
CreatedByUserId = scope.Current.UserId,
|
||||
SelectedBatchCount = batchIds.Length
|
||||
};
|
||||
db.DormitoryHygieneCalculations.Add(calculation);
|
||||
db.DormitoryHygieneCalculationBatches.AddRange(batchIds.Select(batchId =>
|
||||
new DormitoryHygieneCalculationBatch
|
||||
{
|
||||
DormitoryHygieneCalculationId = calculation.Id,
|
||||
DormitoryHygieneBatchId = batchId
|
||||
}));
|
||||
db.BackgroundJobOutboxMessages.Add(BackgroundJobOutboxMessage.Create(
|
||||
BackgroundJobKind.DormitoryHygieneCalculation, calculation.Id));
|
||||
await db.SaveChangesAsync(ct);
|
||||
return Accepted(new { calculation.Id, calculation.Status, calculation.SelectedBatchCount });
|
||||
}
|
||||
|
||||
[HttpGet("mine")]
|
||||
[Authorize(Roles = SystemRoles.Student)]
|
||||
public async Task<ActionResult> Mine(CancellationToken ct)
|
||||
@@ -297,7 +463,7 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
x.DormitoryHygieneBatch.InspectionDate, x.DormitoryHygieneBatch.Inspector,
|
||||
x.DormitoryRoomId,
|
||||
x.FloorScore, x.BeddingScore, x.BelongingsScore, x.FacilitiesScore,
|
||||
x.SafetyScore, x.TotalScore, x.Grade, x.Issues, x.Notes
|
||||
x.SafetyScore, x.TotalScore, x.Grade, x.IsViolation, x.Issues, x.Notes
|
||||
}).ToListAsync(ct);
|
||||
var scores = scoreRows.Select(score => new { Score = score, Stay = stays.FirstOrDefault(stay => stay.RoomId == score.DormitoryRoomId &&
|
||||
score.InspectionDate >= stay.StartDate && (!stay.EndDate.HasValue || score.InspectionDate < stay.EndDate.Value)) })
|
||||
@@ -307,7 +473,7 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
x.Score.Id, x.Score.DormitoryHygieneBatchId, x.Score.BatchName, x.Score.InspectionDate, x.Score.Inspector,
|
||||
BuildingName = x.Stay!.BuildingName, x.Stay.RoomNumber, x.Stay.BedNumber,
|
||||
x.Score.FloorScore, x.Score.BeddingScore, x.Score.BelongingsScore, x.Score.FacilitiesScore,
|
||||
x.Score.SafetyScore, x.Score.TotalScore, x.Score.Grade, x.Score.Issues, x.Score.Notes
|
||||
x.Score.SafetyScore, x.Score.TotalScore, x.Score.Grade, x.Score.IsViolation, x.Score.Issues, x.Score.Notes
|
||||
}).ToList();
|
||||
return Ok(new { Room = room, Scores = scores });
|
||||
}
|
||||
@@ -340,7 +506,8 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
score.BelongingsScore = input.BelongingsScore; score.FacilitiesScore = input.FacilitiesScore;
|
||||
score.SafetyScore = input.SafetyScore;
|
||||
score.TotalScore = input.FloorScore + input.BeddingScore + input.BelongingsScore + input.FacilitiesScore + input.SafetyScore;
|
||||
score.Grade = GradeFor(score.TotalScore); score.Issues = Normalize(input.Issues); score.Notes = Normalize(input.Notes);
|
||||
score.Grade = GradeFor(score.TotalScore); score.IsViolation = input.IsViolation;
|
||||
score.Issues = Normalize(input.Issues); score.Notes = Normalize(input.Notes);
|
||||
}
|
||||
await db.SaveChangesAsync(ct);
|
||||
return null;
|
||||
@@ -366,6 +533,14 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
if (decimal.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out var score) && score is >= 0 and <= 20) return score;
|
||||
errors.Add($"第 {row} 行:{field}必须填写 0 到 20 之间的数字。"); return 0;
|
||||
}
|
||||
private static bool ParseViolation(string value, int row, List<string> errors)
|
||||
{
|
||||
var normalized = value.Trim();
|
||||
if (normalized is "" or "否" or "0" || normalized.Equals("false", StringComparison.OrdinalIgnoreCase)) return false;
|
||||
if (normalized is "是" or "1" or "违纪" || normalized.Equals("true", StringComparison.OrdinalIgnoreCase)) return true;
|
||||
errors.Add($"第 {row} 行:是否违纪只能填写“是”或“否”。");
|
||||
return false;
|
||||
}
|
||||
private static string? Normalize(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim();
|
||||
private ActionResult ConflictProblem(string detail) => Conflict(new ProblemDetails { Status = 409, Detail = detail });
|
||||
private ActionResult ValidationProblem(string detail) => BadRequest(new ValidationProblemDetails { Detail = detail });
|
||||
@@ -387,5 +562,7 @@ public sealed class DormitoryHygieneController(AppDbContext db, ICurrentUserData
|
||||
|
||||
public sealed record CreateDormitoryHygieneBatchRequest(string Name, DateOnly InspectionDate, string? Inspector);
|
||||
public sealed record DormitoryHygieneScoreRequest(Guid DormitoryRoomId, decimal FloorScore, decimal BeddingScore,
|
||||
decimal BelongingsScore, decimal FacilitiesScore, decimal SafetyScore, string? Issues, string? Notes);
|
||||
decimal BelongingsScore, decimal FacilitiesScore, decimal SafetyScore, string? Issues, string? Notes,
|
||||
bool IsViolation = false);
|
||||
public sealed record UpsertDormitoryHygieneScoresRequest(IReadOnlyList<DormitoryHygieneScoreRequest> Scores);
|
||||
public sealed record CreateDormitoryHygieneCalculationRequest(Guid AcademicTermId, IReadOnlyList<Guid> BatchIds);
|
||||
@@ -346,15 +346,24 @@ public sealed class DormitoryServicesController(AppDbContext db, ICurrentUserDat
|
||||
[HttpGet("unassigned-students")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<ActionResult> GetUnassignedStudents(Guid? collegeId = null, Guid? majorId = null,
|
||||
Guid? classId = null, Gender? gender = null, string? keyword = null, int page = 1, int pageSize = 50,
|
||||
Guid? classId = null, [FromQuery] int[]? grades = null, [FromQuery] Guid[]? majorIds = null,
|
||||
[FromQuery] Guid[]? classIds = null, Gender? gender = null, string? keyword = null, int page = 1, int pageSize = 50,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
page = Math.Max(1, page); pageSize = Math.Clamp(pageSize, 10, 200);
|
||||
grades = grades?.Distinct().ToArray();
|
||||
majorIds = majorIds?.Distinct().ToArray();
|
||||
classIds = classIds?.Distinct().ToArray();
|
||||
if (grades?.Length > 100 || majorIds?.Length > 500 || classIds?.Length > 500)
|
||||
return ValidationProblem("年级、专业或班级筛选项过多。");
|
||||
var source = db.Students.AsNoTracking().Where(x => x.Status == StudentStatus.Active &&
|
||||
!db.DormitoryOccupancies.Any(occupancy => occupancy.StudentId == x.Id));
|
||||
if (collegeId.HasValue) source = source.Where(x => x.AdministrativeClass!.Major!.CollegeId == collegeId);
|
||||
if (majorId.HasValue) source = source.Where(x => x.AdministrativeClass!.MajorId == majorId);
|
||||
if (classId.HasValue) source = source.Where(x => x.AdministrativeClassId == classId);
|
||||
if (grades?.Length > 0) source = source.WhereIn(grades, x => x.AdministrativeClass!.Grade);
|
||||
if (majorIds?.Length > 0) source = source.WhereIn(majorIds, x => x.AdministrativeClass!.MajorId);
|
||||
if (classIds?.Length > 0) source = source.WhereIn(classIds, x => x.AdministrativeClassId);
|
||||
if (gender.HasValue) source = source.Where(x => x.Gender == gender);
|
||||
if (!string.IsNullOrWhiteSpace(keyword)) { keyword = keyword.Trim(); source = source.Where(x => x.StudentNumber.Contains(keyword) || x.Name.Contains(keyword)); }
|
||||
var total = await source.CountAsync(ct);
|
||||
@@ -365,11 +374,45 @@ public sealed class DormitoryServicesController(AppDbContext db, ICurrentUserDat
|
||||
x.Id, x.StudentNumber, x.Name, x.Gender, x.AdministrativeClassId,
|
||||
ClassName = x.AdministrativeClass!.Name, MajorId = x.AdministrativeClass.MajorId,
|
||||
MajorName = x.AdministrativeClass.Major!.Name,
|
||||
CollegeId = x.AdministrativeClass.Major.CollegeId, CollegeName = x.AdministrativeClass.Major.College!.Name
|
||||
CollegeId = x.AdministrativeClass.Major.CollegeId, CollegeName = x.AdministrativeClass.Major.College!.Name,
|
||||
Grade = x.AdministrativeClass.Grade
|
||||
}).ToListAsync(ct);
|
||||
return Ok(new { Items = items, Total = total, Page = page, PageSize = pageSize });
|
||||
}
|
||||
|
||||
[HttpGet("unassigned-students/filter-options")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<ActionResult> GetUnassignedStudentFilterOptions(CancellationToken ct)
|
||||
{
|
||||
var classes = await db.AdministrativeClasses.AsNoTracking()
|
||||
.Where(x => x.Students.Any(student => student.Status == StudentStatus.Active &&
|
||||
!db.DormitoryOccupancies.Any(occupancy => occupancy.StudentId == student.Id)))
|
||||
.OrderByDescending(x => x.Grade)
|
||||
.ThenBy(x => x.Major!.Name)
|
||||
.ThenBy(x => x.Name)
|
||||
.Select(x => new
|
||||
{
|
||||
x.Id,
|
||||
x.Name,
|
||||
x.Code,
|
||||
x.Grade,
|
||||
x.MajorId,
|
||||
MajorName = x.Major!.Name
|
||||
})
|
||||
.ToListAsync(ct);
|
||||
var majors = classes
|
||||
.GroupBy(x => new { x.MajorId, x.MajorName })
|
||||
.Select(x => new { Id = x.Key.MajorId, Name = x.Key.MajorName })
|
||||
.OrderBy(x => x.Name)
|
||||
.ToList();
|
||||
return Ok(new
|
||||
{
|
||||
Grades = classes.Select(x => x.Grade).Distinct().OrderByDescending(x => x),
|
||||
Majors = majors,
|
||||
Classes = classes
|
||||
});
|
||||
}
|
||||
|
||||
[HttpPost("allocations/preview")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<ActionResult> PreviewAllocation(DormitoryAllocationPreviewRequest request, CancellationToken ct)
|
||||
@@ -544,6 +587,32 @@ public sealed class DormitoryServicesController(AppDbContext db, ICurrentUserDat
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPut("occupancies/{studentId:guid}/check-in-date")]
|
||||
[Authorize(Roles = Managers)]
|
||||
public async Task<ActionResult> UpdateCheckInDate(Guid studentId,
|
||||
UpdateDormitoryCheckInDateRequest request, CancellationToken ct)
|
||||
{
|
||||
var occupancy = await db.DormitoryOccupancies
|
||||
.FirstOrDefaultAsync(x => x.StudentId == studentId, ct);
|
||||
if (occupancy is null) return ConflictProblem("该学生当前没有在住宿舍。");
|
||||
var previousDate = occupancy.CheckInDate;
|
||||
occupancy.CheckInDate = request.CheckInDate;
|
||||
var history = await db.DormitoryStayHistories
|
||||
.Where(x => x.StudentId == studentId && x.ToBedId == occupancy.DormitoryBedId &&
|
||||
(x.Action == DormitoryStayAction.CheckIn || x.Action == DormitoryStayAction.Transfer))
|
||||
.OrderByDescending(x => x.OccurredAt)
|
||||
.FirstOrDefaultAsync(ct);
|
||||
if (history is not null) history.EffectiveDate = request.CheckInDate;
|
||||
await db.SaveChangesAsync(ct);
|
||||
return Ok(new
|
||||
{
|
||||
occupancy.StudentId,
|
||||
PreviousCheckInDate = previousDate,
|
||||
occupancy.CheckInDate,
|
||||
HistoryUpdated = history is not null
|
||||
});
|
||||
}
|
||||
|
||||
[HttpGet("applications")]
|
||||
[Authorize(Roles = Viewers)]
|
||||
public async Task<ActionResult> GetApplications(DormitoryApplicationStatus? status = null,
|
||||
@@ -886,6 +955,7 @@ public sealed record DormitoryAllocationItem(Guid StudentId, Guid BedId);
|
||||
public sealed record CommitDormitoryAllocationRequest(string? Name, DormitoryAllocationMode Mode, DateOnly CheckInDate,
|
||||
string? Reason, IReadOnlyList<DormitoryAllocationItem> Assignments, Guid? JobId = null);
|
||||
public sealed record DormitoryTransferRequest(Guid TargetBedId, DateOnly TransferDate, string? Reason);
|
||||
public sealed record UpdateDormitoryCheckInDateRequest(DateOnly CheckInDate);
|
||||
public sealed record CreateDormitoryApplicationRequest(DormitoryApplicationType Type, Guid? PreferredBuildingId,
|
||||
Guid? PreferredRoomId, string Reason);
|
||||
public sealed record DormitoryReviewRequest(bool Approved, string? Comment);
|
||||
|
||||
@@ -126,10 +126,51 @@ public sealed class DormitoryHygieneScore : EntityBase
|
||||
public decimal SafetyScore { get; set; }
|
||||
public decimal TotalScore { get; set; }
|
||||
public DormitoryHygieneGrade Grade { get; set; }
|
||||
public bool IsViolation { get; set; }
|
||||
public string? Issues { get; set; }
|
||||
public string? Notes { get; set; }
|
||||
}
|
||||
|
||||
public sealed class DormitoryHygieneCalculation : EntityBase
|
||||
{
|
||||
public Guid AcademicTermId { get; set; }
|
||||
public AcademicTerm? AcademicTerm { get; set; }
|
||||
public DormitoryHygieneCalculationStatus Status { get; set; } = DormitoryHygieneCalculationStatus.Queued;
|
||||
public Guid CreatedByUserId { get; set; }
|
||||
public ApplicationUser? CreatedByUser { get; set; }
|
||||
public int SelectedBatchCount { get; set; }
|
||||
public int StudentCount { get; set; }
|
||||
public int StudentWithoutScoreCount { get; set; }
|
||||
public DateTime? StartedAt { get; set; }
|
||||
public DateTime? CompletedAt { get; set; }
|
||||
public string? ErrorMessage { get; set; }
|
||||
public ICollection<DormitoryHygieneCalculationBatch> SelectedBatches { get; set; } = [];
|
||||
public ICollection<DormitoryHygieneStudentSummary> Results { get; set; } = [];
|
||||
}
|
||||
|
||||
public sealed class DormitoryHygieneCalculationBatch : EntityBase
|
||||
{
|
||||
public Guid DormitoryHygieneCalculationId { get; set; }
|
||||
public DormitoryHygieneCalculation? DormitoryHygieneCalculation { get; set; }
|
||||
public Guid DormitoryHygieneBatchId { get; set; }
|
||||
public DormitoryHygieneBatch? DormitoryHygieneBatch { get; set; }
|
||||
}
|
||||
|
||||
public sealed class DormitoryHygieneStudentSummary : EntityBase
|
||||
{
|
||||
public Guid DormitoryHygieneCalculationId { get; set; }
|
||||
public DormitoryHygieneCalculation? DormitoryHygieneCalculation { get; set; }
|
||||
public Guid AcademicTermId { get; set; }
|
||||
public AcademicTerm? AcademicTerm { get; set; }
|
||||
public Guid StudentId { get; set; }
|
||||
public Student? Student { get; set; }
|
||||
public decimal? AverageScore { get; set; }
|
||||
public int ExpectedBatchCount { get; set; }
|
||||
public int IncludedScoreCount { get; set; }
|
||||
public int MissingScoreCount { get; set; }
|
||||
public int ViolationCount { get; set; }
|
||||
}
|
||||
|
||||
public enum DormitoryGenderPolicy { Any = 0, Male = 1, Female = 2 }
|
||||
public enum DormitoryAllocationMode { Manual = 1, Automatic = 2 }
|
||||
public enum DormitoryAllocationBatchStatus { Queued = 1, Running = 2, PreviewReady = 3, Committed = 4, Failed = 5 }
|
||||
@@ -138,3 +179,4 @@ public enum DormitoryApplicationType { Transfer = 1, Checkout = 2 }
|
||||
public enum DormitoryApplicationStatus { Submitted = 1, CounselorApproved = 2, Approved = 3, Rejected = 4, Cancelled = 5 }
|
||||
public enum DormitoryHygieneBatchStatus { Draft = 1, Published = 2 }
|
||||
public enum DormitoryHygieneGrade { Excellent = 1, Good = 2, Qualified = 3, Unqualified = 4 }
|
||||
public enum DormitoryHygieneCalculationStatus { Queued = 1, Running = 2, Succeeded = 3, Failed = 4 }
|
||||
Loaded 3 of 21 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user