优化检录返回数据

This commit is contained in:
马梓彭 2026-09-01 14:16:11 +08:00
parent c95f85f99f
commit 61309a8974
2 changed files with 42 additions and 1 deletions

View File

@ -102,8 +102,21 @@ public partial class CheckinController
.Where(x => taskIds.Contains(x.TaskId) && x.SessionKey != null && x.Checked && !x.DeleteMark)
.ToListAsync();
// 检录员姓名按 CheckerUserId 批量查一次,避免逐条检录记录反查用户;
// AppUser.Name 为实名认证姓名,未认证的检录员退回昵称展示。
var checkerIds = records.Select(x => x.CheckerUserId).Distinct().ToList();
var checkers = checkerIds.Count == 0
? new List<YD_Event.Core.Entities.AppUser>()
: await _userRepository.AsQueryable()
.Where(x => checkerIds.Contains(x.Id) && !x.DeleteMark)
.ToListAsync();
var checkerNameMap = checkers.ToDictionary(
x => x.Id,
x => !string.IsNullOrWhiteSpace(x.Name) ? x.Name! : x.NickName ?? string.Empty);
var taskCheckinNoMap = activeTasks.ToDictionary(x => x.Id, x => x.CheckinNo);
var passedByKey = new Dictionary<string, Dictionary<int, DateTime?>>();
var checkerNameByKey = new Dictionary<string, Dictionary<int, string>>();
foreach (var record in records.Where(x => validUnitKeys.Contains(x.SessionKey!)))
{
if (!taskCheckinNoMap.TryGetValue(record.TaskId, out var checkinNo)) continue;
@ -113,6 +126,13 @@ public partial class CheckinController
passedByKey[record.SessionKey!] = rounds;
}
rounds[checkinNo] = record.CheckedTime;
if (!checkerNameByKey.TryGetValue(record.SessionKey!, out var checkerNames))
{
checkerNames = new Dictionary<int, string>();
checkerNameByKey[record.SessionKey!] = checkerNames;
}
checkerNames[checkinNo] = checkerNameMap.GetValueOrDefault(record.CheckerUserId) ?? string.Empty;
}
var firstCheckinNo = activeTasks.Count == 0 ? 1 : activeTasks.Min(x => x.CheckinNo);
@ -120,6 +140,7 @@ public partial class CheckinController
var context = new CheckinProgressContext
{
PassedByKey = passedByKey,
CheckerNameByKey = checkerNameByKey,
FirstCheckinNo = firstCheckinNo,
LastCheckinNo = lastCheckinNo,
ActiveCheckinNumbers = activeTasks.Select(x => x.CheckinNo).OrderBy(x => x).ToList()
@ -179,6 +200,9 @@ public partial class CheckinController
StageStatus = 1,
StageStatusText = "比赛中",
TeamName = ResolveTeamName(competingUnit),
UnitName = string.IsNullOrWhiteSpace(competingUnit.UnitName) ? "/" : competingUnit.UnitName,
// 秩序册列没有独立的报名类型字段,以 TeamName 是否为空区分个人与团队报名。
RegistrationTypeText = string.IsNullOrWhiteSpace(competingUnit.TeamName) ? "个人报名" : "团队报名",
AthleteNames = competingUnit.Athletes.Select(x => x.Name).ToList()
};
}
@ -230,7 +254,8 @@ public partial class CheckinController
CheckinNo = checkinNo,
CheckinName = ToCheckinName(checkinNo),
Passed = context.IsPassed(unit.UnitKey, checkinNo),
CheckedTime = context.GetCheckedTime(unit.UnitKey, checkinNo)
CheckedTime = context.GetCheckedTime(unit.UnitKey, checkinNo),
CheckerName = context.GetCheckerName(unit.UnitKey, checkinNo)
}).ToList(),
Notice = status switch
{
@ -311,7 +336,9 @@ public partial class CheckinController
/// <summary>检录进度计算的中间上下文,避免重复查询检录记录。</summary>
private sealed class CheckinProgressContext
{
// 两个字典的键结构一致:外层为秩序册列 UnitKey,内层为检次号。
public Dictionary<string, Dictionary<int, DateTime?>> PassedByKey { get; init; } = new();
public Dictionary<string, Dictionary<int, string>> CheckerNameByKey { get; init; } = new();
public int FirstCheckinNo { get; init; } = 1;
public int LastCheckinNo { get; init; } = 1;
public List<int> ActiveCheckinNumbers { get; init; } = new();
@ -326,5 +353,10 @@ public partial class CheckinController
PassedByKey.TryGetValue(unitKey, out var rounds) && rounds.TryGetValue(checkinNo, out var time)
? time
: null;
public string GetCheckerName(string unitKey, int checkinNo) =>
CheckerNameByKey.TryGetValue(unitKey, out var rounds) && rounds.TryGetValue(checkinNo, out var name)
? name
: string.Empty;
}
}

View File

@ -416,6 +416,12 @@ public class CheckinProgressStageDto
/// <summary>该阶段正在比赛的队伍名称。</summary>
public string TeamName { get; set; } = string.Empty;
/// <summary>该阶段正在比赛的单位名称。</summary>
public string UnitName { get; set; } = string.Empty;
/// <summary>报名类型展示文本:个人报名或团队报名。</summary>
public string RegistrationTypeText { get; set; } = string.Empty;
/// <summary>该阶段正在比赛的队员名称列表。</summary>
public List<string> AthleteNames { get; set; } = new();
}
@ -457,6 +463,9 @@ public class CheckinProgressRoundDto
public string CheckinName { get; set; } = string.Empty;
public bool Passed { get; set; }
public DateTime? CheckedTime { get; set; }
/// <summary>执行本次检录的检录员姓名。</summary>
public string CheckerName { get; set; } = string.Empty;
}
/// <summary>检录投屏数据。</summary>