From 221b5c8d0de3f6b17a00d543503a79c13b28ba12 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期三, 13 十一月 2019 16:45:57 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/div' into div --- fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackBalanceServiceImpl.java | 67 +++++++++++++++++++-------------- 1 files changed, 39 insertions(+), 28 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackBalanceServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackBalanceServiceImpl.java index 2cf5583..04176c2 100644 --- a/fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackBalanceServiceImpl.java +++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackBalanceServiceImpl.java @@ -29,34 +29,29 @@ return redPackBalanceMapper.selectByPrimaryKey(uid); } - @Override - public BigDecimal getBalance(Long uid) { - if (uid == null || uid == 0) - return null; - - RedPackBalance redPackBalance = redPackBalanceMapper.selectByPrimaryKey(uid); - if (redPackBalance != null) - return redPackBalance.getMoney(); - - return null; - } - - @Override - public void addRedPack(Long uid, BigDecimal money) { - if (uid == null || uid == 0 || money == null) + private void verify(RedPackBalance redPackBalance) throws RedPackBalanceException{ + if (redPackBalance == null) return; - RedPackBalance redPackBalance = redPackBalanceMapper.selectForUpdate(uid); - if (redPackBalance == null) { - redPackBalance = new RedPackBalance(); - redPackBalance.setId(uid); - redPackBalance.setMoney(money); - redPackBalance.setCreateTime(new Date()); - redPackBalance.setUpdateTime(new Date()); - redPackBalanceMapper.insertSelective(redPackBalance); - } else { - redPackBalanceMapper.addRedPack(uid, money); - } + + if (redPackBalance.getState() != null && redPackBalance.getState() == RedPackBalance.STATE_LOCKED) + throw new RedPackBalanceException(1, "绾㈠寘宸插皝绂侊紝鎰熻阿浣跨敤"); } + + @Override + public BigDecimal getBalance(Long uid) throws RedPackBalanceException{ + if (uid == null || uid == 0) + return new BigDecimal(0); + + RedPackBalance redPackBalance = redPackBalanceMapper.selectByPrimaryKey(uid); + if (redPackBalance != null) { + // 楠岃瘉 + verify(redPackBalance); + + return redPackBalance.getMoney(); + } + return new BigDecimal(0); + } + @Override @Transactional(rollbackFor = Exception.class) @@ -65,8 +60,21 @@ throw new RedPackBalanceException(1, "鍙傛暟浼犻�掍笉瀹屾暣"); // 鍔犲叆鏄庣粏 redPackDetailService.insertSelective(detail); - // 鍑忓皯绾㈠寘 - redPackBalanceMapper.addRedPack(uid, money); + // 娣诲姞绾㈠寘 + 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 @@ -84,6 +92,9 @@ if (balance.getMoney().compareTo(money) < 0) throw new RedPackBalanceException(2, "绾㈠寘浣欓涓嶈冻"); + // 楠岃瘉 + verify(balance); + // 鍑忓皯绾㈠寘 redPackBalanceMapper.subRedPack(uid, money); } -- Gitblit v1.8.0