@@ -2,7 +2,7 @@ package cn.iocoder.yudao.module.pay.service.wallet;
import cn.hutool.core.date.LocalDateTimeUtil ;
import cn.hutool.core.lang.Assert ;
import cn.hutool.core.map.Map Util ;
import cn.hutool.extra.spring.Spring Util ;
import cn.iocoder.yudao.framework.common.pojo.PageParam ;
import cn.iocoder.yudao.framework.common.pojo.PageResult ;
import cn.iocoder.yudao.framework.pay.core.enums.refund.PayRefundStatusRespEnum ;
@@ -15,21 +15,22 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargeDO ;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletRechargePackageDO ;
import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletRechargeMapper ;
import cn.iocoder.yudao.module.pay.enums.MessageTemplateConstants ;
import cn.iocoder.yudao.module.pay.enums.order.PayOrderStatusEnum ;
import cn.iocoder.yudao.module.pay.enums.refund.PayRefundStatusEnum ;
import cn.iocoder.yudao.module.pay.enums.wallet.PayWalletBizTypeEnum ;
import cn.iocoder.yudao.module.pay.message.subscribe.SubscribeMessageClient ;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService ;
import cn.iocoder.yudao.module.pay.service.refund.PayRefundService ;
import cn.iocoder.yudao.module.system.api.social.SocialClientApi ;
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxSubscribeMessageSendReqDTO ;
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum ;
import jakarta.annotation.Resource ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.scheduling.annotation.Async ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import java.time.Duration ;
import java.time.LocalDateTime ;
import java.util.Map ;
import java.util.Objects ;
import static cn.hutool.core.util.ObjectUtil.notEqual ;
@@ -39,6 +40,7 @@ import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString
import static cn.iocoder.yudao.framework.common.util.number.MoneyUtils.fenToYuanStr ;
import static cn.iocoder.yudao.module.pay.convert.wallet.PayWalletRechargeConvert.INSTANCE ;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.* ;
import static cn.iocoder.yudao.module.pay.enums.MessageTemplateConstants.PAY_WALLET_CHANGE ;
import static cn.iocoder.yudao.module.pay.enums.refund.PayRefundStatusEnum.* ;
/**
@@ -57,6 +59,8 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
private static final String WALLET_RECHARGE_ORDER_SUBJECT = " 钱包余额充值 " ;
public static final String WALLET_MONEY_PATH = " pages/user/wallet/money " ; // 钱包详情页
@Resource
private PayWalletRechargeMapper walletRechargeMapper ;
@Resource
@@ -68,7 +72,7 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
@Resource
private PayWalletRechargePackageService payWalletRechargePackageService ;
@Resource
private SubscribeMessage Client subscribeMessage Client ;
public Social ClientApi social ClientApi ;
@Override
@Transactional ( rollbackFor = Exception . class )
@@ -136,21 +140,21 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
PayWalletBizTypeEnum . RECHARGE , walletRecharge . getTotalPrice ( ) ) ;
// 4. 发送订阅消息
sendPayWalletChangeMessage ( payOrderId , walletRecharge ) ;
getSelf ( ) . sendPayWalletChangeMessage( payOrderId , walletRecharge ) ;
}
// TODO @puhui999: 发送, 使用异步发送; @Async
private void sendPayWalletChangeMessage ( Long payOrderId , PayWalletRechargeDO walletRecharge ) {
@Async
public void sendPayWalletChangeMessage ( Long payOrderId , PayWalletRechargeDO walletRecharge ) {
// 1. 获得会员钱包信息
PayWalletDO wallet = payWalletService . getWallet ( walletRecharge . getWalletId ( ) ) ;
// TODO @puhui999: 可以使用 MapUtil.builder();另外,不应该是并发 hashmap 哈
Map < String , String > messages = MapUtil . newConcurrentHashMap ( 4 ) ;
messages . put ( MessageTemplateConstants . PayWalletChangeTemplateParams . NO , String . valueOf ( payOrderId ) ) ;
messages . put ( MessageTemplateConstants . PayWalletChangeTemplateParams . PRICE ,
fenToYuanStr ( walletRecharge . getTotalPrice ( ) ) ) ;
m essages . put ( MessageTemplateConstants . PayWalletChangeTemplateParams . STATUS , " 充值成功 " ) ;
m essages . put ( MessageTemplateConstants . PayW alletC han geTemplateParams . PAY_TIME ,
LocalDateTimeUtil . formatNormal ( LocalDateTime . now ( ) ) ) ;
subscribeMessageClient . sendPayWalletChangeMessage ( messages , wallet . getUserType ( ) , wallet . getUserId ( ) ) ;
// 2. 构建并发送模版消息
socialClientApi . sendSubscribeMessage ( new SocialWxSubscribeMessageSendReqDTO ( ) . setPage ( WALLET_MONEY_PATH )
. setUserId ( wallet . getUserId ( ) ) . setUserType ( wallet . getUserType ( ) ) . setTemplateTitle ( PAY_WALLET_CHANGE )
. setSocialType ( SocialTypeEnum . WECHAT_MINI_APP . getType ( ) )
// 添加模版消息
. addM essage ( " phrase4 " , " 充值成功 " ) . addMessage ( " character_string1 " , String . valueOf ( payOrderId ) )
. addM essage ( " amount2 " , fenToYuanStr ( w alletRec har ge. getTotalPrice ( ) ) )
. addMessage ( " time3 " , LocalDateTimeUtil . formatNormal ( LocalDateTime . now ( ) ) ) ) ;
}
@Override
@@ -307,4 +311,13 @@ public class PayWalletRechargeServiceImpl implements PayWalletRechargeService {
return payOrder ;
}
/**
* 获得自身的代理对象,解决 AOP 生效问题
*
* @return 自己
*/
private PayWalletRechargeServiceImpl getSelf ( ) {
return SpringUtil . getBean ( getClass ( ) ) ;
}
}