From cd637932a970e7935b17d41568fe89c92775bccc Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期五, 06 十二月 2019 17:18:27 +0800
Subject: [PATCH] 双12活动添加,邀请码发布修改,爬单新规则添加

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java |  596 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 542 insertions(+), 54 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 faf6869..d80e726 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
@@ -1,6 +1,10 @@
 package com.yeshi.fanli.service.impl.user;
 
+import java.io.InputStream;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -9,34 +13,45 @@
 import javax.annotation.Resource;
 
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 import org.yeshi.utils.DateUtil;
+import org.yeshi.utils.HttpUtil;
+import org.yeshi.utils.entity.FileUploadResult;
+import org.yeshi.utils.tencentcloud.COSManager;
 
 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;
 import com.yeshi.fanli.entity.bus.user.UserRankRecord;
-import com.yeshi.fanli.exception.ThreeSaleException;
+import com.yeshi.fanli.entity.bus.user.WeiXinUser;
+import com.yeshi.fanli.exception.user.ThreeSaleException;
 import com.yeshi.fanli.exception.user.UserInfoExtraException;
 import com.yeshi.fanli.log.LogHelper;
 import com.yeshi.fanli.service.inter.config.ConfigService;
-import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
 import com.yeshi.fanli.service.inter.order.CommonOrderCountService;
+import com.yeshi.fanli.service.inter.user.ForbiddenUserIdentifyCodeService;
+import com.yeshi.fanli.service.inter.user.UserAccountService;
 import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
 import com.yeshi.fanli.service.inter.user.UserInfoService;
+import com.yeshi.fanli.service.inter.user.UserInviteRecordService;
 import com.yeshi.fanli.service.inter.user.UserRankService;
+import com.yeshi.fanli.service.inter.user.invite.ThreeSaleSerivce;
 import com.yeshi.fanli.util.Constant;
+import com.yeshi.fanli.util.StringUtil;
 import com.yeshi.fanli.util.account.UserUtil;
+import com.yeshi.fanli.util.wx.WXLoginUtil;
 import com.yeshi.fanli.vo.user.UserInfoExtraVO;
