| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package com.izouma.nineth.service;
- import com.izouma.nineth.config.Constants;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.dto.PageWrapper;
- import com.izouma.nineth.dto.UserHoldDTO;
- import com.izouma.nineth.exception.BusinessException;
- import lombok.AllArgsConstructor;
- import org.apache.commons.collections.CollectionUtils;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Service;
- import java.util.ArrayList;
- import java.util.List;
- @Service
- @AllArgsConstructor
- public class UserHoldCountService {
- private RedisTemplate<String, Object> redisTemplate;
- public PageWrapper<UserHoldDTO> all(PageQuery pageQuery) {
- if (Boolean.FALSE.equals(redisTemplate.hasKey(Constants.USER_HOLD_CACHE_KEY))) {
- throw new BusinessException("key失效,请刷新!如果已经刷新,请等待!");
- }
- // 当前页
- int page = pageQuery.getPage();
- // pageSize 每页查询多少条数据
- int size = pageQuery.getSize();
- // limit 开始偏移量
- int start = page * size;
- List<UserHoldDTO> userHoldDTOS = (List<UserHoldDTO>) redisTemplate.opsForValue().get(Constants.USER_HOLD_CACHE_KEY);
- int totalElements;
- if (CollectionUtils.isEmpty(userHoldDTOS)) {
- totalElements = 0;
- return new PageWrapper<>(new ArrayList<>(), page,
- size, totalElements);
- }
- totalElements = userHoldDTOS.size();
- List<UserHoldDTO> newUserHoldList;
- if (userHoldDTOS.size() < start + size) {
- newUserHoldList = userHoldDTOS.subList(start, userHoldDTOS.size());
- } else {
- newUserHoldList = userHoldDTOS.subList(start, start + size);
- }
- return new PageWrapper<>(newUserHoldList, page,
- size, totalElements);
- }
- }
|