From 534bd5f40fe1d3b1ede4e331fd781d37354dd3b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A9=AC=E6=A2=93=E5=BD=AD?= Date: Mon, 2 Mar 2026 22:45:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=8A=B5=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- YD_Event.Application/Coupon/CouponService.cs | 59 ++++++++++++++------ 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/YD_Event.Application/Coupon/CouponService.cs b/YD_Event.Application/Coupon/CouponService.cs index 9b4f3c2..d6526a3 100644 --- a/YD_Event.Application/Coupon/CouponService.cs +++ b/YD_Event.Application/Coupon/CouponService.cs @@ -36,29 +36,56 @@ public class CouponService : BaseService, ITransi // 先获取实体列表 var entityList = await query.ToPagedListAsync(input); - // 转换并填充赛事名称 - var resultList = new List(); - foreach (var item in entityList.Rows) + // 获取所有相关的赛事ID + var allEventIds = entityList.Rows + .Where(x => !string.IsNullOrEmpty(x.EventIds)) + .SelectMany(x => x.EventIds.Split(',', StringSplitOptions.RemoveEmptyEntries)) + .Select(long.Parse) + .Distinct() + .ToList(); + + var eventMap = new Dictionary(); + if (allEventIds.Any()) { - var output = item.Adapt(); - output.EventIds = string.IsNullOrEmpty(item.EventIds) + var events = await _eventRepository.AsQueryable() + .Where(x => allEventIds.Contains(x.Id)) + .ToListAsync(); + eventMap = events.ToDictionary(x => x.Id, x => x.EventName); + } + + // 转换并填充赛事名称 + var resultList = entityList.Rows.Select(item => + { + var eventIdList = string.IsNullOrEmpty(item.EventIds) ? new List() : item.EventIds.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToList(); - if (output.EventIds.Any()) + var eventNames = "通用"; + if (eventIdList.Any()) { - var eventNames = await _eventRepository.AsQueryable() - .Where(x => output.EventIds.Contains(x.Id)) - .Select(x => x.EventName) - .ToListAsync(); - output.EventNames = string.Join(",", eventNames); + var names = eventIdList + .Where(id => eventMap.ContainsKey(id)) + .Select(id => eventMap[id]); + eventNames = string.Join(",", names); } - else + + return new CouponPageOutput { - output.EventNames = "通用"; - } - resultList.Add(output); - } + Id = item.Id, + Name = item.Name, + Type = item.Type, + Value = item.Value, + StartTime = item.StartTime, + EndTime = item.EndTime, + RedemptionCode = item.RedemptionCode, + TotalQuantity = item.TotalQuantity, + ClaimedQuantity = item.ClaimedQuantity, + UsedQuantity = item.UsedQuantity, + CreatedTime = item.CreatedTime, + EventIds = eventIdList, + EventNames = eventNames + }; + }).ToList(); return new CouponPageResult {