优化秒杀活动用户下单能力业务
This commit is contained in:
@@ -69,7 +69,7 @@ public class ShopFlashSaleActivity implements Serializable {
|
||||
@Schema(description = "库存")
|
||||
private Integer stock;
|
||||
|
||||
@Schema(description = "展示类型,0:普通用户,1:新用户")
|
||||
@Schema(description = "展示类型,0:普通用户,1:新用户 2:老用户")
|
||||
private Integer displayType;
|
||||
|
||||
@Schema(description = "备注")
|
||||
|
||||
@@ -100,13 +100,14 @@ public class ShopFlashSaleActivityServiceImpl extends ServiceImpl<ShopFlashSaleA
|
||||
@Override
|
||||
public List<ShopFlashSaleActivityVO> getMyActive(Integer tenantId, Integer popFlag) {
|
||||
List<ShopFlashSaleActivityVO> resultVOList = new ArrayList<>();
|
||||
//1.判断当前登录用户
|
||||
User loginUser = LoginUserUtil.getLoginUser();
|
||||
if(loginUser == null){
|
||||
throw new BusinessException(GlobalErrorCodeConstants.UNAUTHORIZED.getMsg());
|
||||
}
|
||||
|
||||
Boolean newUser = true;
|
||||
//判断是否为新用户【只要未成功下单都判定为新用户】
|
||||
//2.判断是否为新用户【只要未成功下单都判定为新用户】
|
||||
LambdaQueryWrapper<ShopOrder> shopOrderLambdaQueryWrapper = new LambdaQueryWrapper<ShopOrder>().eq(ShopOrder::getUserId, loginUser.getUserId()).eq(ShopOrder::getPayStatus, 1)
|
||||
.in(ShopOrder::getOrderStatus, Arrays.asList(0, 1));
|
||||
List<ShopOrder> shopOrderList = shopOrderMapper.selectList(shopOrderLambdaQueryWrapper);
|
||||
@@ -114,55 +115,65 @@ public class ShopFlashSaleActivityServiceImpl extends ServiceImpl<ShopFlashSaleA
|
||||
newUser = false;
|
||||
}
|
||||
|
||||
//查询满足条件的活动数据
|
||||
LambdaQueryChainWrapper<ShopFlashSaleActivity> activityWrapper = lambdaQuery().eq(ShopFlashSaleActivity::getStatus, 0).gt(ShopFlashSaleActivity::getStock, 0).eq(ShopFlashSaleActivity::getTenantId, tenantId)
|
||||
.apply("NOW() BETWEEN start_time AND end_time");
|
||||
//3.查询满足条件的活动数据
|
||||
LambdaQueryChainWrapper<ShopFlashSaleActivity> activityWrapper = lambdaQuery().eq(ShopFlashSaleActivity::getStatus, 0).gt(ShopFlashSaleActivity::getStock, 0)
|
||||
.eq(ShopFlashSaleActivity::getTenantId, tenantId).apply("NOW() BETWEEN start_time AND end_time");
|
||||
if(popFlag != null){
|
||||
activityWrapper.eq(ShopFlashSaleActivity::getPopFlag, popFlag);
|
||||
}
|
||||
activityWrapper.orderByAsc(ShopFlashSaleActivity::getSortNumber);
|
||||
List<ShopFlashSaleActivity> activityList = activityWrapper.list();
|
||||
|
||||
//4.查询老用户已下单爆款活动订单数据
|
||||
Map<Integer, Integer> activityMap = new HashMap<>();
|
||||
if(!newUser){
|
||||
//查询当前用户是否有下过秒杀活动订单数据【判断下单数量是否超过限制】
|
||||
List<Integer> activityIdList = activityWrapper.list().stream().map(ShopFlashSaleActivity::getId).collect(Collectors.toList());
|
||||
List<Integer> activityIdList = activityList.stream().map(ShopFlashSaleActivity::getId).collect(Collectors.toList());
|
||||
if(CollectionUtils.isNotEmpty(activityIdList)){
|
||||
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);
|
||||
List<ShopFlashSaleActivity> activityList = activityWrapper.list();
|
||||
|
||||
//5.存在活动爆款,则判断用户购买能力
|
||||
if(CollectionUtils.isNotEmpty(activityList)){
|
||||
resultVOList = BeanUtil.copyToList(activityList, ShopFlashSaleActivityVO.class);
|
||||
List<Integer> goodsIdList = resultVOList.stream().map(ShopFlashSaleActivityVO::getGoodsId).distinct().collect(Collectors.toList());
|
||||
List<ShopGoods> shopGoods = shopGoodsMapper.selectBatchIds(goodsIdList);
|
||||
resultVOList.forEach(activity -> {
|
||||
ShopGoods shopGood = shopGoods.stream().filter(goods -> activity.getGoodsId().equals(goods.getGoodsId())).findFirst().orElse(null);
|
||||
if(shopGood != null){
|
||||
activity.setGoodsPrice(shopGood.getPrice());
|
||||
activity.setGoodsTotalPrice(shopGood.getPrice().multiply(new BigDecimal(activity.getNum())));
|
||||
activity.setGoodsName(shopGood.getName());
|
||||
activity.setImage(shopGood.getImage());
|
||||
activity.setUnitName(shopGood.getUnitName());
|
||||
}
|
||||
});
|
||||
}
|
||||
for(ShopFlashSaleActivityVO activityVO : resultVOList){
|
||||
if(newUser){
|
||||
if(activityVO.getDisplayType() == 2){
|
||||
activityVO.setEnable(false);
|
||||
activityVO.setEnableMsg("当前商品仅限老用户购买!");
|
||||
}
|
||||
}else {
|
||||
if(activityVO.getDisplayType() == 1){
|
||||
activityVO.setEnable(false);
|
||||
activityVO.setEnableMsg("当前商品仅限新用户购买!");
|
||||
}
|
||||
|
||||
//过滤超下单数量活动
|
||||
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();
|
||||
if(activityVO.getEnable()){
|
||||
//超下单数量活动不可选
|
||||
Integer orderTotalNum = activityMap.get(activityVO.getId());
|
||||
if(orderTotalNum != null && orderTotalNum >= activityVO.getSaleLimit()){
|
||||
activityVO.setEnable(false);
|
||||
activityVO.setEnableMsg("当前商品您购买次数已达上限!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//5.2 回显商品信息
|
||||
ShopGoods shopGood = shopGoods.stream().filter(goods -> activityVO.getGoodsId().equals(goods.getGoodsId())).findFirst().orElse(null);
|
||||
if(shopGood != null){
|
||||
activityVO.setGoodsPrice(shopGood.getPrice());
|
||||
activityVO.setGoodsTotalPrice(shopGood.getPrice().multiply(new BigDecimal(activityVO.getNum())));
|
||||
activityVO.setGoodsName(shopGood.getName());
|
||||
activityVO.setImage(shopGood.getImage());
|
||||
activityVO.setUnitName(shopGood.getUnitName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return resultVOList;
|
||||
}
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ public class ShopFlashSaleActivityVO implements Serializable {
|
||||
@Schema(description = "库存")
|
||||
private Integer stock;
|
||||
|
||||
@Schema(description = "展示类型,0:普通用户,1:新用户")
|
||||
@Schema(description = "展示类型,0:普通用户,1:新用户 2:老用户")
|
||||
private Integer displayType;
|
||||
|
||||
@Schema(description = "备注")
|
||||
@@ -77,6 +77,12 @@ public class ShopFlashSaleActivityVO implements Serializable {
|
||||
@Schema(description = "是否弹窗 0-否 1-是")
|
||||
private Integer popFlag;
|
||||
|
||||
@Schema(description = "是否可选")
|
||||
private Boolean enable = true;
|
||||
|
||||
@Schema(description = "不可购买原因!")
|
||||
private String enableMsg;
|
||||
|
||||
@Schema(description = "租户id")
|
||||
private Integer tenantId;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user