|
|
|
@ -0,0 +1,228 @@
|
|
|
|
|
package com.luoo.user.service;
|
|
|
|
|
|
|
|
|
|
import com.luoo.user.dao.TaskPointDao;
|
|
|
|
|
import com.luoo.user.dao.UserInfoDao;
|
|
|
|
|
import com.luoo.user.dao.UserPointLogDao;
|
|
|
|
|
import com.luoo.user.dto.point.InvitationUserDto;
|
|
|
|
|
import com.luoo.user.pojo.TaskPoint;
|
|
|
|
|
import com.luoo.user.pojo.UserInfo;
|
|
|
|
|
import com.luoo.user.pojo.UserPointLog;
|
|
|
|
|
import constants.TaskPointIdConstants;
|
|
|
|
|
import enums.PointEnums;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
import util.IdWorker;
|
|
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @program: luoo_parent
|
|
|
|
|
*
|
|
|
|
|
* @description: 新用户邀请业务逻辑
|
|
|
|
|
*
|
|
|
|
|
* @author: yawei.huang
|
|
|
|
|
*
|
|
|
|
|
* @create: 2024-11-27 10:28
|
|
|
|
|
**/
|
|
|
|
|
@Service
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class InvitationService {
|
|
|
|
|
|
|
|
|
|
private final UserInfoDao userInfoDao;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private IdWorker idWorker;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private TaskPointDao taskPointDao;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserPointLogDao userPointLogDao;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserCollectInfoService userCollectInfoService;
|
|
|
|
|
|
|
|
|
|
public InvitationService(UserInfoDao userInfoDao) {
|
|
|
|
|
this.userInfoDao = userInfoDao;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void doInvitationUser(InvitationUserDto invitationUserDto) {
|
|
|
|
|
String myUserId = invitationUserDto.getMyUserId();
|
|
|
|
|
String invitationUserId = invitationUserDto.getInvitationUserId();
|
|
|
|
|
// 我
|
|
|
|
|
UserInfo myUserInfo = userInfoDao.findById(myUserId).get();
|
|
|
|
|
// 邀请人
|
|
|
|
|
UserInfo invitationUserInfo = userInfoDao.findById(invitationUserId).get();
|
|
|
|
|
|
|
|
|
|
// 本次我要修改的积分
|
|
|
|
|
Integer myScore = 0;
|
|
|
|
|
// 邀请人要修改的积分
|
|
|
|
|
Integer invitationUserScore = 0;
|
|
|
|
|
|
|
|
|
|
// 设置我的邀请人userId
|
|
|
|
|
myUserInfo.setInvitationUserId(invitationUserId);
|
|
|
|
|
|
|
|
|
|
// 新用户邀请事件
|
|
|
|
|
String newUserInviteId = TaskPointIdConstants.NEW_USER_INVITE;
|
|
|
|
|
TaskPoint taskPoint = taskPointDao.findById(newUserInviteId).get();
|
|
|
|
|
|
|
|
|
|
// 我 和 邀请人 同时新增log
|
|
|
|
|
UserPointLog myNewUserInviteLog = UserPointLog.builder()
|
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
|
.type(PointEnums.TASK_POINT_TYPE_ADD.getCode())
|
|
|
|
|
.createUser(myUserId)
|
|
|
|
|
.description(taskPoint.getDescription())
|
|
|
|
|
.userId(myUserId)
|
|
|
|
|
.score(taskPoint.getScore())
|
|
|
|
|
.taskPointId(taskPoint.getId())
|
|
|
|
|
.build();
|
|
|
|
|
userPointLogDao.save(myNewUserInviteLog);
|
|
|
|
|
myScore = myScore + taskPoint.getScore();
|
|
|
|
|
|
|
|
|
|
UserPointLog invitationUserNewUserInviteLog = UserPointLog.builder()
|
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
|
.type(PointEnums.TASK_POINT_TYPE_ADD.getCode())
|
|
|
|
|
.createUser(invitationUserId)
|
|
|
|
|
.description(taskPoint.getDescription())
|
|
|
|
|
.userId(invitationUserId)
|
|
|
|
|
.score(taskPoint.getScore())
|
|
|
|
|
.taskPointId(taskPoint.getId())
|
|
|
|
|
.build();
|
|
|
|
|
userPointLogDao.save(invitationUserNewUserInviteLog);
|
|
|
|
|
invitationUserScore = invitationUserScore + taskPoint.getScore();
|
|
|
|
|
|
|
|
|
|
// 互相关注
|
|
|
|
|
userCollectInfoService.saveCollect(myUserId, invitationUserId, 2);
|
|
|
|
|
userCollectInfoService.saveCollect(invitationUserId, myUserId, 2);
|
|
|
|
|
|
|
|
|
|
// 关注事件
|
|
|
|
|
String followId = TaskPointIdConstants.SUCCESS_FOLLOW_ONE_USER;
|
|
|
|
|
TaskPoint followTaskPoint = taskPointDao.findById(followId).get();
|
|
|
|
|
if (Objects.equals(PointEnums.TASK_TYPE_NEW.getCode(), followTaskPoint.getType())) {
|
|
|
|
|
// 只能领取一次
|
|
|
|
|
myScore += checkFollowForNewUser(myUserId, followId, followTaskPoint);
|
|
|
|
|
invitationUserScore += checkFollowForNewUser(invitationUserId, followId, followTaskPoint);
|
|
|
|
|
|
|
|
|
|
} else if (Objects.equals(PointEnums.TASK_TYPE_DAILY.getCode(), followTaskPoint.getType())) {
|
|
|
|
|
// 今天只能领取一次
|
|
|
|
|
myScore += checkFollowForDaily(myUserId, followId, followTaskPoint);
|
|
|
|
|
invitationUserScore += checkFollowForDaily(invitationUserId, followId, followTaskPoint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 连续邀请
|
|
|
|
|
myScore += mulInvite(myUserId);
|
|
|
|
|
invitationUserScore += mulInvite(invitationUserId);
|
|
|
|
|
|
|
|
|
|
myUserInfo.setPoint((myUserInfo.getPoint() == null ? 0 : myUserInfo.getPoint()) + myScore);
|
|
|
|
|
invitationUserInfo.setPoint((invitationUserInfo.getPoint() == null ? 0 : invitationUserInfo.getPoint()) + invitationUserScore);
|
|
|
|
|
userInfoDao.save(myUserInfo);
|
|
|
|
|
userInfoDao.save(invitationUserInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 多次邀请
|
|
|
|
|
private Integer mulInvite(String userId) {
|
|
|
|
|
Integer score = 0;
|
|
|
|
|
List<UserPointLog> list = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
|
|
|
|
|
userId, TaskPointIdConstants.NEW_USER_INVITE);
|
|
|
|
|
if (list.size() >= 3 - 1) {
|
|
|
|
|
String inviteUserId3 = TaskPointIdConstants.INVITE_USER_3;
|
|
|
|
|
TaskPoint inviteUser3TaskPoint = taskPointDao.findById(inviteUserId3).get();
|
|
|
|
|
// 邀请人数 >= 3人,并且没有获得对应的积分
|
|
|
|
|
List<UserPointLog> userPointLogsByUserIdAndTaskPointId3 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
|
|
|
|
|
TaskPointIdConstants.INVITE_USER_3, userId);
|
|
|
|
|
score += doMulInvitation(userId, inviteUser3TaskPoint, userPointLogsByUserIdAndTaskPointId3);
|
|
|
|
|
}
|
|
|
|
|
if (list.size() >= 6 - 1) {
|
|
|
|
|
String inviteUserId6 = TaskPointIdConstants.INVITE_USER_6;
|
|
|
|
|
TaskPoint inviteUser6TaskPoint = taskPointDao.findById(inviteUserId6).get();
|
|
|
|
|
// 邀请人数 >= 6人,并且没有获得对应的积分
|
|
|
|
|
List<UserPointLog> userPointLogsByUserIdAndTaskPointId6 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
|
|
|
|
|
TaskPointIdConstants.INVITE_USER_6, userId);
|
|
|
|
|
score += doMulInvitation(userId, inviteUser6TaskPoint, userPointLogsByUserIdAndTaskPointId6);
|
|
|
|
|
}
|
|
|
|
|
if (list.size() >= 10 - 1) {
|
|
|
|
|
String inviteUserId10 = TaskPointIdConstants.INVITE_USER_10;
|
|
|
|
|
TaskPoint inviteUser10TaskPoint = taskPointDao.findById(inviteUserId10).get();
|
|
|
|
|
// 邀请人数 >= 10人,并且没有获得对应的积分
|
|
|
|
|
List<UserPointLog> userPointLogsByUserIdAndTaskPointId10 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
|
|
|
|
|
TaskPointIdConstants.INVITE_USER_10, userId);
|
|
|
|
|
score += doMulInvitation(userId, inviteUser10TaskPoint, userPointLogsByUserIdAndTaskPointId10);
|
|
|
|
|
}
|
|
|
|
|
if (list.size() >= 20 - 1) {
|
|
|
|
|
String inviteUserId20 = TaskPointIdConstants.INVITE_USER_20;
|
|
|
|
|
TaskPoint inviteUser20TaskPoint = taskPointDao.findById(inviteUserId20).get();
|
|
|
|
|
// 邀请人数 >= 20人,并且没有获得对应的积分
|
|
|
|
|
List<UserPointLog> userPointLogsByUserIdAndTaskPointId20 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
|
|
|
|
|
TaskPointIdConstants.INVITE_USER_20, userId);
|
|
|
|
|
score += doMulInvitation(userId, inviteUser20TaskPoint, userPointLogsByUserIdAndTaskPointId20);
|
|
|
|
|
}
|
|
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 多次邀请构建实体类并计算积分
|
|
|
|
|
private Integer doMulInvitation(String userId, TaskPoint invitationPoint, List<UserPointLog> userPointLogsByUserIdAndTaskPointId) {
|
|
|
|
|
int score = 0;
|
|
|
|
|
if (userPointLogsByUserIdAndTaskPointId.isEmpty()) {
|
|
|
|
|
UserPointLog inviteUserLog = UserPointLog.builder()
|
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
|
.type(PointEnums.TASK_POINT_TYPE_ADD.getCode())
|
|
|
|
|
.createUser(userId)
|
|
|
|
|
.description(invitationPoint.getDescription())
|
|
|
|
|
.userId(userId)
|
|
|
|
|
.score(invitationPoint.getScore())
|
|
|
|
|
.taskPointId(invitationPoint.getId())
|
|
|
|
|
.build();
|
|
|
|
|
userPointLogDao.save(inviteUserLog);
|
|
|
|
|
|
|
|
|
|
score = score + invitationPoint.getScore();
|
|
|
|
|
}
|
|
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果是每日任务
|
|
|
|
|
private Integer checkFollowForDaily(String invitationUserId, String followId, TaskPoint followTaskPoint) {
|
|
|
|
|
int score = 0;
|
|
|
|
|
UserPointLog invitationUserFollowLog = userPointLogDao.findTodayByUserIdAndTaskPointId(invitationUserId, followId);
|
|
|
|
|
if (invitationUserFollowLog == null) {
|
|
|
|
|
UserPointLog userPointLog = UserPointLog.builder()
|
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
|
.type(PointEnums.TASK_POINT_TYPE_ADD.getCode())
|
|
|
|
|
.createUser(invitationUserId)
|
|
|
|
|
.description(followTaskPoint.getDescription())
|
|
|
|
|
.userId(invitationUserId)
|
|
|
|
|
.score(followTaskPoint.getScore())
|
|
|
|
|
.taskPointId(followTaskPoint.getId())
|
|
|
|
|
.build();
|
|
|
|
|
userPointLogDao.save(userPointLog);
|
|
|
|
|
|
|
|
|
|
score += followTaskPoint.getScore();
|
|
|
|
|
}
|
|
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果是新用户任务
|
|
|
|
|
private Integer checkFollowForNewUser(String myUserId, String followId, TaskPoint followTaskPoint) {
|
|
|
|
|
int score = 0;
|
|
|
|
|
List<UserPointLog> myFollowLog = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(myUserId, followId);
|
|
|
|
|
if (myFollowLog.isEmpty()) {
|
|
|
|
|
UserPointLog userPointLog = UserPointLog.builder()
|
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
|
.type(PointEnums.TASK_POINT_TYPE_ADD.getCode())
|
|
|
|
|
.createUser(myUserId)
|
|
|
|
|
.description(followTaskPoint.getDescription())
|
|
|
|
|
.userId(myUserId)
|
|
|
|
|
.score(followTaskPoint.getScore())
|
|
|
|
|
.taskPointId(followTaskPoint.getId())
|
|
|
|
|
.build();
|
|
|
|
|
userPointLogDao.save(userPointLog);
|
|
|
|
|
|
|
|
|
|
score += followTaskPoint.getScore();
|
|
|
|
|
}
|
|
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
}
|