修复秒杀库存前后端显示不一致问题

This commit is contained in:
paulGao 2021-12-30 17:00:32 +08:00
parent 194d0b6ac2
commit d4ca3d5817
3 changed files with 62 additions and 41 deletions

View File

@ -54,6 +54,14 @@ public interface SeckillService extends AbstractPromotionsService<Seckill> {
*/ */
void updateEsGoodsSeckill(Seckill seckill, List<SeckillApply> seckillApplies); void updateEsGoodsSeckill(Seckill seckill, List<SeckillApply> seckillApplies);
/**
* 删除商品索引限时抢购信息
*
* @param seckill 限时抢购信息
* @param skuIds 商品skuId列表
*/
void deleteEsGoodsSeckill(Seckill seckill, List<String> skuIds);
/** /**
* 设置秒杀活动的每个参与活动商品的详细时间 * 设置秒杀活动的每个参与活动商品的详细时间
* *

View File

@ -4,7 +4,6 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.cache.Cache; import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix; import cn.lili.cache.CachePrefix;
import cn.lili.common.enums.PromotionTypeEnum; import cn.lili.common.enums.PromotionTypeEnum;
@ -27,7 +26,6 @@ import cn.lili.modules.promotion.mapper.SeckillApplyMapper;
import cn.lili.modules.promotion.service.PromotionGoodsService; import cn.lili.modules.promotion.service.PromotionGoodsService;
import cn.lili.modules.promotion.service.SeckillApplyService; import cn.lili.modules.promotion.service.SeckillApplyService;
import cn.lili.modules.promotion.service.SeckillService; import cn.lili.modules.promotion.service.SeckillService;
import cn.lili.modules.promotion.tools.PromotionCacheKeys;
import cn.lili.modules.promotion.tools.PromotionTools; import cn.lili.modules.promotion.tools.PromotionTools;
import cn.lili.mybatis.util.PageUtil; import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@ -58,7 +56,7 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
* 缓存 * 缓存
*/ */
@Autowired @Autowired
private Cache<List<SeckillTimelineVO>> cache; private Cache<Object> cache;
/** /**
* 规格商品 * 规格商品
*/ */
@ -78,28 +76,17 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
@Override @Override
public List<SeckillTimelineVO> getSeckillTimeline() { public List<SeckillTimelineVO> getSeckillTimeline() {
//秒杀活动缓存key //秒杀活动缓存key
return getSeckillTimelineToCache(null); return getSeckillTimelineInfo();
} }
@Override @Override
public List<SeckillGoodsVO> getSeckillGoods(Integer timeline) { public List<SeckillGoodsVO> getSeckillGoods(Integer timeline) {
List<SeckillGoodsVO> seckillGoodsVoS = new ArrayList<>(); List<SeckillGoodsVO> seckillGoodsVoS = new ArrayList<>();
//秒杀活动缓存key //获取
String seckillCacheKey = PromotionCacheKeys.getSeckillTimelineKey(DateUtil.format(DateUtil.beginOfDay(new DateTime()), "yyyyMMdd")); List<SeckillTimelineVO> seckillTimelineToCache = getSeckillTimelineInfo();
List<SeckillTimelineVO> cacheSeckill = cache.get(seckillCacheKey); Optional<SeckillTimelineVO> first = seckillTimelineToCache.stream().filter(i -> i.getTimeLine().equals(timeline)).findFirst();
if (cacheSeckill == null || cacheSeckill.isEmpty()) { if (first.isPresent()) {
//如缓存中不存在则单独获取 seckillGoodsVoS = first.get().getSeckillGoodsList();
List<SeckillTimelineVO> seckillTimelineToCache = getSeckillTimelineToCache(seckillCacheKey);
Optional<SeckillTimelineVO> first = seckillTimelineToCache.stream().filter(i -> i.getTimeLine().equals(timeline)).findFirst();
if (first.isPresent()) {
seckillGoodsVoS = first.get().getSeckillGoodsList();
}
} else {
//如缓存中存在则取缓存中转为展示的信息
Optional<SeckillTimelineVO> first = cacheSeckill.stream().filter(i -> i.getTimeLine().equals(timeline)).findFirst();
if (first.isPresent()) {
seckillGoodsVoS = first.get().getSeckillGoodsList();
}
} }
return seckillGoodsVoS; return seckillGoodsVoS;
} }
@ -191,6 +178,7 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
PromotionGoods promotionGoods = this.setSeckillGoods(goodsSku, seckillApply, seckill); PromotionGoods promotionGoods = this.setSeckillGoods(goodsSku, seckillApply, seckill);
promotionGoodsList.add(promotionGoods); promotionGoodsList.add(promotionGoods);
} }
boolean result = true;
this.remove(new LambdaQueryWrapper<SeckillApply>().eq(SeckillApply::getSeckillId, seckillId).in(SeckillApply::getSkuId, skuIds)); this.remove(new LambdaQueryWrapper<SeckillApply>().eq(SeckillApply::getSeckillId, seckillId).in(SeckillApply::getSkuId, skuIds));
this.saveBatch(originList); this.saveBatch(originList);
//保存促销活动商品信息 //保存促销活动商品信息
@ -201,12 +189,14 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
promotionGoodsService.deletePromotionGoods(searchParams); promotionGoodsService.deletePromotionGoods(searchParams);
//初始化促销商品 //初始化促销商品
PromotionTools.promotionGoodsInit(promotionGoodsList, seckill, PromotionTypeEnum.SECKILL); PromotionTools.promotionGoodsInit(promotionGoodsList, seckill, PromotionTypeEnum.SECKILL);
promotionGoodsService.saveBatch(promotionGoodsList); result = promotionGoodsService.saveBatch(promotionGoodsList);
} }
//设置秒杀活动的商品数量店铺数量 //设置秒杀活动的商品数量店铺数量
seckillService.updateSeckillGoodsNum(seckillId); seckillService.updateSeckillGoodsNum(seckillId);
cache.vagueDel(CachePrefix.STORE_ID_SECKILL); cache.vagueDel(CachePrefix.STORE_ID_SECKILL);
this.seckillService.updateEsGoodsSeckill(seckill, originList); if (result) {
this.seckillService.updateEsGoodsSeckill(seckill, originList);
}
} }
@ -233,6 +223,7 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
.eq(SeckillApply::getSeckillId, seckillId) .eq(SeckillApply::getSeckillId, seckillId)
.in(SeckillApply::getId, id)); .in(SeckillApply::getId, id));
this.seckillService.deleteEsGoodsSeckill(seckill, Collections.singletonList(seckillApply.getSkuId()));
//删除促销商品 //删除促销商品
this.promotionGoodsService.deletePromotionGoods(seckillId, Collections.singletonList(seckillApply.getSkuId())); this.promotionGoodsService.deletePromotionGoods(seckillId, Collections.singletonList(seckillApply.getSkuId()));
} }
@ -281,12 +272,11 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
} }
/** /**
* 从缓存中获取秒杀活动信息 * 获取秒杀活动信息
* *
* @param seckillCacheKey 秒杀活动缓存键
* @return 秒杀活动信息 * @return 秒杀活动信息
*/ */
private List<SeckillTimelineVO> getSeckillTimelineToCache(String seckillCacheKey) { private List<SeckillTimelineVO> getSeckillTimelineInfo() {
List<SeckillTimelineVO> timelineList = new ArrayList<>(); List<SeckillTimelineVO> timelineList = new ArrayList<>();
LambdaQueryWrapper<Seckill> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<Seckill> queryWrapper = new LambdaQueryWrapper<>();
//查询当天时间段内的秒杀活动活动 //查询当天时间段内的秒杀活动活动
@ -321,9 +311,6 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
} }
} }
} }
if (CharSequenceUtil.isNotEmpty(seckillCacheKey)) {
cache.put(seckillCacheKey, timelineList);
}
return timelineList; return timelineList;
} }
@ -347,6 +334,13 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
goodsVO.setGoodsImage(goodsSku.getThumbnail()); goodsVO.setGoodsImage(goodsSku.getThumbnail());
goodsVO.setGoodsId(goodsSku.getGoodsId()); goodsVO.setGoodsId(goodsSku.getGoodsId());
goodsVO.setGoodsName(goodsSku.getGoodsName()); goodsVO.setGoodsName(goodsSku.getGoodsName());
String promotionGoodsStockCacheKey = PromotionGoodsService.getPromotionGoodsStockCacheKey(
PromotionTypeEnum.SECKILL,
seckillId, seckillApply.getSkuId());
Object quantity = cache.get(promotionGoodsStockCacheKey);
if (quantity != null) {
goodsVO.setQuantity((Integer) quantity);
}
seckillGoodsVoS.add(goodsVO); seckillGoodsVoS.add(goodsVO);
} }
} }

