diff --git a/buyer-api/src/main/java/cn/lili/controller/trade/CartController.java b/buyer-api/src/main/java/cn/lili/controller/trade/CartController.java index fce86afd..b5ffaeea 100644 --- a/buyer-api/src/main/java/cn/lili/controller/trade/CartController.java +++ b/buyer-api/src/main/java/cn/lili/controller/trade/CartController.java @@ -50,8 +50,17 @@ public class CartController { public ResultMessage add(@NotNull(message = "产品id不能为空") String skuId, @NotNull(message = "购买数量不能为空") @Min(value = 1, message = "加入购物车数量必须大于0") Integer num, String cartType) { - cartService.add(skuId, num, cartType); - return ResultUtil.success(); + try { + // 读取选中的列表 + cartService.add(skuId, num, cartType); + return ResultUtil.success(); + } catch (ServiceException se) { + log.error(se.getMsg(), se); + return ResultUtil.error(se.getResultCode().code(), se.getResultCode().message()); + } catch (Exception e) { + log.error(ResultCode.CART_ERROR.message(), e); + throw new ServiceException(ResultCode.CART_ERROR); + } } @@ -150,8 +159,9 @@ public class CartController { try { // 读取选中的列表 return ResultUtil.data(this.cartService.getCheckedTradeDTO(CartTypeEnum.valueOf(way))); - } catch (ServiceException e) { - throw e; + } catch (ServiceException se) { + log.error(se.getMsg(), se); + return ResultUtil.error(se.getResultCode().code(), se.getResultCode().message()); } catch (Exception e) { log.error(ResultCode.CART_ERROR.message(), e); throw new ServiceException(ResultCode.CART_ERROR); @@ -193,6 +203,9 @@ public class CartController { try { cartService.shippingMethod(selleId, shippingMethod, way); return ResultUtil.success(); + } catch (ServiceException se) { + log.error(se.getMsg(), se); + return ResultUtil.error(se.getResultCode().code(), se.getResultCode().message()); } catch (Exception e) { log.error(ResultCode.CART_ERROR.message(), e); throw new ServiceException(ResultCode.CART_ERROR); @@ -228,8 +241,9 @@ public class CartController { try { // 读取选中的列表 return ResultUtil.data(this.cartService.createTrade(tradeParams)); - } catch (ServiceException e) { - throw e; + } catch (ServiceException se) { + log.error(se.getMsg(), se); + return ResultUtil.error(se.getResultCode().code(), se.getResultCode().message()); } catch (Exception e) { log.error(ResultCode.ORDER_ERROR.message(), e); throw new ServiceException(ResultCode.ORDER_ERROR); diff --git a/consumer/src/main/java/cn/lili/listener/OrderMessageListener.java b/consumer/src/main/java/cn/lili/listener/OrderMessageListener.java index 02371a0d..dfbb0c2f 100644 --- a/consumer/src/main/java/cn/lili/listener/OrderMessageListener.java +++ b/consumer/src/main/java/cn/lili/listener/OrderMessageListener.java @@ -44,6 +44,7 @@ public class OrderMessageListener implements RocketMQListener { case ORDER_CREATE: String key = new String(messageExt.getBody()); TradeDTO tradeDTO = (TradeDTO) cache.get(key); + boolean result = true; for (TradeEvent event : tradeEvent) { try { event.orderCreate(tradeDTO); @@ -52,8 +53,14 @@ public class OrderMessageListener implements RocketMQListener { tradeDTO.getSn(), event.getClass().getName(), e); + result = false; } } + // 如所有步骤顺利完成 + if (Boolean.TRUE.equals(result)) { + // 清除记录信息的trade cache key + cache.remove(key); + } break; //订单状态变更 case STATUS_CHANGE: 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 c2683cc8..d2fa9eba 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 @@ -1,6 +1,7 @@ package cn.lili.modules.order.cart.render.impl; import cn.hutool.core.date.DateUtil; +import cn.lili.common.exception.ServiceException; import cn.lili.common.utils.CurrencyUtil; import cn.lili.common.utils.StringUtils; import cn.lili.modules.order.cart.entity.dto.MemberCouponDTO; @@ -13,10 +14,14 @@ import cn.lili.modules.order.cart.entity.vo.PriceDetailVO; import cn.lili.modules.order.cart.render.CartRenderStep; import cn.lili.modules.order.order.entity.dto.PriceDetailDTO; import cn.lili.modules.promotion.entity.dos.MemberCoupon; +import cn.lili.modules.promotion.entity.dos.Pintuan; +import cn.lili.modules.promotion.entity.dos.PromotionGoods; import cn.lili.modules.promotion.entity.dto.GoodsSkuPromotionPriceDTO; import cn.lili.modules.promotion.entity.dto.PromotionPriceDTO; import cn.lili.modules.promotion.entity.dto.PromotionPriceParamDTO; import cn.lili.modules.promotion.entity.dto.StorePromotionPriceDTO; +import cn.lili.modules.promotion.entity.enums.PromotionTypeEnum; +import cn.lili.modules.promotion.service.PintuanService; import cn.lili.modules.promotion.service.PromotionGoodsService; import cn.lili.modules.promotion.service.PromotionPriceService; import org.springframework.beans.factory.annotation.Autowired; @@ -45,12 +50,16 @@ public class SkuPromotionRender implements CartRenderStep { @Autowired private PromotionGoodsService promotionGoodsService; + @Autowired + private PintuanService pintuanService; + @Override public void render(TradeDTO tradeDTO) { //主要渲染各个优惠的价格 this.renderSkuPromotion(tradeDTO); + this.checkPromotionLimit(tradeDTO); } @@ -289,4 +298,20 @@ public class SkuPromotionRender implements CartRenderStep { } return false; } + + private void checkPromotionLimit(TradeDTO tradeDTO) { + if (tradeDTO.getCartTypeEnum().equals(CartTypeEnum.PINTUAN)) { + // 如果为拼团订单,则获取拼团活动ID + Optional pintuanId = tradeDTO.getSkuList().get(0).getPromotions().stream().filter(i -> i.getPromotionType().equals(PromotionTypeEnum.PINTUAN.name())).map(PromotionGoods::getPromotionId).findFirst(); + if (pintuanId.isPresent()) { + Pintuan pintuan = pintuanService.getPintuanById(pintuanId.get()); + Integer limitNum = pintuan.getLimitNum(); + for (CartSkuVO cartSkuVO : tradeDTO.getSkuList()) { + if (limitNum != 0 && cartSkuVO.getNum() > limitNum) { + throw new ServiceException("购买数量超过拼团活动限制数量"); + } + } + } + } + } } 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 c317dfe1..4f334cd1 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 @@ -149,6 +149,8 @@ public class CartServiceImpl implements CartService { tradeDTO.setStoreCoupons(null); tradeDTO.setPlatformCoupon(null); this.resetTradeDTO(tradeDTO); + } catch (ServiceException se) { + throw se; } catch (Exception e) { log.error("购物车渲染异常", e); throw new ServiceException(errorMessage); @@ -324,14 +326,16 @@ public class CartServiceImpl implements CartService { List storeIds = new ArrayList<>(); List esGoodsList = esGoodsSearchService.getEsGoodsBySkuIds(ids); for (EsGoodsIndex esGoodsIndex : esGoodsList) { - if (esGoodsIndex.getPromotionMap() != null) { - List couponIds = esGoodsIndex.getPromotionMap().keySet().parallelStream().filter(i -> i.contains(PromotionTypeEnum.COUPON.name())).map(i -> i.substring(i.lastIndexOf("-") + 1)).collect(Collectors.toList()); - if (!couponIds.isEmpty()) { - List currentGoodsCanUse = memberCouponService.getCurrentGoodsCanUse(tradeDTO.getMemberId(), couponIds, totalPrice); - count = currentGoodsCanUse.size(); + if (esGoodsIndex != null) { + if (esGoodsIndex.getPromotionMap() != null) { + List couponIds = esGoodsIndex.getPromotionMap().keySet().parallelStream().filter(i -> i.contains(PromotionTypeEnum.COUPON.name())).map(i -> i.substring(i.lastIndexOf("-") + 1)).collect(Collectors.toList()); + if (!couponIds.isEmpty()) { + List currentGoodsCanUse = memberCouponService.getCurrentGoodsCanUse(tradeDTO.getMemberId(), couponIds, totalPrice); + count = currentGoodsCanUse.size(); + } } + storeIds.add(esGoodsIndex.getStoreId()); } - storeIds.add(esGoodsIndex.getStoreId()); } List allScopeMemberCoupon = memberCouponService.getAllScopeMemberCoupon(tradeDTO.getMemberId(), storeIds); if (allScopeMemberCoupon != null && !allScopeMemberCoupon.isEmpty()) { @@ -628,7 +632,7 @@ public class CartServiceImpl implements CartService { promotionGoods -> (promotionGoods.getPromotionType().equals(PromotionTypeEnum.PINTUAN.name()))) .collect(Collectors.toList()); //拼团活动判定 - if (currentPromotion.size() > 0) { + if (!currentPromotion.isEmpty()) { //写入拼团信息 cartSkuVO.setPintuanId(currentPromotion.get(0).getPromotionId()); } else { diff --git a/framework/src/main/java/cn/lili/modules/order/order/serviceimpl/OrderServiceImpl.java b/framework/src/main/java/cn/lili/modules/order/order/serviceimpl/OrderServiceImpl.java index 5a4a7503..32f86ed5 100644 --- a/framework/src/main/java/cn/lili/modules/order/order/serviceimpl/OrderServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/order/order/serviceimpl/OrderServiceImpl.java @@ -672,10 +672,10 @@ public class OrderServiceImpl extends ServiceImpl implements * @param list 需要更新拼团状态为成功的拼团订单列表 */ private void pintuanOrderSuccess(List list) { - list.stream().forEach(order -> { - if (order.getOrderType().equals(OrderTypeEnum.VIRTUAL)) { + list.forEach(order -> { + if (order.getOrderType().equals(OrderTypeEnum.VIRTUAL.name())) { this.virtualOrderConfirm(order.getSn()); - } else if (order.getOrderType().equals(OrderTypeEnum.NORMAL)) { + } else if (order.getOrderType().equals(OrderTypeEnum.NORMAL.name())) { this.normalOrderConfirm(order.getSn()); } }); @@ -793,7 +793,7 @@ public class OrderServiceImpl extends ServiceImpl implements */ private void checkOrder(Order order) { //订单类型为拼团订单,检测购买数量是否超过了限购数量 - if (OrderPromotionTypeEnum.PINTUAN.name().equals(order.getOrderPromotionType())) { + if (OrderPromotionTypeEnum.PINTUAN.name().equals(order.getOrderType())) { Pintuan pintuan = pintuanService.getPintuanById(order.getPromotionId()); Integer limitNum = pintuan.getLimitNum(); if (limitNum != 0 && order.getGoodsNum() > limitNum) {