fix: 修复充值审批后资金账户余额未更新的问题
主要修改: 1. FundService.java - 所有账户更新都改用 LambdaUpdateWrapper 显式更新 - 充值审批通过:显式更新 balance, totalDeposit, updateTime - 提现审批通过:显式更新 frozen, totalWithdraw, updateTime - 提现审批驳回:显式更新 balance, frozen, updateTime - 取消订单:显式更新 balance, frozen, updateTime 2. 添加更新后立即验证机制 3. 添加详细的日志输出 4. 添加诊断文档 关键改进: - 使用 LambdaUpdateWrapper.set() 显式指定所有要更新的字段 - 更新后立即查询数据库验证 - 失败时抛出异常回滚事务 - 添加完整的日志追踪
This commit is contained in:
@@ -137,11 +137,32 @@ public class FundService {
|
||||
throw new RuntimeException("资金账户余额不足");
|
||||
}
|
||||
|
||||
// 冻结余额
|
||||
fund.setBalance(fund.getBalance().subtract(amount));
|
||||
fund.setFrozen(fund.getFrozen() != null ? fund.getFrozen().add(amount) : amount);
|
||||
fund.setUpdateTime(LocalDateTime.now());
|
||||
accountFundMapper.updateById(fund);
|
||||
// 冻结余额 - 使用 LambdaUpdateWrapper 显式更新
|
||||
BigDecimal newBalance = fund.getBalance().subtract(amount);
|
||||
BigDecimal newFrozen = fund.getFrozen() != null ? fund.getFrozen().add(amount) : amount;
|
||||
LocalDateTime updateTime = LocalDateTime.now();
|
||||
|
||||
System.out.println("[提现申请] 冻结余额: 用户ID=" + userId +
|
||||
", 原余额=" + fund.getBalance() + ", 新余额=" + newBalance +
|
||||
", 原冻结=" + fund.getFrozen() + ", 新冻结=" + newFrozen);
|
||||
|
||||
LambdaUpdateWrapper<AccountFund> fundUpdateWrapper = new LambdaUpdateWrapper<>();
|
||||
fundUpdateWrapper.eq(AccountFund::getId, fund.getId())
|
||||
.set(AccountFund::getBalance, newBalance)
|
||||
.set(AccountFund::getFrozen, newFrozen)
|
||||
.set(AccountFund::getUpdateTime, updateTime);
|
||||
int fundUpdateResult = accountFundMapper.update(null, fundUpdateWrapper);
|
||||
|
||||
if (fundUpdateResult <= 0) {
|
||||
System.err.println("[提现申请] 资金账户冻结失败!");
|
||||
throw new RuntimeException("资金账户冻结失败");
|
||||
}
|
||||
System.out.println("[提现申请] 资金账户冻结成功,更新行数: " + fundUpdateResult);
|
||||
|
||||
// 更新本地对象状态
|
||||
fund.setBalance(newBalance);
|
||||
fund.setFrozen(newFrozen);
|
||||
fund.setUpdateTime(updateTime);
|
||||
|
||||
// 创建订单
|
||||
OrderFund order = new OrderFund();
|
||||
@@ -188,12 +209,28 @@ public class FundService {
|
||||
if (order.getStatus() != 1) {
|
||||
throw new RuntimeException("当前状态不可取消");
|
||||
}
|
||||
// 解冻余额
|
||||
// 解冻余额 - 使用 LambdaUpdateWrapper 显式更新
|
||||
AccountFund fund = assetService.getOrCreateFundAccount(userId);
|
||||
fund.setBalance(fund.getBalance().add(order.getAmount()));
|
||||
fund.setFrozen(fund.getFrozen().subtract(order.getAmount()));
|
||||
fund.setUpdateTime(LocalDateTime.now());
|
||||
accountFundMapper.updateById(fund);
|
||||
BigDecimal newBalance = fund.getBalance().add(order.getAmount());
|
||||
BigDecimal newFrozen = fund.getFrozen().subtract(order.getAmount());
|
||||
LocalDateTime updateTime = LocalDateTime.now();
|
||||
|
||||
System.out.println("[取消订单] 解冻余额: 用户ID=" + userId +
|
||||
", 原余额=" + fund.getBalance() + ", 新余额=" + newBalance +
|
||||
", 原冻结=" + fund.getFrozen() + ", 新冻结=" + newFrozen);
|
||||
|
||||
LambdaUpdateWrapper<AccountFund> fundUpdateWrapper = new LambdaUpdateWrapper<>();
|
||||
fundUpdateWrapper.eq(AccountFund::getId, fund.getId())
|
||||
.set(AccountFund::getBalance, newBalance)
|
||||
.set(AccountFund::getFrozen, newFrozen)
|
||||
.set(AccountFund::getUpdateTime, updateTime);
|
||||
int fundUpdateResult = accountFundMapper.update(null, fundUpdateWrapper);
|
||||
|
||||
if (fundUpdateResult <= 0) {
|
||||
System.err.println("[取消订单] 资金账户解冻失败!");
|
||||
throw new RuntimeException("资金账户解冻失败");
|
||||
}
|
||||
System.out.println("[取消订单] 资金账户解冻成功,更新行数: " + fundUpdateResult);
|
||||
}
|
||||
|
||||
order.setStatus(5); // 已取消
|
||||
@@ -298,55 +335,105 @@ public class FundService {
|
||||
System.out.println("[FundService.approve] 步骤4: 处理审批通过逻辑...");
|
||||
|
||||
if (order.getType() == 1) {
|
||||
// 充值通过:增加余额
|
||||
// 充值通过:增加余额 - 使用 LambdaUpdateWrapper 显式更新
|
||||
System.out.println("[FundService.approve] 处理充值通过...");
|
||||
BigDecimal balanceBefore = fund.getBalance();
|
||||
BigDecimal totalDepositBefore = fund.getTotalDeposit();
|
||||
System.out.println(" - 审批前余额: " + balanceBefore);
|
||||
|
||||
fund.setBalance(fund.getBalance().add(order.getAmount()));
|
||||
fund.setTotalDeposit(fund.getTotalDeposit().add(order.getAmount()));
|
||||
fund.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
System.out.println(" - 准备更新账户余额: " + fund.getBalance());
|
||||
int updateResult = accountFundMapper.updateById(fund);
|
||||
System.out.println(" - 审批前累计充值: " + totalDepositBefore);
|
||||
|
||||
BigDecimal newBalance = fund.getBalance().add(order.getAmount());
|
||||
BigDecimal newTotalDeposit = fund.getTotalDeposit().add(order.getAmount());
|
||||
LocalDateTime updateTime = LocalDateTime.now();
|
||||
|
||||
System.out.println(" - 准备更新账户余额: " + newBalance);
|
||||
System.out.println(" - 准备更新累计充值: " + newTotalDeposit);
|
||||
|
||||
LambdaUpdateWrapper<AccountFund> fundUpdateWrapper = new LambdaUpdateWrapper<>();
|
||||
fundUpdateWrapper.eq(AccountFund::getId, fund.getId())
|
||||
.set(AccountFund::getBalance, newBalance)
|
||||
.set(AccountFund::getTotalDeposit, newTotalDeposit)
|
||||
.set(AccountFund::getUpdateTime, updateTime);
|
||||
|
||||
System.out.println(" - 执行 SQL UPDATE (使用 LambdaUpdateWrapper)...");
|
||||
int updateResult = accountFundMapper.update(null, fundUpdateWrapper);
|
||||
System.out.println(" - 账户更新结果: " + updateResult + " (1=成功, 0=失败)");
|
||||
System.out.println(" - 审批后余额: " + fund.getBalance());
|
||||
|
||||
if (updateResult <= 0) {
|
||||
System.err.println("[FundService.approve] 充值审批更新账户余额失败!");
|
||||
throw new RuntimeException("充值审批更新账户余额失败");
|
||||
}
|
||||
|
||||
// 验证更新结果
|
||||
AccountFund verifyFund = accountFundMapper.selectById(fund.getId());
|
||||
System.out.println(" - 验证更新后余额: " + verifyFund.getBalance());
|
||||
System.out.println(" - 验证更新后累计充值: " + verifyFund.getTotalDeposit());
|
||||
|
||||
// 更新本地对象状态
|
||||
fund.setBalance(newBalance);
|
||||
fund.setTotalDeposit(newTotalDeposit);
|
||||
fund.setUpdateTime(updateTime);
|
||||
|
||||
// 记录流水
|
||||
System.out.println("[FundService.approve] 创建资金流水记录...");
|
||||
assetService.createFlow(order.getUserId(), 1, order.getAmount(),
|
||||
balanceBefore, fund.getBalance(), "USDT", orderNo, "充值");
|
||||
balanceBefore, newBalance, "USDT", orderNo, "充值");
|
||||
System.out.println(" - 流水记录创建成功");
|
||||
|
||||
System.out.println("[充值审批成功] 订单号: " + orderNo + ", 用户ID: " + order.getUserId() +
|
||||
", 充值金额: " + order.getAmount() + " USDT");
|
||||
} else {
|
||||
// 提现通过:从冻结转为扣除,更新累计提现
|
||||
// 提现通过:从冻结转为扣除,更新累计提现 - 使用 LambdaUpdateWrapper 显式更新
|
||||
System.out.println("[FundService.approve] 处理提现通过...");
|
||||
if (fund.getFrozen().compareTo(order.getAmount()) < 0) {
|
||||
System.err.println("[FundService.approve] 冻结金额不足: 冻结=" + fund.getFrozen() +
|
||||
System.err.println("[FundService.approve] 冻结金额不足: 冻结=" + fund.getFrozen() +
|
||||
", 需要=" + order.getAmount());
|
||||
throw new RuntimeException("冻结金额不足");
|
||||
}
|
||||
|
||||
|
||||
BigDecimal balanceBefore = fund.getBalance();
|
||||
BigDecimal frozenBefore = fund.getFrozen();
|
||||
BigDecimal totalWithdrawBefore = fund.getTotalWithdraw();
|
||||
System.out.println(" - 审批前余额: " + balanceBefore);
|
||||
System.out.println(" - 审批前冻结: " + frozenBefore);
|
||||
|
||||
fund.setFrozen(fund.getFrozen().subtract(order.getAmount()));
|
||||
fund.setTotalWithdraw(fund.getTotalWithdraw().add(order.getAmount()));
|
||||
fund.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
System.out.println(" - 准备更新账户冻结: " + fund.getFrozen());
|
||||
int updateResult = accountFundMapper.updateById(fund);
|
||||
System.out.println(" - 审批前累计提现: " + totalWithdrawBefore);
|
||||
|
||||
BigDecimal newFrozen = fund.getFrozen().subtract(order.getAmount());
|
||||
BigDecimal newTotalWithdraw = fund.getTotalWithdraw().add(order.getAmount());
|
||||
LocalDateTime updateTime = LocalDateTime.now();
|
||||
|
||||
System.out.println(" - 准备更新账户冻结: " + newFrozen);
|
||||
System.out.println(" - 准备更新累计提现: " + newTotalWithdraw);
|
||||
|
||||
LambdaUpdateWrapper<AccountFund> fundUpdateWrapper = new LambdaUpdateWrapper<>();
|
||||
fundUpdateWrapper.eq(AccountFund::getId, fund.getId())
|
||||
.set(AccountFund::getFrozen, newFrozen)
|
||||
.set(AccountFund::getTotalWithdraw, newTotalWithdraw)
|
||||
.set(AccountFund::getUpdateTime, updateTime);
|
||||
|
||||
System.out.println(" - 执行 SQL UPDATE (使用 LambdaUpdateWrapper)...");
|
||||
int updateResult = accountFundMapper.update(null, fundUpdateWrapper);
|
||||
System.out.println(" - 账户更新结果: " + updateResult + " (1=成功, 0=失败)");
|
||||
System.out.println(" - 审批后冻结: " + fund.getFrozen());
|
||||
|
||||
if (updateResult <= 0) {
|
||||
System.err.println("[FundService.approve] 提现审批更新账户冻结失败!");
|
||||
throw new RuntimeException("提现审批更新账户冻结失败");
|
||||
}
|
||||
|
||||
// 验证更新结果
|
||||
AccountFund verifyFund = accountFundMapper.selectById(fund.getId());
|
||||
System.out.println(" - 验证更新后冻结: " + verifyFund.getFrozen());
|
||||
System.out.println(" - 验证更新后累计提现: " + verifyFund.getTotalWithdraw());
|
||||
|
||||
// 更新本地对象状态
|
||||
fund.setFrozen(newFrozen);
|
||||
fund.setTotalWithdraw(newTotalWithdraw);
|
||||
fund.setUpdateTime(updateTime);
|
||||
|
||||
// 记录流水 (负数表示支出)
|
||||
System.out.println("[FundService.approve] 创建资金流水记录...");
|
||||
assetService.createFlow(order.getUserId(), 2, order.getAmount().negate(),
|
||||
balanceBefore, fund.getBalance(), "USDT", orderNo, "提现");
|
||||
balanceBefore, balanceBefore, "USDT", orderNo, "提现");
|
||||
System.out.println(" - 流水记录创建成功");
|
||||
|
||||
System.out.println("[提现审批成功] 订单号: " + orderNo + ", 用户ID: " + order.getUserId() +
|
||||
@@ -367,25 +454,48 @@ public class FundService {
|
||||
System.out.println("[FundService.approve] 驳回原因: " + rejectReason);
|
||||
|
||||
if (order.getType() == 2) {
|
||||
// 提现驳回:解冻金额退还
|
||||
// 提现驳回:解冻金额退还 - 使用 LambdaUpdateWrapper 显式更新
|
||||
System.out.println("[FundService.approve] 处理提现驳回,解冻金额退还...");
|
||||
BigDecimal balanceBefore = fund.getBalance();
|
||||
BigDecimal frozenBefore = fund.getFrozen();
|
||||
System.out.println(" - 驳回前余额: " + balanceBefore);
|
||||
System.out.println(" - 驳回前冻结: " + frozenBefore);
|
||||
|
||||
fund.setBalance(fund.getBalance().add(order.getAmount()));
|
||||
fund.setFrozen(fund.getFrozen().subtract(order.getAmount()));
|
||||
fund.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
System.out.println(" - 准备更新账户: 余额=" + fund.getBalance() + ", 冻结=" + fund.getFrozen());
|
||||
int updateResult = accountFundMapper.updateById(fund);
|
||||
|
||||
BigDecimal newBalance = fund.getBalance().add(order.getAmount());
|
||||
BigDecimal newFrozen = fund.getFrozen().subtract(order.getAmount());
|
||||
LocalDateTime updateTime = LocalDateTime.now();
|
||||
|
||||
System.out.println(" - 准备更新账户: 新余额=" + newBalance + ", 新冻结=" + newFrozen);
|
||||
|
||||
LambdaUpdateWrapper<AccountFund> fundUpdateWrapper = new LambdaUpdateWrapper<>();
|
||||
fundUpdateWrapper.eq(AccountFund::getId, fund.getId())
|
||||
.set(AccountFund::getBalance, newBalance)
|
||||
.set(AccountFund::getFrozen, newFrozen)
|
||||
.set(AccountFund::getUpdateTime, updateTime);
|
||||
|
||||
System.out.println(" - 执行 SQL UPDATE (使用 LambdaUpdateWrapper)...");
|
||||
int updateResult = accountFundMapper.update(null, fundUpdateWrapper);
|
||||
System.out.println(" - 账户更新结果: " + updateResult + " (1=成功, 0=失败)");
|
||||
|
||||
if (updateResult <= 0) {
|
||||
System.err.println("[FundService.approve] 提现驳回更新账户失败!");
|
||||
throw new RuntimeException("提现驳回更新账户失败");
|
||||
}
|
||||
|
||||
// 验证更新结果
|
||||
AccountFund verifyFund = accountFundMapper.selectById(fund.getId());
|
||||
System.out.println(" - 验证更新后余额: " + verifyFund.getBalance());
|
||||
System.out.println(" - 验证更新后冻结: " + verifyFund.getFrozen());
|
||||
|
||||
// 更新本地对象状态
|
||||
fund.setBalance(newBalance);
|
||||
fund.setFrozen(newFrozen);
|
||||
fund.setUpdateTime(updateTime);
|
||||
|
||||
// 记录流水
|
||||
System.out.println("[FundService.approve] 创建资金流水记录...");
|
||||
assetService.createFlow(order.getUserId(), 2, order.getAmount(),
|
||||
balanceBefore, fund.getBalance(), "USDT", orderNo, "提现驳回退还");
|
||||
balanceBefore, newBalance, "USDT", orderNo, "提现驳回退还");
|
||||
System.out.println(" - 流水记录创建成功");
|
||||
|
||||
System.out.println("[提现驳回成功] 订单号: " + orderNo + ", 用户ID: " + order.getUserId() +
|
||||
|
||||
Reference in New Issue
Block a user