From d8359ddb48dab5cc797a9d552e11fde571f4920c Mon Sep 17 00:00:00 2001
From: yujian <yujian@123.com>
Date: 星期二, 27 八月 2019 12:32:46 +0800
Subject: [PATCH] Merge branch 'div' of ssh://193.112.35.168:29418/fanli-server into div

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/user/BindingAccountServiceImpl.java |  126 ++++++++++++++++++++++-------------------
 1 files changed, 67 insertions(+), 59 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/BindingAccountServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/BindingAccountServiceImpl.java
index 7010778..efff08f 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/BindingAccountServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/BindingAccountServiceImpl.java
@@ -1,6 +1,5 @@
 package com.yeshi.fanli.service.impl.user;
 
-import java.io.Serializable;
 import java.math.BigDecimal;
 import java.util.Calendar;
 import java.util.Date;
@@ -9,11 +8,6 @@
 
 import javax.annotation.Resource;
 
-import org.hibernate.HibernateException;
-import org.hibernate.Query;
-import org.hibernate.Session;
-import org.hibernate.Transaction;
-import org.springframework.orm.hibernate4.HibernateCallback;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -22,36 +16,36 @@
 import com.alipay.api.DefaultAlipayClient;
 import com.alipay.api.request.AlipayFundTransToaccountTransferRequest;
 import com.alipay.api.response.AlipayFundTransToaccountTransferResponse;
-import com.yeshi.fanli.dao.mybatis.AccountDetailsMapper;
 import com.yeshi.fanli.dao.mybatis.AlipayAccountValidNormalHistoryMapper;
 import com.yeshi.fanli.dao.mybatis.BindingAccountMapper;
 import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
 import com.yeshi.fanli.dao.mybatis.money.UserMoneyDetailMapper;
-import com.yeshi.fanli.dao.user.BindingAccountDao;
-import com.yeshi.fanli.entity.bus.user.AccountDetails;
 import com.yeshi.fanli.entity.bus.user.AlipayAccountValidNormalHistory;
 import com.yeshi.fanli.entity.bus.user.BindingAccount;
+import com.yeshi.fanli.entity.bus.user.UserAccountBindingHistory;
 import com.yeshi.fanli.entity.bus.user.UserInfo;
+import com.yeshi.fanli.entity.money.UserMoneyDebt;
+import com.yeshi.fanli.entity.money.UserMoneyDebt.UserMoneyDebtTypeEnum;
 import com.yeshi.fanli.entity.money.UserMoneyDetail;
 import com.yeshi.fanli.exception.AlipayAccountException;
 import com.yeshi.fanli.exception.AlipayTransferException;
 import com.yeshi.fanli.exception.BindingAccountException;
+import com.yeshi.fanli.exception.money.UserMoneyDebtException;
 import com.yeshi.fanli.exception.money.UserMoneyDetailException;
 import com.yeshi.fanli.log.LogHelper;
+import com.yeshi.fanli.service.inter.money.UserMoneyDebtService;
+import com.yeshi.fanli.service.inter.msg.UserMoneyMsgNotificationService;
 import com.yeshi.fanli.service.inter.user.BindingAccountService;
-import com.yeshi.fanli.service.inter.user.UserNotificationService;
+import com.yeshi.fanli.service.inter.user.UserAccountBindingHistoryService;
+import com.yeshi.fanli.service.inter.user.UserMoneyService;
 import com.yeshi.fanli.util.Constant;
 import com.yeshi.fanli.util.StringUtil;
