| | |
| | |
|
| | | 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;
|
| | |
| | | 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 {
|
| | |
| | |
|
| | | @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>() {
|
| | |
| | | }
|
| | |
|
| | | @Transactional
|
| | | public void bind(UserInfo invitee, UserInfo inviter) throws ThreeSaleException {
|
| | | if (invitee == null || inviter == null)
|
| | | public void bind(UserInfo worker, UserInfo inviter) throws ThreeSaleException {
|
| | | if (worker == null || inviter == null)
|
| | | throw new ThreeSaleException(1, "用户信息为空");
|
| | | if (invitee.getId() == null)// 该用户还不存在
|
| | |
|
| | | if (worker.getId() == null)// 该用户还不存在
|
| | | {
|
| | | // 保存用户
|
| | | UserInfo inviteeUser = userInfoService.addUser(invitee, Constant.APPID);
|
| | | UserInfo inviteeUser = userInfoService.addUser(worker, Constant.APPID);
|
| | | if (inviteeUser == null)
|
| | | throw new ThreeSaleException(2, "创建用户失败");
|
| | | // 保存关系
|
| | |
| | | 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 {// 是已经存在的用户
|
| | | if (worker.getId().longValue() == inviter.getId())
|
| | | throw new ThreeSaleException(4, "不能绑定自己");
|
| | |
|
| | | worker = userInfoService.selectByPKey(worker.getId());
|
| | |
|
| | | // 用户已经存在我们的用户库中
|
| | | if (invitee.getLastLoginTime() == null || invitee.getLastLoginTime() == 0L) {
|
| | | if (worker.getLastLoginTime() == null || worker.getLastLoginTime() == 0L) {
|
| | | // 用户未登录
|
| | | int outOfDateCount = 0;// 过期数量
|
| | | // 查询邀请关系
|
| | | List<ThreeSale> list = listByWorkerId(invitee.getId());
|
| | | List<ThreeSale> list = listByWorkerId(worker.getId());
|
| | | if (list != null && list.size() > 0) {
|
| | | for (ThreeSale ts : list) {
|
| | | if (!ts.getState() && ts.getExpire() == ThreeSale.EXPIRE_OUTOFDATE) {
|
| | |
| | | threeSale.setExpire(ThreeSale.EXPIRE_NORMAL);
|
| | | threeSale.setState(false);
|
| | | threeSale.setUpdateTime(java.lang.System.currentTimeMillis());
|
| | | threeSale.setWorker(invitee);
|
| | | threeSale.setWorker(worker);
|
| | | threeSaleMapper.insertSelective(threeSale);
|
| | |
|
| | | // 删除已失效关系
|
| | | threeSaleMapper.deleteExpireRecord(inviter.getId(), worker.getId());
|
| | |
|
| | | List<ThreeSale> listExpire = threeSaleMapper.getExpireRecord(inviter.getId(), worker.getId());
|
| | | if (listExpire != null && listExpire.size() > 0) {
|
| | | // 删除历史通知
|
| | | for (ThreeSale saleExpire : listExpire) {
|
| | | msgInviteDetailService.deleteByThreeSale(saleExpire);
|
| | | }
|
| | | // 删除已失效关系
|
| | | threeSaleMapper.deleteExpireRecord(inviter.getId(), worker.getId());
|
| | | }
|
| | |
|
| | | userInviteMsgNotificationService.inviteScan(inviter.getId(), threeSale);
|
| | | // threeSaleSerivce.bind(invitee, inviter);
|
| | |
|
| | | } else {
|
| | |
| | | // 用户未登录,又不存在邀请关系,不做处理
|
| | | }
|
| | | } else {
|
| | | // 已经登录,不做处理
|
| | | // 已经登录,而且没有上下级关系或者上下级关系未生效,则绑定关系
|
| | | List<ThreeSale> list = threeSaleMapper.listByWorkerId(worker.getId());
|
| | | // 删除无效的
|
| | | 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.remove(i);
|
| | | i--;
|
| | | }
|
| | | }
|
| | | // 查询上级是否有和现在上级一样的
|
| | | if (list == null || list.size() == 0)// 尚未确立关系,建立关系
|
| | | {
|
| | | ThreeSale ts = new ThreeSale();
|
| | | ts.setBoss(inviter);
|
| | | ts.setCreateTime(System.currentTimeMillis());
|
| | | ts.setExpire(ThreeSale.EXPIRE_NORMAL);
|
| | | ts.setState(true);
|
| | | 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()) {// 未生效的上级和现在上级一样就生效
|
| | | ThreeSale update = new ThreeSale(list.get(0).getId());
|
| | | update.setState(true);
|
| | | update.setSucceedTime(System.currentTimeMillis());
|
| | | threeSaleMapper.updateByPrimaryKeySelective(update);
|
| | | ThreeSaleCMQManager.getInstance().addThreeSaleMsg(list.get(0));// 添加通知
|
| | | userInviteMsgNotificationService.inviteSuccess(inviter.getId(), list.get(0));
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | @Transactional
|
| | | public void effective(UserInfo invitee) {
|
| | | List<ThreeSale> list = dao.list("from ThreeSale ts where ts.worker.id = ? and ts.state=0 and ts.expire=0",
|
| | | new Serializable[] { invitee.getId() });
|
| | | public void effective(UserInfo worker) {
|
| | | List<ThreeSale> list = threeSaleMapper.listByWorkerId(worker.getId());
|
| | | 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_NORMAL) {
|
| | | ;
|
| | | } else {
|
| | | list.remove(i);
|
| | | i--;
|
| | | }
|
| | | }
|
| | | if (list.size() > 0) {
|
| | | 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());
|
| | | // }
|
| | | // });
|
| | | ThreeSale oldSale = list.get(0);
|
| | |
|
| | | ThreeSale update = new ThreeSale(list.get(0).getId());
|
| | | update.setState(true);
|
| | | update.setSucceedTime(System.currentTimeMillis());
|
| | | threeSaleMapper.updateByPrimaryKeySelective(update);
|
| | |
|
| | | 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);
|
| | | }
|
| | | }
|
| | |
|
| | |
| | | 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());
|
| | | }
|
| | |
|
| | | // 添加到队列
|
| | | ThreeSaleCMQManager.getInstance().addThreeSaleMsg(threeSale);
|
| | |
|
| | | // 邀请消息
|
| | | userInviteMsgNotificationService.inviteSuccess(inviter.getId(), threeSale);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public long countSuccessFirstTeam(Long uid) {
|
| | | return threeSaleMapper.countFirstTeam(uid, 1);
|
| | | }
|
| | |
|
| | | }
|