YD_Event.Admin/YD_Event.Application/Event/EventRecordService.cs

157 lines
5.7 KiB
C#
Raw Normal View History

2026-03-08 15:46:19 +08:00
using YD_Event.Application.Event.Dtos;
2026-03-06 12:10:09 +08:00
using YD_Event.Core.Entities;
using YD_Event.Core.Enum;
2026-03-06 12:29:07 +08:00
using AppUserEntity = YD_Event.Core.Entities.AppUser;
2026-04-13 06:58:21 +08:00
using SysUserEntity = YD_Event.Core.Entities.SysUser;
using MerchantEntity = YD_Event.Core.Entities.Merchant;
2026-03-06 12:10:09 +08:00
2026-03-08 15:46:19 +08:00
namespace YD_Event.Application.Event;
2026-03-06 12:10:09 +08:00
/// <summary>
/// 参赛记录管理
/// </summary>
2026-03-08 15:46:19 +08:00
public class EventRecordService : BaseService<EventRegistration>, ITransient
2026-03-06 12:10:09 +08:00
{
private readonly ISqlSugarRepository<EventRegistration> _registrationRep;
private readonly ISqlSugarRepository<EventInfo> _eventRep;
private readonly ISqlSugarRepository<EventCertificate> _certificateRep;
2026-03-06 12:29:07 +08:00
private readonly ISqlSugarRepository<AppUserEntity> _appUserRep;
2026-04-13 06:58:21 +08:00
private readonly ISqlSugarRepository<SysUserEntity> _sysUserRep;
private readonly ISqlSugarRepository<MerchantEntity> _merchantRep;
2026-03-06 12:10:09 +08:00
public EventRecordService(ISqlSugarRepository<EventRegistration> registrationRep,
ISqlSugarRepository<EventInfo> eventRep,
2026-03-06 12:29:07 +08:00
ISqlSugarRepository<EventCertificate> certificateRep,
ISqlSugarRepository<AppUserEntity> appUserRep,
2026-04-13 06:58:21 +08:00
ISqlSugarRepository<SysUserEntity> sysUserRep,
ISqlSugarRepository<MerchantEntity> merchantRep) : base(registrationRep)
2026-03-06 12:10:09 +08:00
{
_registrationRep = registrationRep;
_eventRep = eventRep;
_certificateRep = certificateRep;
2026-03-06 12:29:07 +08:00
_appUserRep = appUserRep;
2026-04-13 06:58:21 +08:00
_sysUserRep = sysUserRep;
2026-03-06 12:29:07 +08:00
_merchantRep = merchantRep;
2026-03-06 12:10:09 +08:00
}
/// <summary>
/// 分页查询参赛记录
/// </summary>
[HttpGet("page")]
public async Task<PageResult<EventRecordOutput>> Page([FromQuery] EventRecordPageInput input)
{
2026-04-13 06:58:21 +08:00
// 参数验证
ValidateInput(input);
2026-03-06 12:10:09 +08:00
// Join EventRegistration and EventInfo
var query = _registrationRep.AsQueryable()
.LeftJoin<EventInfo>((r, e) => r.EventId == e.Id)
.LeftJoin<AppUserEntity>((r, e, u) => r.CreatedUserId == u.Id)
2026-04-13 06:58:21 +08:00
.LeftJoin<SysUserEntity>((r, e, u, s) => r.CreatedUserId == s.Id)
.LeftJoin<MerchantEntity>((r, e, u, s, m) => r.CreatedUserId == m.CreatedUserId)
.WhereIF(!string.IsNullOrWhiteSpace(input.EventName), (r, e, u, s, m) => e.EventName.Contains(input.EventName))
.WhereIF(input.StartTime.HasValue, (r, e, u, s, m) => e.MatchStartTime >= input.StartTime)
.WhereIF(input.EndTime.HasValue, (r, e, u, s, m) => e.MatchStartTime <= input.EndTime)
.WhereIF(input.PaymentStatus.HasValue, (r, e, u, s, m) => r.PaymentStatus == input.PaymentStatus)
// Filter by User ID (AppUser, SysUser, or Merchant)
.WhereIF(input.AppUserId.HasValue, (r, e, u, s, m) => u.Id == input.AppUserId)
.WhereIF(input.SysUserId.HasValue, (r, e, u, s, m) => s.Id == input.SysUserId)
.WhereIF(input.MerchantId.HasValue, (r, e, u, s, m) => m.CreatedUserId == input.MerchantId)
.OrderByDescending((r, e, u, s, m) => r.CreatedTime)
.Select((r, e, u, s, m) => new EventRecordOutput
2026-03-06 12:10:09 +08:00
{
Id = r.Id,
EventName = e.EventName,
EventType = e.EventType.ToString(),
StartTime = e.MatchStartTime,
EndTime = e.MatchEndTime,
2026-04-09 23:12:51 +08:00
PaymentStatus = r.PaymentStatus,
2026-03-06 12:29:07 +08:00
EventId = e.Id,
ContactName = r.ContactName,
ContactPhone = r.ContactPhone,
TeamName = r.TeamName
2026-03-06 12:10:09 +08:00
});
return await query.ToPagedListAsync(input);
}
2026-04-13 06:58:21 +08:00
/// <summary>
/// 验证输入参数
/// </summary>
private void ValidateInput(EventRecordPageInput input)
{
// 检查是否同时提供了多个用户ID类型
var userTypeCount = 0;
if (input.AppUserId.HasValue) userTypeCount++;
if (input.SysUserId.HasValue) userTypeCount++;
if (input.MerchantId.HasValue) userTypeCount++;
if (userTypeCount > 1)
{
throw Oops.Bah("只能提供一种用户类型ID参数(AppUserId、SysUserId或MerchantId)");
}
// 验证用户ID有效性
if (input.AppUserId.HasValue && input.AppUserId.Value <= 0)
{
throw Oops.Bah("普通用户ID无效");
}
if (input.SysUserId.HasValue && input.SysUserId.Value <= 0)
{
throw Oops.Bah("系统用户ID无效");
}
if (input.MerchantId.HasValue && input.MerchantId.Value <= 0)
{
throw Oops.Bah("商户ID无效");
}
}
2026-03-06 12:10:09 +08:00
/// <summary>
/// 获取证书列表
/// </summary>
[HttpGet("{registrationId}/certificates")]
public async Task<List<CertificateOutput>> 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 => "参与证书",
_ => "未知"
};
}
}