release- 积分日志单线程运行

release-2024-04-25
pikaqiudeshujia 2 months ago
parent a96b71bd06
commit ccab7280b3

@ -0,0 +1,35 @@
package com.luoo.user.dto.point;/**
* @Author: yawei.huang
* @Package: com.luoo.user.dto.point
* @Project: luoo_parent
* @Date: 2024/11/27 9:50
* @Filename: InvitationUserDto
* @Describe:
*/
import lombok.Builder;
import lombok.Data;
/**
* @program: luoo_parent
*
* @description: mqdto
*
* @author: yawei.huang
*
* @create: 2024-11-27 09:50
**/
@Data
@Builder
public class InvitationUserDto {
/**
* id
*/
private String myUserId;
/**
* id
*/
private String invitationUserId;
}

@ -1,10 +1,13 @@
package com.luoo.user.listener;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.luoo.user.dto.point.InvitationUserDto;
import com.luoo.user.pojo.UserPointLog;
import com.luoo.user.service.UserPointLogService;
import com.luoo.user.service.InvitationService;
import com.luoo.user.service.SignService;
import com.luoo.user.util.DistributedLock;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
@ -25,7 +28,10 @@ import java.io.IOException;
public class PointLogListener {
@Autowired
private UserPointLogService userPointLogService;
private SignService signService;
@Autowired
private InvitationService invitationService;
@Autowired
private DistributedLock distributedLock;
@ -41,16 +47,24 @@ public class PointLogListener {
try {
UserPointLog userPointLog = objectMapper.readValue(json, UserPointLog.class);
String userId = userPointLog.getUserId();
// 对userId进行redis分布式锁
// springboot 写一段setnx的分布式锁
String key = "pointLog:" + userId;
String key = "pointLog:";
distributedLock.unlock(key);
if (distributedLock.tryLock(key, "1")) {
try {
// 执行需要加锁的业务逻辑
userPointLogService.add(userPointLog);
if (StringUtils.isBlank(userPointLog.getMyInvitationUserId())) {
signService.add(userPointLog);
} else {
// 新用户邀请
InvitationUserDto invitationUserDto = InvitationUserDto.builder()
.myUserId(userPointLog.getUserId())
.invitationUserId(userPointLog.getMyInvitationUserId())
.build();
invitationService.doInvitationUser(invitationUserDto);
}
} finally {
// 释放锁
distributedLock.unlock(key);

@ -2,7 +2,6 @@ package com.luoo.user.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import java.time.LocalDateTime;
import lombok.*;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
@ -14,6 +13,7 @@ import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@ -80,5 +80,9 @@ public class UserPointLog implements Serializable {
@ApiModelProperty(value = "更新人")
private String updateUser;
@Transient
@ApiModelProperty(value = "我的邀请人id")
private String myInvitationUserId;
}

@ -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;
}
}

@ -0,0 +1,170 @@
package com.luoo.user.service;
import cn.hutool.core.util.ObjectUtil;
import com.luoo.user.dao.UserInfoDao;
import com.luoo.user.dao.UserPointLogDao;
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.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:45
**/
@Service
@Slf4j
public class SignService {
private final TaskPointService taskPointService;
private final UserPointLogDao userPointLogDao;
private final UserInfoDao userInfoDao;
private final IdWorker idWorker;
public SignService(TaskPointService taskPointService, UserPointLogDao userPointLogDao, UserInfoDao userInfoDao, IdWorker idWorker) {
this.taskPointService = taskPointService;
this.userPointLogDao = userPointLogDao;
this.userInfoDao = userInfoDao;
this.idWorker = idWorker;
}
/**
*
* <p>
* @Transactional
*
* @param userPointLog IDID
*/
@Transactional(rollbackFor = Exception.class)
public void add(UserPointLog userPointLog) {
// 根据积分任务ID获取积分任务详情
// 保存积分记录
String taskPointId = userPointLog.getTaskPointId();
if (taskPointId != null) {
TaskPoint taskPoint = taskPointService.getTaskPoint(taskPointId);
// 如果不存在于数据库,就是后台控制的积分,如取消抽奖的返还的积分
// 积分
Integer score = taskPoint == null ? userPointLog.getScore() : taskPoint.getScore();
// 获取积分任务类型
Integer type = taskPoint == null ? userPointLog.getType() : taskPoint.getType();
// 描述
String description =
taskPoint == null ? userPointLog.getDescription() : taskPoint.getDescription();
// 更新用户积分,获取用户信息并累加积分得分
// 对用户进行积分计算
UserInfo userInfo = userInfoDao.findById(userPointLog.getUserId()).get();
Integer point = 0;
// 对于新手任务 || 特殊任务,只允许完成一次,如果用户已经完成过,则直接返回,不重复添加积分
if (Objects.equals(type, PointEnums.TASK_TYPE_NEW.getCode()) || Objects.equals(type, PointEnums.TASK_TYPE_SPECIAL.getCode())) {
// 新手任务 || 特殊任务,只触发一次
if (!userPointLogDao.findUserPointLogByUserIdAndTaskPointId(
userPointLog.getUserId(),
userPointLog.getTaskPointId()).isEmpty()) {
return;
}
}
// 对于日常任务,每天只允许完成一次,如果用户已经完成过,则直接返回,不重复添加积分
if (Objects.equals(type, PointEnums.TASK_TYPE_DAILY.getCode())) {
if (!Objects.equals(taskPointId, TaskPointIdConstants.NEW_USER_INVITE)) {
// 日常任务,每天只触发一次(新用户邀请除外)
if (userPointLogDao.findTodayByUserIdAndTaskPointId(userPointLog.getUserId(),
userPointLog.getTaskPointId()) != null) {
return;
} else if (Objects.equals(taskPointId, TaskPointIdConstants.DAILY_SIGN)) {
// 连续3天签到
List<UserPointLog> daysByUserIdAndTaskPointId3 = userPointLogDao.findDaysByUserIdAndTaskPointId(
userPointLog.getUserId(), TaskPointIdConstants.DAILY_SIGN, 3 - 1);
if (daysByUserIdAndTaskPointId3.size() == 3 - 1) {
List<UserPointLog> sign3 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
TaskPointIdConstants.DAILY_SIGN_3, userPointLog.getUserId());
if (sign3.isEmpty()) {
point += executeMulSign(TaskPointIdConstants.DAILY_SIGN_3, userPointLog.getUserId());
}
}
// 连续7天签到
List<UserPointLog> daysByUserIdAndTaskPointId7 = userPointLogDao.findDaysByUserIdAndTaskPointId(
userPointLog.getUserId(), TaskPointIdConstants.DAILY_SIGN, 7 - 1);
if (daysByUserIdAndTaskPointId7.size() == 7 - 1) {
List<UserPointLog> sign7 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
TaskPointIdConstants.DAILY_SIGN_7, userPointLog.getUserId());
if (sign7.isEmpty()) {
point += executeMulSign(TaskPointIdConstants.DAILY_SIGN_7, userPointLog.getUserId());
}
}
// 连续30天签到
List<UserPointLog> daysByUserIdAndTaskPointId30 = userPointLogDao.findDaysByUserIdAndTaskPointId(
userPointLog.getUserId(), TaskPointIdConstants.DAILY_SIGN, 30 - 1);
if (daysByUserIdAndTaskPointId30.size() == 30 - 1) {
List<UserPointLog> sign30 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
TaskPointIdConstants.DAILY_SIGN_30, userPointLog.getUserId());
if (sign30.isEmpty()) {
point += executeMulSign(TaskPointIdConstants.DAILY_SIGN_30, userPointLog.getUserId());
}
}
}
}
}
userPointLog.setScore(score);
userPointLog.setDescription(description);
// 保存用户积分记录
userPointLogDao.save(userPointLog);
log.info("用户 {} 添加积分记录 {} 成功", userPointLog.getUserId(),
userPointLog.getId());
if (ObjectUtil.equals(PointEnums.TASK_POINT_TYPE_ADD.getCode(), userPointLog.getType())) {
point += userInfo.getPoint() == null ? 0 : userInfo.getPoint();
} else {
point -= userInfo.getPoint() == null ? 0 : userInfo.getPoint();
}
point += userPointLog.getScore();
userInfo.setPoint(point);
// 更新用户积分信息
userInfoDao.save(userInfo);
}
}
private Integer executeMulSign(String taskPointId, String userId) {
int score = 0;
TaskPoint taskPoint = taskPointService.getTaskPoint(taskPointId);
UserPointLog userPointLog = UserPointLog.builder()
.id(String.valueOf(idWorker.nextId()))
.type(PointEnums.TASK_POINT_TYPE_ADD.getCode())
.createUser(userId)
.description(taskPoint.getDescription())
.userId(userId)
.score(taskPoint.getScore())
.taskPointId(taskPoint.getId())
.build();
userPointLogDao.save(userPointLog);
score += taskPoint.getScore();
return score;
}
}

