From a980ade25b3dc1a7da482e37113f2935a2719c0b Mon Sep 17 00:00:00 2001
From: yujian <yujian@123.com>
Date: 星期二, 05 十一月 2019 10:01:11 +0800
Subject: [PATCH] 红包表明修改

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/redpack/RedPackBalanceServiceImpl.java |   37 ++++++++++++++++++++++++++-----------
 1 files changed, 26 insertions(+), 11 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 5e87890..e6ef61b 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;
 
@@ -40,8 +40,7 @@
 		
 		return new BigDecimal(0);
 	}
-	
-	
+
 	@Override
 	public void addRedPack(Long uid, BigDecimal money) {
 		if (uid == null || uid == 0 || money == null)
@@ -58,27 +57,43 @@
 			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);
-		// 鍑忓皯绾㈠寘
-		redPackBalanceMapper.addRedPack(uid, money);
+		// 娣诲姞绾㈠寘
+		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 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, "绾㈠寘浣欓涓嶈冻");
+
 		// 鍑忓皯绾㈠寘
 		redPackBalanceMapper.subRedPack(uid, money);
 	}

--
Gitblit v1.8.0