From 2a5259a9fe9edd86f341affff301ad87db1c0a50 Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 5 Feb 2024 18:48:18 +0800 Subject: [PATCH] 1.update collect model for journal and music --- .../music/controller/JournalController.java | 34 +++++++---- .../luoo/music/controller/SongController.java | 31 ++++++---- .../com/luoo/music/dao/UserCollectDao.java | 10 --- .../luoo/music/dao/UserCollectInfoDao.java | 14 +++++ .../com/luoo/music/pojo/UserCollectInfo.java | 43 +++++++++++++ .../music/service/UserCollectInfoService.java | 47 ++++++++++++++ .../music/service/UserCollectService.java | 61 ------------------- 7 files changed, 145 insertions(+), 95 deletions(-) delete mode 100644 luoo_music/src/main/java/com/luoo/music/dao/UserCollectDao.java create mode 100644 luoo_music/src/main/java/com/luoo/music/dao/UserCollectInfoDao.java create mode 100644 luoo_music/src/main/java/com/luoo/music/pojo/UserCollectInfo.java create mode 100644 luoo_music/src/main/java/com/luoo/music/service/UserCollectInfoService.java delete mode 100644 luoo_music/src/main/java/com/luoo/music/service/UserCollectService.java diff --git a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java index 07b26ae..37e81d8 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java @@ -29,6 +29,8 @@ import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; 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.aggregation.Aggregation; 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.Tag; import com.luoo.music.service.JournalService; -import com.luoo.music.service.UserCollectService; - +import com.luoo.music.service.UserCollectInfoService; import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.PageResult; @@ -64,7 +65,8 @@ import util.StringTools; @RequestMapping("/journal") public class JournalController { @Autowired - private UserCollectService userCollectService; + private UserCollectInfoService userCollectInfoService; + @Autowired private JournalService journalService; @@ -120,8 +122,9 @@ public class JournalController { queryReq.setPageSize(10); } Page pageList = journalService.queryPage(queryReq); + List ids=pageList.stream().map(Journal::getId).collect(Collectors.toList()); Set journalCollectSet = null == user ? Collections.emptySet() - : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.JOURNAL); + : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL); List list = pageList.stream().map(a -> getJournalRespDTO(a, journalCollectSet)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); @@ -138,12 +141,13 @@ public class JournalController { @PathVariable @VerifyParam(required = true) String userId, @PathVariable @VerifyParam(required = true) Integer pageNum, @PathVariable @VerifyParam(required = true) Integer pageSize) { - List objectIds = userCollectService.getCollectList(userId, pageNum, pageSize, CollectTypeEnum.JOURNAL); + Pageable pageable = PageRequest.of(pageNum-1,pageSize); + List objectIds=userCollectInfoService.findByUserIdAndCollectType(userId, CollectTypeEnum.JOURNAL, pageable); if (objectIds.isEmpty()) { return Result.success(new PageResult(0L, Collections.emptyList())); } List pageList = journalService.orderByField(objectIds); - Set journalCollectSet = objectIds.isEmpty() ? Collections.emptySet() : new HashSet<>(objectIds); + Set journalCollectSet = new HashSet<>(objectIds); List list = pageList.stream().map(a -> getJournalRespDTO(a, journalCollectSet)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); @@ -157,9 +161,10 @@ public class JournalController { @PathVariable @VerifyParam(required = true) String id) { UserLoginDto user = jwtUtil.getUserLoginDto(authorization); Journal journal = journalService.findById(id); - Set journalCollectSet = null == user ? Collections.emptySet() - : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.JOURNAL); - return Result.success(getJournalRespDTO(journal, journalCollectSet)); + JournalRespDTO journalRespDTO=getJournalRespDTO(journal, Collections.emptySet()); + boolean isCollect=null == user ? false:userCollectInfoService.isCollect(user.getUserId(),id, CollectTypeEnum.JOURNAL); + journalRespDTO.setHaveCollect(isCollect); + return Result.success(journalRespDTO); } private JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet) { @@ -171,7 +176,7 @@ public class JournalController { journalRespDTO.setDate(getEditDate(journal)); journalRespDTO.setHaveCollect(journalCollectSet.contains(journalRespDTO.getId())); journalRespDTO.setIpLocation("广东"); - + journalRespDTO.setTags(getTags(journalRespDTO.getJournalNo())); String content = journal.getContent(); if (StringTools.isEmpty(content)) { @@ -180,9 +185,8 @@ public class JournalController { journalRespDTO.setContent(Arrays.stream(poem.getContent()).collect(Collectors.joining("\r\n"))); } else { journalRespDTO.setContent(content); - int index = RandomSource.numberSource().randomInt(0, EDITOR.length); - String editor = EDITOR[index]; - journalRespDTO.setEditor(editor); + + journalRespDTO.setEditor(getEditor(journal.getUserName())); } /** @@ -220,6 +224,10 @@ public class JournalController { return journalRespDTO; } + private String getEditor(String userName) { + return StringTools.isEmpty(userName)?EDITOR[RandomSource.numberSource().randomInt(0, EDITOR.length)]:userName; + } + private List getTags(String journalNo) { return journalTagMap.computeIfAbsent(journalNo, a -> getTags()); } diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java index 68c8752..70c44fe 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java @@ -4,8 +4,7 @@ import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.pojo.JournalSong; import com.luoo.music.service.JournalService; import com.luoo.music.service.JournalSongService; -import com.luoo.music.service.UserCollectService; - +import com.luoo.music.service.UserCollectInfoService; import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.PageResult; @@ -21,6 +20,8 @@ import io.swagger.annotations.ApiOperation; import util.JwtUtil; 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 java.util.Collections; @@ -35,7 +36,7 @@ import java.util.stream.Collectors; @RequestMapping("/song") public class SongController { @Autowired - private UserCollectService userCollectService; + private UserCollectInfoService userCollectInfoService; @Autowired private JournalSongService journalSongService; @Autowired @@ -64,8 +65,9 @@ public class SongController { return Result.unauthorized(null); } List songs = journalSongService.findByVolid(journalNo); + List ids=songs.stream().map(JournalSong::getId).collect(Collectors.toList()); Set songCollectSet = null == user ? Collections.emptySet() - : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); + : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.SONG); List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)) .collect(Collectors.toList()); return Result.success(results); @@ -86,13 +88,15 @@ public class SongController { @PathVariable @VerifyParam(required = true) String userId, @PathVariable @VerifyParam(required = true) Integer pageNum, @PathVariable @VerifyParam(required = true) Integer pageSize) { - List objectIds = userCollectService.getCollectList(userId, pageNum, pageSize, CollectTypeEnum.SONG); - if(objectIds.isEmpty()) { + + Pageable pageable = PageRequest.of(pageNum-1,pageSize); + List objectIds=userCollectInfoService.findByUserIdAndCollectType(userId, CollectTypeEnum.SONG, pageable); + if (objectIds.isEmpty()) { return Result.success(new PageResult(0L, Collections.emptyList())); } List songs = journalSongService.orderByField(objectIds); - Set songCollectSet = objectIds.isEmpty() ? Collections.emptySet() : new HashSet<>(objectIds); + Set songCollectSet = new HashSet<>(objectIds); List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(results.size()), results)); @@ -107,8 +111,11 @@ public class SongController { @PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { List songs = journalSongService.random(limit); UserLoginDto user = jwtUtil.getUserLoginDto(authorization); + + List ids=songs.stream().map(JournalSong::getId).collect(Collectors.toList()); Set songCollectSet = null == user ? Collections.emptySet() - : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); + : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.SONG); + List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)) .collect(Collectors.toList()); return Result.success(results); @@ -121,9 +128,11 @@ public class SongController { @PathVariable @VerifyParam(required = true) String id) { UserLoginDto user = jwtUtil.getUserLoginDto(authorization); JournalSong song = journalSongService.findById(id); - Set songCollectSet = null == user ? Collections.emptySet() - : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); - return Result.success(getSongRespDTO(song, songCollectSet)); + + SongRespDTO songRespDTO=getSongRespDTO(song, Collections.emptySet()); + 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 songCollectSet) { diff --git a/luoo_music/src/main/java/com/luoo/music/dao/UserCollectDao.java b/luoo_music/src/main/java/com/luoo/music/dao/UserCollectDao.java deleted file mode 100644 index 440eb7b..0000000 --- a/luoo_music/src/main/java/com/luoo/music/dao/UserCollectDao.java +++ /dev/null @@ -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 { -} diff --git a/luoo_music/src/main/java/com/luoo/music/dao/UserCollectInfoDao.java b/luoo_music/src/main/java/com/luoo/music/dao/UserCollectInfoDao.java new file mode 100644 index 0000000..0002bb8 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dao/UserCollectInfoDao.java @@ -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 { + List findByUserIdAndCollectTypeOrderByCreateTimeDesc(String userId, Integer collectType, + Pageable pageable); +} diff --git a/luoo_music/src/main/java/com/luoo/music/pojo/UserCollectInfo.java b/luoo_music/src/main/java/com/luoo/music/pojo/UserCollectInfo.java new file mode 100644 index 0000000..809a441 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/pojo/UserCollectInfo.java @@ -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; +} diff --git a/luoo_music/src/main/java/com/luoo/music/service/UserCollectInfoService.java b/luoo_music/src/main/java/com/luoo/music/service/UserCollectInfoService.java new file mode 100644 index 0000000..3501544 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/service/UserCollectInfoService.java @@ -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 findByUserIdAndCollectType(String userId, CollectTypeEnum collectTypeEnum, Pageable pageable) { + return userCollectInfoDao.findByUserIdAndCollectTypeOrderByCreateTimeDesc(userId, + collectTypeEnum.getType(), pageable).stream().map(UserCollectInfo::getObjectId).collect(Collectors.toList()); + } + + public Set getCollectSet(String userId, List 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 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); + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/service/UserCollectService.java b/luoo_music/src/main/java/com/luoo/music/service/UserCollectService.java deleted file mode 100644 index 35058b6..0000000 --- a/luoo_music/src/main/java/com/luoo/music/service/UserCollectService.java +++ /dev/null @@ -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 getCollectSet(String userId, CollectTypeEnum collectTypeEnum) { - List collectList = getCollectList(userId, null, null, collectTypeEnum); - if (collectList.isEmpty()) { - return Collections.emptySet(); - } - return new HashSet<>(collectList); - } - - public List getCollectList(String userId, Integer pageNum, Integer pageSize, - CollectTypeEnum collectTypeEnum) { - if (null == userId) { - return Collections.emptyList(); - } - Optional 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 getPageResult(Integer pageNum, Integer pageSize, LinkedList 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; - } -}