核销记录额度变化

This commit is contained in:
luojiaquan
2026-09-22 09:53:03 +08:00
parent 81bc69c268
commit c4b58c8cd3
7 changed files with 78 additions and 120 deletions
@@ -376,9 +376,10 @@ public class CustomerAccountApiImpl implements ICustomerAccountApi
BigDecimal afterBalance = beforeBalance.subtract(changeAmount);
BigDecimal frozenBalance = nz(account.getFrozenBalance());
account.setBalance(afterBalance);
BigDecimal beforeUsedCredit = nz(account.getUsedCredit());
if (Boolean.TRUE.equals(command.getReleaseUsedCredit()))
{
account.setUsedCredit(nz(account.getUsedCredit()).subtract(changeAmount).max(BigDecimal.ZERO)
account.setUsedCredit(beforeUsedCredit.subtract(changeAmount).max(BigDecimal.ZERO)
.setScale(MONEY_SCALE, RoundingMode.HALF_UP));
}
account.setUpdateBy(username);
@@ -390,6 +391,18 @@ public class CustomerAccountApiImpl implements ICustomerAccountApi
CustomerBalanceFlow flow = newBalanceFlow(account, command, flowCurrency, originalAmount, -1,
beforeBalance, afterBalance, frozenBalance, frozenBalance, username, userId);
customerMoneyMapper.insertFlow(flow);
// 核销扣款联动释放已用额度:补写额度流水(RELEASE),保证 used_credit 变动台账完整
BigDecimal afterUsedCredit = nz(account.getUsedCredit());
if (Boolean.TRUE.equals(command.getReleaseUsedCredit())
&& afterUsedCredit.compareTo(beforeUsedCredit) < 0)
{
writeUsedCreditFlow(account, toCreditUsageCommand(command, accountCurrency, null),
CustomerCreditFlow.CHANGE_RELEASE, -1,
beforeUsedCredit, afterUsedCredit,
beforeUsedCredit.subtract(afterUsedCredit),
username, userId);
}
return flow;
}
@@ -412,10 +425,11 @@ public class CustomerAccountApiImpl implements ICustomerAccountApi
BigDecimal afterBalance = beforeBalance.add(changeAmount);
BigDecimal frozenBalance = nz(account.getFrozenBalance());
account.setBalance(afterBalance);
BigDecimal beforeUsedCredit = nz(account.getUsedCredit());
if (Boolean.TRUE.equals(command.getReoccupyUsedCredit()))
{
// 反核销回补:账单恢复未清偿,授信占用同步加回(允许出现高于总额度的占用存量)
account.setUsedCredit(nz(account.getUsedCredit()).add(changeAmount)
account.setUsedCredit(beforeUsedCredit.add(changeAmount)
.setScale(MONEY_SCALE, RoundingMode.HALF_UP));
}
account.setUpdateBy(username);
@@ -427,11 +441,39 @@ public class CustomerAccountApiImpl implements ICustomerAccountApi
CustomerBalanceFlow flow = newBalanceFlow(account, command, flowCurrency, originalAmount, 1,
beforeBalance, afterBalance, frozenBalance, frozenBalance, username, userId);
customerMoneyMapper.insertFlow(flow);
// 反核销回补联动重新占用已用额度:补写额度流水(OCCUPY),与核销释放流水方向相反、可勾稽
BigDecimal afterUsedCredit = nz(account.getUsedCredit());
if (Boolean.TRUE.equals(command.getReoccupyUsedCredit())
&& afterUsedCredit.compareTo(beforeUsedCredit) > 0)
{
String reoccupyReason = "反核销重新占用授信额度"
+ (StringUtils.hasText(command.getBizNo()) ? ":" + command.getBizNo() : "");
writeUsedCreditFlow(account, toCreditUsageCommand(command, accountCurrency, reoccupyReason),
CustomerCreditFlow.CHANGE_OCCUPY, 1,
beforeUsedCredit, afterUsedCredit,
afterUsedCredit.subtract(beforeUsedCredit),
username, userId);
}
return flow;
}
/* ================= 内部实现 ================= */
/** 余额变动命令 -> 额度占用命令(复用业务单据信息,金额取实际联动额由调用方写流水时传入) */
private CreditUsageCommand toCreditUsageCommand(BalanceChangeCommand source, String accountCurrency, String reason)
{
CreditUsageCommand target = new CreditUsageCommand();
target.setCustomerId(source.getCustomerId());
target.setCurrency(accountCurrency);
target.setAmount(money(source.getAmount()));
target.setBizType(source.getBizType());
target.setBizId(source.getBizId());
target.setBizNo(source.getBizNo());
target.setReason(reason);
return target;
}
/** 额度占用/释放写额度流水(before/after 为已用额度快照) */
private CustomerCreditFlow writeUsedCreditFlow(CustomerAccount account, CreditUsageCommand command,
String changeType, int direction, BigDecimal beforeUsed, BigDecimal afterUsed,