-
 
 @Service
 public class UserInfoExtraServiceImpl implements UserInfoExtraService {
 
 	@Resource
 	private UserInfoExtraMapper userInfoExtraMapper;
-	
+
 	@Resource
 	private UserRankRecordMapper userRankRecordMapper;
 
@@ -48,13 +63,21 @@
 
 	@Resource
 	private ConfigService configService;
-	
+
 	@Resource
 	private ThreeSaleSerivce threeSaleSerivce;
-	
+
 	@Resource
 	private UserInfoService userInfoService;
-	
+
+	@Resource
+	private UserAccountService userAccountService;
+
+	@Resource
+	private ForbiddenUserIdentifyCodeService forbiddenUserIdentifyCodeService;
+
+	@Resource
+	private UserInviteRecordService userInviteRecordService;
 
 	@Override
 	public UserInfoExtraVO getRankInfo(Long uid) throws UserInfoExtraException, Exception {
@@ -72,7 +95,11 @@
 		extraVO.setRankTip(rankTip);
 
 		Integer rankOrderNum = extraVO.getRankOrderNum();
-		if (rankOrderNum != null && rankOrderNum > 0) {
+
+		UserRank userRank = extraVO.getUserRank();
+		// 闈掗摐绛夌骇涓嶈繑鍥炴伃鍠滆鍙� 娉細闈掗摐绛夌骇id 蹇呴』涓�1
+		if (userRank != null && userRank.getId() != null && userRank.getId() > 1) {
+
 			String rankMsg = null;
 			Integer rankSource = extraVO.getRankSource();
 			switch (rankSource) {
@@ -127,43 +154,62 @@
 	}
 
 	@Override
-	public UserInfoExtra updateUserRankByUid(Long uid) throws UserInfoExtraException{
-		
+	public void updateUserRankByUid(Long uid) throws UserInfoExtraException {
+		if (uid == null || uid == 0) {
+			return;
+		}
+
 		boolean isupdateRank = true;
-		
+
 		UserInfoExtra userInfoExtra = userInfoExtraMapper.getInfoExtraByUid(uid);
 		if (userInfoExtra != null && userInfoExtra.getId() != null) {
 			// 鍒ゆ柇褰撴湀鏄惁宸叉洿鏂�
 			Date rankUpdateTime = userInfoExtra.getRankUpdateTime();
-			if (rankUpdateTime != null && DateUtil.isSameMonth(rankUpdateTime, new Date())) {
-				isupdateRank = false;
-			} 
+			if (rankUpdateTime != null) {
+				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
+				if (sdf.format(rankUpdateTime).equals(sdf.format(new Date()))) {
+					isupdateRank = false;
+				}
+			}
 		} else {
 			userInfoExtra = new UserInfoExtra();
 			userInfoExtra.setUserInfo(new UserInfo(uid));
 		}
-		
+
 		// 鏇存柊绛夌骇
 		if (isupdateRank) {
 			updateRank(userInfoExtra);
 		}
-		
-		return userInfoExtra;
 	}
-	
-	
+
+	@Override
+	public void createUserInfoExtra(Long uid) throws UserInfoExtraException {
+		UserInfoExtra userInfoExtra = userInfoExtraMapper.getInfoExtraByUid(uid);
+		if (userInfoExtra != null) {
+			return;
+		}
+
+		userInfoExtra = new UserInfoExtra();
+		userInfoExtra.setUserInfo(new UserInfo(uid));
+		userInfoExtra.setFirstLoginTime(new Date());
+		userInfoExtra.setCreateTime(new Date());
+		userInfoExtra.setUpdateTime(new Date());
+		userInfoExtra.setActiveTime(new Date());
+		userInfoExtraMapper.insertSelective(userInfoExtra);
+	}
+
 	@Override
 	public UserInfoExtra updateRank(UserInfoExtra userInfoExtra) throws UserInfoExtraException {
 		UserInfo userInfo = userInfoExtra.getUserInfo();
-		if (userInfo == null ) {
+		if (userInfo == null) {
 			throw new UserInfoExtraException(1, "鐢ㄦ埛涓嶅瓨鍦�");
 		}
-		
+
 		Long uid = userInfo.getId();
-		if (uid == null ) {
+		if (uid == null) {
 			throw new UserInfoExtraException(1, "鐢ㄦ埛ID涓嶅瓨鍦�");
-		}		
-				
+		}
+
 		List<UserRank> listRank = userRankService.getAllRank();
 		if (listRank == null || listRank.size() == 0) {
 			throw new UserInfoExtraException(1, "绯荤粺绛夌骇涓嶅瓨鍦�");
@@ -216,29 +262,32 @@
 				userInfoExtra.setRankSource(Constant.TYPE_REBATE);
 			}
 
-			
 			if (rank != null) {
 				userInfoExtra.setUserRank(rank);
 			} else {
 				rank = userRank;
 				// 榛樿鏈�浣庣瓑绾�:闈掗摐
 				userInfoExtra.setUserRank(rank);
-				userInfoExtra.setRankOrderNum(0);
+				userInfoExtra.setRankOrderNum(selfOrderNum);
 				userInfoExtra.setRankSource(Constant.TYPE_REBATE);
 				break;
 			}
 		}
-		// 绛夌骇鏇存柊鏃堕棿
-		userInfoExtra.setRankUpdateTime(new Date());
+
+		// 绛夌骇鏇存柊鏃堕棿 褰撴湀1鍙�
+		Calendar calendar1 = Calendar.getInstance();
+		calendar1.set(Calendar.DAY_OF_MONTH, 1);
+		userInfoExtra.setRankUpdateTime(calendar1.getTime());
+
 		// 淇濆瓨淇℃伅骞惰繑鍥�
 		saveUserInfoExtra(userInfoExtra);
-		
+
 		UserRankRecord userRankRecord = new UserRankRecord();
 		userRankRecord.setUid(uid);
 		userRankRecord.setRankId(rank.getId());
 		userRankRecord.setCreateTime(new Date());
 		userRankRecordMapper.insertSelective(userRankRecord);
-		
+
 		return userInfoExtra;
 	}
 
@@ -274,25 +323,33 @@
 		}
 		return userInfoExtra;
 	}
-	
+
 	@Override
-	public String activateInviteCode(Long uid, String inviteCode) throws UserInfoExtraException{
+	public String activateInviteCode(Long uid, String inviteCode) throws UserInfoExtraException {
 		if (uid == null || inviteCode == null) {
 			throw new UserInfoExtraException(1, "鐢ㄦ埛id銆侀個璇风爜涓嶈兘涓虹┖");
 		}
-		
+
 		// 琚個璇蜂汉淇℃伅
 		UserInfo invitee = userInfoService.selectByPKey(uid);
 		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, "宸插瓨鍦ㄩ個璇风爜,璇峰埛鏂伴〉闈�");
+			}
+		}
+
 		// 閭�璇蜂汉淇℃伅
 		UserInfo inviter = userInfoService.getInfoByPhoneOrInviteCode(inviteCode, inviteCode);
 		if (inviter == null) {
-			throw new UserInfoExtraException(1, "璇疯緭鍏ユ湁鏁堢殑閭�璇风爜");
+			throw new UserInfoExtraException(1, "涓婄骇閭�璇风爜涓嶅瓨鍦�");
 		}
-		
+
 		// 缁戝畾鍏崇郴
 		try {
 			threeSaleSerivce.bindRelationshipByInviteCode(invitee, inviter);
@@ -304,57 +361,348 @@
 			}
 			throw new UserInfoExtraException(1, "婵�娲诲け璐�");
 		}
-		
-		
+
 		// 閭�璇风爜鏈夋晥銆佺敓鎴愰個璇风爜
-		String code = UserUtil.getInviteCode(uid); 
+		String code = UserUtil.getInviteCode(uid);
 		if (code == null || code.trim().length() == 0) {
 			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, "婵�娲荤爜鐢熸垚澶辫触");
 		}
