|
|
|
|
@ -6,10 +6,10 @@ import cn.iocoder.yudao.module.pay.dal.dataobject.refund.PayRefundDO;
|
|
|
|
|
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
|
|
|
|
|
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletTransactionDO;
|
|
|
|
|
import cn.iocoder.yudao.module.pay.dal.mysql.wallet.PayWalletMapper;
|
|
|
|
|
import cn.iocoder.yudao.module.pay.dal.redis.no.PayNoRedisDAO;
|
|
|
|
|
import cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum;
|
|
|
|
|
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
|
|
|
|
|
import cn.iocoder.yudao.module.pay.service.refund.PayRefundService;
|
|
|
|
|
import cn.iocoder.yudao.module.pay.service.wallet.bo.CreateWalletTransactionBO;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
@ -18,7 +18,6 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.TOO_MANY_REQUESTS;
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
|
|
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;
|
|
|
|
|
import static cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum.PAYMENT;
|
|
|
|
|
@ -33,20 +32,8 @@ import static cn.iocoder.yudao.module.pay.enums.member.PayWalletBizTypeEnum.PAYM
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class PayWalletServiceImpl implements PayWalletService {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 余额支付的 no 前缀
|
|
|
|
|
*/
|
|
|
|
|
private static final String WALLET_PAY_NO_PREFIX = "WP";
|
|
|
|
|
/**
|
|
|
|
|
* 余额退款的 no 前缀
|
|
|
|
|
*/
|
|
|
|
|
private static final String WALLET_REFUND_NO_PREFIX = "WR";
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private PayWalletMapper walletMapper;
|
|
|
|
|
@Resource
|
|
|
|
|
private PayNoRedisDAO noRedisDAO;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private PayWalletTransactionService walletTransactionService;
|
|
|
|
|
@Resource
|
|
|
|
|
@ -61,7 +48,7 @@ public class PayWalletServiceImpl implements PayWalletService {
|
|
|
|
|
PayWalletDO wallet = walletMapper.selectByUserIdAndType(userId, userType);
|
|
|
|
|
if (wallet == null) {
|
|
|
|
|
wallet = new PayWalletDO().setUserId(userId).setUserType(userType)
|
|
|
|
|
.setBalance(0).setTotalExpense(0L).setTotalRecharge(0L);
|
|
|
|
|
.setBalance(0).setTotalExpense(0).setTotalRecharge(0);
|
|
|
|
|
wallet.setCreateTime(LocalDateTime.now());
|
|
|
|
|
walletMapper.insert(wallet);
|
|
|
|
|
}
|
|
|
|
|
@ -127,68 +114,50 @@ public class PayWalletServiceImpl implements PayWalletService {
|
|
|
|
|
Long bizId, PayWalletBizTypeEnum bizType, Integer price) {
|
|
|
|
|
// 1.1 获取钱包
|
|
|
|
|
PayWalletDO payWallet = getOrCreateWallet(userId, userType);
|
|
|
|
|
// 1.2 判断余额是否足够
|
|
|
|
|
int afterBalance = payWallet.getBalance() - price;
|
|
|
|
|
if (afterBalance < 0) {
|
|
|
|
|
throw exception(WALLET_BALANCE_NOT_ENOUGH);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO jason:建议基于 where price >= 来做哈;然后抛出 WALLET_BALANCE_NOT_ENOUGH
|
|
|
|
|
// 2.1 扣除余额
|
|
|
|
|
int number = walletMapper.updateWhenDecBalance(bizType, payWallet.getBalance(),
|
|
|
|
|
payWallet.getTotalRecharge(), payWallet.getTotalExpense(), price, payWallet.getId());
|
|
|
|
|
int number = 0 ;
|
|
|
|
|
switch (bizType) {
|
|
|
|
|
case PAYMENT: {
|
|
|
|
|
number = walletMapper.updateWhenConsumption(price, payWallet.getId());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case RECHARGE_REFUND: {
|
|
|
|
|
// TODO
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (number == 0) {
|
|
|
|
|
throw exception(TOO_MANY_REQUESTS);
|
|
|
|
|
throw exception(WALLET_BALANCE_NOT_ENOUGH);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int afterBalance = payWallet.getBalance() - price;
|
|
|
|
|
// 2.2 生成钱包流水
|
|
|
|
|
// TODO @jason:walletNo 交给 payWalletTransactionService 自己生成哈;
|
|
|
|
|
String walletNo = generateWalletNo(bizType);
|
|
|
|
|
PayWalletTransactionDO walletTransaction = new PayWalletTransactionDO().setWalletId(payWallet.getId())
|
|
|
|
|
.setNo(walletNo).setPrice(-price).setBalance(afterBalance)
|
|
|
|
|
.setBizId(String.valueOf(bizId)).setBizType(bizType.getType()).setTitle(bizType.getDescription());
|
|
|
|
|
// TODO @jason:是不是可以 createWalletTransaction 搞个 bo 参数,然后 PayWalletTransactionDO 交回给 walletTransactionService 更好;然后把参数简化下
|
|
|
|
|
walletTransactionService.createWalletTransaction(walletTransaction);
|
|
|
|
|
return walletTransaction;
|
|
|
|
|
CreateWalletTransactionBO bo = new CreateWalletTransactionBO().setWalletId(payWallet.getId())
|
|
|
|
|
.setPrice(-price).setBalance(afterBalance).setBizId(String.valueOf(bizId))
|
|
|
|
|
.setBizType(bizType.getType()).setTitle(bizType.getDescription());
|
|
|
|
|
return walletTransactionService.createWalletTransaction(bo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public PayWalletTransactionDO addWalletBalance(Long userId, Integer userType,
|
|
|
|
|
Long bizId, PayWalletBizTypeEnum bizType, Integer price) {
|
|
|
|
|
// 1.1 获取钱包
|
|
|
|
|
// 获取钱包
|
|
|
|
|
PayWalletDO payWallet = getOrCreateWallet(userId, userType);
|
|
|
|
|
|
|
|
|
|
// 2.1 增加余额
|
|
|
|
|
// TODO @jason:类似上面的思路哈;
|
|
|
|
|
int number = walletMapper.updateWhenIncBalance(bizType, payWallet.getBalance(), payWallet.getTotalRecharge(),
|
|
|
|
|
payWallet.getTotalExpense(), price, payWallet.getId());
|
|
|
|
|
if (number == 0) {
|
|
|
|
|
throw exception(TOO_MANY_REQUESTS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2.2 生成钱包流水
|
|
|
|
|
String walletNo = generateWalletNo(bizType);
|
|
|
|
|
PayWalletTransactionDO newWalletTransaction = new PayWalletTransactionDO().setWalletId(payWallet.getId())
|
|
|
|
|
.setNo(walletNo).setPrice(price).setBalance(payWallet.getBalance()+price)
|
|
|
|
|
.setBizId(String.valueOf(bizId)).setBizType(bizType.getType())
|
|
|
|
|
.setTitle(bizType.getDescription());
|
|
|
|
|
walletTransactionService.createWalletTransaction(newWalletTransaction);
|
|
|
|
|
return newWalletTransaction;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String generateWalletNo(PayWalletBizTypeEnum bizType) {
|
|
|
|
|
// TODO @jason:对于余额来说,是不是直接 W+序号就行了,它其实不关注业务;;;不然就耦合啦
|
|
|
|
|
String no = "";
|
|
|
|
|
switch(bizType){
|
|
|
|
|
case PAYMENT:
|
|
|
|
|
no = noRedisDAO.generate(WALLET_PAY_NO_PREFIX);
|
|
|
|
|
switch (bizType) {
|
|
|
|
|
case PAYMENT_REFUND: {
|
|
|
|
|
// 更新退款
|
|
|
|
|
walletMapper.updateWhenConsumptionRefund(price, payWallet.getId());
|
|
|
|
|
break;
|
|
|
|
|
case PAYMENT_REFUND:
|
|
|
|
|
no = noRedisDAO.generate(WALLET_REFUND_NO_PREFIX);
|
|
|
|
|
}
|
|
|
|
|
case RECHARGE: {
|
|
|
|
|
//TODO
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return no;
|
|
|
|
|
// 2.2 生成钱包流水
|
|
|
|
|
CreateWalletTransactionBO bo = new CreateWalletTransactionBO().setWalletId(payWallet.getId())
|
|
|
|
|
.setPrice(price).setBalance(payWallet.getBalance()+price).setBizId(String.valueOf(bizId))
|
|
|
|
|
.setBizType(bizType.getType()).setTitle(bizType.getDescription());
|
|
|
|
|
return walletTransactionService.createWalletTransaction(bo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|