1.update collect model for journal and music

main
Gary 10 months ago
parent d3b0b7e0ba
commit 2a5259a9fe

@ -29,6 +29,8 @@ import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults; import org.springframework.data.mongodb.core.aggregation.AggregationResults;
@ -37,8 +39,7 @@ import org.springframework.web.bind.annotation.*;
import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Journal;
import com.luoo.music.pojo.Tag; import com.luoo.music.pojo.Tag;
import com.luoo.music.service.JournalService; import com.luoo.music.service.JournalService;
import com.luoo.music.service.UserCollectService; import com.luoo.music.service.UserCollectInfoService;
import annotation.GlobalInterceptor; import annotation.GlobalInterceptor;
import annotation.VerifyParam; import annotation.VerifyParam;
import api.PageResult; import api.PageResult;
@ -64,7 +65,8 @@ import util.StringTools;
@RequestMapping("/journal") @RequestMapping("/journal")
public class JournalController { public class JournalController {
@Autowired @Autowired
private UserCollectService userCollectService; private UserCollectInfoService userCollectInfoService;
@Autowired @Autowired
private JournalService journalService; private JournalService journalService;
@ -120,8 +122,9 @@ public class JournalController {
queryReq.setPageSize(10); queryReq.setPageSize(10);
} }
Page<Journal> pageList = journalService.queryPage(queryReq); Page<Journal> pageList = journalService.queryPage(queryReq);
List<String> ids=pageList.stream().map(Journal::getId).collect(Collectors.toList());
Set<String> journalCollectSet = null == user ? Collections.emptySet() Set<String> journalCollectSet = null == user ? Collections.emptySet()
: userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.JOURNAL); : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL);
List<JournalRespDTO> list = pageList.stream().map(a -> getJournalRespDTO(a, journalCollectSet)) List<JournalRespDTO> list = pageList.stream().map(a -> getJournalRespDTO(a, journalCollectSet))
.collect(Collectors.toList()); .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));
@ -138,12 +141,13 @@ public class JournalController {
@PathVariable @VerifyParam(required = true) String userId, @PathVariable @VerifyParam(required = true) String userId,
@PathVariable @VerifyParam(required = true) Integer pageNum, @PathVariable @VerifyParam(required = true) Integer pageNum,
@PathVariable @VerifyParam(required = true) Integer pageSize) { @PathVariable @VerifyParam(required = true) Integer pageSize) {
List<String> objectIds = userCollectService.getCollectList(userId, pageNum, pageSize, CollectTypeEnum.JOURNAL); Pageable pageable = PageRequest.of(pageNum-1,pageSize);
List<String> objectIds=userCollectInfoService.findByUserIdAndCollectType(userId, CollectTypeEnum.JOURNAL, pageable);
if (objectIds.isEmpty()) { if (objectIds.isEmpty()) {
return Result.success(new PageResult<JournalRespDTO>(0L, Collections.emptyList())); return Result.success(new PageResult<JournalRespDTO>(0L, Collections.emptyList()));
} }
List<Journal> pageList = journalService.orderByField(objectIds); List<Journal> pageList = journalService.orderByField(objectIds);
Set<String> journalCollectSet = objectIds.isEmpty() ? Collections.emptySet() : new HashSet<>(objectIds); Set<String> journalCollectSet = new HashSet<>(objectIds);
List<JournalRespDTO> list = pageList.stream().map(a -> getJournalRespDTO(a, journalCollectSet)) List<JournalRespDTO> list = pageList.stream().map(a -> getJournalRespDTO(a, journalCollectSet))
.collect(Collectors.toList()); .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));
@ -157,9 +161,10 @@ public class JournalController {
@PathVariable @VerifyParam(required = true) String id) { @PathVariable @VerifyParam(required = true) String id) {
UserLoginDto user = jwtUtil.getUserLoginDto(authorization); UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
Journal journal = journalService.findById(id); Journal journal = journalService.findById(id);
Set<String> journalCollectSet = null == user ? Collections.emptySet() JournalRespDTO journalRespDTO=getJournalRespDTO(journal, Collections.emptySet());
: userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.JOURNAL); boolean isCollect=null == user ? false:userCollectInfoService.isCollect(user.getUserId(),id, CollectTypeEnum.JOURNAL);
return Result.success(getJournalRespDTO(journal, journalCollectSet)); journalRespDTO.setHaveCollect(isCollect);
return Result.success(journalRespDTO);
} }
private JournalRespDTO getJournalRespDTO(Journal journal, Set<String> journalCollectSet) { private JournalRespDTO getJournalRespDTO(Journal journal, Set<String> journalCollectSet) {
@ -180,9 +185,8 @@ public class JournalController {
journalRespDTO.setContent(Arrays.stream(poem.getContent()).collect(Collectors.joining("\r\n"))); journalRespDTO.setContent(Arrays.stream(poem.getContent()).collect(Collectors.joining("\r\n")));
} else { } else {
journalRespDTO.setContent(content); journalRespDTO.setContent(content);
int index = RandomSource.numberSource().randomInt(0, EDITOR.length);
String editor = EDITOR[index]; journalRespDTO.setEditor(getEditor(journal.getUserName()));
journalRespDTO.setEditor(editor);
} }
/** /**
@ -220,6 +224,10 @@ public class JournalController {
return journalRespDTO; return journalRespDTO;
} }
private String getEditor(String userName) {
return StringTools.isEmpty(userName)?EDITOR[RandomSource.numberSource().randomInt(0, EDITOR.length)]:userName;
}
private List<String> getTags(String journalNo) { private List<String> getTags(String journalNo) {
return journalTagMap.computeIfAbsent(journalNo, a -> getTags()); return journalTagMap.computeIfAbsent(journalNo, a -> getTags());
} }

@ -4,8 +4,7 @@ import com.luoo.music.dto.response.SongRespDTO;
import com.luoo.music.pojo.JournalSong; import com.luoo.music.pojo.JournalSong;
import com.luoo.music.service.JournalService; import com.luoo.music.service.JournalService;
import com.luoo.music.service.JournalSongService; import com.luoo.music.service.JournalSongService;
import com.luoo.music.service.UserCollectService; import com.luoo.music.service.UserCollectInfoService;
import annotation.GlobalInterceptor; import annotation.GlobalInterceptor;
import annotation.VerifyParam; import annotation.VerifyParam;
import api.PageResult; import api.PageResult;
@ -21,6 +20,8 @@ import io.swagger.annotations.ApiOperation;
import util.JwtUtil; import util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Collections; import java.util.Collections;
@ -35,7 +36,7 @@ import java.util.stream.Collectors;
@RequestMapping("/song") @RequestMapping("/song")
public class SongController { public class SongController {
@Autowired @Autowired
private UserCollectService userCollectService; private UserCollectInfoService userCollectInfoService;
@Autowired @Autowired
private JournalSongService journalSongService; private JournalSongService journalSongService;
@Autowired @Autowired
@ -64,8 +65,9 @@ public class SongController {
return Result.unauthorized(null); return Result.unauthorized(null);
} }
List<JournalSong> songs = journalSongService.findByVolid(journalNo); List<JournalSong> songs = journalSongService.findByVolid(journalNo);
List<String> ids=songs.stream().map(JournalSong::getId).collect(Collectors.toList());
Set<String> songCollectSet = null == user ? Collections.emptySet() Set<String> songCollectSet = null == user ? Collections.emptySet()
: userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.SONG);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)) List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet))
.collect(Collectors.toList()); .collect(Collectors.toList());
return Result.success(results); return Result.success(results);
@ -86,13 +88,15 @@ public class SongController {
@PathVariable @VerifyParam(required = true) String userId, @PathVariable @VerifyParam(required = true) String userId,
@PathVariable @VerifyParam(required = true) Integer pageNum, @PathVariable @VerifyParam(required = true) Integer pageNum,
@PathVariable @VerifyParam(required = true) Integer pageSize) { @PathVariable @VerifyParam(required = true) Integer pageSize) {
List<String> objectIds = userCollectService.getCollectList(userId, pageNum, pageSize, CollectTypeEnum.SONG);
Pageable pageable = PageRequest.of(pageNum-1,pageSize);
List<String> objectIds=userCollectInfoService.findByUserIdAndCollectType(userId, CollectTypeEnum.SONG, pageable);
if (objectIds.isEmpty()) { if (objectIds.isEmpty()) {
return Result.success(new PageResult<SongRespDTO>(0L, Collections.emptyList())); return Result.success(new PageResult<SongRespDTO>(0L, Collections.emptyList()));
} }
List<JournalSong> songs = journalSongService.orderByField(objectIds); List<JournalSong> songs = journalSongService.orderByField(objectIds);
Set<String> songCollectSet = objectIds.isEmpty() ? Collections.emptySet() : new HashSet<>(objectIds); Set<String> songCollectSet = new HashSet<>(objectIds);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)) List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet))
.collect(Collectors.toList()); .collect(Collectors.toList());
return Result.success(new PageResult<SongRespDTO>(Long.valueOf(results.size()), results)); return Result.success(new PageResult<SongRespDTO>(Long.valueOf(results.size()), results));
@ -107,8 +111,11 @@ public class SongController {
@PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { @PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) {
List<JournalSong> songs = journalSongService.random(limit); List<JournalSong> songs = journalSongService.random(limit);
UserLoginDto user = jwtUtil.getUserLoginDto(authorization); UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
List<String> ids=songs.stream().map(JournalSong::getId).collect(Collectors.toList());
Set<String> songCollectSet = null == user ? Collections.emptySet() Set<String> songCollectSet = null == user ? Collections.emptySet()
: userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.SONG);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)) List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet))
.collect(Collectors.toList()); .collect(Collectors.toList());
return Result.success(results); return Result.success(results);
@ -121,9 +128,11 @@ public class SongController {
@PathVariable @VerifyParam(required = true) String id) { @PathVariable @VerifyParam(required = true) String id) {
UserLoginDto user = jwtUtil.getUserLoginDto(authorization); UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
JournalSong song = journalSongService.findById(id); JournalSong song = journalSongService.findById(id);
Set<String> songCollectSet = null == user ? Collections.emptySet()
: userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); SongRespDTO songRespDTO=getSongRespDTO(song, Collections.emptySet());
return Result.success(getSongRespDTO(song, songCollectSet)); boolean isCollect=null == user ? false:userCollectInfoService.isCollect(user.getUserId(),id, CollectTypeEnum.SONG);
songRespDTO.setHaveCollect(isCollect);
return Result.success(songRespDTO);
} }
private SongRespDTO getSongRespDTO(JournalSong song, Set<String> songCollectSet) { private SongRespDTO getSongRespDTO(JournalSong song, Set<String> songCollectSet) {

@ -1,10 +0,0 @@
package com.luoo.music.dao;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.luoo.music.pojo.UserCollect;
public interface UserCollectDao extends MongoRepository<UserCollect, String> {
}

@ -0,0 +1,14 @@
package com.luoo.music.dao;
import java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.luoo.music.pojo.UserCollectInfo;
public interface UserCollectInfoDao extends MongoRepository<UserCollectInfo, String> {
List<UserCollectInfo> findByUserIdAndCollectTypeOrderByCreateTimeDesc(String userId, Integer collectType,
Pageable pageable);
}

@ -0,0 +1,43 @@
package com.luoo.music.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;
@Data
public class UserCollectInfo implements Serializable {
private static final long serialVersionUID = 1L;
/**
* ID
*/
@Id
private String _id;
/**
* ID
*/
private String userId;
/**
* ID ID,ID
*/
private String objectId;
/**
* 0:,1:
*/
private Integer collectType;
/**
*
*/
private Boolean isMutualFans;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
}

