Просмотр исходного кода

计算优惠使用商品券返回商品信息

qmj 1 неделя назад
Родитель
Сommit
3c4346bd38

+ 4 - 0
ruoyi-system/src/main/java/com/ruoyi/system/dto/PromotionCalcResponse.java

@@ -163,6 +163,10 @@ public class PromotionCalcResponse {
         private Boolean usable;
         /** 是否与当前促销冲突 */
         private Boolean conflictWithPromotion;
+        /** 商品券预估优惠金额(仅商品券, 预览) */
+        private BigDecimal couponPreviewReduce;
+        /** 商品券作用于的商品预览明细(仅商品券: 名称/原价/优惠后价) */
+        private List<LineItem> productPreview;
         /** 过期时间 */
         @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Taipei")
         private Date expireTime;

+ 21 - 2
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/PromotionCalcServiceImpl.java

@@ -396,7 +396,7 @@ public class PromotionCalcServiceImpl implements IPromotionCalcService
         // ---- 11. 可用优惠券列表 ----
         List<Map<String, Object>> availableCouponMaps = buildAvailableCoupons(storeId, userId,
                 originalAmount, afterPromotion, promotionReduce, itemsWithPrice,
-                rulesByProduct, activityMap);
+                rulesByProduct, activityMap, priceMap, nameMap);
         response.setAvailableCoupons(availableCouponMaps.stream()
                 .map(this::toAvailableCoupon)
                 .collect(Collectors.toList()));
@@ -469,6 +469,13 @@ public class PromotionCalcServiceImpl implements IPromotionCalcService
         ac.setDiscountRate((BigDecimal) couponMap.get("discountRate"));
         ac.setUsable((Boolean) couponMap.get("usable"));
         ac.setConflictWithPromotion((Boolean) couponMap.get("conflictWithPromotion"));
+        ac.setCouponPreviewReduce((BigDecimal) couponMap.get("couponPreviewReduce"));
+        @SuppressWarnings("unchecked")
+        List<Map<String, Object>> productPreview = (List<Map<String, Object>>) couponMap.get("productPreview");
+        if (productPreview != null && !productPreview.isEmpty())
+        {
+            ac.setProductPreview(productPreview.stream().map(this::toLineItem).collect(Collectors.toList()));
+        }
         ac.setExpireTime((java.util.Date) couponMap.get("expireTime"));
         return ac;
     }
@@ -826,7 +833,9 @@ public class PromotionCalcServiceImpl implements IPromotionCalcService
                                                             BigDecimal promotionReduce,
                                                             List<Map<String, Object>> itemsWithPrice,
                                                             Map<Long, List<PromotionActivityRule>> rulesByProduct,
-                                                            Map<Long, PromotionActivity> activityMap)
+                                                            Map<Long, PromotionActivity> activityMap,
+                                                            Map<Long, BigDecimal> priceMap,
+                                                            Map<Long, String> nameMap)
     {
         List<Map<String, Object>> available = new ArrayList<>();
         if (userId == null)
@@ -883,6 +892,16 @@ public class PromotionCalcServiceImpl implements IPromotionCalcService
 
             if (batch.getCouponType() != null && batch.getCouponType() == 2 && rule.getProductId() != null)
             {
+                // 预览: 该商品券作用于的商品能优惠多少(名称/原价/优惠后价)
+                CouponCalcResult preview = calcCouponReduce(rule, batch, itemsWithPrice, afterPromotion, priceMap, nameMap);
+                if (preview.reduce.compareTo(BigDecimal.ZERO) > 0)
+                {
+                    couponInfo.put("couponPreviewReduce", preview.reduce);
+                }
+                if (!preview.items.isEmpty())
+                {
+                    couponInfo.put("productPreview", preview.items);
+                }
                 boolean inCart = itemsWithPrice.stream()
                         .anyMatch(i -> rule.getProductId().equals(toLong(i.get("productId"))));
                 if (!inCart)