diff --git a/YD_Event.Application/Client/MyController.cs b/YD_Event.Application/Client/MyController.cs index 57aaf7f..0c320f0 100644 --- a/YD_Event.Application/Client/MyController.cs +++ b/YD_Event.Application/Client/MyController.cs @@ -489,69 +489,96 @@ public class MyController : IDynamicApiController { var userId = GetCurrentUserId(); - var query = _orderRepository.AsQueryable() - .LeftJoin((o, r) => o.RegistrationId == r.Id) - .Where((o, r) => o.Id == id && o.CreatedUserId == userId && !o.DeleteMark) - .Select((o, r) => new { Order = o, Registration = r }); - - var orderInfo = await query.FirstAsync(); - if (orderInfo == null || orderInfo.Order == null) throw Oops.Bah("订单不存在"); + // 1. 先查询订单 + var order = await _orderRepository.GetFirstAsync(o => o.Id == id && o.CreatedUserId == userId && !o.DeleteMark); + if (order == null) throw Oops.Bah("订单不存在"); + // 2. 查询关联的报名信息 + EventRegistration? registration = null; + if (order.RegistrationId > 0) + { + registration = await _registrationRepository.GetFirstAsync(r => r.Id == order.RegistrationId && !r.DeleteMark); + } + + // 3. 获取赛事名称 var eventName = await _eventRepository.AsQueryable() - .Where(e => e.Id == orderInfo.Order.EventId) + .Where(e => e.Id == order.EventId) .Select(e => e.EventName) .FirstAsync(); - var items = await _registrationItemRepository.AsQueryable() - .Where(i => i.RegistrationId == orderInfo.Order.RegistrationId && !i.DeleteMark) - .Select(i => new MyOrderItemDto - { - ProjectName = i.ProjectName, - GroupName = i.GroupName, - Fee = i.TotalPrice - }) - .ToListAsync(); + // 4. 获取报名项目列表(如果有关联的报名记录) + List items = new List(); + if (order.RegistrationId > 0) + { + items = await _registrationItemRepository.AsQueryable() + .Where(i => i.RegistrationId == order.RegistrationId && !i.DeleteMark) + .Select(i => new MyOrderItemDto + { + ProjectName = i.ProjectName, + GroupName = i.GroupName, + Fee = i.TotalPrice + }) + .ToListAsync(); + } return new MyOrderDetailDto { - Id = orderInfo.Order.Id, - OrderNo = orderInfo.Order.OrderNo, + Id = order.Id, + OrderNo = order.OrderNo, EventName = eventName, - ActualAmount = orderInfo.Order.ActualAmount, - RefundAmount = orderInfo.Order.OrderType == 2 ? orderInfo.Order.ActualAmount : null, - RefundTime = orderInfo.Order.OrderType == 2 ? orderInfo.Order.PaymentTime : null, - OrderStatus = orderInfo.Order.OrderStatus, - PaymentTime = orderInfo.Order.PaymentTime, - PaymentMethod = orderInfo.Registration?.PaymentMethod ?? 0, + ActualAmount = order.ActualAmount, + RefundAmount = order.OrderType == 2 ? order.ActualAmount : null, + RefundTime = order.OrderType == 2 ? order.PaymentTime : null, + OrderStatus = order.OrderStatus, + PaymentTime = order.PaymentTime, + PaymentMethod = registration?.PaymentMethod ?? 0, Items = items }; } /// - /// 我的参赛记录(暂不对外提供) + /// 我的参赛记录 /// /// - [ApiDescriptionSettings(false)] - [NonAction] + [HttpGet("registrations")] + [DisplayName("我的参赛记录")] public async Task> GetRegistrations([FromQuery] MyPageInput input) { var userId = GetCurrentUserId(); - var query = _registrationRepository.AsQueryable() - .LeftJoin((r, e) => r.EventId == e.Id) + + // 1. 先分页查询报名记录 + var regQuery = _registrationRepository.AsQueryable() .Where(r => r.CreatedUserId == userId && !r.DeleteMark) - .Select((r, e) => new MyRegistrationDto + .OrderByDescending(r => r.RegistrationTime); + + var pagedRegs = await regQuery.ToPagedListAsync(input); + + // 2. 获取所有赛事ID,然后批量查询赛事信息 + var eventIds = pagedRegs.Rows.Select(r => r.EventId).Distinct().ToList(); + var eventList = await _eventRepository.AsQueryable() + .Where(e => eventIds.Contains(e.Id)) + .ToListAsync(); + var eventsDict = eventList.ToDictionary(e => e.Id, e => e.EventName); + + // 3. 构建 DTO 列表 + var result = new PageResult + { + PageNo = pagedRegs.PageNo, + PageSize = pagedRegs.PageSize, + Total = pagedRegs.Total, + Pages = pagedRegs.Pages, + Rows = pagedRegs.Rows.Select(r => new MyRegistrationDto { Id = r.Id, - EventName = e.EventName, + EventName = eventsDict.ContainsKey(r.EventId) ? eventsDict[r.EventId] : "", TeamName = r.TeamName, Status = r.PaymentStatus, RegistrationTime = r.RegistrationTime, ProjectNames = "" - }) - .OrderByDescending(r => r.RegistrationTime); + }).ToList() + }; - var result = await query.ToPagedListAsync(input); - + // 4. 填充项目名称 if (result.Rows.Any()) { var registrationIds = result.Rows.Select(r => r.Id).ToList(); diff --git a/YD_Event.Application/YD_Event.Application.xml b/YD_Event.Application/YD_Event.Application.xml index a10381e..ec01904 100644 --- a/YD_Event.Application/YD_Event.Application.xml +++ b/YD_Event.Application/YD_Event.Application.xml @@ -3622,7 +3622,7 @@ - 我的参赛记录(暂不对外提供) + 我的参赛记录