@ -0,0 +1,47 @@
package com.luoo.music.service;
import enums.CollectTypeEnum;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import com.luoo.music.dao.UserCollectInfoDao;
import com.luoo.music.pojo.UserCollectInfo;
@Service
public class UserCollectInfoService {
@Autowired
private UserCollectInfoDao userCollectInfoDao;
@Autowired
private MongoTemplate mongoTemplate;
public List<String> findByUserIdAndCollectType(String userId, CollectTypeEnum collectTypeEnum, Pageable pageable) {
return userCollectInfoDao.findByUserIdAndCollectTypeOrderByCreateTimeDesc(userId,
collectTypeEnum.getType(), pageable).stream().map(UserCollectInfo::getObjectId).collect(Collectors.toList());
}
public Set<String> getCollectSet(String userId, List<String> objectIds, CollectTypeEnum collectTypeEnum) {
Criteria criteria = Criteria.where("userId").is(userId).and("objectId").in(objectIds).and("collectType")
.is(collectTypeEnum.getType());
Query query = new Query(criteria);
List<UserCollectInfo> userCollectInfos = mongoTemplate.find(query, UserCollectInfo.class);
return userCollectInfos.isEmpty() ? Collections.emptySet() : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet());
}
public boolean isCollect(String userId, String objectId, CollectTypeEnum collectTypeEnum) {
Criteria criteria = Criteria.where("userId").is(userId).and("objectId").is(objectId).and("collectType")
.is(collectTypeEnum.getType());
Query query = new Query(criteria);
return mongoTemplate.exists(query, UserCollectInfo.class);
}
}

