|
|
@@ -109,7 +109,7 @@ public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserA
|
|
|
* 更新账户余额并记录日志
|
|
|
*
|
|
|
* @param userId 用户ID
|
|
|
- * @param changeAmount 变更金额(正数为增加,负数为减少)
|
|
|
+ * @param changeAmount 变更金额
|
|
|
* @param changeType 变更类型(1-增加,2-减少)
|
|
|
* @param changeNote 变更备注
|
|
|
* @param changeId 变更记录编号(订单号或充电订单号)
|
|
|
@@ -123,20 +123,37 @@ public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserA
|
|
|
|
|
|
// 创建账户变动日志
|
|
|
UserAccountLog accountLog = new UserAccountLog();
|
|
|
- accountLog.setBeforeBalance(userAccount.getBalance());
|
|
|
+ accountLog.setBeforeBalance(userAccount.getBalance().add(userAccount.getRedeemBalance()));
|
|
|
|
|
|
// 计算变更后余额
|
|
|
BigDecimal finalBalance;
|
|
|
if (SystemConstants.CHANGE_TYPE_ADD.equals(changeType)) {
|
|
|
// 增加余额
|
|
|
finalBalance = userAccount.getBalance().add(changeAmount);
|
|
|
- } else {
|
|
|
- // 减少余额
|
|
|
- finalBalance = userAccount.getBalance().subtract(changeAmount);
|
|
|
+ // 更新账户余额
|
|
|
+ userAccount.setBalance(finalBalance);
|
|
|
+ }
|
|
|
+ else if (SystemConstants.REDEEM_CHANGE_TYPE_ADD.equals(changeType)) {
|
|
|
+ // 增加兑换余额
|
|
|
+ finalBalance = userAccount.getRedeemBalance().subtract(changeAmount);
|
|
|
+ // 减去账户余额
|
|
|
+ userAccount.setRedeemBalance(finalBalance);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ // 减少余额,有限减少兑换余额,不够的再减抵用余额
|
|
|
+ if (userAccount.getRedeemBalance().compareTo(changeAmount) >= 0) {
|
|
|
+ // 兑换余额充足,直接减少
|
|
|
+ finalBalance = userAccount.getBalance().subtract(changeAmount);
|
|
|
+ // 减去账户余额
|
|
|
+ userAccount.setRedeemBalance(finalBalance);
|
|
|
+ } else {
|
|
|
+ // 账户余额不足,先减去兑换余额,不够的再减抵用余额
|
|
|
+ BigDecimal subtract = changeAmount.subtract(userAccount.getRedeemBalance());
|
|
|
+ finalBalance = userAccount.getBalance().subtract(subtract);
|
|
|
+ userAccount.setRedeemBalance(BigDecimal.ZERO);
|
|
|
+ userAccount.setBalance(finalBalance);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- // 更新账户余额
|
|
|
- userAccount.setBalance(finalBalance);
|
|
|
this.updateById(userAccount);
|
|
|
|
|
|
// 保存变动日志
|
|
|
@@ -145,7 +162,7 @@ public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserA
|
|
|
accountLog.setChangeNote(changeNote);
|
|
|
accountLog.setChangeId(changeId);
|
|
|
accountLog.setAccountType(SystemConstants.ACCOUNT_TYPE_PERSONAL);
|
|
|
- accountLog.setChangeBalance(finalBalance);
|
|
|
+ accountLog.setChangeBalance(userAccount.getBalance().add(userAccount.getRedeemBalance()));
|
|
|
userAccountLogMapper.insert(accountLog);
|
|
|
|
|
|
return userAccount;
|