113 lines
4.4 KiB
C#
Raw Normal View History

using YD_Event.Application.Auth;
using YD_Event.Application.Client.Dtos;
namespace YD_Event.Application.Client;
/// <summary>
/// 小程序裁判执裁报名接口。
/// </summary>
[ApiDescriptionSettings("微信小程序接口")]
[Authorize]
public class RefereeController : IDynamicApiController
{
private readonly ISqlSugarRepository<YD_Event.Core.Entities.AppUser> _userRepository;
private readonly ISqlSugarRepository<EventInfo> _eventRepository;
private readonly ISqlSugarRepository<RefereeApplication> _applicationRepository;
private readonly AuthManager _authManager;
public RefereeController(
ISqlSugarRepository<YD_Event.Core.Entities.AppUser> userRepository,
ISqlSugarRepository<EventInfo> eventRepository,
ISqlSugarRepository<RefereeApplication> applicationRepository,
AuthManager authManager)
{
_userRepository = userRepository;
_eventRepository = eventRepository;
_applicationRepository = applicationRepository;
_authManager = authManager;
}
[HttpGet("applications")]
[DisplayName("获取我的执裁报名")]
public async Task<RefereeApplicationListOutput> GetApplications()
{
var userId = await EnsureRefereeAsync();
var applications = await _applicationRepository.AsQueryable()
.Where(x => x.RefereeUserId == userId && x.Status == AvailabilityStatus.Enable)
.OrderByDescending(x => x.CreatedTime)
.ToListAsync();
var eventIds = applications.Select(x => x.EventId).Distinct().ToList();
var events = eventIds.Count == 0
? new List<EventInfo>()
: await _eventRepository.AsQueryable().Where(x => eventIds.Contains(x.Id)).ToListAsync();
var eventMap = events.ToDictionary(x => x.Id);
var rows = applications.Select(x =>
{
eventMap.TryGetValue(x.EventId, out var evt);
return new RefereeApplicationDto
{
Id = x.Id,
ApplicationId = x.Id,
EventId = x.EventId,
EventName = evt?.EventName ?? string.Empty,
CoverImage = evt?.CoverImage,
Address = evt?.DetailAddress,
EventStatus = evt == null ? 0 : (int)evt.EventStatus,
ApplicationStatus = x.ApplicationStatus,
ApplicationStatusText = ApplicationStatusText(x.ApplicationStatus),
AppliedAt = x.CreatedTime,
MatchStartTime = evt?.MatchStartTime ?? DateTime.MinValue,
MatchEndTime = evt?.MatchEndTime ?? DateTime.MinValue
};
}).ToList();
return new RefereeApplicationListOutput { Rows = rows, Total = rows.Count };
}
[HttpPost("applications/apply")]
[UnitOfWork]
[DisplayName("报名执裁")]
public async Task<object> Apply([FromBody] RefereeApplyInput input)
{
var userId = await EnsureRefereeAsync();
var evt = await _eventRepository.GetFirstAsync(x => x.Id == input.EventId && x.Status == AvailabilityStatus.Enable);
if (evt == null) throw Oops.Bah("赛事不存在");
if (evt.EventStatus != EventStatus.Enrolling) throw Oops.Bah("当前赛事不在执裁报名时间内");
var exists = await _applicationRepository.IsAnyAsync(x =>
x.EventId == input.EventId && x.RefereeUserId == userId && x.ApplicationStatus != 4);
if (exists) throw Oops.Bah("已报名执裁当前赛事");
await _applicationRepository.InsertAsync(new RefereeApplication
{
EventId = input.EventId,
RefereeUserId = userId,
ApplicationStatus = 1,
Status = AvailabilityStatus.Enable,
CreatedUserId = userId,
CreatedTime = DateTime.Now
});
return new { success = true };
}
private async Task<long> EnsureRefereeAsync()
{
var userId = _authManager.UserId;
var user = await _userRepository.GetFirstAsync(x => x.Id == userId);
if (user == null) throw Oops.Bah("用户不存在");
if (user.MiniProgramRole != MiniProgramRole.Referee) throw Oops.Bah("当前账号不是裁判身份");
return userId;
}
private static string ApplicationStatusText(int status) => status switch
{
2 => "已确认执裁",
3 => "报名未通过",
4 => "已取消",
_ => "已报名执裁"
};
}