-import com.yeshi.fanli.util.factory.AccountDetailsFactory;
 import com.yeshi.fanli.util.factory.UserMoneyDetailFactory;
 
 import net.sf.json.JSONObject;
 
 @Service
 public class BindingAccountServiceImpl implements BindingAccountService {
-
-	@Resource
-	private BindingAccountDao bindingAccountDao;
 	@Resource
 	private BindingAccountMapper bindingAccountMapper;
 	@Resource
@@ -59,52 +53,43 @@
 	@Resource
 	private UserInfoMapper userInfoMapper;
 	@Resource
-	private AccountDetailsMapper accountDetailsMapper;
-	@Resource
-	private UserNotificationService userNotificationService;
-	
+	private UserAccountBindingHistoryService userAccountBindingHistoryService;
 	@Resource
 	private UserMoneyDetailMapper userMoneyDetailMapper;
+	@Resource
+	private UserMoneyService userMoneyService;
+	@Resource
+	private UserMoneyMsgNotificationService userMoneyMsgNotificationService;
+	@Resource
+	private UserMoneyDebtService userMoneyDebtService;
 
 	public List<BindingAccount> getBindingAccountByUid(long uid) {
-		return bindingAccountDao.list("from BindingAccount ba where ba.userInfo.id=?", new Serializable[] { uid });
+		return bindingAccountMapper.selectByUid(uid);
 	}
 
 	public void addBindingAccount(BindingAccount addAccount) throws BindingAccountException {
-		List<BindingAccount> list = bindingAccountDao.list(
-				"from BindingAccount ba where ba.userInfo.id=? and ba.type=?",
-				new Serializable[] { addAccount.getUserInfo().getId(), addAccount.getType() });
-		if (list == null || list.size() == 0) {
-			bindingAccountDao.save(addAccount);
+		BindingAccount account = bindingAccountMapper.selectByUidAndType(addAccount.getUserInfo().getId(),
+				addAccount.getType());
+
+		if (account == null) {
+			bindingAccountMapper.insertSelective(addAccount);
 		} else {
 			throw new BindingAccountException(Constant.BA_EXIST);
 		}
 	}
 
-	public Integer deleteBindingAccount(final BindingAccount account) {
-		return (Integer) bindingAccountDao.excute(new HibernateCallback() {
-
-			public Integer doInHibernate(Session session) throws HibernateException {
-				Transaction transaction = session.beginTransaction();
-				Query delete = session.createQuery("delete BindingAccount ba where ba.userInfo.id=? and ba.type=?");
-				delete.setLong(0, account.getUserInfo().getId());
-				delete.setInteger(1, account.getType());
-				int update = delete.executeUpdate();
-				transaction.commit();
-				return update;
-			}
-
-		});
+	@Transactional
+	public Integer deleteBindingAccount(BindingAccount account) {
+		BindingAccount oldAccount = bindingAccountMapper.selectByUidAndType(account.getUserInfo().getId(),
+				account.getType());
+		if (oldAccount != null)
+			bindingAccountMapper.deleteByPrimaryKey(oldAccount.getId());
+		return 1;
 	}
 
 	public BindingAccount getBindingAccountByUidAndType(long uid, int type) {
-
-		List<BindingAccount> list = bindingAccountDao
-				.list("from BindingAccount ba where ba.userInfo.id=? and ba.type=?", new Serializable[] { uid, type });
-		if (list != null && list.size() != 0) {
-			return list.get(0);
-		}
-		return null;
+		BindingAccount account = bindingAccountMapper.selectByUidAndType(uid, type);
+		return account;
 	}
 
 	@Override
@@ -117,11 +102,11 @@
 			bindingAccount.setName(name);
 			bindingAccount.setType(BindingAccount.TYPE_ALIPAY);
 			bindingAccount.setUserInfo(new UserInfo(uid));
-			bindingAccountDao.create(bindingAccount);
+			bindingAccountMapper.insertSelective(bindingAccount);
 		} else {
 			bindingAccount.setName(name);
 			bindingAccount.setAccount(account);
-			bindingAccountDao.update(bindingAccount);
+			bindingAccountMapper.updateByPrimaryKeySelective(bindingAccount);
 		}
 		return bindingAccount;
 	}
@@ -161,13 +146,12 @@
 
 		UserInfo userInfo = userInfoMapper.selectByPrimaryKeyForUpdate(uid);
 		if (userInfo.getMyHongBao().compareTo(new BigDecimal("0")) <= 0)
-			throw new AlipayAccountException(AlipayAccountException.CODE_NO_MONEY, "浣犵殑璐︽埛鐩墠娌℃湁浣欓锛屾棤闇�缁戝畾鎻愮幇甯愬彿銆�");
+			throw new AlipayAccountException(AlipayAccountException.CODE_NO_MONEY, "浣犵殑璐︽埛鏃犱綑棰濓紝鏃犻渶缁戝畾鎻愮幇甯愬彿銆�");
 
 		// 闇�瑕佽浆璐﹂獙璇�
 		BigDecimal money = new BigDecimal("0.1");
 		transferAlipayWithVerify(account, name);
-		// 鎵f
-		userInfoMapper.subHongBaoByUid(uid, money);
+
 		// 杞处鎴愬姛
 		// 鎻掑叆杞处鎴愬姛琛�
 		AlipayAccountValidNormalHistory history = new AlipayAccountValidNormalHistory();
@@ -176,15 +160,10 @@
 		history.setName(name);
 		history.setUid(uid);
 		alipayAccountValidNormalHistoryMapper.insertSelective(history);
-		// 璧勯噾璁板綍 绔欏唴淇�
-		AccountDetails accountDetails = AccountDetailsFactory.create("-" + money.toString(),
-				AccountDetailsFactory.VALID_ALIPAY_ACCOUNT, null, null, new UserInfo(uid));
-		accountDetailsMapper.insertSelective(accountDetails);
+		UserMoneyDetail userMoneyDetail = null;
 		// 鏂扮増璧勯噾
 		try {
-			UserMoneyDetail userMoneyDetail = UserMoneyDetailFactory.createExtractAccountValid(history, money);
-			userMoneyDetail.setId(accountDetails.getId());
-			userMoneyDetailMapper.insert(userMoneyDetail);
+			userMoneyDetail = UserMoneyDetailFactory.createExtractAccountValid(history, money);
 		} catch (UserMoneyDetailException e) {
 			try {
 				LogHelper.errorDetailInfo(e);
@@ -193,7 +172,26 @@
 			}
 		}
 
-		userNotificationService.alipayAccountValidRight(uid, money, account);
+		// 浣欓鍏呰冻
+		if (userInfo.getMyHongBao().compareTo(money) >= 0) {
+			// 鎵f
+			userMoneyService.subUserMoney(uid, money, userMoneyDetail);
+			userMoneyMsgNotificationService.alipayAccountValid(history);
+		} else {// 浣欓涓嶈冻锛屾殏鏃朵笉鎵f锛屽姞鍏ュ�熻捶鍏崇郴
+			UserMoneyDebt debt = new UserMoneyDebt();
+			debt.setCreateTime(new Date());
+			debt.setLeftMoney(money);
+			debt.setOriginMoney(money);
+			debt.setSourceId(history.getId());
+			debt.setType(UserMoneyDebtTypeEnum.extractVerify);
+			debt.setUid(uid);
+			try {
+				userMoneyDebtService.addUserMoneyDebt(debt);
+			} catch (UserMoneyDebtException e) {
+				LogHelper.errorDetailInfo(e, "楠岃瘉ID锛�" + history.getId(), "");
+			}
+
+		}
 
 	}
 
