增加检测促销商品库存

This commit is contained in:
paulGao 2022-03-02 20:26:50 +08:00
parent c4307aa821
commit 3506e1c738
6 changed files with 82 additions and 12 deletions

View File

@ -246,6 +246,7 @@ public enum ResultCode {
* 活动
*/
PROMOTION_GOODS_NOT_EXIT(40000, "当前促销商品不存在!"),
PROMOTION_GOODS_QUANTITY_NOT_EXIT(40020, "当前促销商品库存不足!"),
PROMOTION_SAME_ACTIVE_EXIST(40001, "活动时间内已存在同类活动,请选择关闭、删除当前时段的活动"),
PROMOTION_START_TIME_ERROR(40002, "活动起始时间不能小于当前时间"),
PROMOTION_END_TIME_ERROR(40003, "活动结束时间不能小于当前时间"),

View File

@ -2,6 +2,7 @@ package cn.lili.modules.order.cart.render.impl;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.lili.cache.Cache;
import cn.lili.common.enums.PromotionTypeEnum;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
@ -19,10 +20,14 @@ import cn.lili.modules.promotion.entity.dto.search.KanjiaActivitySearchParams;
import cn.lili.modules.promotion.entity.enums.KanJiaStatusEnum;
import cn.lili.modules.promotion.entity.vos.PromotionSkuVO;
import cn.lili.modules.promotion.entity.vos.kanjia.KanjiaActivityVO;
import cn.lili.modules.promotion.service.KanjiaActivityGoodsService;
import cn.lili.modules.promotion.service.KanjiaActivityService;
import cn.lili.modules.promotion.service.PointsGoodsService;
import cn.lili.modules.promotion.service.PromotionGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@ -39,9 +44,24 @@ public class SkuPromotionRender implements CartRenderStep {
@Autowired
private KanjiaActivityService kanjiaActivityService;
@Autowired
private KanjiaActivityGoodsService kanjiaActivityGoodsService;
@Autowired
private PointsGoodsService pointsGoodsService;
/**
* 促销商品
*/
@Autowired
private PromotionGoodsService promotionGoodsService;
@Autowired
private MemberService memberService;
@Autowired
private Cache cache;
@Override
public RenderStepEnums step() {
return RenderStepEnums.SKU_PROMOTION;
@ -54,6 +74,10 @@ public class SkuPromotionRender implements CartRenderStep {
renderBasePrice(tradeDTO);
//渲染单品促销
renderSkuPromotion(tradeDTO);
checkPromotionQuantity(tradeDTO);
}
/**
@ -155,6 +179,51 @@ public class SkuPromotionRender implements CartRenderStep {
}
}
/**
* 检查促销库存
*
* @param tradeDTO 购物车视图
*/
private void checkPromotionQuantity(TradeDTO tradeDTO) {
for (CartSkuVO cartSkuVO : tradeDTO.getCheckedSkuList()) {
cartSkuVO.getPromotionMap();
List<PromotionSkuVO> joinPromotion = cartSkuVO.getPriceDetailDTO().getJoinPromotion();
if (!joinPromotion.isEmpty()) {
for (PromotionSkuVO promotionSkuVO : joinPromotion) {
String promotionGoodsStockCacheKey = PromotionGoodsService.getPromotionGoodsStockCacheKey(PromotionTypeEnum.valueOf(promotionSkuVO.getPromotionType()), promotionSkuVO.getActivityId(), cartSkuVO.getGoodsSku().getId());
Object quantity = cache.get(promotionGoodsStockCacheKey);
if (quantity == null) {
//如果促销有库存信息
PromotionTypeEnum promotionTypeEnum = PromotionTypeEnum.valueOf(promotionSkuVO.getPromotionType());
switch (promotionTypeEnum) {
case KANJIA:
quantity = kanjiaActivityGoodsService.getKanjiaGoodsBySkuId(cartSkuVO.getGoodsSku().getId()).getStock();
break;
case POINTS_GOODS:
quantity = pointsGoodsService.getPointsGoodsDetailBySkuId(cartSkuVO.getGoodsSku().getId()).getActiveStock();
break;
case SECKILL:
case PINTUAN:
quantity = promotionGoodsService.getPromotionGoodsStock(PromotionTypeEnum.valueOf(promotionSkuVO.getPromotionType()), promotionSkuVO.getActivityId(), cartSkuVO.getGoodsSku().getId());
break;
default:
break;
}
}
if (cartSkuVO.getNum() > (Integer) quantity) {//设置购物车未选中
cartSkuVO.setChecked(false);
//设置失效消息
cartSkuVO.setErrorMessage("促销商品库存不足,现有库存数量[" + quantity + "]");
}
}
}
}
}
/**
* 购物车促销类型
*/

View File

@ -185,6 +185,7 @@ public class CartServiceImpl implements CartService {
//购物车中不存在此商品则新建立一个
CartSkuVO cartSkuVO = new CartSkuVO(dataSku, promotionMap);
this.checkSetGoodsQuantity(cartSkuVO, skuId, num);
cartSkuVO.setCartType(cartTypeEnum);
//检测购物车数据
checkCart(cartTypeEnum, cartSkuVO, skuId, num);

View File

@ -11,6 +11,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.beans.BeanUtils;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Field;
@ -30,6 +31,7 @@ import java.util.Date;
@TableName("li_seckill")
@ApiModel(value = "秒杀活动活动")
@NoArgsConstructor
@ToString(callSuper = true)
public class Seckill extends BasePromotions {
private static final long serialVersionUID = -9116425737163730836L;

View File

@ -105,7 +105,7 @@ public class PromotionServiceImpl implements PromotionService {
/**
* 根据商品索引获取当前商品索引的所有促销活动信息
*
* @param storeId 店铺id
* @param storeId 店铺id
* @param goodsSkuId 商品skuId
* @return 当前促销活动集合
*/
@ -129,7 +129,7 @@ public class PromotionServiceImpl implements PromotionService {
promotionMap.put(esPromotionKey, fullDiscount);
break;
case SECKILL:
this.getGoodsCurrentSeckill(promotionGoods, promotionMap);
this.getGoodsCurrentSeckill(esPromotionKey, promotionGoods, promotionMap);
break;
case POINTS_GOODS:
PointsGoods pointsGoods = pointsGoodsService.getById(promotionGoods.getPromotionId());
@ -143,7 +143,7 @@ public class PromotionServiceImpl implements PromotionService {
}
private void getGoodsCurrentSeckill(PromotionGoods promotionGoods, Map<String, Object> promotionMap) {
private void getGoodsCurrentSeckill(String esPromotionKey, PromotionGoods promotionGoods, Map<String, Object> promotionMap) {
Seckill seckill = seckillService.getById(promotionGoods.getPromotionId());
SeckillSearchParams searchParams = new SeckillSearchParams();
searchParams.setSeckillId(promotionGoods.getPromotionId());
@ -151,19 +151,12 @@ public class PromotionServiceImpl implements PromotionService {
List<SeckillApply> seckillApplyList = seckillApplyService.getSeckillApplyList(searchParams);
if (seckillApplyList != null && !seckillApplyList.isEmpty()) {
SeckillApply seckillApply = seckillApplyList.get(0);
int nextHour = 23;
String[] split = seckill.getHours().split(",");
int[] hoursSored = Arrays.stream(split).mapToInt(Integer::parseInt).toArray();
Arrays.sort(hoursSored);
for (int i : hoursSored) {
if (seckillApply.getTimeLine() < i) {
nextHour = i;
}
}
String seckillKey = promotionGoods.getPromotionType() + "-" + nextHour;
seckill.setStartTime(promotionGoods.getStartTime());
seckill.setEndTime(promotionGoods.getEndTime());
promotionMap.put(seckillKey, seckill);
promotionMap.put(esPromotionKey, seckill);
}
}

View File

@ -424,7 +424,11 @@ public class SeckillApplyServiceImpl extends ServiceImpl<SeckillApplyMapper, Sec
//设置单独每个促销商品的结束时间
DateTime startTime = DateUtil.offsetHour(DateUtil.beginOfDay(seckill.getStartTime()), seckillApply.getTimeLine());
promotionGoods.setStartTime(startTime);
promotionGoods.setEndTime(seckill.getEndTime());
if (seckill.getEndTime() == null) {
promotionGoods.setEndTime(DateUtil.endOfDay(startTime));
} else {
promotionGoods.setEndTime(seckill.getEndTime());
}
return promotionGoods;
}