using YD_Event.Application.Auth; using YD_Event.Application.Client.Dtos; namespace YD_Event.Application.Client; /// /// 小程序裁判执裁报名接口。 /// [ApiDescriptionSettings("微信小程序接口")] [Authorize] public class RefereeController : IDynamicApiController { private readonly ISqlSugarRepository _userRepository; private readonly ISqlSugarRepository _eventRepository; private readonly ISqlSugarRepository _applicationRepository; private readonly AuthManager _authManager; public RefereeController( ISqlSugarRepository userRepository, ISqlSugarRepository eventRepository, ISqlSugarRepository applicationRepository, AuthManager authManager) { _userRepository = userRepository; _eventRepository = eventRepository; _applicationRepository = applicationRepository; _authManager = authManager; } [HttpGet("applications")] [DisplayName("获取我的执裁报名")] public async Task 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() : 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 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 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 => "已取消", _ => "已报名执裁" }; }