@ -2,6 +2,7 @@ package com.luoo.music.controller;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.HashMap ;
import java.util.HashSet ;
import java.util.List ;
import java.util.Map ;
@ -22,6 +23,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable ;
import org.springframework.data.mongodb.core.MongoTemplate ;
import org.springframework.data.redis.core.RedisTemplate ;
import org.springframework.util.StopWatch ;
import org.springframework.web.bind.annotation.* ;
import com.luoo.music.pojo.Journal ;
import com.luoo.music.pojo.Tag ;
@ -32,9 +34,11 @@ import annotation.GlobalInterceptor;
import annotation.VerifyParam ;
import api.PageResult ;
import api.Result ;
import constants.Constants ;
import dto.UserLoginDto ;
import enums.CollectTypeEnum ;
import util.JwtUtil ;
import util.StringTools ;
/ * *
* 控 制 器 层
@ -69,6 +73,7 @@ public class JournalController {
private RedisTemplate redisTemplate ;
private static final int JOURNAL_FILTER_NUMBER_RANGE = 100 ;
private static final Map < String , JournalRespDTO > journalRespDTOMap = new HashMap < > ( ) ;
@ApiOperation ( value = "1.查询期刊信息" , notes = "若authorization为空或authorization校验失败, 默认返回最新的10期, 期刊筛选条件对游客不可用" )
@GetMapping ( "/list" )
@ -82,15 +87,50 @@ public class JournalController {
queryReq . setPageNum ( 1 ) ;
queryReq . setPageSize ( 10 ) ;
}
Set < String > journalCollectSet = null = = user ? Collections . emptySet ( ) : getJournalCollectSet ( user . getUserId ( ) ) ;
if ( StringTools . isEmpty ( queryReq . getCategoryId ( ) ) & & StringTools . isEmpty ( queryReq . getJournalNoRange ( ) ) ) {
List < JournalRespDTO > cacheList = getPagableJournalRespDTO ( queryReq , journalCollectSet ) ;
List < String > ids = cacheList . stream ( ) . map ( JournalRespDTO : : getId ) . collect ( Collectors . toList ( ) ) ;
Map < String , List < String > > tagMap = tagService . getTagMap ( ids ) ;
List < JournalRespDTO > list = cacheList . stream ( ) . map ( j - > updateCollectAndTag ( j , journalCollectSet , tagMap ) ) . collect ( Collectors . toList ( ) ) ;
return Result . success ( new PageResult < JournalRespDTO > ( Long . valueOf ( list . size ( ) ) , list ) ) ;
}
List < Journal > pageList = journalService . queryPage ( queryReq ) ;
List < String > ids = pageList . stream ( ) . map ( Journal : : getId ) . collect ( Collectors . toList ( ) ) ;
Set < String > journalCollectSet = null = = user ? Collections . emptySet ( )
: userCollectInfoService . getCollectSet ( user . getUserId ( ) , ids , CollectTypeEnum . JOURNAL ) ;
Map < String , List < String > > tagMap = tagService . getTagMap ( ids ) ;
List < JournalRespDTO > list = pageList . stream ( ) . map ( a - > JournalMapper . getJournalRespDTO ( a , journalCollectSet , mongoTemplate , redisTemplate , commentDao , tagMap ) )
. collect ( Collectors . toList ( ) ) ;
List < JournalRespDTO > list = pageList . stream ( ) . map ( a - > JournalMapper . getJournalRespDTO ( a , journalCollectSet , tagMap ) ) . collect ( Collectors . toList ( ) ) ;
return Result . success ( new PageResult < JournalRespDTO > ( Long . valueOf ( list . size ( ) ) , list ) ) ;
}
private JournalRespDTO updateCollectAndTag ( JournalRespDTO journalRespDTO , Set < String > journalCollectSet , Map < String , List < String > > tagMap ) {
journalRespDTO . setTags ( tagMap . getOrDefault ( journalRespDTO . getId ( ) , Collections . emptyList ( ) ) ) ;
journalRespDTO . setHaveCollect ( journalCollectSet . contains ( journalRespDTO . getId ( ) ) ) ;
return journalRespDTO ;
}
private List < JournalRespDTO > getPagableJournalRespDTO ( JournalQueryReq queryReq , Set < String > journalCollectSet ) {
String redisPageJournalResponseDtoKey = Constants . REDIS_KEY_PAGE_JOURNAL_RESPONSE_DTO + queryReq . getPageNum ( ) + "_" + queryReq . getPageSize ( ) ;
List < JournalRespDTO > journalRespDTOs = ( List < JournalRespDTO > ) redisTemplate . opsForValue ( ) . get ( redisPageJournalResponseDtoKey ) ;
if ( null = = journalRespDTOs ) {
List < Journal > pageList = journalService . queryPage ( queryReq ) ;
List < String > ids = pageList . stream ( ) . map ( Journal : : getId ) . collect ( Collectors . toList ( ) ) ;
Map < String , List < String > > tagMap = tagService . getTagMap ( ids ) ;
journalRespDTOs = pageList . stream ( ) . map ( journal - > JournalMapper . getJournalRespDTO ( journal , journalCollectSet , mongoTemplate , redisTemplate , commentDao , tagMap ) ) . collect ( Collectors . toList ( ) ) ;
redisTemplate . opsForValue ( ) . set ( redisPageJournalResponseDtoKey , journalRespDTOs ) ;
}
return journalRespDTOs ;
}
private Set < String > getJournalCollectSet ( String userId ) {
Set < String > set = ( Set < String > ) redisTemplate . opsForValue ( ) . get ( Constants . REDIS_KEY_USER_COLLECT_JOURNAL + userId ) ;
if ( null = = set ) {
set = userCollectInfoService . getCollectSet ( userId , CollectTypeEnum . JOURNAL ) ;
redisTemplate . opsForValue ( ) . set ( Constants . REDIS_KEY_USER_COLLECT_JOURNAL + userId , set ) ;
}
return set ;
}
@ApiOperation ( value = "2.查询收藏期刊信息" )
@ApiImplicitParams ( { @ApiImplicitParam ( name = "userId" , value = "用户id" , required = true ) ,