1optimise collect operation

main
Gary 10 months ago
parent ba9c167a7c
commit baaa64c3ad

@ -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,46 +49,61 @@ public class JournalController {
private ArticleService articleService;
@Autowired
private JwtUtil jwtUtil;
@ApiOperation(value = "1.查询期刊信息", notes="若token为空或token校验失败默认返回最新的10期期刊筛选条件对游客不可用")
@ApiOperation(value = "1.查询期刊信息", notes = "若token为空或token校验失败默认返回最新的10期期刊筛选条件对游客不可用")
@GetMapping("/list")
@GlobalInterceptor
public Result<PageResult<JournalRespDTO>> page(@RequestHeader(value = "token", required = false) String token,
@VerifyParam JournalQueryReq queryReq){
UserLoginDto user=jwtUtil.getUserLoginDto(token);
if(null==user) {
public Result<PageResult<JournalRespDTO>> page(@RequestHeader(value = "token", required = false) String token,
@VerifyParam JournalQueryReq queryReq) {
UserLoginDto user = jwtUtil.getUserLoginDto(token);
if (null == user) {
queryReq.setLanguage(null);
queryReq.setStyle(null);
queryReq.setPageNum(1);
queryReq.setPageSize(10);
}
Page<Article> pageList = articleService.queryPage(queryReq);
List<JournalRespDTO> list=pageList.stream().map(a->getArticleRespDTO(a,user)).collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list) );
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");
JournalRespDTO journalRespDTO=new JournalRespDTO();
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.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();
Date date = null == article.getUpdatetime() ? article.getCreatetime() : article.getUpdatetime();
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.web.bind.annotation.*;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@ -64,7 +65,9 @@ public class SongController {
return Result.unauthorized(null);
}
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);
}
@ -79,11 +82,11 @@ public class SongController {
public Result<List<SongRespDTO>> random(
@PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer 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);
}
private SongRespDTO getSongRespDTO(Song song, UserLoginDto userLoginDto) {
private SongRespDTO getSongRespDTO(Song song, Set<String> songCollectSet) {
SongRespDTO songRespDTO = new SongRespDTO();
songRespDTO.setJournalNo(song.getVolid());
songRespDTO.setAlbum(song.getAlbum());
@ -94,14 +97,28 @@ public class SongController {
songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".jpg"));
songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".lyric"));
songRespDTO.setSongNo(song.getSongno());
// 根据userId查询是否已收藏
//if (null != userLoginDto) {
// long userCollectCount=userCollectDao.countByUserIdAndObjectIdAndCollectType(userLoginDto.getUserId(), songRespDTO.getId(), CollectTypeEnum.SONG.getType());
// songRespDTO.setHaveCollect(userCollectCount>0);
//}
songRespDTO.setHaveCollect(songCollectSet.contains(songRespDTO.getId()));
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
*

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

Loading…
Cancel
Save