@@ -230,6 +228,17 @@
 	@Override
 	public BindingAccount changeAlipayBindingWithVerify(Long uid, String name, String account)
 			throws AlipayTransferException, AlipayApiException, AlipayAccountException {
+		BindingAccount bindingAccount = bindingAccountMapper.selectByUidAndType(uid, BindingAccount.TYPE_ALIPAY);
+		if (bindingAccount != null) {
+			// 楠岃瘉鏄惁7澶╁唴鏇存崲浜嗘墜鏈哄彿
+			UserAccountBindingHistory history = userAccountBindingHistoryService.getLatestHistory(uid,
+					UserAccountBindingHistory.TYPE_PHONE);
+			// 鎵嬫満鍙锋洿鎹㈢粦瀹氱殑7澶╁唴涓嶈兘鎻愮幇
+			if (history != null && !history.getFirst()
+					&& (System.currentTimeMillis() - history.getCreateTime().getTime()) < 1000 * 60 * 60 * 24 * 7L) {
+				throw new AlipayAccountException(111, "淇敼鎵嬫満鍙�7澶╁唴涓嶅厑璁镐慨鏀规敮浠樺疂璐﹀彿");
+			}
+		}
 
 		try {
 			validAlipayAccount(uid, account, name);
@@ -241,7 +250,6 @@
 			throw new AlipayAccountException(e1.getCode(), e1.getMsg());
 		}
 
-		BindingAccount bindingAccount = bindingAccountMapper.selectByUidAndType(uid, BindingAccount.TYPE_ALIPAY);
 		if (bindingAccount == null)// 鍒涘缓璐﹀彿
 		{
 			bindingAccount = new BindingAccount();

--
Gitblit v1.8.0