BUGFIX:作品广场
This commit is contained in:
@@ -421,9 +421,13 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
|
|||||||
public PageBaseResponse<PortfolioVO> page(QueryPortfolioPageDTO query) {
|
public PageBaseResponse<PortfolioVO> page(QueryPortfolioPageDTO query) {
|
||||||
AuthPrincipalVo userHolder = UserContext.getUserHolder();
|
AuthPrincipalVo userHolder = UserContext.getUserHolder();
|
||||||
QueryWrapper<Portfolio> qw = new QueryWrapper<>();
|
QueryWrapper<Portfolio> qw = new QueryWrapper<>();
|
||||||
|
|
||||||
|
// 查询用户的作品集
|
||||||
if (query.getGetMyPortfolio() == 1) {
|
if (query.getGetMyPortfolio() == 1) {
|
||||||
qw.lambda().eq(Portfolio::getAccountId, userHolder.getId());
|
qw.lambda().eq(Portfolio::getAccountId, userHolder.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 查询用户喜欢的作品集
|
||||||
if (query.getGetLikePortfolio() == 1) {
|
if (query.getGetLikePortfolio() == 1) {
|
||||||
List<Long> likedPortfolioIdList = redisUtil.getLikedPortfolios(userHolder.getId());
|
List<Long> likedPortfolioIdList = redisUtil.getLikedPortfolios(userHolder.getId());
|
||||||
if (!CollectionUtils.isEmpty(likedPortfolioIdList)) {
|
if (!CollectionUtils.isEmpty(likedPortfolioIdList)) {
|
||||||
@@ -432,31 +436,54 @@ 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 &&
|
List<Portfolio> topThree = new ArrayList<>();
|
||||||
(query.getGetMyPortfolio() != 1 || query.getGetLikePortfolio() != 1)) {
|
List<Long> excludeIds = new ArrayList<>(); // 存放需要排除的 ID 列表
|
||||||
|
|
||||||
|
// 获取前三点赞和前三浏览的作品集,并将其排除在分页查询之外
|
||||||
|
if (query.getPage() == 1 && (query.getGetMyPortfolio() != 1 || query.getGetLikePortfolio() != 1)) {
|
||||||
|
// 获取前三点赞的作品集 ID
|
||||||
List<Long> topThreeLike = getTopThreeLikeFromRedis(RedisUtil.PORTFOLIO_LIKE_KEY);
|
List<Long> topThreeLike = getTopThreeLikeFromRedis(RedisUtil.PORTFOLIO_LIKE_KEY);
|
||||||
log.info("top three like 的作品集的id : {}", topThreeLike);
|
List<Long> topThreeView = getTopThreeViewFromRedis(RedisUtil.PORTFOLIO_VIEW_KEY, topThreeLike);
|
||||||
List<Long> topThreeView = getTopThreeViewFromRedis(RedisUtil.PORTFOLIO_VIEW_KEY);
|
|
||||||
log.info("top three view 的作品集的id : {}", topThreeView);
|
// 获取前三点赞的作品集
|
||||||
QueryWrapper<Portfolio> queryLike = new QueryWrapper<>();
|
if (!CollectionUtils.isEmpty(topThreeLike)) {
|
||||||
queryLike.in("id", topThreeLike);
|
QueryWrapper<Portfolio> queryLike = new QueryWrapper<>();
|
||||||
QueryWrapper<Portfolio> queryView = new QueryWrapper<>();
|
queryLike.in("id", topThreeLike);
|
||||||
queryView.in("id", topThreeView);
|
List<Portfolio> topThreeLikePortfolio = baseMapper.selectList(queryLike);
|
||||||
List<Portfolio> topThreeLikePortfolio = baseMapper.selectList(queryLike);
|
topThree.addAll(topThreeLikePortfolio);
|
||||||
List<Portfolio> topThreeViewPortfolio = baseMapper.selectList(queryView);
|
excludeIds.addAll(topThreeLike); // 添加到排除 ID 列表
|
||||||
topThree.addAll(topThreeLikePortfolio);
|
}
|
||||||
topThree.addAll(topThreeViewPortfolio);
|
|
||||||
|
// 获取前三浏览的作品集
|
||||||
|
if (!CollectionUtils.isEmpty(topThreeView)) {
|
||||||
|
QueryWrapper<Portfolio> queryView = new QueryWrapper<>();
|
||||||
|
queryView.in("id", topThreeView);
|
||||||
|
List<Portfolio> topThreeViewPortfolio = baseMapper.selectList(queryView);
|
||||||
|
topThree.addAll(topThreeViewPortfolio);
|
||||||
|
excludeIds.addAll(topThreeView); // 添加到排除 ID 列表
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 在正常分页查询中排除前三点赞和前三浏览的作品集
|
||||||
|
if (!CollectionUtils.isEmpty(excludeIds)) {
|
||||||
|
qw.lambda().notIn(Portfolio::getId, excludeIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按更新时间排序
|
||||||
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()) {
|
if (!topThree.isEmpty()) {
|
||||||
List<Portfolio> records = page.getRecords();
|
List<Portfolio> records = page.getRecords();
|
||||||
records.addAll(0, topThree);
|
records.addAll(0, topThree); // 添加到查询结果的开头
|
||||||
page.setRecords(records);
|
page.setRecords(records);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 将 Portfolio 转换为 PortfolioVO 并进行相关处理
|
||||||
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);
|
||||||
@@ -473,9 +500,11 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|
||||||
return PageBaseResponse.success(convert);
|
return PageBaseResponse.success(convert);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PortfolioVO detail(PortfolioDTO portfolioDTO) {
|
public PortfolioVO detail(PortfolioDTO portfolioDTO) {
|
||||||
AuthPrincipalVo userHolder = UserContext.getUserHolder();
|
AuthPrincipalVo userHolder = UserContext.getUserHolder();
|
||||||
@@ -980,10 +1009,11 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Long> getTopThreeViewFromRedis(String prefix) {
|
private List<Long> getTopThreeViewFromRedis(String prefix, List<Long> excludeIds) {
|
||||||
// Step 1: Get all keys with the specific prefix
|
// Step 1: Get all keys with the specific prefix
|
||||||
Set<String> keys = redisUtil.getKeysFromString(prefix + "*");
|
Set<String> keys = redisUtil.getKeysFromString(prefix + "*");
|
||||||
// Step 2: Create a map to store portfolioId and its corresponding value
|
|
||||||
|
// Step 2: Create a map to store portfolioId and its corresponding value
|
||||||
Map<Long, Long> portfolioViews = new HashMap<>();
|
Map<Long, Long> portfolioViews = new HashMap<>();
|
||||||
if (keys != null) {
|
if (keys != null) {
|
||||||
for (String key : keys) {
|
for (String key : keys) {
|
||||||
@@ -992,11 +1022,15 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
|
|||||||
if (!Objects.isNull(value)) {
|
if (!Objects.isNull(value)) {
|
||||||
// Extract the portfolioId from the key (assuming the key structure is consistent)
|
// Extract the portfolioId from the key (assuming the key structure is consistent)
|
||||||
Long portfolioId = Long.valueOf(key.replace(prefix, ""));
|
Long portfolioId = Long.valueOf(key.replace(prefix, ""));
|
||||||
portfolioViews.put(portfolioId, value);
|
// 只将不在点赞前三的 portfolioId 加入
|
||||||
|
if (!excludeIds.contains(portfolioId)) {
|
||||||
|
portfolioViews.put(portfolioId, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Step 3: Find the top 3 portfolio IDs with the largest values
|
|
||||||
|
// Step 3: Find the top 3 portfolio IDs with the largest values
|
||||||
if (!portfolioViews.isEmpty()){
|
if (!portfolioViews.isEmpty()){
|
||||||
List<Long> top3PortfolioIds = portfolioViews.entrySet().stream()
|
List<Long> top3PortfolioIds = portfolioViews.entrySet().stream()
|
||||||
.sorted(Map.Entry.<Long, Long>comparingByValue().reversed())
|
.sorted(Map.Entry.<Long, Long>comparingByValue().reversed())
|
||||||
@@ -1004,7 +1038,7 @@ public class PortfolioServiceImpl extends ServiceImpl<PortfolioMapper, Portfolio
|
|||||||
.map(Map.Entry::getKey)
|
.map(Map.Entry::getKey)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
System.out.println("Top 3 Portfolio IDs: " + top3PortfolioIds);
|
System.out.println("Top 3 Portfolio IDs after exclusion: " + top3PortfolioIds);
|
||||||
return top3PortfolioIds;
|
return top3PortfolioIds;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user