| | |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.util.Date; |
| | | import java.util.Objects; |
| | | import java.util.Random; |
| | | |
| | | /** |
| | |
| | | */ |
| | | 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(); |
| | |
| | | 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; |
| | | } |
| | | } |