using YD_Event.Application.Coupon.Dtos; using YD_Event.Core.Entities; namespace YD_Event.Application.Coupon; /// /// 优惠券服务 /// public class CouponService : BaseService, ITransient { private readonly ISqlSugarRepository _repository; private readonly ISqlSugarRepository _eventRepository; private readonly IIdGenerator _idGenerator; public CouponService( ISqlSugarRepository repository, ISqlSugarRepository eventRepository, IIdGenerator idGenerator) : base(repository) { _repository = repository; _eventRepository = eventRepository; _idGenerator = idGenerator; } /// /// 优惠券分页查询 /// [DisplayName("优惠券分页查询")] public async Task Page(CouponQueryInput input) { var query = _repository.AsQueryable() .WhereIF(!string.IsNullOrWhiteSpace(input.Name), x => x.Name.Contains(input.Name)) .WhereIF(input.Type.HasValue, x => x.Type == input.Type.Value) .OrderByDescending(x => x.CreatedTime); // 先获取实体列表 var entityList = await query.ToPagedListAsync(input); // 获取所有相关的赛事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 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(); var eventNames = "通用"; if (eventIdList.Any()) { var names = eventIdList .Where(id => eventMap.ContainsKey(id)) .Select(id => eventMap[id]); eventNames = string.Join(",", names); } return new CouponPageOutput { 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 { PageNo = entityList.PageNo, PageSize = entityList.PageSize, Total = entityList.Total, Rows = resultList, TotalPage = (int)Math.Ceiling((double)entityList.Total / entityList.PageSize) }; } /// /// 添加优惠券 /// [DisplayName("添加优惠券")] public async Task Add(AddCouponInput input) { var entity = input.Adapt(); entity.Id = _idGenerator.NextId(); // 处理赛事ID集合 if (input.EventIds != null && input.EventIds.Any()) { entity.EventIds = string.Join(",", input.EventIds); } await _repository.InsertAsync(entity); } /// /// 更新优惠券 /// [DisplayName("更新优惠券")] public async Task Update(UpdateCouponInput input) { var entity = await _repository.GetByIdAsync(input.Id); if (entity == null) throw Oops.Bah("优惠券不存在"); input.Adapt(entity); // 处理赛事ID集合 if (input.EventIds != null && input.EventIds.Any()) { entity.EventIds = string.Join(",", input.EventIds); } else { entity.EventIds = null; } await _repository.UpdateAsync(entity); } /// /// 删除优惠券 /// [DisplayName("删除优惠券")] public async Task Delete(List ids) { if (ids == null || !ids.Any()) return; await _repository.DeleteAsync(x => ids.Contains(x.Id)); } /// /// 生成兑换码 /// [DisplayName("生成兑换码")] public async Task GenerateCode(long id) { var entity = await _repository.GetByIdAsync(id); if (entity == null) throw Oops.Bah("优惠券不存在"); // 如果已经有码,直接返回;如果没有,生成新的 if (string.IsNullOrWhiteSpace(entity.RedemptionCode)) { // 简单生成一个随机码,实际业务可能需要更复杂的规则 entity.RedemptionCode = Guid.NewGuid().ToString("N").Substring(0, 8).ToUpper(); await _repository.UpdateAsync(entity); } return entity.RedemptionCode; } /// /// 获取赛事列表(下拉选择用) /// [DisplayName("获取赛事列表")] public async Task> GetEventList() { // 这里可以根据需要过滤赛事状态 return await _eventRepository.AsQueryable() .Where(x => x.DeleteMark == false) .OrderByDescending(x => x.CreatedTime) .Select(x => new EventSelectOutput { Id = x.Id, Name = x.EventName }) .ToListAsync(); } }