From 678dd30c708e7457228a7696573ada707f810269 Mon Sep 17 00:00:00 2001
From: yujian <yujian@163.com>
Date: 星期五, 06 十二月 2019 17:27:15 +0800
Subject: [PATCH] 会员审核 + 队员激活

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java |  293 +++++++++++++++++++++++++++-------------------------------
 1 files changed, 136 insertions(+), 157 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java
index d80e726..56deccf 100644
--- a/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java
+++ b/fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java
@@ -22,7 +22,6 @@
 import com.yeshi.fanli.dao.mybatis.user.UserInfoExtraMapper;
 import com.yeshi.fanli.dao.mybatis.user.UserRankRecordMapper;
 import com.yeshi.fanli.dto.wx.WXAccountInfoDTO;
-import com.yeshi.fanli.entity.bus.user.ThreeSale;
 import com.yeshi.fanli.entity.bus.user.UserInfo;
 import com.yeshi.fanli.entity.bus.user.UserInfoExtra;
 import com.yeshi.fanli.entity.bus.user.UserRank;
@@ -326,30 +325,24 @@
 
 	@Override
 	public String activateInviteCode(Long uid, String inviteCode) throws UserInfoExtraException {
-		if (uid == null || inviteCode == null) {
-			throw new UserInfoExtraException(1, "鐢ㄦ埛id銆侀個璇风爜涓嶈兘涓虹┖");
-		}
-
+		if (uid == null || inviteCode == null) 
+			throw new UserInfoExtraException(1, "婵�娲讳俊鎭笉鑳戒负绌�");
+		
 		// 琚個璇蜂汉淇℃伅
 		UserInfo invitee = userInfoService.selectByPKey(uid);
-		if (invitee == null) {
+		if (invitee == null)
 			throw new UserInfoExtraException(1, "鐢ㄦ埛涓嶅瓨鍦�");
-		}
 
+		// 棰濆淇℃伅
 		UserInfoExtra extra = userInfoExtraMapper.getInfoExtraByUid(uid);
-		if (extra != null) {
-			String inviteCodeHas = extra.getInviteCode();
-			if (inviteCodeHas != null && inviteCodeHas.trim().length() > 0) {
-				throw new UserInfoExtraException(1, "宸插瓨鍦ㄩ個璇风爜,璇峰埛鏂伴〉闈�");
-			}
-		}
+		if (extra != null && !StringUtil.isNullOrEmpty(extra.getInviteCode()))
+			throw new UserInfoExtraException(1, "宸茬粡婵�娲�, 鏃犻渶鍐嶆婵�娲�");
 
-		// 閭�璇蜂汉淇℃伅
-		UserInfo inviter = userInfoService.getInfoByPhoneOrInviteCode(inviteCode, inviteCode);
-		if (inviter == null) {
+		// 閭�璇风爜瀵瑰簲鐢ㄦ埛淇℃伅
+		UserInfo inviter = userInfoService.getUserInfoByInviteCode(inviteCode);
+		if (inviter == null)
 			throw new UserInfoExtraException(1, "涓婄骇閭�璇风爜涓嶅瓨鍦�");
-		}
-
+		
 		// 缁戝畾鍏崇郴
 		try {
 			threeSaleSerivce.bindRelationshipByInviteCode(invitee, inviter);
@@ -362,47 +355,46 @@
 			throw new UserInfoExtraException(1, "婵�娲诲け璐�");
 		}
 
-		// 閭�璇风爜鏈夋晥銆佺敓鎴愰個璇风爜
-		String code = UserUtil.getInviteCode(uid);
-		if (code == null || code.trim().length() == 0) {
-			throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触");
+		// 鐢熸垚閭�璇风爜
+		String mycode = null;
+		for (int i = 1; i <= 5; i ++) {
+			mycode = UserUtil.getInviteCode(i, uid);
+			long countCode = userInfoExtraMapper.countByInviteCode(mycode);
+			
+			if (countCode <= 0)
+				break;
 		}
+			
+		if (StringUtil.isNullOrEmpty(mycode)) 
+			throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触");
 
 		UserInfoExtra userInfoExtra = new UserInfoExtra();
 		userInfoExtra.setUserInfo(invitee);
-		userInfoExtra.setInviteCode(code);
-
-		// 淇濆瓨棰濆淇℃伅
-		saveUserInfoExtra(userInfoExtra);
-
-		Long id = userInfoExtra.getId();
-		if (id == null) {
-			throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触");
+		userInfoExtra.setInviteCode(mycode);
+		userInfoExtra.setUpdateTime(new Date());
+		if (extra != null) {
+			userInfoExtra.setId(extra.getId());
+			userInfoExtraMapper.updateByPrimaryKeySelective(userInfoExtra);
+		} else {
+			userInfoExtra.setCreateTime(new Date());
+			userInfoExtraMapper.insertSelective(userInfoExtra);
 		}
-
-		return code;
+		return mycode;
 	}
 
 	@Override
 	public void activationInviteWX(WXAccountInfoDTO wxAccount, Long uid, String code) throws UserInfoExtraException {
-		if (uid == null || code == null) {
+		if (uid == null || code == null)
 			throw new UserInfoExtraException(1, "婵�娲讳俊鎭笉瀹屾暣");
-		}
-
-		// 鐢ㄦ埛淇℃伅
+		 
 		UserInfo invitee = userInfoService.selectByPKey(uid);
-		if (invitee == null) {
+		if (invitee == null)
 			throw new UserInfoExtraException(1, "鐢ㄦ埛涓嶅瓨鍦�");
-		}
 
-		// 鐢ㄦ埛棰濆淇℃伅
+		// 棰濆淇℃伅
 		UserInfoExtra extra = userInfoExtraMapper.getInfoExtraByUid(uid);
-		if (extra != null) {
-			String inviteCodeHas = extra.getInviteCode();
-			if (inviteCodeHas != null && inviteCodeHas.trim().length() > 0) {
-				throw new UserInfoExtraException(1, "宸茬粡婵�娲�, 鏃犻渶鍐嶆婵�娲�");
-			}
-		}
+		if (extra != null && !StringUtil.isNullOrEmpty(extra.getInviteCode()))
+			throw new UserInfoExtraException(1, "宸茬粡婵�娲�, 鏃犻渶鍐嶆婵�娲�");
 
 		// 鑾峰彇寰俊淇℃伅
 		WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(code, wxAccount.getAppId(), wxAccount.getAppSecret());
@@ -465,31 +457,14 @@
 			throw new UserInfoExtraException(1, "缁戝畾寰俊涓庢縺娲诲井淇′笉涓�鑷�");
 		}
 
-		// 閭�璇蜂汉ID -1.5.3鏂扮増
+		// 鏄惁瀛樺湪閭�璇蜂汉
 		Long inviterId = userInviteRecordService.getNewestInviterId(wxUnionId);
-
-		// 鍏煎1.5.3 涔嬪墠鐗堟湰
-		ThreeSale threeSale = threeSaleSerivce.getByWorkerId(uid);
-		if (inviterId == null && threeSale != null) {
-			Long bossId = threeSale.getBoss().getId();
-			UserInfoExtra inviterExtra = userInfoExtraMapper.getInfoExtraByUid(bossId);
-			if (inviterExtra != null && inviterExtra.getInviteCode() == null) {
-				// 鏇存柊閭�璇风爜
-				UserInfoExtra inviterInfoExtra = new UserInfoExtra();
-				inviterInfoExtra.setId(inviterExtra.getId());
-				inviterInfoExtra.setInviteCode(UserUtil.getInviteCode(bossId));
-				userInfoExtraMapper.updateByPrimaryKeySelective(inviterInfoExtra);
-			}
-		}
-
-		// 娌℃湁琚個璇疯繃
-		if (inviterId == null && threeSale == null) {
+		if (inviterId == null)
 			throw new UserInfoExtraException(1, "娌℃湁瀵瑰簲鐨勯個璇峰叧绯�");
-		}
 
 		// 缁戝畾鍏崇郴
 		try {
-			threeSaleSerivce.bindRelationshipByWX(invitee, inviterId, threeSale);
+			threeSaleSerivce.bindRelationshipByWX(invitee, inviterId);
 		} catch (ThreeSaleException e) {
 			try {
 				LogHelper.errorDetailInfo(e);
@@ -499,12 +474,19 @@
 			throw new UserInfoExtraException(1, "鍏崇郴缁戝畾鏃跺け璐�");
 		}
 
-		// 閭�璇风爜鏈夋晥銆佺敓鎴愰個璇风爜
-		String inviteCode = UserUtil.getInviteCode(uid);
-		if (inviteCode == null || inviteCode.trim().length() == 0) {
-			throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触");
+		// 鐢熸垚閭�璇风爜
+		String inviteCode = null;
+		for (int i = 1; i <= 5; i ++) {
+			inviteCode = UserUtil.getInviteCode(i, uid);
+			long countCode = userInfoExtraMapper.countByInviteCode(inviteCode);
+			
+			if (countCode <= 0)
+				break;
 		}
-		// 淇濆瓨閭�璇风爜
+			
+		if (StringUtil.isNullOrEmpty(inviteCode)) 
+			throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触");
+		
 		UserInfoExtra userInfoExtra = new UserInfoExtra();
 		userInfoExtra.setUserInfo(invitee);
 		userInfoExtra.setInviteCode(inviteCode);
@@ -597,29 +579,13 @@
 
 		// 閭�璇蜂汉ID -1.5.3鏂扮増
 		Long inviterId = userInviteRecordService.getNewestInviterId(wxUnionId);
-
-		// 鍏煎1.5.3 涔嬪墠鐗堟湰
-		ThreeSale threeSale = threeSaleSerivce.getByWorkerId(uid);
-		if (inviterId == null && threeSale != null) {
-			Long bossId = threeSale.getBoss().getId();
-			UserInfoExtra inviterExtra = userInfoExtraMapper.getInfoExtraByUid(bossId);
-			if (inviterExtra != null && inviterExtra.getInviteCode() == null) {
-				// 鏇存柊閭�璇风爜
-				UserInfoExtra inviterInfoExtra = new UserInfoExtra();
-				inviterInfoExtra.setId(inviterExtra.getId());
-				inviterInfoExtra.setInviteCode(UserUtil.getInviteCode(bossId));
-				userInfoExtraMapper.updateByPrimaryKeySelective(inviterInfoExtra);
-			}
-		}
-
-		// 娌℃湁琚個璇疯繃
-		if (inviterId == null && threeSale == null) {
+		if (inviterId == null) {
 			throw new UserInfoExtraException(1, "娌℃湁瀵瑰簲鐨勯個璇峰叧绯�");
 		}
 
 		// 缁戝畾鍏崇郴
 		try {
-			threeSaleSerivce.bindRelationshipByWX(invitee, inviterId, threeSale);
+			threeSaleSerivce.bindRelationshipByWX(invitee, inviterId);
 		} catch (ThreeSaleException e) {
 			try {
 				LogHelper.errorDetailInfo(e);
@@ -629,12 +595,19 @@
 			throw new UserInfoExtraException(1, "鍏崇郴缁戝畾鏃跺け璐�");
 		}
 
-		// 閭�璇风爜鏈夋晥銆佺敓鎴愰個璇风爜
-		String inviteCode = UserUtil.getInviteCode(uid);
-		if (inviteCode == null || inviteCode.trim().length() == 0) {
-			throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触");
+		// 鐢熸垚閭�璇风爜
+		String inviteCode = null;
+		for (int i = 1; i <= 5; i ++) {
+			inviteCode = UserUtil.getInviteCode(i, uid);
+			long countCode = userInfoExtraMapper.countByInviteCode(inviteCode);
+			
+			if (countCode <= 0)
+				break;
 		}
-		// 淇濆瓨閭�璇风爜
+			
+		if (StringUtil.isNullOrEmpty(inviteCode)) 
+			throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触");
+		
 		UserInfoExtra userInfoExtra = new UserInfoExtra();
 		userInfoExtra.setUserInfo(invitee);
 		userInfoExtra.setInviteCode(inviteCode);
@@ -682,38 +655,54 @@
 	}
 
 	@Override
-	public String getUserInviteCode(Long uid) throws UserInfoExtraException {
-
+	public String getInviteCodeByUid(Long uid) {
 		UserInfoExtra userInfoExtra = userInfoExtraMapper.getInfoExtraByUid(uid);
-		if (userInfoExtra == null) {
-			userInfoExtra = new UserInfoExtra();
-		}
-
-		// 閭�璇风爜
-		String inviteCode = userInfoExtra.getInviteCode();
-
-		if (inviteCode == null || inviteCode.trim().length() == 0) {
-			// 鍒ゆ柇鐢ㄦ埛
+		if (userInfoExtra != null) {
+			// vip閭�璇风爜浼樺厛
+			if (!StringUtil.isNullOrEmpty(userInfoExtra.getInviteCodeVip()))
+				return userInfoExtra.getInviteCodeVip();
+			
+			if (!StringUtil.isNullOrEmpty(userInfoExtra.getInviteCode()))
+				return userInfoExtra.getInviteCode();
+			
+			// 鏄惁婊¤冻鐢熶骇閭�璇风爜
 			int relationshipNum = threeSaleSerivce.getSuccessRelationshipNum(uid);
-
 			if (relationshipNum > 0) {
-				// 閭�璇风爜鏈夋晥銆佺敓鎴愰個璇风爜
-				inviteCode = UserUtil.getInviteCode(uid);
-				if (inviteCode == null || inviteCode.trim().length() == 0) {
-					throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触");
+				try {
+					String inviteCode = createInviteCode(uid);
+					if (!StringUtil.isNullOrEmpty(inviteCode)) {
+						userInfoExtra.setUserInfo(new UserInfo(uid));
+						userInfoExtra.setInviteCode(inviteCode);
+						
+						saveUserInfoExtra(userInfoExtra);
+						
+						return inviteCode;
+					}
+				} catch (Exception e) {
+					LogHelper.errorDetailInfo(e);
 				}
-
-				// 淇濆瓨閭�璇风爜
-				userInfoExtra.setUserInfo(new UserInfo(uid));
-				userInfoExtra.setInviteCode(inviteCode);
-				// 淇濆瓨闄勫姞淇℃伅
-				saveUserInfoExtra(userInfoExtra);
 			}
 		}
-
-		return inviteCode;
+		return "";
 	}
 
+	/**
+	 * 鐢熶骇閭�璇风爜
+	 * @param uid
+	 * @return
+	 */
+	private String createInviteCode(Long uid) {
+		String inviteCode = null;
+		for (int i = 1; i <= 5; i ++) {
+			inviteCode = UserUtil.getInviteCode(i, uid);
+			long countCode = userInfoExtraMapper.countByInviteCode(inviteCode);
+			if (countCode <= 0)
+				break;
+		}
+		return inviteCode;
+	}
+	
+	
 	@Override
 	public long countByRankId(Long rankId) {
 		return userInfoExtraMapper.countByRankId(rankId);
@@ -755,14 +744,13 @@
 
 	@Override
 	public UserInfo getUserByInviteCode(String inviteCode) throws UserInfoExtraException {
-		if (StringUtil.isNullOrEmpty(inviteCode)) {
+		if (StringUtil.isNullOrEmpty(inviteCode))
 			throw new UserInfoExtraException(1, "閭�璇风爜涓嶈兘涓虹┖");
-		}
-		// 閭�璇蜂汉淇℃伅
-		UserInfo inviter = userInfoService.getInfoByPhoneOrInviteCode(inviteCode, inviteCode);
-		if (inviter == null) {
+		 
+		UserInfo inviter = userInfoService.getUserInfoByInviteCode(inviteCode);
+		if (inviter == null)
 			throw new UserInfoExtraException(1, "涓婄骇閭�璇风爜涓嶅瓨鍦�");
-		}
+		
 		return inviter;
 	}
 
@@ -818,40 +806,31 @@
 
 	@Transactional
 	@Override
-	public void updateInviteCode(String inviteCode, Long uid) throws UserInfoExtraException {
-		if (inviteCode.length() >= 6 && inviteCode.length() <= 12) {
-			UserInfoExtra extra = getUserInfoExtra(uid);
-			if (extra == null) {
-				throw new UserInfoExtraException(2, "鐢ㄦ埛淇℃伅涓嶅瓨鍦�");
-			}
+	public void updateInviteCodeVip(String inviteCodeVip, Long uid) throws UserInfoExtraException {
+		if (inviteCodeVip == null || inviteCodeVip.length() < 4 || inviteCodeVip.length() > 12)
+			throw new UserInfoExtraException(1, "閭�璇风爜蹇呴』涓�4鍒�12浣�");
+		
+		UserInfoExtra extra = getUserInfoExtra(uid);
+		if (extra == null)
+			throw new UserInfoExtraException(2, "鐢ㄦ埛淇℃伅涓嶅瓨鍦�");
 
-			if (StringUtil.isNullOrEmpty(extra.getInviteCode())) {
-				throw new UserInfoExtraException(3, "閭�璇风爜灏氭湭婵�娲�");
-			}
-
-			if (extra.getInviteCodeState() == null
-					|| extra.getInviteCodeState() != UserInfoExtra.INVITE_CODE_SATTE_VALID_NO_UPDATE) {
-				throw new UserInfoExtraException(4, "鍙兘淇敼涓�娆�");
-			}
-
-			if (inviteCode.equalsIgnoreCase(extra.getInviteCode())) {
-				throw new UserInfoExtraException(5, "涓嶈兘涓哄師閭�璇风爜");
-			}
-			UserInfoExtra update = new UserInfoExtra();
-			update.setId(extra.getId());
-
-			extra = userInfoExtraMapper.selectByInviteCode(inviteCode);
-			if (extra != null) {
-				throw new UserInfoExtraException(6, "閭�璇风爜宸插瓨鍦�");
-			}
-
-			update.setInviteCode(inviteCode);
-			update.setInviteCodeState(UserInfoExtra.INVITE_CODE_SATTE_VALID_UPDATED);
-			update.setUpdateTime(new Date());
-			userInfoExtraMapper.updateByPrimaryKeySelective(update);
-		} else {
-			throw new UserInfoExtraException(1, "閭�璇风爜蹇呴』涓�6鍒�12浣�");
-		}
-
+		if (StringUtil.isNullOrEmpty(extra.getInviteCode()))
+			throw new UserInfoExtraException(3, "閭�璇风爜灏氭湭婵�娲�");
+		
+		if (!StringUtil.isNullOrEmpty(extra.getInviteCodeVip()))
+			throw new UserInfoExtraException(4, "鍙兘淇敼涓�娆�");
+		
+		if (inviteCodeVip.equalsIgnoreCase(extra.getInviteCode()))
+			throw new UserInfoExtraException(5, "涓嶈兘涓哄師閭�璇风爜");
+		
+		long count = userInfoExtraMapper.countByInviteCode(inviteCodeVip);
+		if (count > 0)
+			throw new UserInfoExtraException(6, "閭�璇风爜宸插瓨鍦�");
+		
+		UserInfoExtra update = new UserInfoExtra();
+		update.setId(extra.getId());
+		update.setInviteCodeVip(inviteCodeVip);
+		update.setUpdateTime(new Date());
+		userInfoExtraMapper.updateByPrimaryKeySelective(update);
 	}
 }

--
Gitblit v1.8.0