@ -2,7 +2,11 @@ package com.luoo.user.service;
import api.PageResult;
import client.vo.SimpleUser;
import com.luoo.user.dao.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.luoo.user.dao.UserBankDao;
import com.luoo.user.dao.UserInfoDao;
import com.luoo.user.dao.UserRealNameDao;
import com.luoo.user.dao.WithdrawDao;
import com.luoo.user.dto.UserQueryReq;
import com.luoo.user.dto.UserRealNameCheckDto;
import com.luoo.user.dto.UserRealNameFormDto;
@ -35,6 +39,7 @@ import util.JwtUtil;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
@ -284,22 +289,37 @@ public class UserInfoService {
if (userInfo.getId().equals(userInfoDaoByInvitationCode.getId())) {
throw new BizException(ErrorConstants.USER_INVITE_CODE_CANNOT_BE_SELF);
}
// 设置邀请人
userInfo.setInvitationUserId(userInfoDaoByInvitationCode.getId());
this.update(userInfo);
// 邀请人与被邀请人同时获取积分
userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.NEW_USER_INVITE,
userInfoDaoByInvitationCode.getId());
userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.NEW_USER_INVITE,
userInfo.getId());
// 互相关注
userCollectInfoService.saveCollect(userInfo.getId(), userInfoDaoByInvitationCode.getId(), 2);
userCollectInfoService.saveCollect(userInfoDaoByInvitationCode.getId(), userInfo.getId(), 2);
// 首次关注有积分
userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.SUCCESS_FOLLOW_ONE_USER, userInfo.getId());
userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.SUCCESS_FOLLOW_ONE_USER, userInfoDaoByInvitationCode.getId());
// // 设置邀请人
// userInfo.setInvitationUserId(userInfoDaoByInvitationCode.getId());
// this.update(userInfo);
UserPointLog userPointLog = UserPointLog.builder()
.userId(userInfo.getId())
.myInvitationUserId(userInfoDaoByInvitationCode.getId())
.build();
ObjectMapper objectMapper = new ObjectMapper();
try {
String json = objectMapper.writeValueAsString(userPointLog);
System.out.println(json);
rabbitTemplate.convertAndSend("pointLog", json);
} catch (IOException e) {
e.printStackTrace();
}
// // 邀请人与被邀请人同时获取积分
// userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.NEW_USER_INVITE,
// userInfoDaoByInvitationCode.getId());
// userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.NEW_USER_INVITE,
// userInfo.getId());
//
// // 互相关注
// userCollectInfoService.saveCollect(userInfo.getId(), userInfoDaoByInvitationCode.getId(), 2);
// userCollectInfoService.saveCollect(userInfoDaoByInvitationCode.getId(), userInfo.getId(), 2);
//
// // 首次关注有积分
// userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.SUCCESS_FOLLOW_ONE_USER, userInfo.getId());
// userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.SUCCESS_FOLLOW_ONE_USER, userInfoDaoByInvitationCode.getId());
} else {
throw new BizException(ErrorConstants.INVITATION_CODE_IS_INCORRECT);
}

