1optimise collect operation

main
Gary 10 months ago
parent ba9c167a7c
commit baaa64c3ad

@ -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,45 +49,60 @@ 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
public Result<PageResult<JournalRespDTO>> page(@RequestHeader(value = "token", required = false) String token, public Result<PageResult<JournalRespDTO>> page(@RequestHeader(value = "token", required = false) String token,
@VerifyParam JournalQueryReq queryReq){ @VerifyParam JournalQueryReq queryReq) {
UserLoginDto user=jwtUtil.getUserLoginDto(token); UserLoginDto user = jwtUtil.getUserLoginDto(token);
if(null==user) { if (null == user) {
queryReq.setLanguage(null); queryReq.setLanguage(null);
queryReq.setStyle(null); queryReq.setStyle(null);
queryReq.setPageNum(1); queryReq.setPageNum(1);
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());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list) ); 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(); private Set<String> getCollectSet(List<String> collectIds) {
//sw.start("stage1"); if (collectIds.isEmpty()) {
JournalRespDTO journalRespDTO=new JournalRespDTO(); 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.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());
} }

@ -25,6 +25,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -64,7 +65,9 @@ public class SongController {
return Result.unauthorized(null); return Result.unauthorized(null);
} }
List<Song> songs = songService.findByVolid(journalNo); List<Song> songs = songService.findByVolid(journalNo);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, user)).collect(Collectors.toList()); List<String> collectIds = getCollectIds(songs, user, CollectTypeEnum.SONG.getType());
Set<String> songCollectSet = getCollectSet(collectIds);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList());
return Result.success(results); return Result.success(results);
} }
@ -79,11 +82,11 @@ public class SongController {
public Result<List<SongRespDTO>> random( public Result<List<SongRespDTO>> random(
@PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { @PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) {
List<Song> songs = songService.random(limit); List<Song> songs = songService.random(limit);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, null)).collect(Collectors.toList()); List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, Collections.emptySet())).collect(Collectors.toList());
return Result.success(results); return Result.success(results);
} }
private SongRespDTO getSongRespDTO(Song song, UserLoginDto userLoginDto) { private SongRespDTO getSongRespDTO(Song song, Set<String> songCollectSet) {
SongRespDTO songRespDTO = new SongRespDTO(); SongRespDTO songRespDTO = new SongRespDTO();
songRespDTO.setJournalNo(song.getVolid()); songRespDTO.setJournalNo(song.getVolid());
songRespDTO.setAlbum(song.getAlbum()); songRespDTO.setAlbum(song.getAlbum());
@ -94,14 +97,28 @@ public class SongController {
songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".jpg")); songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".jpg"));
songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".lyric")); songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".lyric"));
songRespDTO.setSongNo(song.getSongno()); songRespDTO.setSongNo(song.getSongno());
// 根据userId查询是否已收藏 songRespDTO.setHaveCollect(songCollectSet.contains(songRespDTO.getId()));
//if (null != userLoginDto) {
// long userCollectCount=userCollectDao.countByUserIdAndObjectIdAndCollectType(userLoginDto.getUserId(), songRespDTO.getId(), CollectTypeEnum.SONG.getType());
// songRespDTO.setHaveCollect(userCollectCount>0);
//}
return songRespDTO; return songRespDTO;
} }
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(List<Song> list, UserLoginDto user, Integer type) {
if (null == user) {
return Collections.emptyList();
}
return list.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;
}
/** /**
* ID * ID
* *

@ -1,6 +1,7 @@
package com.luoo.user.service; package com.luoo.user.service;
import java.util.Date; import java.util.Date;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -22,11 +23,13 @@ public class UserCollectService {
if (null == collectTypeEnum) { if (null == collectTypeEnum) {
throw new BizException(StatusCode.VALIDATE_FAILED); throw new BizException(StatusCode.VALIDATE_FAILED);
} }
UserCollect dbCollect = userCollectDao.findByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); String collectId=userId+"_"+objectId+"_"+collectType;
if (null!=dbCollect) { Optional<UserCollect> dbCollect = userCollectDao.findById(collectId);//.findByUserIdAndObjectIdAndCollectType(userId, objectId, collectType);
if (dbCollect.isPresent()) {
return; return;
} }
UserCollect userCollect = new UserCollect(); UserCollect userCollect = new UserCollect();
userCollect.setCollectId(collectId);
userCollect.setUserId(userId); userCollect.setUserId(userId);
userCollect.setCollectTime(new Date()); userCollect.setCollectTime(new Date());
userCollect.setObjectId(objectId); userCollect.setObjectId(objectId);
@ -36,6 +39,8 @@ public class UserCollectService {
public void deleteUserCollectByUserIdAndObjectIdAndCollectType(String userId, String objectId, public void deleteUserCollectByUserIdAndObjectIdAndCollectType(String userId, String objectId,
Integer collectType) { Integer collectType) {
userCollectDao.deleteByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); String collectId=userId+"_"+objectId+"_"+collectType;
userCollectDao.deleteById(collectId);
//userCollectDao.deleteByUserIdAndObjectIdAndCollectType(userId, objectId, collectType);
} }
} }

Loading…
Cancel
Save