yujian
2019-02-28 70c9043f312a52a922cb15490d90a172b3515060
fanli/src/main/java/com/yeshi/fanli/service/impl/hongbao/ThreeSaleSerivceImpl.java
@@ -10,9 +10,6 @@
import javax.annotation.Resource;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
@@ -32,9 +29,15 @@
import com.yeshi.fanli.service.inter.config.InviteGetMoneyService;
import com.yeshi.fanli.service.inter.hongbao.HongBaoManageService;
import com.yeshi.fanli.service.inter.hongbao.ThreeSaleSerivce;
import com.yeshi.fanli.service.inter.msg.MsgInviteDetailService;
import com.yeshi.fanli.service.inter.msg.UserInviteMsgNotificationService;
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 net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Service
public class ThreeSaleSerivceImpl implements ThreeSaleSerivce {
@@ -56,6 +59,12 @@
   @Resource
   private ThreeSaleMapper threeSaleMapper;
   @Resource
   private UserInviteMsgNotificationService userInviteMsgNotificationService;
   @Resource
   private MsgInviteDetailService msgInviteDetailService;
   public UserInfo getBoss(final long uid) {
      return (UserInfo) dao.excute(new HibernateCallback<UserInfo>() {
@@ -108,6 +117,20 @@
         threeSale.setUpdateTime(java.lang.System.currentTimeMillis());
         threeSale.setWorker(inviteeUser);
         threeSaleMapper.insertSelective(threeSale);
         List<ThreeSale> listExpire = threeSaleMapper.getExpireRecord(inviter.getId(), inviteeUser.getId());
         if (listExpire != null && listExpire.size() > 0) {
            // 删除失效
            for (ThreeSale saleExpire : listExpire) {
               msgInviteDetailService.deleteByThreeSale(saleExpire);
            }
            // 删除已失效关系
            threeSaleMapper.deleteExpireRecord(inviter.getId(), inviteeUser.getId());
         }
         // 创建通知
         userInviteMsgNotificationService.inviteScan(inviter.getId(), threeSale);
      } else {// 是已经存在的用户
         // 用户已经存在我们的用户库中
@@ -134,6 +157,20 @@
                  threeSale.setWorker(invitee);
                  threeSaleMapper.insertSelective(threeSale);
                  // 删除已失效关系
                  threeSaleMapper.deleteExpireRecord(inviter.getId(), invitee.getId());
                  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());
                  }
                  userInviteMsgNotificationService.inviteScan(inviter.getId(), threeSale);
                  // threeSaleSerivce.bind(invitee, inviter);
               } else {
@@ -156,9 +193,14 @@
         ThreeSale threeSale = list.get(0);
         UserInfo inviter = threeSale.getBoss();
         threeSale.setState(true);
         threeSale.setSucceedTime(System.currentTimeMillis());
         dao.update(threeSale);
         if (inviter != null)
            reComputeUserRank(inviter.getId());
         //添加到队列
         ThreeSaleCMQManager.getInstance().addThreeSaleMsg(threeSale);
         // 通知
         userInviteMsgNotificationService.inviteSuccess(inviter.getId(), threeSale);
         // }
         // });
      }
@@ -516,10 +558,10 @@
            if (formatYears.format(todayTime).equals(formatYears.format(lastLoginTime))) {
               // 本年
               contentJson.put("content", "最近登陆 " + sdf.format(lastLoginTime));
               contentJson.put("content", "最近登录 " + sdf.format(lastLoginTime));
            } else {
               // 往年
               contentJson.put("content", "最近登陆 " + format.format(lastLoginTime));
               contentJson.put("content", "最近登录 " + format.format(lastLoginTime));
            }
            array.add(contentJson);
@@ -577,4 +619,59 @@
      return threeSaleMapper.listByWorkerId(workerId);
   }
   @Override
   public int getSuccessRelationshipNum(Long uid) {
      return threeSaleMapper.getSuccessRelationshipNum(uid);
   }
   @Override
   @Transactional
   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.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());
      }
      // 邀请消息
      userInviteMsgNotificationService.inviteSuccess(inviter.getId(), threeSale);
   }
   @Override
   public long countSuccessFirstTeam(Long uid) {
      return threeSaleMapper.countFirstTeam(uid,1);
   }
}