From 573c491b4a1ba60e12a5678a01c1546c0077c1ee Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期二, 30 七月 2019 09:07:42 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 fanli/src/main/java/com/yeshi/fanli/service/impl/user/UserInfoExtraServiceImpl.java |  211 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 200 insertions(+), 11 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 5ffaff0..03a867b 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,5 +1,6 @@
 package com.yeshi.fanli.service.impl.user;
 
+import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.Calendar;
@@ -11,24 +12,32 @@
 import javax.annotation.Resource;
 
 import org.springframework.stereotype.Service;
+import org.yeshi.utils.DateUtil;
 
 import com.yeshi.fanli.dao.mybatis.user.UserInfoExtraMapper;
 import com.yeshi.fanli.dao.mybatis.user.UserRankRecordMapper;
+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.entity.bus.user.WeiXinUser;
 import com.yeshi.fanli.exception.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.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;
 
 
@@ -55,6 +64,15 @@
 	
 	@Resource
 	private UserInfoService userInfoService;
+	
+	@Resource
+	private UserAccountService userAccountService;
+	
+	@Resource
+	private ForbiddenUserIdentifyCodeService forbiddenUserIdentifyCodeService;
+	
+	@Resource
+	private UserInviteRecordService userInviteRecordService;
 	
 
 	@Override
@@ -132,7 +150,10 @@
 	}
 
 	@Override
-	public UserInfoExtra updateUserRankByUid(Long uid) throws UserInfoExtraException{
+	public void updateUserRankByUid(Long uid) throws UserInfoExtraException{
+		if (uid == null || uid == 0) {
+			return;
+		}
 		
 		boolean isupdateRank = true;
 		
@@ -155,8 +176,21 @@
 		if (isupdateRank) {
 			updateRank(userInfoExtra);
 		}
+	}
+	
+	@Override
+	public void createUserInfoExtra (Long uid) throws UserInfoExtraException{
+		UserInfoExtra userInfoExtra = userInfoExtraMapper.getInfoExtraByUid(uid);
+		if (userInfoExtra != null) {
+			return;
+		} 
 		
-		return userInfoExtra;
+		userInfoExtra = new UserInfoExtra();
+		userInfoExtra.setUserInfo(new UserInfo(uid));
+		userInfoExtra.setFirstLoginTime(new Date());
+		userInfoExtra.setCreateTime(new Date());
+		userInfoExtra.setUpdateTime(new Date());
+		userInfoExtraMapper.insertSelective(userInfoExtra);
 	}
 	
 	
@@ -288,9 +322,15 @@
 	}
 	
 	@Override
-	public String activateInviteCode(Long uid, String inviteCode) throws UserInfoExtraException{
+	public String activateInviteCode(Long uid, String inviteCode, String platform, String version) 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);
@@ -301,21 +341,15 @@
 			}
 		}
 		
-		// 琚個璇蜂汉淇℃伅
-		UserInfo invitee = userInfoService.selectByPKey(uid);
-		if (invitee == null) {
-			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);
+			threeSaleSerivce.bindRelationshipByInviteCode(invitee, inviter, platform, version);
 		} catch (ThreeSaleException e) {
 			try {
 				LogHelper.errorDetailInfo(e);
@@ -347,6 +381,120 @@
 		
 		return code;
 	}
+	
+	
+	@Override
+	public void activationInviteWX(Long uid, String code, String platform, String version) 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);
+		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());
+			if (!StringUtil.isNullOrEmpty(weiXinUser.getNickname())) {
+				updateUserInfo.setWxName(weiXinUser.getNickname());
+				updateUserInfo.setNickName(weiXinUser.getNickname());
+			}
+			if (!StringUtil.isNullOrEmpty(weiXinUser.getHeadimgurl())) {
+				updateUserInfo.setWxPic(weiXinUser.getHeadimgurl());
+				updateUserInfo.setPortrait(weiXinUser.getHeadimgurl());
+			}
+			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, platform, version);
+		} 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 UserInfoExtra getUserInfoExtra(Long uid) {
@@ -392,4 +540,45 @@
 		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();
+	}
+	
+	
 }

--
Gitblit v1.8.0