| | |
| | |
|
| | | @Resource
|
| | | private RedPackBalanceMapper redPackBalanceMapper;
|
| | | |
| | |
|
| | | @Resource
|
| | | private RedPackDetailService redPackDetailService;
|
| | |
|
| | |
| | | return redPackBalanceMapper.selectByPrimaryKey(uid);
|
| | | }
|
| | |
|
| | | private void verify(RedPackBalance redPackBalance) throws RedPackBalanceException{
|
| | | if (redPackBalance == null)
|
| | | return;
|
| | | |
| | | if (redPackBalance.getState() != null && redPackBalance.getState() == RedPackBalance.STATE_LOCKED) |
| | | throw new RedPackBalanceException(1, "红包已封禁,感谢使用");
|
| | | }
|
| | | |
| | | @Override
|
| | | public BigDecimal getBalance(Long uid) {
|
| | | public BigDecimal getBalance(Long uid) throws RedPackBalanceException{
|
| | | if (uid == null || uid == 0)
|
| | | return new BigDecimal(0);
|
| | |
|
| | | RedPackBalance redPackBalance = redPackBalanceMapper.selectByPrimaryKey(uid);
|
| | | if (redPackBalance != null)
|
| | | if (redPackBalance != null) {
|
| | | // 验证
|
| | | verify(redPackBalance);
|
| | | |
| | | return redPackBalance.getMoney();
|
| | | |
| | | } |
| | | return new BigDecimal(0);
|
| | | }
|
| | | |
| | | |
| | |
|
| | |
|
| | | @Override
|
| | | public void addRedPack(Long uid, BigDecimal money) {
|
| | | if (uid == null || uid == 0 || money == null)
|
| | | return;
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | public void addRedPack(Long uid, BigDecimal money, RedPackDetail detail) throws RedPackBalanceException {
|
| | | if (uid == null || uid == 0 || money == null || detail == null)
|
| | | throw new RedPackBalanceException(1, "参数传递不完整");
|
| | | // 加入明细
|
| | | redPackDetailService.insertSelective(detail);
|
| | | // 添加红包
|
| | | RedPackBalance redPackBalance = redPackBalanceMapper.selectForUpdate(uid);
|
| | | if (redPackBalance == null) {
|
| | | redPackBalance = new RedPackBalance();
|
| | | redPackBalance.setId(uid);
|
| | | redPackBalance.setMoney(money);
|
| | | redPackBalance.setState(RedPackBalance.STATE_INIT);
|
| | | redPackBalance.setCreateTime(new Date());
|
| | | redPackBalance.setUpdateTime(new Date());
|
| | | redPackBalanceMapper.insertSelective(redPackBalance);
|
| | | } else {
|
| | | // 验证
|
| | | verify(redPackBalance);
|
| | | redPackBalanceMapper.addRedPack(uid, money);
|
| | | }
|
| | | }
|
| | | |
| | | |
| | | @Override
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | public void addRedPack(Long uid, BigDecimal money, RedPackDetail detail) throws RedPackBalanceException {
|
| | | if (uid == null || uid == 0 || money == null || detail == null)
|
| | | throw new RedPackBalanceException(1, "参数传递不完整");
|
| | | // 加入明细
|
| | | redPackDetailService.insertSelective(detail);
|
| | | // 减少红包
|
| | | redPackBalanceMapper.addRedPack(uid, money);
|
| | | }
|
| | | |
| | | |
| | |
|
| | | @Override
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | public void subRedPack(Long uid, BigDecimal money, RedPackDetail detail) throws RedPackBalanceException {
|
| | | if (uid == null || uid == 0 || money == null || detail == null)
|
| | | if (uid == null || uid == 0 || money == null || detail == null)
|
| | | throw new RedPackBalanceException(1, "参数传递不完整");
|
| | | // 加入明细
|
| | | redPackDetailService.insertSelective(detail);
|
| | |
|
| | | RedPackBalance balance = redPackBalanceMapper.selectForUpdate(uid);
|
| | | if (balance.getMoney() == null)
|
| | | throw new RedPackBalanceException(2, "红包余额不足");
|
| | |
|
| | | if (balance.getMoney().compareTo(money) < 0)
|
| | | throw new RedPackBalanceException(2, "红包余额不足");
|
| | |
|
| | | // 验证
|
| | | verify(balance);
|
| | | |
| | | // 减少红包
|
| | | redPackBalanceMapper.subRedPack(uid, money);
|
| | | }
|