Administrator
2020-02-23 388be9f734f5b80c348bcc956726c206002e832a
fanli/src/main/java/com/yeshi/fanli/service/impl/user/invite/ThreeSaleSerivceImpl.java
@@ -15,7 +15,6 @@
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.google.gson.Gson;
import com.yeshi.fanli.dao.mybatis.ThreeSaleMapper;
import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
import com.yeshi.fanli.dto.mq.user.UserTopicTagEnum;
@@ -37,6 +36,7 @@
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.VersionUtil;
import com.yeshi.fanli.util.rocketmq.MQMsgBodyFactory;
import com.yeshi.fanli.util.rocketmq.MQTopicName;
import net.sf.json.JSONArray;
@@ -86,7 +86,7 @@
      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, "用户信息为空");
@@ -500,100 +500,47 @@
   }
   @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());
      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());
      }
      // 新建立确定关系
      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);
      inviteSuccess(inviter.getId(), invitee.getId(), threeSale);
   }
   @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, "关系绑定失败");
      }
      // 新建立确定关系
      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);
      UserInfo boss = userInfoService.selectByPKey(threeSale.getBoss().getId());
      threeSale.setBoss(boss);
      threeSale.setWorker(invitee);
      // 邀请成功:消息、相关处理
      inviteSuccess(boss.getId(), invitee.getId(), threeSale);
   }
   /**
@@ -603,17 +550,13 @@
   private void inviteSuccess(Long bossId, Long workerId, ThreeSale threeSale) {
      // 邀请消息
      UserActiveLog activeLog = userActiveLogService.getUserLatestActiveInfo(bossId);
      if (activeLog != null) {
         if (VersionUtil.greaterThan_2_1("appstore".equalsIgnoreCase(activeLog.getChannel()) ? "ios" : "android",
               activeLog.getVersionCode())) { // 2.0.2版本以上
            userInviteMsgNotificationService.inviteSuccess2_0_2(bossId, threeSale);
         } else { // 最初消息
            userInviteMsgNotificationService.inviteSuccess(threeSale.getBoss().getId(), threeSale);
         }
      if (activeLog != null && VersionUtil.greaterThan_2_0_5(
            "appstore".equalsIgnoreCase(activeLog.getChannel()) ? "ios" : "android", activeLog.getVersionCode())) { // 2.1版本以上
         userInviteMsgNotificationService.inviteSuccess2_1(bossId, threeSale);
      } else { // 最初消息
         userInviteMsgNotificationService.inviteSuccess(threeSale.getBoss().getId(), threeSale);
      }
      executor.execute(new Runnable() {
         @Override
         public void run() {
@@ -622,9 +565,9 @@
         }
      });
      UserInviteMQMsg msg = new UserInviteMQMsg(bossId, workerId, null, new Date(), UserInviteMQMsg.STATE_SUCCESS);
      Message message = new Message(MQTopicName.TOPIC_USER.name(), UserTopicTagEnum.inviteSuccess.name(),
            new Gson().toJson(msg).getBytes());
      producer.send(message);
      Message message = MQMsgBodyFactory.create(MQTopicName.TOPIC_USER, UserTopicTagEnum.inviteSuccess, msg);
      if (!Constant.IS_TEST)
         producer.send(message);
   }
   /**
@@ -646,6 +589,11 @@
   @Override
   public ThreeSale selectByWorkerId(Long workerId) {
      return threeSaleMapper.selectSuccessByWorkerId(workerId);
   }
   @Override
   public ThreeSale getNearRelationByBossIdAndWorkerId(Long bossId, Long workerId) {
      return threeSaleMapper.getNearRelationByBossIdAndWorkerId(bossId, workerId);
   }
   @Override
@@ -672,4 +620,15 @@
   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);
   }
}