diff --git a/luoo_user/src/main/java/com/luoo/user/service/TaskPointService.java b/luoo_user/src/main/java/com/luoo/user/service/TaskPointService.java index 21af1ab..9d9d42b 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/TaskPointService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/TaskPointService.java @@ -9,6 +9,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import util.IdWorker; @@ -24,16 +25,23 @@ import util.JwtUtil; @Slf4j public class TaskPointService { + // 缓存key + public static final String TASK_POINT = "TASK-POINT-"; + private final TaskPointDao taskPointDao; private final IdWorker idWorker; private final JwtUtil jwtUtil; - public TaskPointService(TaskPointDao taskPointDao, IdWorker idWorker, JwtUtil jwtUtil) { + private final RedisTemplate redisTemplate; + + public TaskPointService(TaskPointDao taskPointDao, IdWorker idWorker, JwtUtil jwtUtil, + RedisTemplate redisTemplate) { this.taskPointDao = taskPointDao; this.idWorker = idWorker; this.jwtUtil = jwtUtil; + this.redisTemplate = redisTemplate; } @@ -52,6 +60,8 @@ public class TaskPointService { taskPointDao.save(taskPoint); + redisTemplate.opsForValue().set(TASK_POINT + taskPoint.getId(), taskPoint); + } /** @@ -66,6 +76,9 @@ public class TaskPointService { taskPoint.setUpdateUser(userLoginDto.getUserId()); taskPointDao.save(taskPoint); + + redisTemplate.delete(TASK_POINT + taskPoint.getId()); + redisTemplate.opsForValue().set(TASK_POINT + taskPoint.getId(), taskPoint); } /** @@ -96,6 +109,9 @@ public class TaskPointService { taskPoint.setUpdateUser(userLoginDto.getUserId()); taskPointDao.save(taskPoint); + + redisTemplate.delete(TASK_POINT + taskPoint.getId()); + redisTemplate.opsForValue().set(TASK_POINT + taskPoint.getId(), taskPoint); } /** @@ -121,4 +137,15 @@ public class TaskPointService { long totalElements = taskPointPage.getTotalElements(); return new PageResult<>(totalElements, taskPointPage.getContent()); } + + /** + * 单个查询 + */ + public TaskPoint getTaskPoint(String id) { + TaskPoint taskPoint = (TaskPoint) redisTemplate.opsForValue().get(TASK_POINT + id); + if(taskPoint == null) { + taskPoint = taskPointDao.findById(id).get(); + } + return taskPoint; + } } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java b/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java index b6567bf..0934be7 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserPointLogService.java @@ -7,7 +7,6 @@ import com.luoo.user.dto.point.UserPointLogSearchDto; import com.luoo.user.pojo.TaskPoint; import com.luoo.user.pojo.UserInfo; import com.luoo.user.pojo.UserPointLog; -import com.mysql.fabric.xmlrpc.base.Param; import dto.UserLoginDto; import enums.PointEnums; import java.util.ArrayList; @@ -18,16 +17,14 @@ import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; import org.springframework.amqp.rabbit.core.RabbitTemplate; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.jpa.domain.Specification; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import util.IdWorker; @@ -55,16 +52,23 @@ public class UserPointLogService { private final UserInfoService userInfoService; + private final RedisTemplate redisTemplate; + + private final TaskPointService taskPointService; + public UserPointLogService(UserPointLogDao userPointLogDao, IdWorker idWorker, JwtUtil jwtUtil, TaskPointDao taskPointDao, RabbitTemplate rabbitTemplate, - UserInfoService userInfoService) { + UserInfoService userInfoService, RedisTemplate redisTemplate, + TaskPointService taskPointService) { this.userPointLogDao = userPointLogDao; this.idWorker = idWorker; this.jwtUtil = jwtUtil; this.taskPointDao = taskPointDao; this.rabbitTemplate = rabbitTemplate; this.userInfoService = userInfoService; + this.redisTemplate = redisTemplate; + this.taskPointService = taskPointService; } /** @@ -77,10 +81,10 @@ public class UserPointLogService { UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); // 新手任务,只触发一次 - if (!userPointLogDao.findUserPointLogByUserIdAndTaskPointId(userLoginDto.getUserId(), - taskPointId).isEmpty()) { - return; - } +// if (!userPointLogDao.findUserPointLogByUserIdAndTaskPointId(userLoginDto.getUserId(), +// taskPointId).isEmpty()) { +// return; +// } addByTask(taskPointId, userLoginDto); } @@ -102,13 +106,10 @@ public class UserPointLogService { } private void addByTask(String taskPointId, UserLoginDto userLoginDto) { - TaskPoint taskPoint = taskPointDao.findById(taskPointId).get(); UserPointLog userPointLog = UserPointLog.builder() .id(String.valueOf(idWorker.nextId())) - .score(taskPoint.getScore()) .type(PointEnums.TASK_POINT_TYPE_ADD.getCode()) - .description(taskPoint.getDescription()) .createUser(userLoginDto.getUserId()) .taskPointId(taskPointId) .userId(userLoginDto.getUserId()) @@ -129,7 +130,7 @@ public class UserPointLogService { // 根据积分任务ID获取积分任务详情 // 保存积分记录 String taskPointId = userPointLog.getTaskPointId(); - TaskPoint taskPoint = taskPointDao.findById(taskPointId).get(); + TaskPoint taskPoint = taskPointService.getTaskPoint(taskPointId); // 获取积分任务类型 Integer type = taskPoint.getType(); @@ -143,6 +144,9 @@ public class UserPointLogService { } } + userPointLog.setScore(taskPoint.getScore()); + userPointLog.setDescription(taskPoint.getDescription()); + // 保存用户积分记录 userPointLogDao.save(userPointLog);