using YD_Event.Application.Event.Dtos;
using YD_Event.Core.Entities;
using YD_Event.Core.Enum;
using AppUserEntity = YD_Event.Core.Entities.AppUser;
namespace YD_Event.Application.Event;
///
/// 参赛记录管理
///
public class EventRecordService : BaseService, ITransient
{
private readonly ISqlSugarRepository _registrationRep;
private readonly ISqlSugarRepository _eventRep;
private readonly ISqlSugarRepository _certificateRep;
private readonly ISqlSugarRepository _appUserRep;
private readonly ISqlSugarRepository _merchantRep;
public EventRecordService(ISqlSugarRepository registrationRep,
ISqlSugarRepository eventRep,
ISqlSugarRepository certificateRep,
ISqlSugarRepository appUserRep,
ISqlSugarRepository merchantRep) : base(registrationRep)
{
_registrationRep = registrationRep;
_eventRep = eventRep;
_certificateRep = certificateRep;
_appUserRep = appUserRep;
_merchantRep = merchantRep;
}
///
/// 分页查询参赛记录
///
[HttpGet("page")]
public async Task> Page([FromQuery] EventRecordPageInput input)
{
// Join EventRegistration and EventInfo
var query = _registrationRep.AsQueryable()
.LeftJoin((r, e) => r.EventId == e.Id)
.LeftJoin((r, e, u) => r.CreatedUserId == u.Id)
.LeftJoin((r, e, u, m) => r.CreatedUserId == m.CreatedUserId)
.WhereIF(!string.IsNullOrWhiteSpace(input.EventName), (r, e, u, m) => e.EventName.Contains(input.EventName))
.WhereIF(input.StartTime.HasValue, (r, e, u, m) => e.MatchStartTime >= input.StartTime)
.WhereIF(input.EndTime.HasValue, (r, e, u, m) => e.MatchStartTime <= input.EndTime)
.WhereIF(input.PaymentStatus.HasValue, (r, e, u, m) => r.PaymentStatus == input.PaymentStatus)
// Filter by User ID (AppUser or Merchant)
.WhereIF(input.AppUserId.HasValue, (r, e, u, m) => u.Id == input.AppUserId)
.WhereIF(input.MerchantId.HasValue, (r, e, u, m) => m.Id == input.MerchantId)
.OrderByDescending((r, e, u, m) => r.CreatedTime)
.Select((r, e, u, m) => new EventRecordOutput
{
Id = r.Id,
EventName = e.EventName,
EventType = e.EventType.ToString(),
StartTime = e.MatchStartTime,
EndTime = e.MatchEndTime,
PaymentStatus = r.PaymentStatus,
EventId = e.Id,
ContactName = r.ContactName,
ContactPhone = r.ContactPhone,
TeamName = r.TeamName
});
return await query.ToPagedListAsync(input);
}
///
/// 获取证书列表
///
[HttpGet("{registrationId}/certificates")]
public async Task> GetCertificates([FromRoute] long registrationId)
{
var certs = await _certificateRep.AsQueryable()
.Where(c => c.RegistrationId == registrationId)
.ToListAsync();
return certs.Select(c => new CertificateOutput
{
Id = c.Id,
Name = c.ParticipantName,
AwardLevel = GetAwardLevelName(c.AwardLevel),
CertificateType = GetCertificateTypeName(c.CertificateType),
ImageUrl = c.CertificateUrl
}).ToList();
}
private string GetAwardLevelName(int level)
{
return level switch
{
1 => "特等奖",
2 => "一等奖",
3 => "二等奖",
4 => "三等奖",
5 => "优秀奖",
_ => "未知"
};
}
private string GetCertificateTypeName(int type)
{
return type switch
{
1 => "冠军证书",
2 => "亚军证书",
3 => "季军证书",
4 => "优胜证书",
5 => "参与证书",
_ => "未知"
};
}
}