修复秒杀库存前后端显示不一致问题
This commit is contained in:
parent
194d0b6ac2
commit
d4ca3d5817
@ -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);
|
||||
|
||||
/**
|
||||
* 设置秒杀活动的每个参与活动商品的详细时间
|
||||
*
|
||||
|
@ -4,7 +4,6 @@ import cn.hutool.core.bean.BeanUtil;
|
||||
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;
|
||||
@ -27,7 +26,6 @@ import cn.lili.modules.promotion.mapper.SeckillApplyMapper;
|
||||
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;
|
||||
@ -58,7 +56,7 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
|
||||
* 缓存
|
||||
*/
|
||||
@Autowired
|
||||
private Cache<List<SeckillTimelineVO>> cache;
|
||||
private Cache<Object> cache;
|
||||
/**
|
||||
* 规格商品
|
||||
*/
|
||||
@ -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;
|
||||
}
|
||||
@ -191,6 +178,7 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
|
||||
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);
|
||||
//保存促销活动商品信息
|
||||
@ -201,12 +189,14 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -233,6 +223,7 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
|
||||
.eq(SeckillApply::getSeckillId, seckillId)
|
||||
.in(SeckillApply::getId, id));
|
||||
|
||||
this.seckillService.deleteEsGoodsSeckill(seckill, 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 秒杀活动信息
|
||||
*/
|
||||
private List<SeckillTimelineVO> getSeckillTimelineToCache(String seckillCacheKey) {
|
||||
private List<SeckillTimelineVO> getSeckillTimelineInfo() {
|
||||
List<SeckillTimelineVO> timelineList = new ArrayList<>();
|
||||
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;
|
||||
}
|
||||
|
||||
@ -347,6 +334,13 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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.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;
|
||||
@ -150,24 +151,42 @@ public class SeckillServiceImpl extends AbstractPromotionsServiceImpl<SeckillMap
|
||||
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) {
|
||||
//下一个时间,默认为当天结束时间
|
||||
|
Loading…
x
Reference in New Issue
Block a user