Skip to content

Commit

Permalink
修复秒杀库存前后端显示不一致问题
Browse files Browse the repository at this point in the history
  • Loading branch information
LeiGaoRobot committed Dec 30, 2021
1 parent 194d0b6 commit d4ca3d5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public interface SeckillService extends AbstractPromotionsService<Seckill> {
*/
void updateEsGoodsSeckill(Seckill seckill, List<SeckillApply> seckillApplies);

/**
* 删除商品索引限时抢购信息
*
* @param seckill 限时抢购信息
* @param skuIds 商品skuId列表
*/
void deleteEsGoodsSeckill(Seckill seckill, List<String> skuIds);

/**
* 设置秒杀活动的每个参与活动商品的详细时间
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.common.enums.PromotionTypeEnum;
Expand All @@ -27,7 +26,6 @@
import cn.lili.modules.promotion.service.PromotionGoodsService;
import cn.lili.modules.promotion.service.SeckillApplyService;
import cn.lili.modules.promotion.service.SeckillService;
import cn.lili.modules.promotion.tools.PromotionCacheKeys;
import cn.lili.modules.promotion.tools.PromotionTools;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
Expand Down Expand Up @@ -58,7 +56,7 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
* 缓存
*/
@Autowired
private Cache<List<SeckillTimelineVO>> cache;
private Cache<Object> cache;
/**
* 规格商品
*/
Expand All @@ -78,28 +76,17 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
@Override
public List<SeckillTimelineVO> getSeckillTimeline() {
//秒杀活动缓存key
return getSeckillTimelineToCache(null);
return getSeckillTimelineInfo();
}

@Override
public List<SeckillGoodsVO> getSeckillGoods(Integer timeline) {
List<SeckillGoodsVO> seckillGoodsVoS = new ArrayList<>();
//秒杀活动缓存key
String seckillCacheKey = PromotionCacheKeys.getSeckillTimelineKey(DateUtil.format(DateUtil.beginOfDay(new DateTime()), "yyyyMMdd"));
List<SeckillTimelineVO> cacheSeckill = cache.get(seckillCacheKey);
if (cacheSeckill == null || cacheSeckill.isEmpty()) {
//如缓存中不存在,则单独获取
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();
}
//获取
List<SeckillTimelineVO> seckillTimelineToCache = getSeckillTimelineInfo();
Optional<SeckillTimelineVO> first = seckillTimelineToCache.stream().filter(i -> i.getTimeLine().equals(timeline)).findFirst();
if (first.isPresent()) {
seckillGoodsVoS = first.get().getSeckillGoodsList();
}
return seckillGoodsVoS;
}
Expand Down Expand Up @@ -191,6 +178,7 @@ public void addSeckillApply(String seckillId, String storeId, List<SeckillApplyV
PromotionGoods promotionGoods = this.setSeckillGoods(goodsSku, seckillApply, seckill);
promotionGoodsList.add(promotionGoods);
}
boolean result = true;
this.remove(new LambdaQueryWrapper<SeckillApply>().eq(SeckillApply::getSeckillId, seckillId).in(SeckillApply::getSkuId, skuIds));
this.saveBatch(originList);
//保存促销活动商品信息
Expand All @@ -201,12 +189,14 @@ public void addSeckillApply(String seckillId, String storeId, List<SeckillApplyV
promotionGoodsService.deletePromotionGoods(searchParams);
//初始化促销商品
PromotionTools.promotionGoodsInit(promotionGoodsList, seckill, PromotionTypeEnum.SECKILL);
promotionGoodsService.saveBatch(promotionGoodsList);
result = promotionGoodsService.saveBatch(promotionGoodsList);
}
//设置秒杀活动的商品数量、店铺数量
seckillService.updateSeckillGoodsNum(seckillId);
cache.vagueDel(CachePrefix.STORE_ID_SECKILL);
this.seckillService.updateEsGoodsSeckill(seckill, originList);
if (result) {
this.seckillService.updateEsGoodsSeckill(seckill, originList);
}
}


Expand All @@ -233,6 +223,7 @@ public void removeSeckillApply(String seckillId, String id) {
.eq(SeckillApply::getSeckillId, seckillId)
.in(SeckillApply::getId, id));

this.seckillService.deleteEsGoodsSeckill(seckill, Collections.singletonList(seckillApply.getSkuId()));
//删除促销商品
this.promotionGoodsService.deletePromotionGoods(seckillId, Collections.singletonList(seckillApply.getSkuId()));
}
Expand Down Expand Up @@ -281,12 +272,11 @@ private void checkSeckillApplyList(String hours, List<SeckillApplyVO> seckillApp
}

/**
* 从缓存中获取秒杀活动信息
* 获取秒杀活动信息
*
* @param seckillCacheKey 秒杀活动缓存键
* @return 秒杀活动信息
*/
private List<SeckillTimelineVO> getSeckillTimelineToCache(String seckillCacheKey) {
private List<SeckillTimelineVO> getSeckillTimelineInfo() {
List<SeckillTimelineVO> timelineList = new ArrayList<>();
LambdaQueryWrapper<Seckill> queryWrapper = new LambdaQueryWrapper<>();
//查询当天时间段内的秒杀活动活动
Expand Down Expand Up @@ -321,9 +311,6 @@ private List<SeckillTimelineVO> getSeckillTimelineToCache(String seckillCacheKey
}
}
}
if (CharSequenceUtil.isNotEmpty(seckillCacheKey)) {
cache.put(seckillCacheKey, timelineList);
}
return timelineList;
}

Expand All @@ -347,6 +334,13 @@ private List<SeckillGoodsVO> wrapperSeckillGoods(Integer startTimeline, String s
goodsVO.setGoodsImage(goodsSku.getThumbnail());
goodsVO.setGoodsId(goodsSku.getGoodsId());
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import cn.lili.modules.promotion.entity.dos.SeckillApply;
import cn.lili.modules.promotion.entity.dto.search.SeckillSearchParams;
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.mapper.SeckillMapper;
import cn.lili.modules.promotion.service.SeckillApplyService;
Expand Down Expand Up @@ -150,24 +151,42 @@ public void updateEsGoodsSeckill(Seckill seckill, List<SeckillApply> seckillAppl
for (SeckillApply seckillApply : seckillApplies) {
if (seckillApply.getPromotionApplyStatus().equals(PromotionsApplyStatusEnum.PASS.name())) {
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
public void setSeckillApplyTime(Seckill seckill, SeckillApply seckillApply) {
//下一个时间,默认为当天结束时间
Expand Down

0 comments on commit d4ca3d5

Please sign in to comment.