99 lines
3.9 KiB
C#
99 lines
3.9 KiB
C#
|
|
using YD_Event.Application.Client.Dtos;
|
||
|
|
|
||
|
|
namespace YD_Event.Application.Client;
|
||
|
|
|
||
|
|
public partial class EventClientController
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 获取已有成绩的赛事。
|
||
|
|
/// </summary>
|
||
|
|
[HttpGet("scores/events")]
|
||
|
|
[AllowAnonymous]
|
||
|
|
[DisplayName("获取成绩赛事")]
|
||
|
|
public async Task<List<ScoreEventDto>> GetScoreEvents(
|
||
|
|
[FromServices] ISqlSugarRepository<EventResult> resultRepository,
|
||
|
|
[FromServices] ISqlSugarRepository<EventInfo> eventRepository)
|
||
|
|
{
|
||
|
|
var eventIds = await resultRepository.AsQueryable()
|
||
|
|
.Where(x => x.Status == AvailabilityStatus.Enable)
|
||
|
|
.Select(x => x.EventId)
|
||
|
|
.Distinct()
|
||
|
|
.ToListAsync();
|
||
|
|
|
||
|
|
if (eventIds.Count == 0) return new List<ScoreEventDto>();
|
||
|
|
|
||
|
|
var events = await eventRepository.AsQueryable()
|
||
|
|
.Where(x => eventIds.Contains(x.Id) && x.Status == AvailabilityStatus.Enable)
|
||
|
|
.OrderByDescending(x => x.MatchStartTime)
|
||
|
|
.ToListAsync();
|
||
|
|
|
||
|
|
return events.Select(x => new ScoreEventDto { Id = x.Id, Name = x.EventName }).ToList();
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 按赛事、姓名或报名人员ID查询成绩。
|
||
|
|
/// </summary>
|
||
|
|
[HttpGet("scores/query")]
|
||
|
|
[AllowAnonymous]
|
||
|
|
[DisplayName("查询赛事成绩")]
|
||
|
|
public async Task<ScoreQueryOutput> QueryScores(
|
||
|
|
[FromQuery] ScoreQueryInput input,
|
||
|
|
[FromServices] ISqlSugarRepository<EventResult> resultRepository,
|
||
|
|
[FromServices] ISqlSugarRepository<EventInfo> eventRepository,
|
||
|
|
[FromServices] ISqlSugarRepository<YD_Event.Core.Entities.Project> projectRepository)
|
||
|
|
{
|
||
|
|
var query = resultRepository.AsQueryable()
|
||
|
|
.Where(x => x.Status == AvailabilityStatus.Enable)
|
||
|
|
.WhereIF(input.EventId.HasValue, x => x.EventId == input.EventId.Value);
|
||
|
|
|
||
|
|
if (!string.IsNullOrWhiteSpace(input.Keyword))
|
||
|
|
{
|
||
|
|
var keyword = input.Keyword.Trim();
|
||
|
|
if (long.TryParse(keyword, out var personId))
|
||
|
|
{
|
||
|
|
query = query.Where(x => x.ParticipantName.Contains(keyword) || x.RegistrationPersonId == personId);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
query = query.Where(x => x.ParticipantName.Contains(keyword) || x.GroupName.Contains(keyword));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
var results = await query.OrderBy(x => x.Rank).OrderByDescending(x => x.CreatedTime).ToListAsync();
|
||
|
|
if (results.Count == 0) return new ScoreQueryOutput();
|
||
|
|
|
||
|
|
var eventIds = results.Select(x => x.EventId).Distinct().ToList();
|
||
|
|
var projectIds = results.Select(x => x.ProjectId).Distinct().ToList();
|
||
|
|
var events = await eventRepository.AsQueryable().Where(x => eventIds.Contains(x.Id)).ToListAsync();
|
||
|
|
var projects = await projectRepository.AsQueryable().Where(x => projectIds.Contains(x.Id)).ToListAsync();
|
||
|
|
var eventMap = events.ToDictionary(x => x.Id, x => x.EventName);
|
||
|
|
var projectMap = projects.ToDictionary(x => x.Id, x => x.Name);
|
||
|
|
|
||
|
|
var rows = results.Select(x => new ClientScoreDto
|
||
|
|
{
|
||
|
|
Id = x.Id,
|
||
|
|
EventId = x.EventId,
|
||
|
|
EventName = eventMap.GetValueOrDefault(x.EventId, string.Empty),
|
||
|
|
AthleteName = x.ParticipantName,
|
||
|
|
AthleteNo = FormatBibNo(x.RegistrationPersonId),
|
||
|
|
Category = projectMap.GetValueOrDefault(x.ProjectId, x.ProjectTypeName ?? string.Empty),
|
||
|
|
GroupName = x.GroupName,
|
||
|
|
Rank = x.Rank,
|
||
|
|
TotalScore = x.Score,
|
||
|
|
Status = "成绩有效",
|
||
|
|
JudgeScores = new List<JudgeScoreDto>
|
||
|
|
{
|
||
|
|
new() { Label = "最终成绩", Score = x.Score }
|
||
|
|
}
|
||
|
|
}).ToList();
|
||
|
|
|
||
|
|
return new ScoreQueryOutput { Rows = rows, Total = rows.Count };
|
||
|
|
}
|
||
|
|
|
||
|
|
private static string FormatBibNo(long personId)
|
||
|
|
{
|
||
|
|
var value = personId.ToString();
|
||
|
|
return value.Length > 6 ? value[^6..] : value.PadLeft(6, '0');
|
||
|
|
}
|
||
|
|
}
|