UserHoldCountService.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.config.Constants;
  3. import com.izouma.nineth.dto.PageQuery;
  4. import com.izouma.nineth.dto.PageWrapper;
  5. import com.izouma.nineth.dto.UserHoldDTO;
  6. import com.izouma.nineth.exception.BusinessException;
  7. import lombok.AllArgsConstructor;
  8. import org.apache.commons.collections.CollectionUtils;
  9. import org.springframework.data.redis.core.RedisTemplate;
  10. import org.springframework.stereotype.Service;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. @Service
  14. @AllArgsConstructor
  15. public class UserHoldCountService {
  16. private RedisTemplate<String, Object> redisTemplate;
  17. public PageWrapper<UserHoldDTO> all(PageQuery pageQuery) {
  18. if (Boolean.FALSE.equals(redisTemplate.hasKey(Constants.USER_HOLD_CACHE_KEY))) {
  19. throw new BusinessException("key失效,请刷新!如果已经刷新,请等待!");
  20. }
  21. // 当前页
  22. int page = pageQuery.getPage();
  23. // pageSize 每页查询多少条数据
  24. int size = pageQuery.getSize();
  25. // limit 开始偏移量
  26. int start = page * size;
  27. List<UserHoldDTO> userHoldDTOS = (List<UserHoldDTO>) redisTemplate.opsForValue().get(Constants.USER_HOLD_CACHE_KEY);
  28. int totalElements;
  29. if (CollectionUtils.isEmpty(userHoldDTOS)) {
  30. totalElements = 0;
  31. return new PageWrapper<>(new ArrayList<>(), page,
  32. size, totalElements);
  33. }
  34. totalElements = userHoldDTOS.size();
  35. List<UserHoldDTO> newUserHoldList;
  36. if (userHoldDTOS.size() < start + size) {
  37. newUserHoldList = userHoldDTOS.subList(start, userHoldDTOS.size());
  38. } else {
  39. newUserHoldList = userHoldDTOS.subList(start, start + size);
  40. }
  41. return new PageWrapper<>(newUserHoldList, page,
  42. size, totalElements);
  43. }
  44. }