订单折扣问题

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

View File

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

View File

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

View File

@@ -12,6 +12,7 @@ import com.czg.config.RedisCst;
import com.czg.entity.req.*; import com.czg.entity.req.*;
import com.czg.entity.resp.*; import com.czg.entity.resp.*;
import com.czg.enums.ShopUserFlowBizEnum; import com.czg.enums.ShopUserFlowBizEnum;
import com.czg.exception.PaySuccessException;
import com.czg.order.dto.CheckOrderPay; import com.czg.order.dto.CheckOrderPay;
import com.czg.order.dto.OrderInfoRefundDTO; import com.czg.order.dto.OrderInfoRefundDTO;
import com.czg.order.entity.OrderDetail; import com.czg.order.entity.OrderDetail;
@@ -89,7 +90,14 @@ public class PayServiceImpl implements PayService {
private final BigDecimal MONEY_RATE = new BigDecimal("100"); private final BigDecimal MONEY_RATE = new BigDecimal("100");
private OrderInfo checkPay(CheckOrderPay checkOrderPay) { 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

@@ -5,34 +5,36 @@
<mapper namespace="com.czg.service.order.mapper.CashierCartMapper"> <mapper namespace="com.czg.service.order.mapper.CashierCartMapper">
<select id="getCartByTableCode" resultType="com.czg.order.entity.OrderDetail"> <select id="getCartByTableCode" resultType="com.czg.order.entity.OrderDetail">
select cart.shop_id as shopId, select cart.shop_id as shopId,
cart.number as num, cart.number as num,
pros.pack_fee as packAmount, pros.pack_fee as packAmount,
cart.pack_number as packNumber, cart.pack_number as packNumber,
cart.is_temporary as isTemporary, cart.is_temporary as isTemporary,
cart.discount_sale_note as discountSaleNote, cart.discount_sale_note as discountSaleNote,
cart.is_print as isPrint, cart.is_print as isPrint,
cart.is_wait_call as isWaitCall, cart.is_wait_call as isWaitCall,
cart.pro_group_info as proGroupInfo, cart.pro_group_info as proGroupInfo,
cart.remark as remark, cart.remark as remark,
cart.product_id as productId, cart.product_id as productId,
pros.cover_img as productImg, pros.cover_img as productImg,
pros.type as productType, pros.type as productType,
cart.sku_id as skuId, cart.sku_id as skuId,
skus.spec_info as skuName, skus.spec_info as skuName,
cart.discount_sale_amount as discountSaleAmount,
case cart.is_gift case cart.is_gift
when 1 then 0 when 1 then 0
else skus.sale_price end as price, else cart.discount_sale_amount end as discountSaleAmount,
case cart.is_gift case cart.is_gift
when 1 then 0 when 1 then 0
else skus.member_price end as memberPrice, else skus.sale_price end as price,
'wait-pay' as status, case cart.is_gift
#{placeNum} as placeNum, when 1 then 0
else skus.member_price end as memberPrice,
'wait-pay' as status,
#{placeNum} as placeNum,
case cart.is_temporary case cart.is_temporary
when 1 then cart.product_name when 1 then cart.product_name
else pros.name else pros.name
end as productName end as productName
from tb_cashier_cart cart from tb_cashier_cart cart
left join tb_product pros on cart.product_id = pros.id left join tb_product pros on cart.product_id = pros.id
left join tb_prod_sku skus on cart.sku_id = skus.id left join tb_prod_sku skus on cart.sku_id = skus.id