@ -1,7 +1,14 @@
package com.luoo.music.controller ;
package com.luoo.music.controller ;
import java.util.Collections ;
import java.util.Date ;
import java.util.Date ;
import java.util.HashSet ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import java.util.stream.Collectors ;
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.dao.UserCollectDao ;
import com.luoo.music.dto.request.JournalQueryReq ;
import com.luoo.music.dto.request.JournalQueryReq ;
@ -28,6 +35,7 @@ import util.JwtUtil;
/ * *
/ * *
* 控 制 器 层
* 控 制 器 层
*
* @author Administrator
* @author Administrator
*
*
* /
* /
@ -41,6 +49,7 @@ public class JournalController {
private ArticleService articleService ;
private ArticleService articleService ;
@Autowired
@Autowired
private JwtUtil jwtUtil ;
private JwtUtil jwtUtil ;
@ApiOperation ( value = "1.查询期刊信息" , notes = "若token为空或token校验失败, 默认返回最新的10期, 期刊筛选条件对游客不可用" )
@ApiOperation ( value = "1.查询期刊信息" , notes = "若token为空或token校验失败, 默认返回最新的10期, 期刊筛选条件对游客不可用" )
@GetMapping ( "/list" )
@GetMapping ( "/list" )
@GlobalInterceptor
@GlobalInterceptor
@ -54,30 +63,44 @@ public class JournalController {
queryReq . setPageSize ( 10 ) ;
queryReq . setPageSize ( 10 ) ;
}
}
Page < Article > pageList = articleService . queryPage ( queryReq ) ;
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 ) ) ;
return Result . success ( new PageResult < JournalRespDTO > ( Long . valueOf ( list . size ( ) ) , list ) ) ;
}
}
private JournalRespDTO getArticleRespDTO ( Article article , UserLoginDto userLoginDto ) {
//StopWatch sw=new StopWatch();
private Set < String > getCollectSet ( List < String > collectIds ) {
//sw.start("stage1");
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 journalRespDTO = new JournalRespDTO ( ) ;
journalRespDTO . setId ( article . getId ( ) ) ;
journalRespDTO . setId ( article . getId ( ) ) ;
journalRespDTO . setJournalNo ( article . getVolid ( ) ) ;
journalRespDTO . setJournalNo ( article . getVolid ( ) ) ;
journalRespDTO . setTitle ( article . getTitle ( ) ) ;
journalRespDTO . setTitle ( article . getTitle ( ) ) ;
journalRespDTO . setImage ( Constants . MUSIC_RESOURCE_PREFIX + article . getImage ( ) ) ;
journalRespDTO . setImage ( Constants . MUSIC_RESOURCE_PREFIX + article . getImage ( ) ) ;
journalRespDTO . setDate ( getEditDate ( article ) ) ;
journalRespDTO . setDate ( getEditDate ( article ) ) ;
//sw.stop();
journalRespDTO . setHaveCollect ( journalCollectSet . contains ( journalRespDTO . getId ( ) ) ) ;
//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());
return journalRespDTO ;
return journalRespDTO ;
}
}
private String getEditDate ( Article article ) {
private String getEditDate ( Article article ) {
Date date = null = = article . getUpdatetime ( ) ? article . getCreatetime ( ) : article . getUpdatetime ( ) ;
Date date = null = = article . getUpdatetime ( ) ? article . getCreatetime ( ) : article . getUpdatetime ( ) ;
return DateUtil . format ( date , DateTimePatternEnum . YYYY_DOT_MM_DOT_DD . getPattern ( ) ) ;
return DateUtil . format ( date , DateTimePatternEnum . YYYY_DOT_MM_DOT_DD . getPattern ( ) ) ;