| | |
| | | |
| | | @Override |
| | | public void initializeCreditBalance(Long userId) { |
| | | if(getCreditBalanceByUserId(userId)!=null){ |
| | | if (getCreditBalanceByUserId(userId) != null) { |
| | | return; |
| | | } |
| | | UserCreditBalance userCreditBalance = new UserCreditBalance(); |
| | |
| | | userCreditBalance.setCreditBalance(0); |
| | | userCreditBalance.setCreateTime(new Date()); |
| | | userCreditBalance.setUpdateTime(new Date()); |
| | | userCreditBalanceMapper.insert(userCreditBalance); |
| | | userCreditBalanceMapper.insertSelective(userCreditBalance); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | public void increaseCreditBalance(Long userId, int amount) { |
| | | UserCreditBalance userCreditBalance = userCreditBalanceMapper.selectByPrimaryKeyForUpdate(userId); |
| | | if (userCreditBalance == null) { |
| | | initializeCreditBalance(userId); |
| | | userCreditBalance = userCreditBalanceMapper.selectByPrimaryKeyForUpdate(userId); |
| | | } |
| | | if (userCreditBalance != null) { |
| | | userCreditBalanceMapper.updateByPrimaryKeySelective(UserCreditBalance.builder() |
| | | .id(userCreditBalance.getId()) |
| | | .creditBalance(userCreditBalance.getCreditBalance() + amount) |
| | | .updateTime(new Date()) |
| | | .id(userCreditBalance.getId()) |
| | | .creditBalance(userCreditBalance.getCreditBalance() + amount) |
| | | .updateTime(new Date()) |
| | | .build()); |
| | | } |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public void decreaseCreditBalance(Long userId, int amount) { |
| | | UserCreditBalance userCreditBalance = userCreditBalanceMapper.selectByPrimaryKeyForUpdate(userId); |
| | | if (userCreditBalance == null) { |
| | | initializeCreditBalance(userId); |
| | | userCreditBalance = userCreditBalanceMapper.selectByPrimaryKeyForUpdate(userId); |
| | | } |
| | | |
| | | if (userCreditBalance != null) { |
| | | userCreditBalanceMapper.updateByPrimaryKeySelective(UserCreditBalance.builder() |
| | | .id(userCreditBalance.getId()) |