-		
+
 		return code;
 	}
-	
+
+	@Override
+	public void activationInviteWX(WXAccountInfoDTO wxAccount, Long uid, String code) throws UserInfoExtraException {
+		if (uid == null || code == null) {
+			throw new UserInfoExtraException(1, "婵�娲讳俊鎭笉瀹屾暣");
+		}
+
+		// 鐢ㄦ埛淇℃伅
+		UserInfo invitee = userInfoService.selectByPKey(uid);
+		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, "宸茬粡婵�娲�, 鏃犻渶鍐嶆婵�娲�");
+			}
+		}
+
+		// 鑾峰彇寰俊淇℃伅
+		WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(code, wxAccount.getAppId(), wxAccount.getAppSecret());
+		if (weiXinUser == null) {
+			throw new UserInfoExtraException(1, "寰俊鎺堟潈澶辫触");
+		}
+		String wxUnionId = weiXinUser.getUnionid();
+		if (wxUnionId == null || wxUnionId.trim().length() == 0) {
+			throw new UserInfoExtraException(1, "寰俊鎺堟潈澶辫触");
+		}
+
+		// 楠岃瘉鏁版嵁
+		String wxUnionIdExist = invitee.getWxUnionId();
+		if (StringUtil.isNullOrEmpty(wxUnionIdExist)) {
+			UserInfo newUser = userInfoService.getEffectiveUserInfoByWXUnionId(weiXinUser.getUnionid());
+			if (newUser != null) {
+				throw new UserInfoExtraException(1, "姝ゅ井淇″凡琚叾浠栧笎鍙风粦瀹�");
+			}
+
+			// 鑷姩缁戝畾寰俊
+			UserInfo updateUserInfo = new UserInfo(uid);
+			updateUserInfo.setWxOpenId(weiXinUser.getOpenid());
+			updateUserInfo.setWxUnionId(weiXinUser.getUnionid());
+			updateUserInfo.setWxName(weiXinUser.getNickname());
+			updateUserInfo.setWxPic(weiXinUser.getHeadimgurl());
+
+			// 灏嗛粯璁ゆ樀绉版浛鎹㈡垚寰俊鏄电О
+			String defaultNickName = Constant.systemCommonConfig.getDefaultNickName();
+			if (!StringUtil.isNullOrEmpty(defaultNickName) && !StringUtil.isNullOrEmpty(weiXinUser.getNickname())) {
+				defaultNickName = defaultNickName + uid;
+				if ((defaultNickName.equals(invitee.getNickName()) || "杩斿埄鍒�".equals(invitee.getNickName())
+						|| invitee.getNickName().startsWith(Constant.systemCommonConfig.getDefaultNickName()))) {
+					updateUserInfo.setNickName(weiXinUser.getNickname());
+				}
+			}
+
+			// 灏嗛粯璁ゅご鍍忔浛鎹㈡垚寰俊澶村儚
+			String defaultPortrait = Constant.systemCommonConfig.getDefaultPortrait();
+			if (!StringUtil.isNullOrEmpty(weiXinUser.getHeadimgurl()) && !StringUtil.isNullOrEmpty(defaultPortrait)
+					&& defaultPortrait.equals(invitee.getPortrait())) {
+				String headimgurl = weiXinUser.getHeadimgurl();
+				InputStream asInputStream = HttpUtil.getAsInputStream(headimgurl);
+				if (asInputStream == null) {
+					LogHelper.test("寰俊澶村儚涓嬭浇澶辫触: " + weiXinUser.getUnionid() + " " + headimgurl);
+				} else {
+					FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream, String.format(
+							"/portrait/wx/%s_%s.jpg", weiXinUser.getUnionid(), System.currentTimeMillis() + ""));
+					if (result != null && !StringUtil.isNullOrEmpty(result.getUrl())) {
+						headimgurl = result.getUrl();
+					} else {
+						LogHelper.test("寰俊澶村儚涓婁紶澶辫触: " + weiXinUser.getUnionid() + " " + headimgurl);
+					}
+				}
+				updateUserInfo.setPortrait(headimgurl);
+			}
+
+			userInfoService.updateByPrimaryKeySelective(updateUserInfo);
+
+		} else if (!wxUnionId.equals(wxUnionIdExist)) {
+			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) {
+			throw new UserInfoExtraException(1, "娌℃湁瀵瑰簲鐨勯個璇峰叧绯�");
+		}
+
+		// 缁戝畾鍏崇郴
+		try {
+			threeSaleSerivce.bindRelationshipByWX(invitee, inviterId, threeSale);
+		} catch (ThreeSaleException e) {
+			try {
+				LogHelper.errorDetailInfo(e);
+			} catch (Exception e1) {
+				e1.printStackTrace();
+			}
+			throw new UserInfoExtraException(1, "鍏崇郴缁戝畾鏃跺け璐�");
+		}
+
+		// 閭�璇风爜鏈夋晥銆佺敓鎴愰個璇风爜
+		String inviteCode = UserUtil.getInviteCode(uid);
+		if (inviteCode == null || inviteCode.trim().length() == 0) {
+			throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触");
+		}
+		// 淇濆瓨閭�璇风爜
+		UserInfoExtra userInfoExtra = new UserInfoExtra();
+		userInfoExtra.setUserInfo(invitee);
+		userInfoExtra.setInviteCode(inviteCode);
+		userInfoExtra.setUpdateTime(new Date());
+		if (extra != null) {
+			userInfoExtra.setId(extra.getId());
+			userInfoExtraMapper.updateByPrimaryKeySelective(userInfoExtra);
+		} else {
+			userInfoExtra.setCreateTime(new Date());
+			userInfoExtraMapper.insertSelective(userInfoExtra);
+		}
+	}
+
+	@Override
+	public void activeInviteWX(Long uid, WeiXinUser weiXinUser) throws UserInfoExtraException {
+		if (uid == null || weiXinUser == null) {
+			throw new UserInfoExtraException(1, "婵�娲讳俊鎭笉瀹屾暣");
+		}
+
+		// 鐢ㄦ埛淇℃伅
+		UserInfo invitee = userInfoService.selectByPKey(uid);
+		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, "宸茬粡婵�娲�, 鏃犻渶鍐嶆婵�娲�");
+			}
+		}
+
+		String wxUnionId = weiXinUser.getUnionid();
+		if (wxUnionId == null || wxUnionId.trim().length() == 0) {
+			throw new UserInfoExtraException(1, "寰俊鎺堟潈澶辫触");
+		}
+
+		// 楠岃瘉鏁版嵁
+		String wxUnionIdExist = invitee.getWxUnionId();
+		if (StringUtil.isNullOrEmpty(wxUnionIdExist)) {
+			UserInfo newUser = userInfoService.getEffectiveUserInfoByWXUnionId(weiXinUser.getUnionid());
+			if (newUser != null) {
+				throw new UserInfoExtraException(1, "姝ゅ井淇″凡琚叾浠栧笎鍙风粦瀹�");
+			}
+
+			// 鑷姩缁戝畾寰俊
+			UserInfo updateUserInfo = new UserInfo(uid);
+			updateUserInfo.setWxOpenId(weiXinUser.getOpenid());
+			updateUserInfo.setWxUnionId(weiXinUser.getUnionid());
+			updateUserInfo.setWxName(weiXinUser.getNickname());
+			updateUserInfo.setWxPic(weiXinUser.getHeadimgurl());
+
+			// 灏嗛粯璁ゆ樀绉版浛鎹㈡垚寰俊鏄电О
+			String defaultNickName = Constant.systemCommonConfig.getDefaultNickName();
+			if (!StringUtil.isNullOrEmpty(defaultNickName) && !StringUtil.isNullOrEmpty(weiXinUser.getNickname())) {
+				defaultNickName = defaultNickName + uid;
+				if ((defaultNickName.equals(invitee.getNickName()) || "杩斿埄鍒�".equals(invitee.getNickName())
+						|| invitee.getNickName().startsWith(Constant.systemCommonConfig.getDefaultNickName()))) {
+					updateUserInfo.setNickName(weiXinUser.getNickname());
+				}
+			}
+
+			// 灏嗛粯璁ゅご鍍忔浛鎹㈡垚寰俊澶村儚
+			String defaultPortrait = Constant.systemCommonConfig.getDefaultPortrait();
+			if (!StringUtil.isNullOrEmpty(weiXinUser.getHeadimgurl()) && !StringUtil.isNullOrEmpty(defaultPortrait)
+					&& defaultPortrait.equals(invitee.getPortrait())) {
+				String headimgurl = weiXinUser.getHeadimgurl();
+				InputStream asInputStream = HttpUtil.getAsInputStream(headimgurl);
+				if (asInputStream == null) {
+					LogHelper.test("寰俊澶村儚涓嬭浇澶辫触: " + weiXinUser.getUnionid() + " " + headimgurl);
+				} else {
+					FileUploadResult result = COSManager.getInstance().uploadFile(asInputStream, String.format(
+							"/portrait/wx/%s_%s.jpg", weiXinUser.getUnionid(), System.currentTimeMillis() + ""));
+					if (result != null && !StringUtil.isNullOrEmpty(result.getUrl())) {
+						headimgurl = result.getUrl();
+					} else {
+						LogHelper.test("寰俊澶村儚涓婁紶澶辫触: " + weiXinUser.getUnionid() + " " + headimgurl);
+					}
+				}
+				updateUserInfo.setPortrait(headimgurl);
+			}
+
+			userInfoService.updateByPrimaryKeySelective(updateUserInfo);
+
+		} else if (!wxUnionId.equals(wxUnionIdExist)) {
+			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) {
+			throw new UserInfoExtraException(1, "娌℃湁瀵瑰簲鐨勯個璇峰叧绯�");
+		}
+
+		// 缁戝畾鍏崇郴
+		try {
+			threeSaleSerivce.bindRelationshipByWX(invitee, inviterId, threeSale);
+		} catch (ThreeSaleException e) {
+			try {
+				LogHelper.errorDetailInfo(e);
+			} catch (Exception e1) {
+				e1.printStackTrace();
+			}
+			throw new UserInfoExtraException(1, "鍏崇郴缁戝畾鏃跺け璐�");
+		}
+
+		// 閭�璇风爜鏈夋晥銆佺敓鎴愰個璇风爜
+		String inviteCode = UserUtil.getInviteCode(uid);
+		if (inviteCode == null || inviteCode.trim().length() == 0) {
+			throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触");
+		}
+		// 淇濆瓨閭�璇风爜
+		UserInfoExtra userInfoExtra = new UserInfoExtra();
+		userInfoExtra.setUserInfo(invitee);
+		userInfoExtra.setInviteCode(inviteCode);
+		userInfoExtra.setUpdateTime(new Date());
+		if (extra != null) {
+			userInfoExtra.setId(extra.getId());
+			userInfoExtraMapper.updateByPrimaryKeySelective(userInfoExtra);
+		} else {
+			userInfoExtra.setCreateTime(new Date());
+			userInfoExtraMapper.insertSelective(userInfoExtra);
+		}
+	}
+
+	@Override
+	public void updateActiveTime(Long uid, Date date) {
+		UserInfoExtra userInfoExtra = new UserInfoExtra();
+		userInfoExtra.setUserInfo(new UserInfo(uid));
+		userInfoExtra.setActiveTime(date);
+		userInfoExtraMapper.updateInfoExtraByUid(userInfoExtra);
+	}
+
 	@Override
 	public UserInfoExtra getUserInfoExtra(Long uid) {
 		return userInfoExtraMapper.getInfoExtraByUid(uid);
 	}
