From 3d894ca7f50891c1b255b1a24b66f34f3fdaa228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=BF=E6=9E=97=E7=8C=9B?= Date: Fri, 18 Aug 2023 19:51:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A#320=20=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E6=89=8B=E7=BB=AD=E8=B4=B9=E8=AE=A1=E7=AE=97=E4=B8=8D?= =?UTF-8?q?=E6=AD=A3=E7=A1=AE=EF=BC=8C=E8=B6=85=E8=BF=87=E4=BB=98=E6=AC=BE?= =?UTF-8?q?=E9=87=91=E9=A2=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 0df2d7191f7a467f2eeeb5bd19217a4a039395db) --- .../service/order/PayOrderServiceImpl.java | 4 ++- .../yudao/module/pay/util/MoneyUtils.java | 28 +++++++++---------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceImpl.java index 20bb26087..19734e8bc 100755 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceImpl.java @@ -39,6 +39,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; +import java.math.RoundingMode; import java.time.LocalDateTime; import java.util.List; import java.util.Objects; @@ -345,7 +346,8 @@ public class PayOrderServiceImpl implements PayOrderService { .channelId(channel.getId()).channelCode(channel.getCode()) .successTime(notify.getSuccessTime()).extensionId(orderExtension.getId()).no(orderExtension.getNo()) .channelOrderNo(notify.getChannelOrderNo()).channelUserId(notify.getChannelUserId()) - .channelFeeRate(channel.getFeeRate()).channelFeePrice(MoneyUtils.calculateRatePrice(order.getPrice(), channel.getFeeRate())) + .channelFeeRate(channel.getFeeRate()) + .channelFeePrice(MoneyUtils.calculateRatePrice(order.getPrice(), channel.getFeeRate(), 0, RoundingMode.HALF_UP).intValue()) .build()); if (updateCounts == 0) { // 校验状态,必须是待支付 throw exception(ORDER_STATUS_IS_NOT_WAITING); diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/MoneyUtils.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/MoneyUtils.java index 76cf5bfa3..567c02bec 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/MoneyUtils.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/util/MoneyUtils.java @@ -3,25 +3,25 @@ package cn.iocoder.yudao.module.pay.util; import java.math.BigDecimal; import java.math.RoundingMode; +import cn.hutool.core.util.NumberUtil; + /** * 金额工具类 * * @author 芋道源码 */ public class MoneyUtils { - - /** - * 计算百分比金额,四舍五入 - * - * @param price 金额 - * @param rate 百分比,例如说 56.77% 则传入 56.77 - * @return 百分比金额 - */ - public static Integer calculateRatePrice(Integer price, Double rate) { - return new BigDecimal(price) - .multiply(BigDecimal.valueOf(rate)) // 乘以 - .setScale(0, RoundingMode.HALF_UP) // 四舍五入 - .intValue(); - } + /** + * 计算百分比金额 + * + * @param price 金额 + * @param rate 百分比,例如说 56.77% 则传入 56.77 + * @param scale 保留小数位数 + * @param roundingMode 舍入模式 + */ + public static BigDecimal calculateRatePrice(Number price, Number rate, int scale, RoundingMode roundingMode) { + return NumberUtil.toBigDecimal(price).multiply(NumberUtil.toBigDecimal(rate)) // 乘以 + .divide(BigDecimal.valueOf(100), scale, roundingMode); // 除以100 + } }