作品广场优化,将点赞前三和浏览量前三的作品放在第一页的最前面
This commit is contained in:
@@ -7,10 +7,7 @@ import org.springframework.stereotype.Component;
|
|||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -154,6 +151,8 @@ public class RedisUtil {
|
|||||||
return redisTemplate.keys(key);
|
return redisTemplate.keys(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getSize(String key){return redisTemplate.opsForSet().size(key);}
|
||||||
|
|
||||||
public List<String> getMultiValue(Set<String> keys){
|
public List<String> getMultiValue(Set<String> keys){
|
||||||
return redisTemplate.opsForValue().multiGet(keys);
|
return redisTemplate.opsForValue().multiGet(keys);
|
||||||
}
|
}
|
||||||
@@ -216,4 +215,17 @@ public class RedisUtil {
|
|||||||
return redisTemplate.opsForValue().increment(key, 0);
|
return redisTemplate.opsForValue().increment(key, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getViewCount(String key) {
|
||||||
|
Object value = redisTemplate.opsForValue().get(key);
|
||||||
|
if (value instanceof Integer) {
|
||||||
|
return Long.valueOf((Integer) value);
|
||||||
|
} else if (value instanceof Long) {
|
||||||
|
return (Long) value;
|
||||||
|
} else if (value instanceof String) {
|
||||||
|
return Long.valueOf((String) value);
|
||||||
|
} else {
|
||||||
|
throw new IllegalArgumentException("Unexpected value type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import com.ai.da.common.utils.MinioUtil;
|
|||||||
import com.ai.da.common.utils.RedisUtil;
|
import com.ai.da.common.utils.RedisUtil;
|
||||||
import com.ai.da.mapper.primary.*;
|
import com.ai.da.mapper.primary.*;
|
||||||
import com.ai.da.mapper.primary.entity.*;
|
import com.ai.da.mapper.primary.entity.*;
|
||||||
|
import com.ai.da.mapper.primary.entity.Collection;
|
||||||
import com.ai.da.model.dto.*;
|
import com.ai.da.model.dto.*;
|
||||||
import com.ai.da.model.enums.Position;
|
import com.ai.da.model.enums.Position;
|
||||||
import com.ai.da.model.enums.Sex;
|
import com.ai.da.model.enums.Sex;
|
||||||
@@ -20,21 +21,20 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
|
|||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import sun.security.krb5.internal.crypto.Des;
|
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Date;
|
import java.util.stream.Collectors;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio> implements PortfolioService {
|
public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio> implements PortfolioService {
|
||||||
|
|
||||||
@@ -432,8 +432,31 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
|
|||||||
return PageBaseResponse.success(new Page<>());
|
return PageBaseResponse.success(new Page<>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
List<Portfolio> topThree = new ArrayList<>();
|
||||||
|
|
||||||
|
if (query.getPage() == 1 &&
|
||||||
|
(query.getGetMyPortfolio() != 1 || query.getGetLikePortfolio() != 1)) {
|
||||||
|
List<Long> topThreeLike = getTopThreeLikeFromRedis(RedisUtil.PORTFOLIO_LIKE_KEY);
|
||||||
|
log.info("top three like 的作品集的id : {}", topThreeLike);
|
||||||
|
List<Long> topThreeView = getTopThreeViewFromRedis(RedisUtil.PORTFOLIO_VIEW_KEY);
|
||||||
|
log.info("top three view 的作品集的id : {}", topThreeView);
|
||||||
|
QueryWrapper<Portfolio> queryLike = new QueryWrapper<>();
|
||||||
|
queryLike.in("id", topThreeLike);
|
||||||
|
QueryWrapper<Portfolio> queryView = new QueryWrapper<>();
|
||||||
|
queryView.in("id", topThreeView);
|
||||||
|
List<Portfolio> topThreeLikePortfolio = baseMapper.selectList(queryLike);
|
||||||
|
List<Portfolio> topThreeViewPortfolio = baseMapper.selectList(queryView);
|
||||||
|
topThree.addAll(topThreeLikePortfolio);
|
||||||
|
topThree.addAll(topThreeViewPortfolio);
|
||||||
|
}
|
||||||
|
|
||||||
qw.lambda().orderByDesc(Portfolio::getUpdateDate);
|
qw.lambda().orderByDesc(Portfolio::getUpdateDate);
|
||||||
IPage<Portfolio> page = portfolioMapper.selectPage(new Page<>(query.getPage(), query.getSize()), qw);
|
IPage<Portfolio> page = portfolioMapper.selectPage(new Page<>(query.getPage(), query.getSize()), qw);
|
||||||
|
if (!topThree.isEmpty()) {
|
||||||
|
List<Portfolio> records = page.getRecords();
|
||||||
|
records.addAll(0, topThree);
|
||||||
|
page.setRecords(records);
|
||||||
|
}
|
||||||
IPage<PortfolioVO> convert = page.convert((Function<Portfolio, PortfolioVO>) portfolio -> {
|
IPage<PortfolioVO> convert = page.convert((Function<Portfolio, PortfolioVO>) portfolio -> {
|
||||||
if (portfolio != null) {
|
if (portfolio != null) {
|
||||||
PortfolioVO vo = CopyUtil.copyObject(portfolio, PortfolioVO.class);
|
PortfolioVO vo = CopyUtil.copyObject(portfolio, PortfolioVO.class);
|
||||||
@@ -795,6 +818,7 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
|
|||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private RedisUtil redisUtil;
|
private RedisUtil redisUtil;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean like(Long id) {
|
public Boolean like(Long id) {
|
||||||
AuthPrincipalVo userHolder = UserContext.getUserHolder();
|
AuthPrincipalVo userHolder = UserContext.getUserHolder();
|
||||||
@@ -804,6 +828,7 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
|
|||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private CommentMapper commentMapper;
|
private CommentMapper commentMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean comment(CommentDTO commentDTO) {
|
public Boolean comment(CommentDTO commentDTO) {
|
||||||
AuthPrincipalVo userHolder = UserContext.getUserHolder();
|
AuthPrincipalVo userHolder = UserContext.getUserHolder();
|
||||||
@@ -925,4 +950,63 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
|
|||||||
// }
|
// }
|
||||||
return resultList;
|
return resultList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<Long> getTopThreeLikeFromRedis(String prefix) {
|
||||||
|
// 获取所有与 PORTFOLIO_LIKE_KEY 相关的 keys
|
||||||
|
Set<String> keys = redisUtil.getKeysFromString(prefix + "*");
|
||||||
|
|
||||||
|
List<Map.Entry<String, Long>> portfolioSizes = new ArrayList<>();
|
||||||
|
|
||||||
|
if (keys != null) {
|
||||||
|
for (String key : keys) {
|
||||||
|
// 获取 Set 的 size
|
||||||
|
Long size = redisUtil.getSize(key);
|
||||||
|
// 保存 key 和 size
|
||||||
|
portfolioSizes.add(new AbstractMap.SimpleEntry<>(key, size));
|
||||||
|
}
|
||||||
|
// 按 size 倒序排序
|
||||||
|
portfolioSizes.sort((e1, e2) -> e2.getValue().compareTo(e1.getValue()));
|
||||||
|
// 获取 size 最大的前三个
|
||||||
|
List<Long> top3PortfolioIds = portfolioSizes.stream()
|
||||||
|
.limit(3)
|
||||||
|
.map(Map.Entry::getKey)
|
||||||
|
.map(key -> key.replace(prefix, "")) // 去掉前缀,获取 portfolioId
|
||||||
|
.map(Long::valueOf)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
// 输出 top3PortfolioIds
|
||||||
|
top3PortfolioIds.forEach(System.out::println);
|
||||||
|
return top3PortfolioIds;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Long> getTopThreeViewFromRedis(String prefix) {
|
||||||
|
// Step 1: Get all keys with the specific prefix
|
||||||
|
Set<String> keys = redisUtil.getKeysFromString(prefix + "*");
|
||||||
|
// Step 2: Create a map to store portfolioId and its corresponding value
|
||||||
|
Map<Long, Long> portfolioViews = new HashMap<>();
|
||||||
|
if (keys != null) {
|
||||||
|
for (String key : keys) {
|
||||||
|
// Get the value associated with the key
|
||||||
|
Long value = redisUtil.getViewCount(key);
|
||||||
|
if (!Objects.isNull(value)) {
|
||||||
|
// Extract the portfolioId from the key (assuming the key structure is consistent)
|
||||||
|
Long portfolioId = Long.valueOf(key.replace(prefix, ""));
|
||||||
|
portfolioViews.put(portfolioId, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Step 3: Find the top 3 portfolio IDs with the largest values
|
||||||
|
if (!portfolioViews.isEmpty()){
|
||||||
|
List<Long> top3PortfolioIds = portfolioViews.entrySet().stream()
|
||||||
|
.sorted(Map.Entry.<Long, Long>comparingByValue().reversed())
|
||||||
|
.limit(3)
|
||||||
|
.map(Map.Entry::getKey)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
System.out.println("Top 3 Portfolio IDs: " + top3PortfolioIds);
|
||||||
|
return top3PortfolioIds;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user