@ -1,7 +1,6 @@
package com.luoo.user.service;
import api.PageResult;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.luoo.user.dao.LotteryUserDao;
import com.luoo.user.dao.TaskPointDao;
@ -38,7 +37,6 @@ import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* @program: luoo_parent
@ -174,147 +172,6 @@ public class UserPointLogService {
}
}
/**
*
* <p>
* @Transactional
*
* @param userPointLog IDID
*/
@Transactional(rollbackFor = Exception.class)
public void add(UserPointLog userPointLog) {
// 根据积分任务ID获取积分任务详情
// 保存积分记录
String taskPointId = userPointLog.getTaskPointId();
if (taskPointId != null) {
TaskPoint taskPoint = taskPointService.getTaskPoint(taskPointId);
// 如果不存在于数据库,就是后台控制的积分,如取消抽奖的返还的积分
// 积分
Integer score = taskPoint == null ? userPointLog.getScore() : taskPoint.getScore();
// 获取积分任务类型
Integer type = taskPoint == null ? userPointLog.getType() : taskPoint.getType();
// 描述
String description =
taskPoint == null ? userPointLog.getDescription() : taskPoint.getDescription();
// 对于新手任务 || 特殊任务,只允许完成一次,如果用户已经完成过,则直接返回,不重复添加积分
if (Objects.equals(type, PointEnums.TASK_TYPE_NEW.getCode()) || Objects.equals(type, PointEnums.TASK_TYPE_SPECIAL.getCode())) {
// 新手任务 || 特殊任务,只触发一次
if (!userPointLogDao.findUserPointLogByUserIdAndTaskPointId(
userPointLog.getUserId(),
userPointLog.getTaskPointId()).isEmpty()) {
return;
}
}
// 对于日常任务,每天只允许完成一次,如果用户已经完成过,则直接返回,不重复添加积分
if (Objects.equals(type, PointEnums.TASK_TYPE_DAILY.getCode())) {
if (!Objects.equals(taskPointId, TaskPointIdConstants.NEW_USER_INVITE)) {
// 日常任务,每天只触发一次(新用户邀请除外)
if (userPointLogDao.findTodayByUserIdAndTaskPointId(userPointLog.getUserId(),
userPointLog.getTaskPointId()) != null) {
return;
} else if (Objects.equals(taskPointId, TaskPointIdConstants.DAILY_SIGN)) {
// 连续3天签到
List<UserPointLog> daysByUserIdAndTaskPointId3 = userPointLogDao.findDaysByUserIdAndTaskPointId(
userPointLog.getUserId(), TaskPointIdConstants.DAILY_SIGN, 3 - 1);
if (daysByUserIdAndTaskPointId3.size() == 3 - 1) {
List<UserPointLog> sign3 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
TaskPointIdConstants.DAILY_SIGN_3, userPointLog.getUserId());
if (sign3.isEmpty()) {
addByTaskDailyAndUserId(TaskPointIdConstants.DAILY_SIGN_3, userPointLog.getUserId());
}
}
// 连续7天签到
List<UserPointLog> daysByUserIdAndTaskPointId7 = userPointLogDao.findDaysByUserIdAndTaskPointId(
userPointLog.getUserId(), TaskPointIdConstants.DAILY_SIGN, 7 - 1);
if (daysByUserIdAndTaskPointId7.size() == 7 - 1) {
List<UserPointLog> sign7 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
TaskPointIdConstants.DAILY_SIGN_7, userPointLog.getUserId());
if (sign7.isEmpty()) {
addByTaskDailyAndUserId(TaskPointIdConstants.DAILY_SIGN_7, userPointLog.getUserId());
}
}
// 连续30天签到
List<UserPointLog> daysByUserIdAndTaskPointId30 = userPointLogDao.findDaysByUserIdAndTaskPointId(
userPointLog.getUserId(), TaskPointIdConstants.DAILY_SIGN, 30 - 1);
if (daysByUserIdAndTaskPointId30.size() == 30 - 1) {
List<UserPointLog> sign30 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
TaskPointIdConstants.DAILY_SIGN_30, userPointLog.getUserId());
if (sign30.isEmpty()) {
addByTaskDailyAndUserId(TaskPointIdConstants.DAILY_SIGN_30, userPointLog.getUserId());
}
}
}
} else {
List<UserPointLog> list = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
userPointLog.getUserId(), TaskPointIdConstants.NEW_USER_INVITE);
if (list.size() >= 3 - 1) {
// 邀请人数 >= 3人并且没有获得对应的积分
List<UserPointLog> userPointLogsByUserIdAndTaskPointId3 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
TaskPointIdConstants.INVITE_USER_3, userPointLog.getUserId());
if (userPointLogsByUserIdAndTaskPointId3.isEmpty()) {
addByTaskDailyAndUserId(
TaskPointIdConstants.INVITE_USER_3, userPointLog.getUserId());
}
}
if (list.size() >= 6 - 1) {
// 邀请人数 >= 6人并且没有获得对应的积分
List<UserPointLog> userPointLogsByUserIdAndTaskPointId6 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
TaskPointIdConstants.INVITE_USER_6, userPointLog.getUserId());
if (userPointLogsByUserIdAndTaskPointId6.isEmpty()) {
addByTaskDailyAndUserId(
TaskPointIdConstants.INVITE_USER_6, userPointLog.getUserId());
}
}
if (list.size() >= 10 - 1) {
// 邀请人数 >= 10人并且没有获得对应的积分
List<UserPointLog> userPointLogsByUserIdAndTaskPointId10 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
TaskPointIdConstants.INVITE_USER_10, userPointLog.getUserId());
if (userPointLogsByUserIdAndTaskPointId10.isEmpty()) {
addByTaskDailyAndUserId(
TaskPointIdConstants.INVITE_USER_10, userPointLog.getUserId());
}
}
if (list.size() >= 20 - 1) {
// 邀请人数 >= 20人并且没有获得对应的积分
List<UserPointLog> userPointLogsByUserIdAndTaskPointId20 = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(
TaskPointIdConstants.INVITE_USER_20, userPointLog.getUserId());
if (userPointLogsByUserIdAndTaskPointId20.isEmpty()) {
addByTaskDailyAndUserId(
TaskPointIdConstants.INVITE_USER_20, userPointLog.getUserId());
}
}
}
}
userPointLog.setScore(score);
userPointLog.setDescription(description);
// 保存用户积分记录
userPointLogDao.save(userPointLog);
log.info("用户 {} 添加积分记录 {} 成功", userPointLog.getUserId(),
userPointLog.getId());
}
// 更新用户积分,获取用户信息并累加积分得分
// 对用户进行积分计算
UserInfo userInfo = userInfoDao.findById(userPointLog.getUserId()).get();
Integer point = 0;
if (ObjectUtil.equals(PointEnums.TASK_POINT_TYPE_ADD.getCode(), userPointLog.getType())) {
point += userInfo.getPoint() == null ? 0 : userInfo.getPoint();
} else {
point -= userInfo.getPoint() == null ? 0 : userInfo.getPoint();
}
point += userPointLog.getScore();
userInfo.setPoint(point);
// 更新用户积分信息
userInfoDao.save(userInfo);
}
@Transactional(rollbackFor = Exception.class)
public void executeDraw(DrawDTO drawDTO) {

Loading…
Cancel
Save