@ -1,61 +0,0 @@
package com.luoo.music.service;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.luoo.music.dao.UserCollectDao;
import com.luoo.music.pojo.UserCollect;
import enums.CollectTypeEnum;
@Service
public class UserCollectService {
@Autowired
private UserCollectDao userCollectDao;
public Set<String> getCollectSet(String userId, CollectTypeEnum collectTypeEnum) {
List<String> collectList = getCollectList(userId, null, null, collectTypeEnum);
if (collectList.isEmpty()) {
return Collections.emptySet();
}
return new HashSet<>(collectList);
}
public List<String> getCollectList(String userId, Integer pageNum, Integer pageSize,
CollectTypeEnum collectTypeEnum) {
if (null == userId) {
return Collections.emptyList();
}
Optional<UserCollect> optional = userCollectDao.findById(userId);
if (!optional.isPresent()) {
return Collections.emptyList();
}
UserCollect userCollect = optional.get();
switch (collectTypeEnum) {
case SONG:
return getPageResult(pageNum, pageSize, userCollect.getSongs());
case JOURNAL:
return getPageResult(pageNum, pageSize, userCollect.getJournals());
default:
return Collections.emptyList();
}
}
private List<String> getPageResult(Integer pageNum, Integer pageSize, LinkedList<String> objectIds) {
if (null == pageNum || null == pageSize) {
return objectIds;
}
int end = pageNum * pageSize;
if (pageNum > 0 && pageSize > 0 && end <= objectIds.size()) {
return objectIds.subList((pageNum - 1) * pageSize, end);
}
return objectIds;
}
}
Loading…
Cancel
Save