@ -1,7 +1,14 @@
package com.luoo.music.controller ;
import java.util.Collections ;
import java.util.Date ;
import java.util.HashSet ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import java.util.stream.Collectors ;
import java.util.stream.Stream ;
import java.util.stream.StreamSupport ;
import com.luoo.music.dao.UserCollectDao ;
import com.luoo.music.dto.request.JournalQueryReq ;
@ -28,6 +35,7 @@ import util.JwtUtil;
/ * *
* 控 制 器 层
*
* @author Administrator
*
* /
@ -41,6 +49,7 @@ public class JournalController {
private ArticleService articleService ;
@Autowired
private JwtUtil jwtUtil ;
@ApiOperation ( value = "1.查询期刊信息" , notes = "若token为空或token校验失败, 默认返回最新的10期, 期刊筛选条件对游客不可用" )
@GetMapping ( "/list" )
@GlobalInterceptor
@ -54,30 +63,44 @@ public class JournalController {
queryReq . setPageSize ( 10 ) ;
}
Page < Article > pageList = articleService . queryPage ( queryReq ) ;
List < JournalRespDTO > list = pageList . stream ( ) . map ( a - > getArticleRespDTO ( a , user ) ) . collect ( Collectors . toList ( ) ) ;
List < String > collectIds = getCollectIds ( pageList , user , CollectTypeEnum . JOURNAL . getType ( ) ) ;
Set < String > journalCollectSet = getCollectSet ( collectIds ) ;
List < JournalRespDTO > list = pageList . stream ( ) . map ( a - > getArticleRespDTO ( a , journalCollectSet ) )
. collect ( Collectors . toList ( ) ) ;
return Result . success ( new PageResult < JournalRespDTO > ( Long . valueOf ( list . size ( ) ) , list ) ) ;
}
private JournalRespDTO getArticleRespDTO ( Article article , UserLoginDto userLoginDto ) {
//StopWatch sw=new StopWatch();
//sw.start("stage1");
private Set < String > getCollectSet ( List < String > collectIds ) {
if ( collectIds . isEmpty ( ) ) {
return Collections . emptySet ( ) ;
}
Set < String > set = new HashSet < > ( ) ;
userCollectDao . findAllById ( collectIds ) . forEach ( u - > set . add ( u . getObjectId ( ) ) ) ;
return set ;
}
private List < String > getCollectIds ( Page < Article > pageList , UserLoginDto user , Integer type ) {
if ( null = = user ) {
return Collections . emptyList ( ) ;
}
return pageList . stream ( ) . map ( a - > getCollectId ( user . getUserId ( ) , a . getId ( ) , type ) ) . collect ( Collectors . toList ( ) ) ;
}
private String getCollectId ( String userId , String objectId , Integer type ) {
return userId + "_" + objectId + "_" + type ;
}
private JournalRespDTO getArticleRespDTO ( Article article , Set < String > journalCollectSet ) {
JournalRespDTO journalRespDTO = new JournalRespDTO ( ) ;
journalRespDTO . setId ( article . getId ( ) ) ;
journalRespDTO . setJournalNo ( article . getVolid ( ) ) ;
journalRespDTO . setTitle ( article . getTitle ( ) ) ;
journalRespDTO . setImage ( Constants . MUSIC_RESOURCE_PREFIX + article . getImage ( ) ) ;
journalRespDTO . setDate ( getEditDate ( article ) ) ;
//sw.stop();
//System.out.println(sw.prettyPrint());
//sw.start("stage2");
//根据userId查询是否已收藏
//if(null!=userLoginDto) {
// long userCollectCount=userCollectDao.countByUserIdAndObjectIdAndCollectType(userLoginDto.getUserId(), journalRespDTO.getId(), CollectTypeEnum.JOURNAL.getType());
// journalRespDTO.setHaveCollect(userCollectCount>0);
//}
//sw.stop();
//System.out.println(sw.prettyPrint());
journalRespDTO . setHaveCollect ( journalCollectSet . contains ( journalRespDTO . getId ( ) ) ) ;
return journalRespDTO ;
}
private String getEditDate ( Article article ) {
Date date = null = = article . getUpdatetime ( ) ? article . getCreatetime ( ) : article . getUpdatetime ( ) ;
return DateUtil . format ( date , DateTimePatternEnum . YYYY_DOT_MM_DOT_DD . getPattern ( ) ) ;