From a6f34b99ee20e528b766c8ed5a61bd9c5aa0e7ae Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期六, 16 十一月 2019 18:10:33 +0800 Subject: [PATCH] 淘宝相似商品推荐接口优化,根据商品ID搜索商品基本信息优化 --- fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackBalanceServiceImpl.java | 57 ++++++++++++++++++++++++++++++++++----------------------- 1 files changed, 34 insertions(+), 23 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 6f2bc5c..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); } + 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 null; - + 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) @@ -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