feat(wallet): 新增余额格式化显示字段
在响应对象中添加 balanceDisplay 字段,用于返回“K”缩写格式的大额余额,
This commit is contained in:
@@ -17,4 +17,10 @@ public class KeyboardUserWalletRespVO {
|
||||
*/
|
||||
@Schema(description = "余额")
|
||||
private BigDecimal balance;
|
||||
|
||||
/**
|
||||
* 格式化后的余额显示
|
||||
*/
|
||||
@Schema(description = "格式化后的余额显示")
|
||||
private String balanceDisplay;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
package com.yolo.keyborad.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.yolo.keyborad.common.ErrorCode;
|
||||
import com.yolo.keyborad.exception.BusinessException;
|
||||
import com.yolo.keyborad.model.vo.wallet.KeyboardUserWalletRespVO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -28,12 +25,17 @@ public class KeyboardUserWalletServiceImpl extends ServiceImpl<KeyboardUserWalle
|
||||
.eq(KeyboardUserWallet::getUserId, userId)
|
||||
.one();
|
||||
KeyboardUserWalletRespVO respVO = new KeyboardUserWalletRespVO();
|
||||
if (wallet == null) {
|
||||
// 如果用户没有钱包记录,返回余额为0
|
||||
respVO.setBalance(BigDecimal.ZERO);
|
||||
} else {
|
||||
respVO.setBalance(wallet.getBalance());
|
||||
}
|
||||
BigDecimal balance = (wallet == null) ? BigDecimal.ZERO : wallet.getBalance();
|
||||
respVO.setBalance(balance);
|
||||
respVO.setBalanceDisplay(formatBalance(balance));
|
||||
return respVO;
|
||||
}
|
||||
|
||||
private String formatBalance(BigDecimal balance) {
|
||||
if (balance.compareTo(new BigDecimal("10000")) >= 0) {
|
||||
BigDecimal kValue = balance.divide(new BigDecimal("1000"), 2, BigDecimal.ROUND_HALF_UP);
|
||||
return kValue.stripTrailingZeros().toPlainString() + "K";
|
||||
}
|
||||
return balance.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user