yujian
2019-09-02 9eeda0d3049b71877c373c04d65533939b5582ef
fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/ThreeSaleSerivceImpl.java
@@ -8,6 +8,8 @@
import javax.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -15,16 +17,24 @@
import com.yeshi.fanli.dao.mybatis.UserInfoMapper;
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.ThreeSaleException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleExtraInfoSerivce;
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.integral.IntegralGetService;
import com.yeshi.fanli.service.inter.msg.MsgInviteDetailService;
import com.yeshi.fanli.service.inter.msg.UserInviteMsgNotificationService;
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;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.ThreeSaleCMQManager;
import com.yeshi.fanli.util.VersionUtil;
import com.yeshi.fanli.util.cmq.ThreeSaleCMQManager;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@@ -49,6 +59,26 @@
   @Resource
   private MsgInviteDetailService msgInviteDetailService;
   @Resource
   private UserTaoLiJinOriginService userTaoLiJinOriginService;
   @Resource
   private UserActiveLogService userActiveLogService;
   @Resource
   private UserInfoExtraService userInfoExtraService;
   @Resource
   private ThreeSaleExtraInfoSerivce threeSaleExtraInfoSerivce;
   @Lazy
   @Resource
   private IntegralGetService integralGetService;
   @Resource(name = "taskExecutor")
   private TaskExecutor executor;
   public UserInfo getBoss(final long uid) {
      return threeSaleMapper.selectBoss(uid);
@@ -298,18 +328,8 @@
   }
   @Override
   public List<ThreeSale> listFirstTeam(long start, int count, Long uid) {
      return threeSaleMapper.listFirstTeam(start, count, uid);
   }
   @Override
   public long countFirstTeam(Long uid, Integer state) {
      return threeSaleMapper.countFirstTeam(uid, state);
   }
   @Override
   public List<ThreeSale> listSecondTeam(long start, int count, Long uid) {
      return threeSaleMapper.listSecondTeam(start, count, uid);
   }
   @Override
@@ -333,26 +353,26 @@
   }
   @Override
   public JSONObject getMyFirstTeam(long start, int count, Long uid) {
   public JSONObject getMyFirstTeam(long start, int count, Long uid, Integer state) {
      List<ThreeSale> list = threeSaleMapper.listFirstTeam(start, count, uid);
      List<ThreeSale> list = threeSaleMapper.listFirstTeam(start, count, uid, state);
      if (list == null) {
         list = new ArrayList<ThreeSale>();
      }
      long countTotal = threeSaleMapper.countFirstTeam(uid, null);
      long countTotal = threeSaleMapper.countFirstTeam(uid, state);
      return organizeTeam(countTotal, list);
   }
   @Override
   public JSONObject getMySecondTeam(long start, int count, Long uid) {
      List<ThreeSale> list = threeSaleMapper.listSecondTeam(start, count, uid);
   public JSONObject getMySecondTeam(long start, int count, Long uid, Integer state) {
      List<ThreeSale> list = threeSaleMapper.listSecondTeam(start, count, uid, state);
      if (list == null) {
         list = new ArrayList<ThreeSale>();
      }
      long countTotal = threeSaleMapper.countSecondTeam(uid, null);
      long countTotal = threeSaleMapper.countSecondTeam(uid, state);
      return organizeTeam(countTotal, list);
   }
@@ -494,6 +514,8 @@
      if (threeSale != null) {
         // 有效关系--生效
         threeSale.setBoss(inviter);
         threeSale.setWorker(invitee);
         threeSale.setState(true);
         threeSale.setExpire(ThreeSale.EXPIRE_NORMAL);
         threeSale.setSucceedTime(java.lang.System.currentTimeMillis());
@@ -510,7 +532,6 @@
         threeSale.setCreateTime(java.lang.System.currentTimeMillis());
         threeSale.setUpdateTime(java.lang.System.currentTimeMillis());
         threeSaleMapper.insertSelective(threeSale);
      }
      List<ThreeSale> listExpire = threeSaleMapper.getExpireRecord(inviter.getId(), invitee.getId());
@@ -528,6 +549,149 @@
      // 邀请消息
      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());
         }
      });
   }
   @Override
   @Transactional
   public void bindRelationshipByWX(UserInfo invitee, Long inviterId, ThreeSale threeSaleOld)
         throws ThreeSaleException {
      if (invitee == null || (inviterId == null && threeSaleOld == 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.setWorker(invitee);
      // 添加到队列
      ThreeSaleCMQManager.getInstance().addThreeSaleMsg(threeSale);
      // 邀请消息
      userInviteMsgNotificationService.inviteSuccess(threeSale.getBoss().getId(), threeSale);
      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);
                  }
               }
            } catch (Exception e) {
               LogHelper.errorDetailInfo(e);
            }
         }
      });
   }
   /**
    * 根据邀请者查询关系
    *
    * @param workerId
    * @return
    */
   @Override
   public ThreeSale getByWorkerId(Long workerId) {
      return threeSaleMapper.getByWorkerId(workerId);
   }
   @Override
@@ -540,4 +704,19 @@
      return threeSaleMapper.selectSuccessByWorkerId(workerId);
   }
   @Override
   public List<ThreeSale> getSuccessByDate(Long bossId, Long minTime, Long maxTime) {
      return threeSaleMapper.getSuccessByDate(bossId, minTime, maxTime);
   }
   @Override
   public List<ThreeSale> listFirstTeam(long start, int count, Long uid, Integer state) {
      return threeSaleMapper.listFirstTeam(start, count, uid, state);
   }
   @Override
   public List<ThreeSale> listSecondTeam(long start, int count, Long uid, Integer state) {
      return threeSaleMapper.listSecondTeam(start, count, uid, state);
   }
}