yanderong
2026-07-11 1baa21c114f634b8ecc3af64efcfaab6a57a571e
UserYKDTravel/guns-admin/src/main/java/com/stylefeng/guns/modular/system/model/vo/BalanceModifyRecordVo.java
@@ -5,6 +5,8 @@
import java.math.BigDecimal;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
/**
@@ -54,6 +56,13 @@
     */
    private BigDecimal money;
    /**
     * 变更类型
     * 1、充值,2消费、3退款、4出行卡兑换
     * {@link com.stylefeng.guns.core.common.constant.state.BalanceChangeType}
     */
    private Integer balanceChangeType;
    public static BalanceModifyRecordVo convert(UserInfo oldUser, UserInfo newUser) {
        Random random = new Random();
        StringBuilder result = new StringBuilder();
@@ -66,19 +75,29 @@
        recordVo.setPhone(newUser.getPhone());
        recordVo.setUserId(newUser.getId());
        recordVo.setCreateTime(new Date());
        if(Objects.nonNull(newUser.getBalanceChangeType())) {
            recordVo.setBalanceChangeType(newUser.getBalanceChangeType());
        }
        BigDecimal newBalance = newUser.getBalance() != null ? BigDecimal.valueOf(newUser.getBalance()) : BigDecimal.ZERO;
        BigDecimal oldBalance = oldUser.getBalance() != null ? BigDecimal.valueOf(oldUser.getBalance()) : BigDecimal.ZERO;
        BigDecimal amount = newBalance.subtract(oldBalance);
        // 判断方向
        if (amount.compareTo(BigDecimal.ZERO) == 0){
        if (amount.compareTo(BigDecimal.ZERO) == 0){ // 余额没变动
            return null;
        } else if (amount.compareTo(BigDecimal.ZERO) > 0) {
            recordVo.setType(1); // 增加金额(充值)
            recordVo.setMoney(amount);
            if(Objects.isNull(recordVo.getBalanceChangeType())) {
                recordVo.setBalanceChangeType(1);
            }
        } else {
            recordVo.setType(2);  // 减少金额,转为正数(消费)
            recordVo.setMoney(amount.abs());
            if(Objects.isNull(recordVo.getBalanceChangeType())) {
                recordVo.setBalanceChangeType(2);
            }
        }
        return recordVo;
    }
}