admin
2020-04-13 dd5b15229cb15459fa7c31ccea77dac28cbfafbd
fanli/src/main/java/com/yeshi/fanli/service/impl/user/invite/ThreeSaleSerivceImpl.java
@@ -13,16 +13,18 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.yeshi.fanli.dao.mybatis.ThreeSaleMapper;
import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
import com.yeshi.fanli.dto.mq.user.body.UserInviteMQMsg;
import com.yeshi.fanli.entity.bus.user.ThreeSale;
import com.yeshi.fanli.entity.bus.user.ThreeSaleExtraInfo;
import com.yeshi.fanli.entity.bus.user.UserActiveLog;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.exception.user.ThreeSaleException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.order.config.HongBaoManageService;
import com.yeshi.fanli.service.inter.tlj.UserTaoLiJinOriginService;
import com.yeshi.fanli.service.inter.user.UserActiveLogService;
import com.yeshi.fanli.service.inter.user.UserInfoExtraService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
@@ -34,7 +36,8 @@
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.VersionUtil;
import com.yeshi.fanli.util.cmq.ThreeSaleCMQManager;
import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@@ -61,9 +64,6 @@
   private MsgInviteDetailService msgInviteDetailService;
   @Resource
   private UserTaoLiJinOriginService userTaoLiJinOriginService;
   @Resource
   private UserActiveLogService userActiveLogService;
   @Resource
@@ -79,11 +79,14 @@
   @Resource(name = "taskExecutor")
   private TaskExecutor executor;
   @Resource(name = "producer")
   private Producer producer;
   public UserInfo getBoss(final long uid) {
      return threeSaleMapper.selectBoss(uid);
   }
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   public void bind(UserInfo worker, UserInfo inviter) throws ThreeSaleException {
      if (worker == null || inviter == null)
         throw new ThreeSaleException(1, "用户信息为空");
@@ -114,9 +117,6 @@
            threeSaleMapper.deleteExpireRecord(inviter.getId(), inviteeUser.getId());
         }
         // 创建通知
         userInviteMsgNotificationService.inviteScan(inviter.getId(), threeSale);
      } else {// 是已经存在的用户
         if (worker.getId().longValue() == inviter.getId())
            throw new ThreeSaleException(4, "不能绑定自己");
@@ -131,7 +131,7 @@
            List<ThreeSale> list = listByWorkerId(worker.getId());
            if (list != null && list.size() > 0) {
               for (ThreeSale ts : list) {
                  if (!ts.getState() && ts.getExpire() == ThreeSale.EXPIRE_OUTOFDATE) {
                  if (!ts.getState() && ts.getExpire() == ThreeSale.EXPIRE_BREAK) {
                     outOfDateCount++;
                  }
               }
@@ -160,9 +160,6 @@
                     threeSaleMapper.deleteExpireRecord(inviter.getId(), worker.getId());
                  }
                  userInviteMsgNotificationService.inviteScan(inviter.getId(), threeSale);
                  // threeSaleSerivce.bind(invitee, inviter);
               } else {
                  // 还存在有效的邀请,则不做处理
               }
@@ -176,7 +173,7 @@
            if (list != null)
               for (int i = 0; i < list.size(); i++) {
                  if (list.get(i).getState() != null && list.get(i).getState() == false
                        && list.get(i).getExpire() == ThreeSale.EXPIRE_OUTOFDATE) {// 过期的
                        && list.get(i).getExpire() == ThreeSale.EXPIRE_BREAK) {// 过期的
                     list.remove(i);
                     i--;
                  }
@@ -192,8 +189,6 @@
               ts.setSucceedTime(System.currentTimeMillis());
               ts.setWorker(worker);
               threeSaleMapper.insertSelective(ts);
               ThreeSaleCMQManager.getInstance().addThreeSaleMsg(ts);
               userInviteMsgNotificationService.inviteSuccess(inviter.getId(), ts);
            } else {
               if (!list.get(0).getState() && list.get(0).getExpire() == ThreeSale.EXPIRE_NORMAL
                     && list.get(0).getBoss().getId() == inviter.getId().longValue()) {// 未生效的上级和现在上级一样就生效
@@ -201,8 +196,6 @@
                  update.setState(true);
                  update.setSucceedTime(System.currentTimeMillis());
                  threeSaleMapper.updateByPrimaryKeySelective(update);
                  ThreeSaleCMQManager.getInstance().addThreeSaleMsg(list.get(0));// 添加通知
                  userInviteMsgNotificationService.inviteSuccess(inviter.getId(), list.get(0));
               }
            }
         }