-	
+
+	@Override
+	public UserInfoExtra getUserALLInfo(Long uid) {
+		return userInfoExtraMapper.gerUserRank(uid);
+	}
+
+	@Override
+	public UserInfoExtra getByUidForUpdate(Long uid) {
+		return userInfoExtraMapper.getByUidForUpdate(uid);
+	}
+
+	@Override
+	public void updateGoldCoin(Long id, Integer goldCoin) {
+		userInfoExtraMapper.updateGoldCoin(id, goldCoin);
+	}
+
+	@Override
+	public UserInfoExtraVO getInfoExtraVOByUid(Long uid) {
+		return userInfoExtraMapper.getInfoExtraVOByUid(uid);
+	}
+
 	@Override
 	public String getUserInviteCode(Long uid) throws UserInfoExtraException {
-		
+
 		UserInfoExtra userInfoExtra = userInfoExtraMapper.getInfoExtraByUid(uid);
 		if (userInfoExtra == null) {
 			userInfoExtra = new UserInfoExtra();
 		}
-		
+
 		// 閭�璇风爜
 		String inviteCode = userInfoExtra.getInviteCode();
-		
+
 		if (inviteCode == null || inviteCode.trim().length() == 0) {
 			// 鍒ゆ柇鐢ㄦ埛
 			int relationshipNum = threeSaleSerivce.getSuccessRelationshipNum(uid);
-			
+
 			if (relationshipNum > 0) {
 				// 閭�璇风爜鏈夋晥銆佺敓鎴愰個璇风爜
-				inviteCode = UserUtil.getInviteCode(uid); 
+				inviteCode = UserUtil.getInviteCode(uid);
 				if (inviteCode == null || inviteCode.trim().length() == 0) {
 					throw new UserInfoExtraException(1, "婵�娲荤爜鐢熸垚澶辫触");
 				}
-				
+
 				// 淇濆瓨閭�璇风爜
 				userInfoExtra.setUserInfo(new UserInfo(uid));
 				userInfoExtra.setInviteCode(inviteCode);
@@ -362,8 +710,148 @@
 				saveUserInfoExtra(userInfoExtra);
 			}
 		}
-		
+
 		return inviteCode;
 	}
