163 lines
5.1 KiB
C#
Raw Normal View History

2026-02-07 16:03:52 +08:00
using YD_Event.Application.Coupon.Dtos;
using YD_Event.Core.Entities;
namespace YD_Event.Application.Coupon;
/// <summary>
/// 优惠券服务
/// </summary>
2026-02-08 18:09:05 +08:00
public class CouponService : BaseService<YD_Event.Core.Entities.Coupon>, ITransient
2026-02-07 16:03:52 +08:00
{
private readonly ISqlSugarRepository<YD_Event.Core.Entities.Coupon> _repository;
private readonly ISqlSugarRepository<EventInfo> _eventRepository;
private readonly IIdGenerator _idGenerator;
public CouponService(
ISqlSugarRepository<YD_Event.Core.Entities.Coupon> repository,
ISqlSugarRepository<EventInfo> eventRepository,
IIdGenerator idGenerator) : base(repository)
{
_repository = repository;
_eventRepository = eventRepository;
_idGenerator = idGenerator;
}
/// <summary>
/// 优惠券分页查询
/// </summary>
[DisplayName("优惠券分页查询")]
public async Task<CouponPageResult> 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<CouponPageOutput>();
foreach (var item in entityList.Rows)
{
var output = item.Adapt<CouponPageOutput>();
output.EventIds = string.IsNullOrEmpty(item.EventIds)
? new List<long>()
: 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)
};
}
/// <summary>
/// 添加优惠券
/// </summary>
[DisplayName("添加优惠券")]
public async Task Add(AddCouponInput input)
{
var entity = input.Adapt<YD_Event.Core.Entities.Coupon>();
entity.Id = _idGenerator.NextId();
// 处理赛事ID集合
if (input.EventIds != null && input.EventIds.Any())
{
entity.EventIds = string.Join(",", input.EventIds);
}
await _repository.InsertAsync(entity);
}
/// <summary>
/// 更新优惠券
/// </summary>
[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);
}
/// <summary>
/// 删除优惠券
/// </summary>
[DisplayName("删除优惠券")]
public async Task Delete(List<long> ids)
{
if (ids == null || !ids.Any()) return;
await _repository.DeleteAsync(x => ids.Contains(x.Id));
}
/// <summary>
/// 生成兑换码
/// </summary>
[DisplayName("生成兑换码")]
public async Task<string> 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;
}
/// <summary>
/// 获取赛事列表(下拉选择用)
/// </summary>
[DisplayName("获取赛事列表")]
public async Task<List<EventSelectOutput>> 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();
}
}