Cui Zhi Feng
2024-09-12 715ba070dde400a6697b0b334fe8e9d2e5706f80
下单优惠券计算
已修改1个文件
33 ■■■■ 文件已修改
src/main/java/com/mzl/flower/service/payment/OrderService.java 33 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/mzl/flower/service/payment/OrderService.java
@@ -442,7 +442,7 @@
        order.setTransportFee(transportFee);
        order.setDeliveryName(deliveryName);
        BigDecimal totalAmount = order.getFlowerAmount().add(p.getPacking()).add(transportFee);//使用优惠券之前的总价
        BigDecimal totalAmount = order.getFlowerAmount();//使用优惠券之前的总价,使用商品价格
        CouponRecordDO coupon = useCouponRecord(order.getId(), totalAmount, dto.getCouponRecordId());
        log.info("用户优惠券: " + coupon);
        if(coupon != null){
@@ -453,7 +453,14 @@
            totalAmount = totalAmount.subtract(order.getMemberCouponAmount());//满足条件需要减去优惠券金额
        }
        order.setTotalAmount(totalAmount);
        if(totalAmount.doubleValue() < 0){//假如扣除优惠券后金额小于0,则按0计算
            totalAmount = new BigDecimal(0);
        }
        totalAmount = totalAmount.add(p.getPacking()).add(transportFee);
        if(totalAmount.doubleValue() <= 0){//假如总价小于等于0,则支付0.01元
            totalAmount = new BigDecimal(0.01);
        }
        order.setTotalAmount(totalAmount.add(p.getPacking()).add(transportFee));
        order.setCustomer(address.getName());
        order.setCustomerTel(address.getTel());
@@ -475,7 +482,9 @@
        Map<Long, List<ParamItemDTO>> paramMap = new HashMap<>();
        final BigDecimal flowerAmount = order.getFlowerAmount();//订单商品总价
        final BigDecimal memberCouponAmount = order.getMemberCouponAmount();//使用优惠券面值
        for (FlowerCartListDTO f : flowers) {
        BigDecimal usedCouponAmount = new BigDecimal(0);
        for (int i = 0; i < flowers.size(); i++) {
            FlowerCartListDTO f = flowers.get(i);
            OrderItem t = new OrderItem();
            t.setId(UUIDGenerator.getUUID());
            t.setOrderId(order.getId());
@@ -504,9 +513,13 @@
            t.setTotal(f.getTotalMember());//使用会员总价
            t.setOriginalPrice(pp.getPrice());//非会员售价
            BigDecimal couponAmount = calculateCoupon(memberCouponAmount, t.getTotal(), flowerAmount, t.getNum());
            boolean isLastOne = i == (flowers.size() - 1);
            BigDecimal couponAmount = calculateCoupon(memberCouponAmount, t.getTotal()
                    , flowerAmount, t.getNum(), t.getPrice(), isLastOne, usedCouponAmount);
            t.setCouponAmount(couponAmount);//每扎平摊的优惠券面值
            t.setRealPrice(t.getPrice().subtract(couponAmount));//退款时使用的真实成交价
            usedCouponAmount = usedCouponAmount.add(couponAmount.multiply(BigDecimal.valueOf(t.getNum())));
            t.create(userId);
            orderItemMapper.insert(t);
@@ -551,13 +564,23 @@
     * @param itemTotalAmount
     * @param totalAmount
     * @param num
     * @param price
     * @return
     */
    private BigDecimal calculateCoupon(BigDecimal couponAmount, BigDecimal itemTotalAmount
            , BigDecimal totalAmount, Integer num){
            , BigDecimal totalAmount, Integer num, BigDecimal price
            , boolean isLastOne, BigDecimal usedCouponAmount){
        if(couponAmount == null){
            return new BigDecimal(0);
        }
        if(couponAmount.doubleValue() >= totalAmount.doubleValue()){
            return price;
        }
        if(isLastOne){//最后的商品使用优惠券剩下的面值
            BigDecimal subCoupon = couponAmount.subtract(usedCouponAmount);//占有的优惠券面值
            return subCoupon.divide(BigDecimal.valueOf(num), 2, RoundingMode.HALF_UP);
        }
        BigDecimal radio = itemTotalAmount.divide(totalAmount, 2, RoundingMode.HALF_UP);//计算该商品总价在订单中的占比
        BigDecimal subCoupon = couponAmount.multiply(radio);//占有的优惠券面值
        return subCoupon.divide(BigDecimal.valueOf(num), 2, RoundingMode.HALF_UP);