-	
+
+	@Override
+	public long countByRankId(Long rankId) {
+		return userInfoExtraMapper.countByRankId(rankId);
+	}
+
+	@Override
+	public boolean isNewUser(Long uid) {
+		boolean isNew = false;
+		if (uid == null) {
+			return isNew;
+		}
+
+		UserInfoExtra extra = userInfoExtraMapper.getInfoExtraByUid(uid);
+		if (extra != null && extra.getFirstLoginTime() != null) {
+			try {
+				int day = DateUtil.daysBetween(extra.getFirstLoginTime(), new Date());
+				if (day <= 15) {
+					isNew = true;
+				}
+			} catch (ParseException e) {
+				e.printStackTrace();
+			}
+		}
+		return isNew;
+	}
+
+	@Override
+	public UserRank gerUserRank(Long uid) {
+		if (uid == null) {
+			return null;
+		}
+
+		UserInfoExtra userInfoExtra = userInfoExtraMapper.gerUserRank(uid);
+		if (userInfoExtra == null) {
+			return null;
+		}
+		return userInfoExtra.getUserRank();
+	}
+
+	@Override
+	public UserInfo getUserByInviteCode(String inviteCode) throws UserInfoExtraException {
+		if (StringUtil.isNullOrEmpty(inviteCode)) {
+			throw new UserInfoExtraException(1, "閭�璇风爜涓嶈兘涓虹┖");
+		}
+		// 閭�璇蜂汉淇℃伅
+		UserInfo inviter = userInfoService.getInfoByPhoneOrInviteCode(inviteCode, inviteCode);
+		if (inviter == null) {
+			throw new UserInfoExtraException(1, "涓婄骇閭�璇风爜涓嶅瓨鍦�");
+		}
+		return inviter;
+	}
+
+	@Override
+	public UserInfo getInviterInfo(Long uid, String wxUnionId) throws UserInfoExtraException {
+		if (wxUnionId == null) {
+			throw new UserInfoExtraException(1, "unionId淇℃伅涓嶅畬鏁�");
+		}
+
+		// 鐢ㄦ埛淇℃伅
+		UserInfo invitee = userInfoService.selectByPKey(uid);
+		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, "宸茬粡婵�娲�, 鏃犻渶鍐嶆婵�娲�");
+			}
+		}
+
+		// 楠岃瘉鏁版嵁
+		String wxUnionIdExist = invitee.getWxUnionId();
+		if (StringUtil.isNullOrEmpty(wxUnionIdExist)) {
+			UserInfo newUser = userInfoService.getEffectiveUserInfoByWXUnionId(wxUnionId);
+			if (newUser != null) {
+				throw new UserInfoExtraException(1, "姝ゅ井淇″凡琚叾浠栧笎鍙风粦瀹�");
+			}
+		} else if (!wxUnionId.equals(wxUnionIdExist)) {
+			throw new UserInfoExtraException(1, "缁戝畾寰俊涓庢縺娲诲井淇′笉涓�鑷�");
+		}
+
+		// 閭�璇蜂汉ID
+		Long inviterId = userInviteRecordService.getNewestInviterId(wxUnionId);
+		if (inviterId == null) {
+			throw new UserInfoExtraException(1, "娌℃湁瀵瑰簲鐨勯個璇峰叧绯�");
+		}
+
+		UserInfo inviter = userInfoService.selectByPKey(inviterId);
+		if (inviter == null) {
+			throw new UserInfoExtraException(1, "瀵瑰簲鐨勯個璇峰叧绯讳笉瀛樺湪");
+		}
+		return inviter;
+	}
+
+	@Override
+	public void updateAutoExtract(Integer days) {
+		userInfoExtraMapper.updateAutoExtract(days);
+	}
+
+	@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, "鐢ㄦ埛淇℃伅涓嶅瓨鍦�");
+			}
+
+			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浣�");
+		}
+
+	}
 }

--
Gitblit v1.8.0