优化秒杀活动限购数量业务

This commit is contained in:
1350250847@qq.com
2026-04-23 17:17:05 +08:00
parent 9344f3750c
commit 95964219a5
6 changed files with 37 additions and 3 deletions

View File

@@ -147,6 +147,9 @@ public class OrderCreateRequest {
@NotNull(message = "租户ID不能为空")
private Integer tenantId;
@Schema(description = "秒杀活动ID")
private Long activityId;
@Schema(description = "订单商品列表")
@Valid
@NotEmpty(message = "订单商品列表不能为空")

View File

@@ -63,6 +63,9 @@ public class ShopFlashSaleActivity implements Serializable {
)
private LocalDateTime endTime;
@Schema(description = "活动限购数量")
private Integer saleLimit;
@Schema(description = "库存")
private Integer stock;

View File

@@ -306,6 +306,9 @@ public class ShopOrder implements Serializable {
@NotNull(message = "租户ID不能为空")
private Integer tenantId;
@Schema(description = "秒杀活动ID")
private Long activityId;
@Schema(description = "修改时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;

View File

@@ -51,6 +51,9 @@ public class ShopFlashSaleActivityParam extends BaseParam {
@Schema(description = "活动结束时间")
private String endTime;
@Schema(description = "活动限购数量")
private Integer saleLimit;
@Schema(description = "库存")
@QueryField(type = QueryType.EQ)
private Integer stock;

View File

@@ -25,9 +25,7 @@ import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -101,7 +99,15 @@ public class ShopFlashSaleActivityServiceImpl extends ServiceImpl<ShopFlashSaleA
//查询满足条件的活动数据
LambdaQueryChainWrapper<ShopFlashSaleActivity> activityWrapper = lambdaQuery().eq(ShopFlashSaleActivity::getStatus, 0).gt(ShopFlashSaleActivity::getStock, 0).eq(ShopFlashSaleActivity::getTenantId, tenantId)
.apply("NOW() BETWEEN start_time AND end_time");
Map<Long, Integer> activityMap = new HashMap<>();
if(!newUser){
//查询当前用户是否有下过秒杀活动订单数据【判断下单数量是否超过限制】
List<Long> activityIdList = activityWrapper.list().stream().map(ShopFlashSaleActivity::getId).collect(Collectors.toList());
LambdaQueryWrapper<ShopOrder> shopOrderWrapper = new LambdaQueryWrapper<ShopOrder>().select(ShopOrder::getOrderId, ShopOrder::getActivityId, ShopOrder::getTotalNum)
.eq(ShopOrder::getUserId, loginUser.getUserId()).in(ShopOrder::getActivityId, activityIdList).in(ShopOrder::getOrderStatus, Arrays.asList(0, 1));
activityMap = shopOrderMapper.selectList(shopOrderWrapper).stream().collect(Collectors.groupingBy(ShopOrder::getActivityId, Collectors.summingInt(ShopOrder::getTotalNum)));
activityWrapper.eq(ShopFlashSaleActivity::getDisplayType, 0);
}
activityWrapper.orderByAsc(ShopFlashSaleActivity::getSortNumber);
@@ -120,6 +126,19 @@ public class ShopFlashSaleActivityServiceImpl extends ServiceImpl<ShopFlashSaleA
}
});
}
//过滤超下单数量活动
if(!activityMap.isEmpty()){
Iterator<ShopFlashSaleActivityVO> iterator = resultVOList.iterator();
while (iterator.hasNext()){
ShopFlashSaleActivityVO vo = iterator.next();
Integer orderTotalNum = activityMap.get(vo.getId());
if(orderTotalNum != null && orderTotalNum >= vo.getSaleLimit()){
iterator.remove();
}
}
}
return resultVOList;
}

View File

@@ -53,6 +53,9 @@ public class ShopFlashSaleActivityVO implements Serializable {
@Schema(description = "活动结束时间")
private LocalDateTime endTime;
@Schema(description = "活动限购数量")
private Integer saleLimit;
@Schema(description = "库存")
private Integer stock;