yujian
2019-08-27 d8359ddb48dab5cc797a9d552e11fde571f4920c
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,6 +12,7 @@
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;
@@ -148,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;
      
@@ -171,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);
   }
   
   
@@ -368,7 +386,7 @@
   @Override
   public void activationInviteWX(Long uid, String code) throws UserInfoExtraException{
      if (uid == null || code == null) {
         throw new UserInfoExtraException(1, "用户id、邀请码不能为空");
         throw new UserInfoExtraException(1, "激活信息不完整");
      }
      
      // 用户信息
@@ -386,9 +404,8 @@
         }
      }
      
      // 获取微信信息
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinWYUser(code);
      WeiXinUser weiXinUser = WXLoginUtil.getWeiXinUser(code);
      if (weiXinUser == null) {
         throw new UserInfoExtraException(1, "微信授权失败");
      }
@@ -397,24 +414,31 @@
         throw new UserInfoExtraException(1, "微信授权失败");
      }
      
      // 自动绑定微信
      UserInfo updateUserInfo = new UserInfo(uid);
      updateUserInfo.setWxName(weiXinUser.getNickname());
      updateUserInfo.setWxOpenId(weiXinUser.getOpenid());
      updateUserInfo.setWxPic(weiXinUser.getHeadimgurl());
      updateUserInfo.setWxUnionId(weiXinUser.getUnionid());
      updateUserInfo.setNickName(weiXinUser.getNickname());
      updateUserInfo.setPortrait(weiXinUser.getHeadimgurl());
      if (StringUtil.isNullOrEmpty(weiXinUser.getNickname())) {
         updateUserInfo.setNickName(Constant.systemCommonConfig.getDefaultNickName());
      // 验证数据
      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, "绑定微信与激活微信不一致");
      }
      if (StringUtil.isNullOrEmpty(weiXinUser.getHeadimgurl())) {
         updateUserInfo.setPortrait(Constant.systemCommonConfig.getDefaultPortrait());
      }
      userInfoService.updateByPrimaryKeySelective(updateUserInfo);
      
      
      // 邀请人ID -1.5.3新版
@@ -436,7 +460,7 @@
      
      // 没有被邀请过
      if (inviterId == null && threeSale == null) {
         throw new UserInfoExtraException(1, "微信激活失败");
         throw new UserInfoExtraException(1, "没有对应的邀请关系");
      }
      
      // 绑定关系
@@ -448,7 +472,7 @@
         } catch (Exception e1) {
            e1.printStackTrace();
         }
         throw new UserInfoExtraException(1, "激活失败");
         throw new UserInfoExtraException(1, "关系绑定时失败");
      }
      
      
@@ -521,4 +545,109 @@
      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 code) throws UserInfoExtraException{
      if (code == null) {
         throw new UserInfoExtraException(1, "code信息不完整");
      }
      // 用户信息
      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, "此微信已被其他帐号绑定");
         }
      } 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;
   }
}