Browse Source

feat(promotion): 实现满减活动的购物车结算逻辑

- 新增满减金额计算逻辑- 更新商品促销类型标记
- 实现满减优惠在商品价格中的分摊
- 优化购物车商品列表的处理流程
hechunping 1 tuần trước cách đây
mục cha
commit
a62c845e9e

+ 56 - 1
src/main/java/com/xynet/marketing/service/factory/promotion/impl/base/PromotionFactoryBase.java

@@ -1,6 +1,7 @@
 package com.xynet.marketing.service.factory.promotion.impl.base;
 
 import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.json.JSONArray;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -167,6 +168,8 @@ public class PromotionFactoryBase extends ServiceImpl<PromotionMapper, Promotion
                 }
             }
         }
+        //满减商品金额
+        int type1Amount = 0;
         //查询商品是否有折扣、满减活动
         for (int p = 0; p < promotionList.size(); p++) {
             Promotion promotion = promotionList.get(p);
@@ -205,7 +208,9 @@ public class PromotionFactoryBase extends ServiceImpl<PromotionMapper, Promotion
                             goods.set("msg", "促销活动:" + priceJson.getStr("scalar").replace("0.", "") + "折;");
                         }
                         if (Objects.equals(promotion.getType(), PromotionTypeEnum.T_1.getCode())) {
-                            goods.set("promotionType3", true);
+                            //计算满减金额
+                            type1Amount += goods.getInt("priceOnsale") * goods.getInt("num");
+                            goods.set("promotionType1", true);
                         }
                         //优惠活动
                         goods.set("havePromotion", true);
@@ -216,6 +221,56 @@ public class PromotionFactoryBase extends ServiceImpl<PromotionMapper, Promotion
                 }
             }
         }
+        if (type1Amount > 0) {
+            Integer priceOnsaleType1 = 0;
+            //判断满减金额
+            for (int p = 0; p < promotionList.size(); p++) {
+                Promotion promotion = promotionList.get(p);
+                JSONArray priceJson = JSONUtil.parseArray(promotion.getPrice());
+                for (int g = 0; g < goodsList.size(); g++) {
+                    JMap<String, Object> goods = goodsList.get(g);
+                    if (goods.getBoolean("promotionType1")) {
+                    }
+                }
+                for (int i = 0; i < priceJson.size(); i++) {
+                    JSONObject priceObject = priceJson.getJSONObject(i);
+                    //格式:[{"limit":200,"price":100,"isLoop":1}]
+                    Integer limit = Integer.valueOf(priceObject.get("limit").toString());
+                    Integer price = Integer.valueOf(priceObject.get("price").toString());
+                    String isLoop = priceObject.get("isLoop").toString();
+                    //判断是哪个挡位
+                    if (type1Amount >= limit) {
+                        if ("1".equals(isLoop)) {
+                            int n = Integer.valueOf(type1Amount / limit);
+                            priceOnsaleType1 = price * n;
+                        } else {
+                            priceOnsaleType1 = price;
+                        }
+                    }
+                }
+                //映射到商品
+                for (int g = 0; g < goodsList.size(); g++) {
+                    JMap<String, Object> goods = goodsList.get(g);
+                    if (goods.getBoolean("promotionType1")) {
+                        //单价
+                        BigDecimal promotionPriceOnsale = goods.getBigDecimal("priceOnsale");
+                        //数量
+                        BigDecimal num = goods.getBigDecimal("num");
+                        //总金额
+                        BigDecimal zje = new BigDecimal(type1Amount);
+                        //总优惠
+                        BigDecimal zyh = new BigDecimal(priceOnsaleType1);
+                        //计算公式:(单价*数量/总金额*总优惠)
+                        Integer goodsYh = promotionPriceOnsale.multiply(num).divide(zje, 2, BigDecimal.ROUND_HALF_UP).multiply(zyh).setScale(0, RoundingMode.CEILING).intValue();
+
+                        Integer priceOnsale = promotionPriceOnsale.multiply(num).subtract(new BigDecimal(goodsYh)).intValue();
+                        goods.set("priceOnsale", priceOnsale);
+                        goods.set("msg", goods.getString("msg") + "满减活动减:" + (goodsYh * 0.01) + "元;");
+                    }
+                }
+            }
+        }
+
         param.getListMap("goodsList");
         param.put("goodsList", goodsList);
         return param;