View File

@ -14,6 +14,7 @@ import cn.lili.modules.promotion.entity.dos.Seckill;
import cn.lili.modules.promotion.entity.dos.SeckillApply; import cn.lili.modules.promotion.entity.dos.SeckillApply;
import cn.lili.modules.promotion.entity.dto.search.SeckillSearchParams; import cn.lili.modules.promotion.entity.dto.search.SeckillSearchParams;
import cn.lili.modules.promotion.entity.enums.PromotionsApplyStatusEnum; import cn.lili.modules.promotion.entity.enums.PromotionsApplyStatusEnum;
import cn.lili.modules.promotion.entity.enums.PromotionsScopeTypeEnum;
import cn.lili.modules.promotion.entity.vos.SeckillVO; import cn.lili.modules.promotion.entity.vos.SeckillVO;
import cn.lili.modules.promotion.mapper.SeckillMapper; import cn.lili.modules.promotion.mapper.SeckillMapper;
import cn.lili.modules.promotion.service.SeckillApplyService; import cn.lili.modules.promotion.service.SeckillApplyService;
@ -150,24 +151,42 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl<SeckillMap
for (SeckillApply seckillApply : seckillApplies) { for (SeckillApply seckillApply : seckillApplies) {
if (seckillApply.getPromotionApplyStatus().equals(PromotionsApplyStatusEnum.PASS.name())) { if (seckillApply.getPromotionApplyStatus().equals(PromotionsApplyStatusEnum.PASS.name())) {
this.setSeckillApplyTime(seckill, seckillApply); this.setSeckillApplyTime(seckill, seckillApply);
log.info("更新限时抢购商品状态:{}", seckill);
String promotionKey = PromotionTypeEnum.SECKILL.name() + "-" + seckill.getId();
Map<String, Object> map = new HashMap<>();
// es促销key
map.put("esPromotionKey", promotionKey);
// 促销类型全路径名
map.put("promotionsType", Seckill.class.getName());
// 促销实体
map.put("promotions", seckill);
//更新商品促销消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_PROMOTIONS.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(map), RocketmqSendCallbackBuilder.commonCallback());
} }
} }
if (!seckillApplies.isEmpty()) {
log.info("更新限时抢购商品状态:{}", seckill);
String promotionKey = PromotionTypeEnum.SECKILL.name() + "-" + seckill.getId();
Map<String, Object> map = new HashMap<>();
// es促销key
map.put("esPromotionKey", promotionKey);
// 促销类型全路径名
map.put("promotionsType", Seckill.class.getName());
// 促销实体
map.put("promotions", seckill);
//更新商品促销消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.UPDATE_GOODS_INDEX_PROMOTIONS.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(map), RocketmqSendCallbackBuilder.commonCallback());
}
} }
} }
/**
* 删除商品索引限时抢购信息
*
* @param seckill 限时抢购信息
* @param skuIds 商品skuId列表
*/
@Override
public void deleteEsGoodsSeckill(Seckill seckill, List<String> skuIds) {
seckill.setScopeType(PromotionsScopeTypeEnum.PORTION_GOODS.name());
seckill.setScopeId(ArrayUtil.join(skuIds.toArray(), ","));
//删除商品促销消息
String destination = rocketmqCustomProperties.getGoodsTopic() + ":" + GoodsTagsEnum.DELETE_GOODS_INDEX_PROMOTIONS.name();
//发送mq消息
rocketMQTemplate.asyncSend(destination, JSONUtil.toJsonStr(seckill), RocketmqSendCallbackBuilder.commonCallback());
}
@Override @Override
public void setSeckillApplyTime(Seckill seckill, SeckillApply seckillApply) { public void setSeckillApplyTime(Seckill seckill, SeckillApply seckillApply) {
//下一个时间默认为当天结束时间 //下一个时间默认为当天结束时间