143 lines
5.8 KiB
C#

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 ISqlSugarRepository<YD_Event.Core.Entities.Referee> _refereeRepository;
private readonly ISqlSugarRepository<EventReferee> _eventRefereeRepository;
private readonly AuthManager _authManager;
private readonly IIdGenerator _idGenerator;
public RefereeController(
ISqlSugarRepository<YD_Event.Core.Entities.AppUser> userRepository,
ISqlSugarRepository<EventInfo> eventRepository,
ISqlSugarRepository<RefereeApplication> applicationRepository,
ISqlSugarRepository<YD_Event.Core.Entities.Referee> refereeRepository,
ISqlSugarRepository<EventReferee> eventRefereeRepository,
AuthManager authManager,
IIdGenerator idGenerator)
{
_userRepository = userRepository;
_eventRepository = eventRepository;
_applicationRepository = applicationRepository;
_refereeRepository = refereeRepository;
_eventRefereeRepository = eventRefereeRepository;
_authManager = authManager;
_idGenerator = idGenerator;
}
[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("已报名执裁当前赛事");
var referee = await _refereeRepository.GetFirstAsync(x => x.AppUserId == userId);
if (referee == null) throw Oops.Bah("裁判档案不存在,请先完善裁判资料");
var application = new RefereeApplication
{
Id = _idGenerator.NextId(),
EventId = input.EventId,
RefereeUserId = userId,
// 小程序报名无需后台审批,提交成功即成为赛事裁判。
ApplicationStatus = 2,
Status = AvailabilityStatus.Enable,
CreatedUserId = userId,
CreatedTime = DateTime.Now
};
await _applicationRepository.InsertAsync(application);
if (!await _eventRefereeRepository.IsAnyAsync(x => x.EventId == input.EventId && x.RefereeId == referee.Id))
{
await _eventRefereeRepository.InsertAsync(new EventReferee
{
Id = _idGenerator.NextId(),
EventId = input.EventId,
RefereeId = referee.Id,
RefereeApplicationId = application.Id,
SourceType = 2,
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 => "已取消",
_ => "待审批"
};
}