From 3506e1c73822b5b31a5057e37d73e73536fdbbf8 Mon Sep 17 00:00:00 2001 From: paulGao Date: Wed, 2 Mar 2022 20:26:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=A3=80=E6=B5=8B=E4=BF=83?= =?UTF-8?q?=E9=94=80=E5=95=86=E5=93=81=E5=BA=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/lili/common/enums/ResultCode.java | 1 + .../cart/render/impl/SkuPromotionRender.java | 69 +++++++++++++++++++ .../order/cart/service/CartServiceImpl.java | 1 + .../modules/promotion/entity/dos/Seckill.java | 2 + .../serviceimpl/PromotionServiceImpl.java | 15 ++-- .../serviceimpl/SeckillApplyServiceImpl.java | 6 +- 6 files changed, 82 insertions(+), 12 deletions(-) diff --git a/framework/src/main/java/cn/lili/common/enums/ResultCode.java b/framework/src/main/java/cn/lili/common/enums/ResultCode.java index b335862d..7b61c34f 100644 --- a/framework/src/main/java/cn/lili/common/enums/ResultCode.java +++ b/framework/src/main/java/cn/lili/common/enums/ResultCode.java @@ -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, "活动结束时间不能小于当前时间"), diff --git a/framework/src/main/java/cn/lili/modules/order/cart/render/impl/SkuPromotionRender.java b/framework/src/main/java/cn/lili/modules/order/cart/render/impl/SkuPromotionRender.java index cfcfe37b..3350fda6 100644 --- a/framework/src/main/java/cn/lili/modules/order/cart/render/impl/SkuPromotionRender.java +++ b/framework/src/main/java/cn/lili/modules/order/cart/render/impl/SkuPromotionRender.java @@ -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 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 + "]"); + } + } + } + } + } + /** * 购物车促销类型 */ diff --git a/framework/src/main/java/cn/lili/modules/order/cart/service/CartServiceImpl.java b/framework/src/main/java/cn/lili/modules/order/cart/service/CartServiceImpl.java index ccf3e7d8..1e8367ce 100644 --- a/framework/src/main/java/cn/lili/modules/order/cart/service/CartServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/order/cart/service/CartServiceImpl.java @@ -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); diff --git a/framework/src/main/java/cn/lili/modules/promotion/entity/dos/Seckill.java b/framework/src/main/java/cn/lili/modules/promotion/entity/dos/Seckill.java index f80c4b77..e5d22caf 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/entity/dos/Seckill.java +++ b/framework/src/main/java/cn/lili/modules/promotion/entity/dos/Seckill.java @@ -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; diff --git a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/PromotionServiceImpl.java b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/PromotionServiceImpl.java index 2d8bda06..cd0df5e8 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/PromotionServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/PromotionServiceImpl.java @@ -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 promotionMap) { + private void getGoodsCurrentSeckill(String esPromotionKey, PromotionGoods promotionGoods, Map 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 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); } } diff --git a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/SeckillApplyServiceImpl.java b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/SeckillApplyServiceImpl.java index 633e453f..32163415 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/SeckillApplyServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/SeckillApplyServiceImpl.java @@ -424,7 +424,11 @@ public class SeckillApplyServiceImpl extends ServiceImpl