1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
@Service @Slf4j public class UserAccountService {
@Autowired private UserAccountMapper userAccountMapper; @Autowired private AccountDetailMapper accountDetailMapper; @Autowired private RedissonClient redissonClient;
@Transactional(rollbackFor = Exception.class) public AccountDeductResult deductAccount(AccountDeductRequest request) { String lockKey = "account:lock:" + request.getUserId(); RLock lock = redissonClient.getLock(lockKey); try { if (lock.tryLock(10, TimeUnit.SECONDS)) { UserAccount account = userAccountMapper.selectByUserId(request.getUserId()); if (account == null) { return AccountDeductResult.failed("用户账户不存在"); } if (account.getStatus() != AccountStatus.NORMAL) { return AccountDeductResult.failed("账户状态异常,无法扣款"); } if (account.getBalance().compareTo(request.getAmount()) < 0) { return AccountDeductResult.failed("账户余额不足,当前余额: " + account.getBalance()); } BigDecimal balanceBefore = account.getBalance(); account.setBalance(account.getBalance().subtract(request.getAmount())); account.setAvailableAmount(account.getAvailableAmount().subtract(request.getAmount())); account.setTotalExpense(account.getTotalExpense().add(request.getAmount())); account.setUpdateTime(new Date()); userAccountMapper.updateById(account); String transactionNo = generateTransactionNo(); recordAccountDetail(account, request.getAmount(), AccountDetailType.PAYMENT, balanceBefore, account.getBalance(), request.getOrderId(), request.getOrderNo(), transactionNo); log.info("账户扣款成功: userId={}, amount={}, balanceBefore={}, balanceAfter={}, transactionNo={}", request.getUserId(), request.getAmount(), balanceBefore, account.getBalance(), transactionNo); AccountDeductResult result = new AccountDeductResult(); result.setSuccess(true); result.setTransactionNo(transactionNo); result.setBalanceBefore(balanceBefore); result.setBalanceAfter(account.getBalance()); result.setMessage("账户扣款成功"); return result; } else { return AccountDeductResult.failed("账户操作超时,请稍后再试"); } } catch (Exception e) { log.error("账户扣款失败: userId={}, amount={}, error={}", request.getUserId(), request.getAmount(), e.getMessage(), e); return AccountDeductResult.failed("账户扣款失败: " + e.getMessage()); } finally { if (lock.isHeldByCurrentThread()) { lock.unlock(); } } }
@Transactional(rollbackFor = Exception.class) public void rollbackAccountDeduct(String transactionNo) { try { AccountDetail detail = accountDetailMapper.selectByTransactionNo(transactionNo); if (detail == null) { log.warn("账户明细不存在,无法回滚: transactionNo={}", transactionNo); return; } if (detail.getStatus() == AccountDetailStatus.ROLLBACK) { log.warn("账户扣款已回滚: transactionNo={}", transactionNo); return; } UserAccount account = userAccountMapper.selectByUserId(detail.getUserId()); if (account == null) { log.error("用户账户不存在,无法回滚: userId={}", detail.getUserId()); return; } account.setBalance(account.getBalance().add(detail.getAmount())); account.setAvailableAmount(account.getAvailableAmount().add(detail.getAmount())); account.setTotalExpense(account.getTotalExpense().subtract(detail.getAmount())); account.setUpdateTime(new Date()); userAccountMapper.updateById(account); recordAccountDetail(account, detail.getAmount(), AccountDetailType.ROLLBACK, account.getBalance().subtract(detail.getAmount()), account.getBalance(), detail.getOrderId(), detail.getOrderNo(), generateTransactionNo()); detail.setStatus(AccountDetailStatus.ROLLBACK); detail.setUpdateTime(new Date()); accountDetailMapper.updateById(detail); log.info("账户扣款回滚成功: transactionNo={}, amount={}", transactionNo, detail.getAmount()); } catch (Exception e) { log.error("账户扣款回滚失败: transactionNo={}, error={}", transactionNo, e.getMessage(), e); throw e; } }
public void recordAccountDetail(AccountDetailRequest request) { try { UserAccount account = userAccountMapper.selectByUserId(request.getUserId()); if (account == null) { log.error("用户账户不存在: userId={}", request.getUserId()); return; } BigDecimal balanceAfter = account.getBalance(); BigDecimal balanceBefore = balanceAfter.add(request.getAmount()); recordAccountDetail(account, request.getAmount(), AccountDetailType.PAYMENT, balanceBefore, balanceAfter, request.getOrderId(), request.getOrderNo(), request.getTransactionNo()); } catch (Exception e) { log.error("记录账户明细失败: userId={}, error={}", request.getUserId(), e.getMessage(), e); } }
private void recordAccountDetail(UserAccount account, BigDecimal amount, AccountDetailType type, BigDecimal balanceBefore, BigDecimal balanceAfter, Long orderId, String orderNo, String transactionNo) { AccountDetail detail = new AccountDetail(); detail.setAccountId(account.getId()); detail.setUserId(account.getUserId()); detail.setAccountNo(account.getAccountNo()); detail.setTransactionNo(transactionNo); detail.setOrderId(orderId); detail.setOrderNo(orderNo); detail.setType(type.getCode()); detail.setTypeName(type.getName()); detail.setAmount(amount); detail.setBalanceBefore(balanceBefore); detail.setBalanceAfter(balanceAfter); detail.setDirection(type.getDirection()); detail.setStatus(AccountDetailStatus.SUCCESS); detail.setRemark(type.getRemark()); detail.setCreateTime(new Date()); detail.setUpdateTime(new Date()); accountDetailMapper.insert(detail); }
private String generateTransactionNo() { return "TXN" + System.currentTimeMillis() + RandomUtils.nextInt(10000, 99999); } }
|