@@ -233,14 +226,11 @@
         oldSale.setState(update.getState());
         oldSale.setSucceedTime(update.getSucceedTime());
         UserInfo inviter = list.get(0).getBoss();
         // 不以邀请人数计算等级
         // if (inviter != null)
         // reComputeUserRank(inviter.getId());
         // 添加到队列
         ThreeSaleCMQManager.getInstance().addThreeSaleMsg(oldSale);
         // 通知
         userInviteMsgNotificationService.inviteSuccess(inviter.getId(), oldSale);
         inviteSuccess(oldSale.getBoss().getId(), worker);
      }
   }
@@ -502,185 +492,81 @@
   }
   @Override
   @Transactional
   @Transactional(rollbackFor = Exception.class)
   public void bindRelationshipByInviteCode(UserInfo invitee, UserInfo inviter) throws ThreeSaleException {
      if (invitee == null || inviter == null)
         throw new ThreeSaleException(1, "用户信息为空");
      // 获取有效的邀请关系
      ThreeSale threeSale = threeSaleMapper.getRelationshipByBossIdAndWorkerId(inviter.getId(), invitee.getId());
      // 新建立确定关系
      ThreeSale threeSale = new ThreeSale();
      threeSale.setBoss(inviter);
      threeSale.setWorker(invitee);
      threeSale.setState(true);
      threeSale.setExpire(ThreeSale.EXPIRE_NORMAL);
      threeSale.setSucceedTime(java.lang.System.currentTimeMillis());
      threeSale.setCreateTime(java.lang.System.currentTimeMillis());
      threeSale.setUpdateTime(java.lang.System.currentTimeMillis());
      threeSaleMapper.insertSelective(threeSale);
      if (threeSale != null) {
         // 有效关系--生效
         threeSale.setBoss(inviter);
         threeSale.setWorker(invitee);
         threeSale.setState(true);
         threeSale.setExpire(ThreeSale.EXPIRE_NORMAL);
         threeSale.setSucceedTime(java.lang.System.currentTimeMillis());
         threeSale.setUpdateTime(java.lang.System.currentTimeMillis());
         threeSaleMapper.updateByPrimaryKeySelective(threeSale);
      } else {
         // 新建立确定关系
         threeSale = new ThreeSale();
         threeSale.setBoss(inviter);
         threeSale.setWorker(invitee);
         threeSale.setState(true);
         threeSale.setExpire(ThreeSale.EXPIRE_NORMAL);
         threeSale.setSucceedTime(java.lang.System.currentTimeMillis());
         threeSale.setCreateTime(java.lang.System.currentTimeMillis());
         threeSale.setUpdateTime(java.lang.System.currentTimeMillis());
         threeSaleMapper.insertSelective(threeSale);
      }
      List<ThreeSale> listExpire = threeSaleMapper.getExpireRecord(inviter.getId(), invitee.getId());
      if (listExpire != null && listExpire.size() > 0) {
         // 删除历史通知
         for (ThreeSale saleExpire : listExpire) {
            msgInviteDetailService.deleteByThreeSale(saleExpire);
         }
         // 删除已失效关系
         threeSaleMapper.deleteExpireRecord(inviter.getId(), invitee.getId());
      }
      // 添加到队列
      ThreeSaleCMQManager.getInstance().addThreeSaleMsg(threeSale);
      // 邀请消息
      userInviteMsgNotificationService.inviteSuccess(inviter.getId(), threeSale);
      executor.execute(new Runnable() {
         @Override
         public void run() {
            UserActiveLog activeLog = userActiveLogService.getUserLatestActiveInfo(inviter.getId());
            try {
               if (activeLog != null && !StringUtil.isNullOrEmpty(activeLog.getVersionCode())) {
                  String versionCode = activeLog.getVersionCode();
                  String channel = activeLog.getChannel();
                  boolean result = false;
                  // 渠道是appstore 判断为IOS
                  if (!StringUtil.isNullOrEmpty(channel) && "appstore".equalsIgnoreCase(channel)) {
                     result = VersionUtil.greaterThan_1_5_60("ios", versionCode);
                  } else {
                     result = VersionUtil.greaterThan_1_5_60("android", versionCode);
                  }
                  if (result) {
                     // 被邀请名称
                     String inviteName = invitee.getNickName();
                     if (StringUtil.isNullOrEmpty(inviteName)) {
                        UserInfo userInfo = userInfoService.selectByPKey(invitee.getId());
                        if (userInfo != null) {
                           inviteName = userInfo.getNickName();
                        }
                     }
                     // 奖励邀请红包(淘礼金)
                     userTaoLiJinOriginService.addInviteWinMoney(inviter.getId(), inviteName);
                  }
               }
            } catch (Exception e) {
               LogHelper.errorDetailInfo(e);
            }
            // 邀请金币
            integralGetService.addInviteLevelOne(inviter.getId(), invitee.getId());
         }
      });
      inviteSuccess(inviter.getId(), invitee);
   }
   @Override
   @Transactional
   public void bindRelationshipByWX(UserInfo invitee, Long inviterId, ThreeSale threeSaleOld)
         throws ThreeSaleException {
      if (invitee == null || (inviterId == null && threeSaleOld == null))
   @Transactional(rollbackFor = Exception.class)
   public void bindRelationshipByWX(UserInfo invitee, Long inviterId) throws ThreeSaleException {
      if (invitee == null || inviterId == null)
         throw new ThreeSaleException(1, "用户信息为空");
      ThreeSale threeSale = null;
      if (inviterId != null) {
         // 新建立确定关系
         threeSale = new ThreeSale();
         threeSale.setBoss(new UserInfo(inviterId));
         threeSale.setWorker(invitee);
         threeSale.setState(true);
         threeSale.setExpire(ThreeSale.EXPIRE_NORMAL);
         threeSale.setSucceedTime(java.lang.System.currentTimeMillis());
         threeSale.setCreateTime(java.lang.System.currentTimeMillis());
         threeSale.setUpdateTime(java.lang.System.currentTimeMillis());
         threeSaleMapper.insertSelective(threeSale);
         // 邀请关系失效 1.5.3 之前失效
         if (threeSaleOld != null) {
            ThreeSale three = new ThreeSale();
            three.setId(threeSaleOld.getId());
            three.setExpire(ThreeSale.EXPIRE_OUTOFDATE);
            three.setUpdateTime(java.lang.System.currentTimeMillis());
            threeSaleMapper.updateByPrimaryKeySelective(three);
         }
      } else if (threeSaleOld != null && inviterId == null) {
         // 1.5.3 之前生效
         threeSaleOld.setState(true);
         threeSaleOld.setExpire(ThreeSale.EXPIRE_NORMAL);
         threeSaleOld.setSucceedTime(java.lang.System.currentTimeMillis());
         threeSaleOld.setUpdateTime(java.lang.System.currentTimeMillis());
         threeSaleMapper.updateByPrimaryKeySelective(threeSaleOld);
         threeSale = threeSaleOld;
      }
      if (threeSale == null) {
         throw new ThreeSaleException(1, "关系绑定失败");
      }
      UserInfo boss = userInfoService.selectByPKey(threeSale.getBoss().getId());
      threeSale.setBoss(boss);
      // 新建立确定关系
      ThreeSale threeSale = new ThreeSale();
      threeSale.setBoss(new UserInfo(inviterId));
      threeSale.setWorker(invitee);
      threeSale.setState(true);
      threeSale.setExpire(ThreeSale.EXPIRE_NORMAL);
      threeSale.setSucceedTime(java.lang.System.currentTimeMillis());
      threeSale.setCreateTime(java.lang.System.currentTimeMillis());
      threeSale.setUpdateTime(java.lang.System.currentTimeMillis());
      threeSaleMapper.insertSelective(threeSale);
      // 添加到队列
      ThreeSaleCMQManager.getInstance().addThreeSaleMsg(threeSale);
      // 邀请成功:消息、相关处理
      inviteSuccess(inviterId, invitee);
   }
      // 邀请消息
      userInviteMsgNotificationService.inviteSuccess(threeSale.getBoss().getId(), threeSale);
   /**
    * 邀请成功
    */
   @Transactional
   private void inviteSuccess(Long bossId, UserInfo invitee) {
      if (!Constant.IS_TEST) {
         UserInviteMQMsg msg = new UserInviteMQMsg(bossId, invitee.getId(), null, new Date(), UserInviteMQMsg.STATE_SUCCESS);
         Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.inviteSuccess, msg);
         producer.send(message);
      }
      executor.execute(new Runnable() {
         @Override
         public void run() {
            try {
               UserActiveLog activeLog = userActiveLogService.getUserLatestActiveInfo(inviterId);
               if (activeLog != null && !StringUtil.isNullOrEmpty(activeLog.getVersionCode())) {
                  boolean result = false;
                  String versionCode = activeLog.getVersionCode();
                  String channel = activeLog.getChannel();
                  // 渠道是appstore 判断为IOS
                  if (!StringUtil.isNullOrEmpty(channel) && "appstore".equalsIgnoreCase(channel)) {
                     result = VersionUtil.greaterThan_1_5_60("ios", versionCode);
                  } else {
                     result = VersionUtil.greaterThan_1_5_60("android", versionCode);
                  }
                  if (result) {
                     // 被邀请名称
                     String inviteName = invitee.getNickName();
                     if (StringUtil.isNullOrEmpty(inviteName)) {
                        UserInfo userInfo = userInfoService.selectByPKey(invitee.getId());
                        if (userInfo != null) {
                           inviteName = userInfo.getNickName();
                        }
                     }
                     // 奖励邀请红包(淘礼金)
                     userTaoLiJinOriginService.addInviteWinMoney(inviterId, inviteName);
                  }
               // 邀请金币
               integralGetService.addInviteLevelOne(bossId, invitee.getId());
            } catch (Exception e) {
               e.printStackTrace();
            }
            try {
               // 直接粉丝邀请成功
               userInviteMsgNotificationService.directInviteSuccess(bossId, invitee.getNickName(),
                     invitee.getPortrait(), new Date());
               // 间接粉丝邀请成功
               ThreeSale myBoss = threeSaleMapper.getMyBoss(bossId);
               if (myBoss != null) {
                  UserInfo boss = userInfoService.selectByPKey(bossId);
                  userInviteMsgNotificationService.indirectInviteSuccess(bossId, boss.getNickName(),
                        invitee.getNickName(), invitee.getPortrait(), new Date());
               }
            } catch (Exception e) {
               LogHelper.errorDetailInfo(e);
               e.printStackTrace();
            }
            // 邀请金币
            integralGetService.addInviteLevelOne(boss.getId(), invitee.getId());
         }
      });
   }
@@ -707,6 +593,11 @@
   }
   @Override
   public ThreeSale getNearRelationByBossIdAndWorkerId(Long bossId, Long workerId) {
      return threeSaleMapper.getNearRelationByBossIdAndWorkerId(bossId, workerId);
   }
   @Override
   public List<ThreeSale> getSuccessByDate(Long bossId, Long minTime, Long maxTime) {
      return threeSaleMapper.getSuccessByDate(bossId, minTime, maxTime);
   }
