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 | 62 +++++++++++++++++-------------- 1 files changed, 34 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 227f4f7..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 @@ -20,7 +20,7 @@ @Resource private RedPackBalanceMapper redPackBalanceMapper; - + @Resource private RedPackDetailService redPackDetailService; @@ -29,41 +29,34 @@ 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; - 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); - } - } - - + + @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) + if (uid == null || uid == 0 || money == null || detail == null) throw new RedPackBalanceException(1, "鍙傛暟浼犻�掍笉瀹屾暣"); // 鍔犲叆鏄庣粏 redPackDetailService.insertSelective(detail); @@ -73,22 +66,35 @@ 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 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); } -- Gitblit v1.8.0