using YD_Event.Application.Coupon.Dtos; using YD_Event.Core.Entities; namespace YD_Event.Application.Coupon; /// /// 优惠券服务 /// public class CouponService : BaseService, ICouponService { 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); // 转换并填充赛事名称 var resultList = new List(); foreach (var item in entityList.Rows) { var output = item.Adapt(); output.EventIds = string.IsNullOrEmpty(item.EventIds) ? new List() : item.EventIds.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToList(); if (output.EventIds.Any()) { var eventNames = await _eventRepository.AsQueryable() .Where(x => output.EventIds.Contains(x.Id)) .Select(x => x.EventName) .ToListAsync(); output.EventNames = string.Join(",", eventNames); } else { output.EventNames = "通用"; } resultList.Add(output); } 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(); } }