From ba444c953811a681535734d5c24cc379ba9e4e77 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Thu, 31 Aug 2023 20:13:12 +0800 Subject: [PATCH] =?UTF-8?q?code=20review=EF=BC=9A=E6=94=AF=E4=BB=98?= =?UTF-8?q?=E9=92=B1=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pay/core/client/PayClientFactory.java | 7 ++++--- .../pay/core/client/impl/PayClientFactoryImpl.java | 14 +++++++------- .../pay/framework/pay/wallet/WalletPayClient.java | 10 +++++----- .../pay/service/channel/PayChannelServiceImpl.java | 2 ++ .../pay/service/wallet/PayWalletServiceImpl.java | 1 + 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java index 12b8cd8d3..53f1a8c06 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/PayClientFactory.java @@ -28,10 +28,11 @@ public interface PayClientFactory { Config config); /** - * 注册支付客户端 Class, 用于模块中实现的 PayClient + * 注册支付客户端 Class,用于模块中实现的 PayClient * - * @param payChannelEnum 支付渠道的编码的枚举 + * @param channel 支付渠道的编码的枚举 * @param payClientClass 支付客户端 class */ - void registerPayClientClass(PayChannelEnum payChannelEnum, Class payClientClass); + void registerPayClientClass(PayChannelEnum channel, Class payClientClass); + } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java index b9ab05830..0b39587ab 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-pay/src/main/java/cn/iocoder/yudao/framework/pay/core/client/impl/PayClientFactoryImpl.java @@ -27,15 +27,15 @@ public class PayClientFactoryImpl implements PayClientFactory { /** * 支付客户端 Map + * * key:渠道编号 */ private final ConcurrentMap> clients = new ConcurrentHashMap<>(); /** * 支付客户端 Class Map - * key: 支付渠道的编码的枚举 */ - private final Map>clientClass = new ConcurrentHashMap<>(16); + private final Map> clientClass = new ConcurrentHashMap<>(); public PayClientFactoryImpl() { // 微信支付客户端 @@ -54,6 +54,11 @@ public class PayClientFactoryImpl implements PayClientFactory { clientClass.put(MOCK, MockPayClient.class); } + @Override + public void registerPayClientClass(PayChannelEnum channel, Class payClientClass) { + clientClass.put(channel, payClientClass); + } + @Override public PayClient getPayClient(Long channelId) { AbstractPayClient client = clients.get(channelId); @@ -77,11 +82,6 @@ public class PayClientFactoryImpl implements PayClientFactory { } } - @Override - public void registerPayClientClass(PayChannelEnum payChannelEnum, Class payClientClass) { - clientClass.put(payChannelEnum, payClientClass); - } - @SuppressWarnings("unchecked") private AbstractPayClient createPayClient(Long channelId, String channelCode, Config config) { diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java index 55b8c3cfd..7e23f5cf7 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/framework/pay/wallet/WalletPayClient.java @@ -25,7 +25,7 @@ import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeC @Slf4j public class WalletPayClient extends AbstractPayClient { - private PayWalletService client; + private PayWalletService wallService; public WalletPayClient(Long channelId, NonePayClientConfig config) { super(channelId, PayChannelEnum.WALLET.getCode(), config); @@ -33,15 +33,15 @@ public class WalletPayClient extends AbstractPayClient { @Override protected void doInit() { - if (client == null) { - client = SpringUtil.getBean(PayWalletService.class); + if (wallService == null) { + wallService = SpringUtil.getBean(PayWalletService.class); } } @Override protected PayOrderRespDTO doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) { try { - PayWalletTransactionDO transaction = client.pay(reqDTO.getOutTradeNo(), reqDTO.getPrice()); + PayWalletTransactionDO transaction = wallService.pay(reqDTO.getOutTradeNo(), reqDTO.getPrice()); return PayOrderRespDTO.successOf(transaction.getNo(), transaction.getCreator(), transaction.getTransactionTime(), reqDTO.getOutTradeNo(), transaction); @@ -72,7 +72,7 @@ public class WalletPayClient extends AbstractPayClient { @Override protected PayRefundRespDTO doUnifiedRefund(PayRefundUnifiedReqDTO reqDTO) { try { - PayWalletTransactionDO payWalletTransaction = client.refund(reqDTO.getOutRefundNo(), + PayWalletTransactionDO payWalletTransaction = wallService.refund(reqDTO.getOutRefundNo(), reqDTO.getRefundPrice(), reqDTO.getReason()); return PayRefundRespDTO.successOf(payWalletTransaction.getNo(), payWalletTransaction.getTransactionTime(), reqDTO.getOutRefundNo(), payWalletTransaction); diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java index e1cfe7d28..37cf1bfc9 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/channel/PayChannelServiceImpl.java @@ -65,6 +65,7 @@ public class PayChannelServiceImpl implements PayChannelService { public void initLocalCache() { // 注册钱包支付 Class payClientFactory.registerPayClientClass(PayChannelEnum.WALLET, WalletPayClient.class); + // 注意:忽略自动多租户,因为要全局初始化缓存 TenantUtils.executeIgnore(() -> { // 第一步:查询数据 @@ -78,6 +79,7 @@ public class PayChannelServiceImpl implements PayChannelService { log.error("[支付模块 yudao-module-pay - 表结构未导入][参考 https://doc.iocoder.cn/pay/build/ 开启]"); } log.info("[initLocalCache][缓存支付渠道,数量为:{}]", channels.size()); + // 第二步:构建缓存:创建或更新支付 Client channels.forEach(payChannel -> payClientFactory.createOrUpdatePayClient(payChannel.getId(), payChannel.getCode(), payChannel.getConfig())); diff --git a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java index a52b12d6d..6992d63f6 100644 --- a/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/main/java/cn/iocoder/yudao/module/pay/service/wallet/PayWalletServiceImpl.java @@ -45,6 +45,7 @@ public class PayWalletServiceImpl implements PayWalletService { private PayWalletMapper payWalletMapper; @Resource private PayNoRedisDAO noRedisDAO; + @Resource private PayWalletTransactionService payWalletTransactionService; @Resource