fix:【pay】pay_wallet 表,部分用户出现两条钱包记录问题,对应 https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/1475

This commit is contained in:
YunaiV 2025-12-13 10:18:31 +08:00
parent 26237ee8da
commit 7347c1c9d6

View File

@ -58,13 +58,22 @@ public class PayWalletServiceImpl implements PayWalletService {
private PayRefundService refundService; private PayRefundService refundService;
@Override @Override
@SneakyThrows
public PayWalletDO getOrCreateWallet(Long userId, Integer userType) { public PayWalletDO getOrCreateWallet(Long userId, Integer userType) {
PayWalletDO wallet = walletMapper.selectByUserIdAndType(userId, userType); PayWalletDO wallet = walletMapper.selectByUserIdAndType(userId, userType);
if (wallet == null) { if (wallet == null) {
wallet = new PayWalletDO().setUserId(userId).setUserType(userType) // 使用双重检查锁保证钱包创建并发问题
.setBalance(0).setTotalExpense(0).setTotalRecharge(0); // https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/1475/files
wallet.setCreateTime(LocalDateTime.now()); wallet = lockRedisDAO.lock(userId, UPDATE_TIMEOUT_MILLIS, () -> {
walletMapper.insert(wallet); PayWalletDO newWallet = walletMapper.selectByUserIdAndType(userId, userType);
if (newWallet == null) {
newWallet = new PayWalletDO().setUserId(userId).setUserType(userType)
.setBalance(0).setTotalExpense(0).setTotalRecharge(0);
newWallet.setCreateTime(LocalDateTime.now());
walletMapper.insert(newWallet);
}
return newWallet;
});
} }
return wallet; return wallet;
} }