This commit is contained in:
gpu 2026-02-04 20:54:38 +08:00
parent e2d32829cd
commit 32c21d5a73

View File

@ -39,9 +39,10 @@ public class QyLevelService : IQyLevelService
/// <inheritdoc />
public async Task<PagedResult<QyLevelResponse>> GetQyLevelsAsync(QyLevelListRequest request)
{
var query = _dbContext.EquityLevels
// 使用 VipLevels 表(与用户端 API 保持一致)
var query = _dbContext.VipLevels
.AsNoTracking()
.Where(e => e.DeletedAt == null);
.Where(e => e.DeletedAt == null && e.Level > 0);
// 按关键词搜索(搜索名称)
if (!string.IsNullOrWhiteSpace(request.Keyword))
@ -57,7 +58,7 @@ public class QyLevelService : IQyLevelService
.Take(request.PageSize)
.ToListAsync();
var list = items.Select(MapToResponse).ToList();
var list = items.Select(MapVipLevelToResponse).ToList();
return PagedResult<QyLevelResponse>.Create(list, total, request.Page, request.PageSize);
}
@ -66,17 +67,17 @@ public class QyLevelService : IQyLevelService
/// <inheritdoc />
public async Task<QyLevelResponse?> GetQyLevelByIdAsync(int id)
{
var entity = await _dbContext.EquityLevels
var entity = await _dbContext.VipLevels
.AsNoTracking()
.FirstOrDefaultAsync(e => e.Id == id && e.DeletedAt == null);
return entity == null ? null : MapToResponse(entity);
return entity == null ? null : MapVipLevelToResponse(entity);
}
/// <inheritdoc />
public async Task<bool> UpdateQyLevelAsync(int id, QyLevelUpdateRequest request)
{
var entity = await _dbContext.EquityLevels
var entity = await _dbContext.VipLevels
.FirstOrDefaultAsync(e => e.Id == id && e.DeletedAt == null);
if (entity == null)
@ -89,7 +90,7 @@ public class QyLevelService : IQyLevelService
entity.Level = request.Level;
entity.Title = request.Title;
entity.RequiredPoints = request.RequiredPoints;
entity.Number = request.RequiredPoints;
entity.UpdatedAt = DateTime.Now;
var result = await _dbContext.SaveChangesAsync() > 0;
@ -107,7 +108,7 @@ public class QyLevelService : IQyLevelService
public async Task<PagedResult<QyLevelPrizeResponse>> GetQyLevelPrizesAsync(int qyLevelId, QyLevelPrizeListRequest request)
{
// 验证权益等级是否存在
var qyLevel = await _dbContext.EquityLevels
var qyLevel = await _dbContext.VipLevels
.AsNoTracking()
.FirstOrDefaultAsync(e => e.Id == qyLevelId && e.DeletedAt == null);
@ -116,14 +117,16 @@ public class QyLevelService : IQyLevelService
throw new BusinessException(BusinessErrorCodes.NotFound, "权益等级不存在");
}
var query = _dbContext.EquityLevelPrizes
// 使用 VipLevelRewards 表
var query = _dbContext.VipLevelRewards
.AsNoTracking()
.Where(p => p.QyLevelId == qyLevelId && p.DeletedAt == null);
.Where(p => p.VipLevelId == qyLevelId && p.DeletedAt == null);
// 按奖品类型筛选
if (request.Type.HasValue)
{
query = query.Where(p => p.Type == request.Type.Value);
var typeFilter = (byte)request.Type.Value;
query = query.Where(p => p.Type == typeFilter);
}
// 按关键词搜索(搜索标题)
@ -135,8 +138,7 @@ public class QyLevelService : IQyLevelService
var total = await query.CountAsync();
var items = await query
.OrderBy(p => p.Sort)
.ThenByDescending(p => p.CreatedAt)
.OrderBy(p => p.Id)
.Skip(request.Skip)
.Take(request.PageSize)
.ToListAsync();
@ -155,7 +157,7 @@ public class QyLevelService : IQyLevelService
.ToDictionaryAsync(c => c.Id)
: new Dictionary<int, Coupon>();
var list = items.Select(p => MapToPrizeResponse(p, coupons)).ToList();
var list = items.Select(p => MapVipLevelRewardToResponse(p, qyLevel.Level, coupons)).ToList();
return PagedResult<QyLevelPrizeResponse>.Create(list, total, request.Page, request.PageSize);
}
@ -163,7 +165,7 @@ public class QyLevelService : IQyLevelService
/// <inheritdoc />
public async Task<QyLevelPrizeResponse?> GetQyLevelPrizeByIdAsync(int prizeId)
{
var entity = await _dbContext.EquityLevelPrizes
var entity = await _dbContext.VipLevelRewards
.AsNoTracking()
.FirstOrDefaultAsync(p => p.Id == prizeId && p.DeletedAt == null);
@ -172,6 +174,12 @@ public class QyLevelService : IQyLevelService
return null;
}
// 获取等级信息
var qyLevel = await _dbContext.VipLevels
.AsNoTracking()
.FirstOrDefaultAsync(e => e.Id == entity.VipLevelId);
var level = qyLevel?.Level ?? 0;
// 如果是优惠券类型,获取优惠券信息
Dictionary<int, Coupon> coupons = new();
if (entity.Type == 1 && entity.CouponId.HasValue)
@ -185,7 +193,7 @@ public class QyLevelService : IQyLevelService
}
}
return MapToPrizeResponse(entity, coupons);
return MapVipLevelRewardToResponse(entity, level, coupons);
}
@ -193,7 +201,7 @@ public class QyLevelService : IQyLevelService
public async Task<int> CreateQyLevelPrizeAsync(int qyLevelId, QyLevelPrizeCreateRequest request)
{
// 验证权益等级是否存在
var qyLevel = await _dbContext.EquityLevels
var qyLevel = await _dbContext.VipLevels
.AsNoTracking()
.FirstOrDefaultAsync(e => e.Id == qyLevelId && e.DeletedAt == null);
@ -205,10 +213,9 @@ public class QyLevelService : IQyLevelService
// 验证请求
ValidatePrizeCreateRequest(request);
var entity = new EquityLevelPrize
var entity = new VipLevelReward
{
QyLevelId = qyLevelId,
QyLevel = qyLevel.Level,
VipLevelId = qyLevelId,
Type = (byte)request.Type,
Title = request.Title,
CouponId = request.CouponId,
@ -218,13 +225,12 @@ public class QyLevelService : IQyLevelService
ScMoney = request.ReferencePrice,
Probability = request.Probability,
ImgUrl = request.Image,
ShangId = request.ShangId,
Sort = request.Sort ?? 0,
PrizeLevelId = request.ShangId,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now
};
_dbContext.EquityLevelPrizes.Add(entity);
_dbContext.VipLevelRewards.Add(entity);
await _dbContext.SaveChangesAsync();
_logger.LogInformation("创建权益等级奖品成功: Id={Id}, QyLevelId={QyLevelId}, Type={Type}",
@ -236,7 +242,7 @@ public class QyLevelService : IQyLevelService
/// <inheritdoc />
public async Task<bool> UpdateQyLevelPrizeAsync(int prizeId, QyLevelPrizeUpdateRequest request)
{
var entity = await _dbContext.EquityLevelPrizes
var entity = await _dbContext.VipLevelRewards
.FirstOrDefaultAsync(p => p.Id == prizeId && p.DeletedAt == null);
if (entity == null)
@ -256,8 +262,7 @@ public class QyLevelService : IQyLevelService
entity.ScMoney = request.ReferencePrice;
entity.Probability = request.Probability;
entity.ImgUrl = request.Image;
entity.ShangId = request.ShangId;
entity.Sort = request.Sort ?? entity.Sort;
entity.PrizeLevelId = request.ShangId;
entity.UpdatedAt = DateTime.Now;
var result = await _dbContext.SaveChangesAsync() > 0;
@ -270,7 +275,7 @@ public class QyLevelService : IQyLevelService
/// <inheritdoc />
public async Task<bool> DeleteQyLevelPrizeAsync(int prizeId)
{
var entity = await _dbContext.EquityLevelPrizes
var entity = await _dbContext.VipLevelRewards
.FirstOrDefaultAsync(p => p.Id == prizeId && p.DeletedAt == null);
if (entity == null)
@ -309,9 +314,9 @@ public class QyLevelService : IQyLevelService
throw new BusinessException(BusinessErrorCodes.ValidationFailed, "等级名称不能为空");
}
if (request.RequiredPoints <= 0)
if (request.RequiredPoints < 0)
{
throw new BusinessException(BusinessErrorCodes.ValidationFailed, "所需欧气值必须大于0");
throw new BusinessException(BusinessErrorCodes.ValidationFailed, "所需欧气值不能为负数");
}
}
@ -464,33 +469,34 @@ public class QyLevelService : IQyLevelService
}
/// <summary>
/// 将权益等级实体映射为响应模型
/// 将 VipLevel 实体映射为响应模型
/// </summary>
private static QyLevelResponse MapToResponse(EquityLevel entity)
private static QyLevelResponse MapVipLevelToResponse(VipLevel entity)
{
return new QyLevelResponse
{
Id = entity.Id,
Level = entity.Level,
Title = entity.Title,
RequiredPoints = entity.RequiredPoints,
RequiredPoints = entity.Number,
CreatedAt = entity.CreatedAt,
UpdatedAt = entity.UpdatedAt
};
}
/// <summary>
/// 将奖品实体映射为响应模型
/// 将 VipLevelReward 实体映射为奖品响应模型
/// </summary>
private static QyLevelPrizeResponse MapToPrizeResponse(EquityLevelPrize entity, Dictionary<int, Coupon> coupons)
private static QyLevelPrizeResponse MapVipLevelRewardToResponse(VipLevelReward entity, int qyLevel, Dictionary<int, Coupon> coupons)
{
var typeValue = (int)entity.Type;
var response = new QyLevelPrizeResponse
{
Id = entity.Id,
QyLevelId = entity.QyLevelId,
QyLevel = entity.QyLevel,
Type = entity.Type,
TypeName = PrizeTypeNames.GetValueOrDefault(entity.Type, "未知"),
QyLevelId = entity.VipLevelId,
QyLevel = qyLevel,
Type = typeValue,
TypeName = PrizeTypeNames.GetValueOrDefault(typeValue, "未知"),
Title = entity.Title,
CouponId = entity.CouponId,
Quantity = entity.ZNum,
@ -500,13 +506,13 @@ public class QyLevelService : IQyLevelService
Probability = entity.Probability,
Image = entity.ImgUrl,
PrizeCode = entity.PrizeCode,
Sort = entity.Sort,
Sort = 0,
CreatedAt = entity.CreatedAt,
UpdatedAt = entity.UpdatedAt
};
// 如果是优惠券类型,添加优惠券信息
if (entity.Type == 1 && entity.CouponId.HasValue &&
if (typeValue == 1 && entity.CouponId.HasValue &&
coupons.TryGetValue(entity.CouponId.Value, out var coupon))
{
response.Coupon = new CouponInfo