@@ -717,14 +608,113 @@
   }
   @Override
   public List<ThreeSale> listFirstTeamByUndeleted(long start, int count, Long uid) {
      return threeSaleMapper.listFirstTeamByUndeleted(start, count, uid);
   }
   @Override
   public long countFirstTeamByUndeleted(Long uid) {
      return threeSaleMapper.countFirstTeamByUndeleted(uid);
   }
   @Override
   public List<ThreeSale> listSecondTeamByUndeleted(long start, int count, Long uid) {
      return threeSaleMapper.listSecondTeamByUndeleted(start, count, uid);
   }
   @Override
   public long countSecondTeamByUndeleted(Long uid) {
      return threeSaleMapper.countSecondTeamByUndeleted(uid);
   }
   @Override
   public List<ThreeSale> listSecondTeam(long start, int count, Long uid, Integer state) {
      return threeSaleMapper.listSecondTeam(start, count, uid, state);
   }
   @Override
   public ThreeSale selectLatestByWorkerIdAndState(Long workerId, int state) {
      return threeSaleMapper.selectLatestByWorkerIdAndState(workerId, state);
   }
   @Override
   public ThreeSale getByWorkerIdAndTime(Long workerId, long time) {
      return threeSaleMapper.getByWorkerIdAndTime(workerId, time);
   }
   @Override
   public void inviteSeparate(Long workerId, Long bossId) {
      threeSaleMapper.inviteSeparate(workerId, bossId);
   }
   @Override
   public long getTeamOrderNumByWorkerId(Long workerId) {
      return threeSaleMapper.getTeamOrderNumByWorkerId(workerId);
   }
   @Override
   public List<ThreeSale> getValidWorkerIdsByTime(Long bossId, long limitTime) {
      return threeSaleMapper.getValidWorkerIdsByTime(bossId, limitTime);
   }
   @Override
   public List<ThreeSale> getMyBossDeepList(Long uid, int deep) {
      List<ThreeSale> deepList = new ArrayList<>();
      ThreeSale boss = getMyBoss(uid);
      while (deep > 0 && boss != null) {
         deep--;
         deepList.add(boss);
         boss = getMyBoss(boss.getBoss().getId());
      }
      return deepList;
   }
   @Override
   public List<ThreeSale> getMyBossDeepList(Long uid) {
      //TODO 需要更加简便的方式处理
      return getMyBossDeepList(uid, 100);
   }
   @Override
   public void remvoeBreak(Long id, Long uid, Long tid) {
      ThreeSale threeSale = threeSaleMapper.selectByPrimaryKey(id);
      if (threeSale == null) {
         return;
      }
      Long workerId = threeSale.getWorker().getId();
      if (tid.longValue() != workerId.longValue()) {
         return;
      }
      Integer expire = threeSale.getExpire();
      if (expire == ThreeSale.EXPIRE_NORMAL) {
         return;
      }
      int upExpire = expire;
      Long bossId = threeSale.getBoss().getId();
      if (bossId.longValue() == uid.longValue()) { // 直接粉丝
         if (expire == ThreeSale.EXPIRE_BREAK_VIP_DEL4) {
            upExpire = ThreeSale.EXPIRE_BREAK_VIP_DEL34;
         } else {
            upExpire = ThreeSale.EXPIRE_BREAK_VIP_DEL3;
         }
      } else { // 间接粉丝
         if (expire == ThreeSale.EXPIRE_BREAK_VIP_DEL3) {
            upExpire = ThreeSale.EXPIRE_BREAK_VIP_DEL34;
         } else {
            upExpire = ThreeSale.EXPIRE_BREAK_VIP_DEL4;
         }
      }
      ThreeSale update = new ThreeSale();
      update.setId(id);
      update.setExpire(upExpire);
      update.setUpdateTime(java.lang.System.currentTimeMillis());
      threeSaleMapper.updateByPrimaryKeySelective(update);
   }
}