订单折扣问题

付款金额0的问题
This commit is contained in:
2025-03-03 14:19:19 +08:00
parent 9c35a6e2cc
commit f757f0698d
7 changed files with 69 additions and 33 deletions

View File

@@ -95,6 +95,15 @@ public class CzgControllerAdvice {
return CzgResult.failure(ex.getCode(), ex.getMsg());
}
/**
* 支付成功
* 支付金额为零的统一处理
*/
@ExceptionHandler(PaySuccessException.class)
public CzgResult<Object> handlePaySuccessException() {
return CzgResult.success();
}
/**
* 处理Hutool的断言抛出异常
*/

View File

@@ -9,6 +9,7 @@ import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
/**
@@ -93,7 +94,10 @@ public class CheckOrderPay implements Serializable {
}
public BigDecimal getDiscountRatio() {
return discountRatio == null ? BigDecimal.ONE : discountRatio;
if (discountRatio == null) {
return BigDecimal.ZERO;
}
return discountRatio.setScale(2, RoundingMode.UP);
}
public BigDecimal getFullCouponDiscountAmount() {

View File

@@ -1,5 +1,3 @@
package com.czg.exception;
import com.czg.resp.CzgRespCode;

View File

@@ -0,0 +1,16 @@
package com.czg.exception;
import java.io.Serial;
/**
* 自定义异常
*
* @author admin admin@cashier.com
* @since 1.0.0
*/
public class PaySuccessException extends RuntimeException {
@Serial
private static final long serialVersionUID = 1L;
}

View File

@@ -242,7 +242,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
}
param.setUserId(userInfo.getId());
}
if (param.getDiscountRatio().compareTo(BigDecimal.ZERO) <= 0 && param.getDiscountRatio().compareTo(BigDecimal.ONE) > 0) {
if (param.getDiscountRatio().compareTo(BigDecimal.ZERO) < 0 || param.getDiscountRatio().compareTo(BigDecimal.ONE) > 0) {
throw new ValidateException("生成支付订单失败,折扣比例不正确");
}
//商品券 <商品id数量>
@@ -266,16 +266,15 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
throw new ValidateException("生成支付订单失败,商品优惠券优惠金额不正确");
}
orderInfo.setOriginAmount(totalAmount.getPrice());
BigDecimal newTotalAmount;
BigDecimal newTotalAmount = totalAmount.getPrice();
//折扣金额 如 9折 计算 为 订单金额*0.9 向上取整
if (param.getDiscountRatio().compareTo(BigDecimal.ONE) != 0) {
if(param.getDiscountRatio().compareTo(BigDecimal.ZERO) > 0) {
newTotalAmount = totalAmount.getPrice().multiply(param.getDiscountRatio()).setScale(2, RoundingMode.UP);
if (param.getDiscountAmount().compareTo(totalAmount.getPrice().subtract(newTotalAmount)) != 0) {
throw new ValidateException("生成支付订单失败,折扣金额不正确");
}
} else {
newTotalAmount = totalAmount.getPrice();
}else if(param.getDiscountAmount().compareTo(BigDecimal.ZERO) > 0) {
newTotalAmount = totalAmount.getPrice().subtract(param.getDiscountAmount());
}
//满减券 校验
if (newTotalAmount.compareTo(fullAmount.getPrice()) < 0) {
throw new ValidateException("生成支付订单失败,满减券不满足条件");

View File

@@ -12,6 +12,7 @@ import com.czg.config.RedisCst;
import com.czg.entity.req.*;
import com.czg.entity.resp.*;
import com.czg.enums.ShopUserFlowBizEnum;
import com.czg.exception.PaySuccessException;
import com.czg.order.dto.CheckOrderPay;
import com.czg.order.dto.OrderInfoRefundDTO;
import com.czg.order.entity.OrderDetail;
@@ -89,7 +90,14 @@ public class PayServiceImpl implements PayService {
private final BigDecimal MONEY_RATE = new BigDecimal("100");
private OrderInfo checkPay(CheckOrderPay checkOrderPay) {
return orderInfoService.checkOrderPay(checkOrderPay);
OrderInfo orderInfo = orderInfoService.checkOrderPay(checkOrderPay);
if (orderInfo.getOrderAmount().compareTo(BigDecimal.ZERO) == 0) {
orderInfoService.upOrderInfo(orderInfo, orderInfo.getOrderAmount(),
LocalDateTime.now(), null, PayEnums.CASH_PAY);
redisService.del(RedisCst.classKeyExpired.EXPIRED_ORDER + orderInfo.getId());
throw new PaySuccessException();
}
return orderInfo;
}

View File

@@ -20,7 +20,9 @@
pros.type as productType,
cart.sku_id as skuId,
skus.spec_info as skuName,
cart.discount_sale_amount as discountSaleAmount,
case cart.is_gift
when 1 then 0
else cart.discount_sale_amount end as discountSaleAmount,
case cart.is_gift
when 1 then 0
else skus.sale_price end as price,