From 2be18b3fd4c8136c82a30ad48fc7da42fe82c545 Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 26 Jan 2024 00:55:34 +0800 Subject: [PATCH] 1.modify UserCollect model --- .../music/controller/JournalController.java | 33 +++------- .../luoo/music/controller/SongController.java | 38 +++++------ .../com/luoo/music/dao/UserCollectDao.java | 1 - .../java/com/luoo/music/pojo/UserCollect.java | 23 ++----- .../luoo/user/controller/MyController.java | 29 ++++----- .../controller/UserCollectController.java | 20 ++++-- .../com/luoo/user/dao/UserCollectDao.java | 11 ---- .../java/com/luoo/user/pojo/UserCollect.java | 23 ++----- .../luoo/user/service/UserCollectService.java | 64 ++++++++++--------- 9 files changed, 93 insertions(+), 149 deletions(-) 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 e692c94..cb040e6 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 @@ -4,21 +4,18 @@ import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.List; -import java.util.Map; +import java.util.Optional; 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; import com.luoo.music.dto.response.JournalRespDTO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; -import org.springframework.util.StopWatch; import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Article; +import com.luoo.music.pojo.UserCollect; import com.luoo.music.service.ArticleService; import annotation.GlobalInterceptor; @@ -27,7 +24,6 @@ import api.PageResult; import api.Result; import constants.Constants; import dto.UserLoginDto; -import enums.CollectTypeEnum; import enums.DateTimePatternEnum; import io.swagger.annotations.ApiOperation; import util.DateUtil; @@ -63,31 +59,18 @@ public class JournalController { queryReq.setPageSize(10); } Page
pageList = articleService.queryPage(queryReq); - List collectIds = getCollectIds(pageList, user, CollectTypeEnum.JOURNAL.getType()); - Set journalCollectSet = getCollectSet(collectIds); + Set journalCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId()); List list = pageList.stream().map(a -> getArticleRespDTO(a, journalCollectSet)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); } - private Set getCollectSet(List collectIds) { - if (collectIds.isEmpty()) { - return Collections.emptySet(); + private Set getCollectSet(String userId) { + Optional optional=userCollectDao.findById(userId); + if(optional.isPresent()) { + return new HashSet<>(optional.get().getJournals()); } - Set set = new HashSet<>(); - userCollectDao.findAllById(collectIds).forEach(u -> set.add(u.getObjectId())); - return set; - } - - private List getCollectIds(Page
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; + return Collections.emptySet(); } private JournalRespDTO getArticleRespDTO(Article article, Set journalCollectSet) { 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 74dcb1d..dbc1543 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 @@ -1,10 +1,10 @@ package com.luoo.music.controller; -import com.luoo.music.client.UserClient; import com.luoo.music.dao.UserCollectDao; import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Song; +import com.luoo.music.pojo.UserCollect; import com.luoo.music.service.ArticleService; import com.luoo.music.service.SongService; @@ -14,7 +14,6 @@ import api.PageResult; import api.Result; import constants.Constants; import dto.UserLoginDto; -import enums.CollectTypeEnum; import enums.VerifyRegexEnum; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -29,6 +28,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; @@ -65,12 +65,19 @@ public class SongController { return Result.unauthorized(null); } List songs = songService.findByVolid(journalNo); - List collectIds = getCollectIds(songs, user, CollectTypeEnum.SONG.getType()); - Set songCollectSet = getCollectSet(collectIds); + Set songCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId()); List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList()); return Result.success(results); } + private Set getCollectSet(String userId) { + Optional optional=userCollectDao.findById(userId); + if(optional.isPresent()) { + return new HashSet<>(optional.get().getSongs()); + } + return Collections.emptySet(); + } + private boolean isLatest10(String journalNo) { return articleService.isLatest10(journalNo); } @@ -80,9 +87,12 @@ public class SongController { @GetMapping("/random/{limit}") @GlobalInterceptor public Result> random( + @RequestHeader(value = "token", required = false) String token, @PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { List songs = songService.random(limit); - List results = songs.stream().map(s -> getSongRespDTO(s, Collections.emptySet())).collect(Collectors.toList()); + UserLoginDto user = jwtUtil.getUserLoginDto(token); + Set songCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId()); + List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList()); return Result.success(results); } @@ -101,24 +111,6 @@ public class SongController { return songRespDTO; } - private Set getCollectSet(List collectIds) { - if (collectIds.isEmpty()) { - return Collections.emptySet(); - } - Set set = new HashSet<>(); - userCollectDao.findAllById(collectIds).forEach(u -> set.add(u.getObjectId())); - return set; - } - - private List getCollectIds(List 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查询 * 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 index 9fc3573..440eb7b 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/UserCollectDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/UserCollectDao.java @@ -7,5 +7,4 @@ import com.luoo.music.pojo.UserCollect; public interface UserCollectDao extends MongoRepository { - public long countByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType); } diff --git a/luoo_music/src/main/java/com/luoo/music/pojo/UserCollect.java b/luoo_music/src/main/java/com/luoo/music/pojo/UserCollect.java index d3ae2f4..dbec8a7 100644 --- a/luoo_music/src/main/java/com/luoo/music/pojo/UserCollect.java +++ b/luoo_music/src/main/java/com/luoo/music/pojo/UserCollect.java @@ -3,13 +3,9 @@ package com.luoo.music.pojo; import lombok.Getter; import lombok.Setter; -import java.util.Date; - -import com.fasterxml.jackson.annotation.JsonFormat; +import java.util.LinkedList; import org.springframework.data.annotation.Id; -import org.springframework.format.annotation.DateTimeFormat; - import java.io.Serializable; /** @@ -19,16 +15,10 @@ import java.io.Serializable; @Setter public class UserCollect implements Serializable { private static final long serialVersionUID = 1L; - - /** - * 收藏ID - */ - @Id - private String collectId; - /** * 用户ID */ + @Id private String userId; /** @@ -41,10 +31,7 @@ public class UserCollect implements Serializable { */ private Integer collectType; - /** - * 收藏时间 - */ - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") - @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") - private Date collectTime; + private LinkedList journals; + + private LinkedList songs; } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java index a4c18af..af26363 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/MyController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/MyController.java @@ -8,10 +8,14 @@ import java.nio.file.Paths; import java.util.Date; import java.util.EnumMap; import java.util.List; +import java.util.Optional; import java.util.Set; +import com.luoo.user.pojo.UserCollect; import com.luoo.user.pojo.UserCollectInfo; import com.luoo.user.service.UserCollectInfoService; +import com.luoo.user.service.UserCollectService; + import org.apache.commons.io.IOUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -63,8 +67,8 @@ public class MyController extends BaseController{ @Autowired private IdWorker idWorker; @Autowired - private UserCollectInfoService userCollectInfoService; -// private UserCollectService userCollectService; +// private UserCollectInfoService userCollectInfoService; + private UserCollectService userCollectService; public static String UPLOAD_DIRECTORY = "user/avatar/"; @@ -72,7 +76,7 @@ public class MyController extends BaseController{ @ApiOperation(value = "1.获取个人信息", notes = "游客无法获取个人信息") @GetMapping("/getUserInfo") @GlobalInterceptor - public Result getUserInfo(@RequestHeader(value = "token", required = false) String token) { + public Result getUserInfo(@RequestHeader(value = "token", required = false) String token) { UserLoginDto userLoginDto = getUserLoginDto(token); if (null == userLoginDto) { return Result.unauthorized(null); @@ -97,19 +101,12 @@ public class MyController extends BaseController{ if(null!=userRespDTO.getAvatar()) { userRespDTO.setAvatar(Constants.RESOURCE_PREFIX+userRespDTO.getAvatar()); } - UserCollectInfo userCollectInfo = userCollectInfoService.findByUserId(userLoginDto.getUserId()); - if(null!=userCollectInfo) { - Set songSet =userCollectInfo.getSongSet(); - Set journalSet = userCollectInfo.getJournalSet(); - userRespDTO.setJournalSet(journalSet); - userRespDTO.setSongSet(songSet); - if(null != songSet){ - userRespDTO.setSongCount(songSet.size()); - } - - if(null != journalSet) { - userRespDTO.setJournalCount(journalSet.size()); - } + + Optional optional=userCollectService.findByUserId(userLoginDto.getUserId()); + if(optional.isPresent()) { + UserCollect userCollect=optional.get(); + userRespDTO.setSongCount(userCollect.getSongs().size()); + userRespDTO.setJournalCount(userCollect.getJournals().size()); } if(null!=user.getBirthday()) { diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java index 941d580..1efcb9f 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserCollectController.java @@ -22,10 +22,13 @@ public class UserCollectController { @Autowired private UserCollectService userCollectService; - @ApiOperation(value = "1.喜欢歌曲/收藏期刊/关注/黑名单/点赞") + //@ApiOperation(value = "1.喜欢歌曲/收藏期刊/关注/黑名单/点赞") + @ApiOperation(value = "1.喜欢歌曲/收藏期刊") @ApiImplicitParams({ - @ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊/关注某用户/某用户列入黑名单/点赞评论的id", required = true), - @ApiImplicitParam(name = "collectType", value = "0:歌曲,1:期刊,2:关注,3:黑名单,4:点赞", required = true) }) + //@ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊/关注某用户/某用户列入黑名单/点赞评论的id", required = true), + //@ApiImplicitParam(name = "collectType", value = "0:歌曲,1:期刊,2:关注,3:黑名单,4:点赞", required = true) }) + @ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊的id", required = true), + @ApiImplicitParam(name = "collectType", value = "0:歌曲,1:期刊", required = true) }) @PostMapping("/addCollect") @GlobalInterceptor(checkLogin = true) public Result addCollect(@RequestHeader(value = "token", required = false) String token, @@ -36,17 +39,20 @@ public class UserCollectController { return Result.success(); } - @ApiOperation(value = "2.取消 喜欢歌曲/收藏期刊/关注/黑名单/点赞") + //@ApiOperation(value = "2.取消 喜欢歌曲/收藏期刊/关注/黑名单/点赞") + @ApiOperation(value = "2.取消 喜欢歌曲/收藏期刊") @ApiImplicitParams({ - @ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊/关注某用户/某用户列入黑名单/点赞评论的id", required = true), - @ApiImplicitParam(name = "collectType", value = "0:歌曲,1:期刊,2:关注,3:黑名单,4:点赞", required = true) }) + //@ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊/关注某用户/某用户列入黑名单/点赞评论的id", required = true), + //@ApiImplicitParam(name = "collectType", value = "0:歌曲,1:期刊,2:关注,3:黑名单,4:点赞", required = true) }) + @ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊的id", required = true), + @ApiImplicitParam(name = "collectType", value = "0:歌曲,1:期刊", required = true) }) @DeleteMapping("/cancelCollect") @GlobalInterceptor(checkLogin = true) public Result cancelCollect(@RequestHeader(value = "token", required = false) String token, @VerifyParam(required = true) @RequestParam("objectId") String objectId, @VerifyParam(required = true) @RequestParam("collectType") Integer collectType) { UserLoginDto loginDto = jwtUtil.getUserLoginDto(token); - userCollectService.deleteUserCollectByUserIdAndObjectIdAndCollectType(loginDto.getUserId(), objectId, + userCollectService.deleteByUserIdAndObjectIdAndCollectType(loginDto.getUserId(), objectId, collectType); return Result.success(); } diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserCollectDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserCollectDao.java index 74ba5e1..1ddc413 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/UserCollectDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserCollectDao.java @@ -1,20 +1,9 @@ package com.luoo.user.dao; -import java.util.List; - -import org.springframework.data.jpa.repository.Query; import org.springframework.data.mongodb.repository.MongoRepository; -import com.luoo.user.dto.UserCollectCount; import com.luoo.user.pojo.UserCollect; public interface UserCollectDao extends MongoRepository { - public UserCollect findByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType); - public long deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType); - //@Query(value = "select NumberInt(collectType),count(*) from common.userCollect where userId=?1 group by collectType", nativeQuery = true) - //public List countByUserIdAndGroupByCollectType(String userId); - - public int countByObjectIdAndCollectType(String objectId, Integer collectType); - public int countByUserIdAndCollectType(String userId, Integer collectType); } diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserCollect.java b/luoo_user/src/main/java/com/luoo/user/pojo/UserCollect.java index 9b75096..d33140d 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/UserCollect.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/UserCollect.java @@ -3,13 +3,9 @@ package com.luoo.user.pojo; import lombok.Getter; import lombok.Setter; -import java.util.Date; - -import com.fasterxml.jackson.annotation.JsonFormat; +import java.util.LinkedList; import org.springframework.data.annotation.Id; -import org.springframework.format.annotation.DateTimeFormat; - import java.io.Serializable; /** @@ -19,16 +15,10 @@ import java.io.Serializable; @Setter public class UserCollect implements Serializable { private static final long serialVersionUID = 1L; - - /** - * 收藏ID - */ - @Id - private String collectId; - /** * 用户ID */ + @Id private String userId; /** @@ -41,10 +31,7 @@ public class UserCollect implements Serializable { */ private Integer collectType; - /** - * 收藏时间 - */ - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") - @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") - private Date collectTime; + private LinkedList journals; + + private LinkedList songs; } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java b/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java index 8b355a3..f83b574 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserCollectService.java @@ -2,6 +2,7 @@ package com.luoo.user.service; import java.util.Date; import java.util.EnumMap; +import java.util.LinkedList; import java.util.List; import java.util.Optional; @@ -15,6 +16,7 @@ import com.luoo.user.pojo.UserCollect; import api.StatusCode; import enums.CollectTypeEnum; +import enums.CollectTypeEnum.*; import exception.BizException; @Service @@ -22,48 +24,50 @@ public class UserCollectService { @Autowired private UserCollectDao userCollectDao; public void saveCollect(String userId, String objectId, Integer collectType) { + addOrRemove(userId,objectId,collectType,true); + } + + private void addOrRemove(String userId, String objectId, Integer collectType, boolean isAdd) { CollectTypeEnum collectTypeEnum = CollectTypeEnum.getByType(collectType); if (null == collectTypeEnum) { throw new BizException(StatusCode.VALIDATE_FAILED); } - String collectId=userId+"_"+objectId+"_"+collectType; - Optional dbCollect = userCollectDao.findById(collectId); + UserCollect userCollect=null; + Optional dbCollect = userCollectDao.findById(userId); if (dbCollect.isPresent()) { - return; + userCollect=dbCollect.get(); + }else { + userCollect = new UserCollect(); + userCollect.setUserId(userId); + userCollect.setJournals(new LinkedList<>()); + userCollect.setSongs(new LinkedList<>()); + } + switch (collectTypeEnum) { + case SONG: + addOrRemove(isAdd,userCollect.getSongs(),objectId); + break; + case JOURNAL: + addOrRemove(isAdd,userCollect.getJournals(),objectId); + default: + break; } - UserCollect userCollect = new UserCollect(); - userCollect.setCollectId(collectId); - userCollect.setUserId(userId); - userCollect.setCollectTime(new Date()); - userCollect.setObjectId(objectId); - userCollect.setCollectType(collectType); userCollectDao.save(userCollect); } - public void deleteUserCollectByUserIdAndObjectIdAndCollectType(String userId, String objectId, - Integer collectType) { - String collectId=userId+"_"+objectId+"_"+collectType; - userCollectDao.deleteById(collectId); + private void addOrRemove(boolean isAdd, LinkedList list, String objectId) { + if(isAdd) { + list.addFirst(objectId); + }else{ + list.removeFirstOccurrence(objectId); + } } - public EnumMap getUserCollectTypeMap(String userId) { - EnumMap userCollectTypeMap=new EnumMap<>(CollectTypeEnum.class); - /* - * List - * userCollectCounts=userCollectDao.countByUserIdAndGroupByCollectType(userId); - * - * userCollectCounts.forEach(u->{ CollectTypeEnum collectTypeEnum = - * CollectTypeEnum.getByType(u.getCollectType()); - * userCollectTypeMap.put(collectTypeEnum, u.getCount()); }); - */ - return userCollectTypeMap; - } - - public int getFansCount(String userId) { - return userCollectDao.countByObjectIdAndCollectType(userId,CollectTypeEnum.FOLLOW.getType()); + public void deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId, + Integer collectType) { + addOrRemove(userId,objectId,collectType,false); } - public int getCount(String userId, Integer type) { - return userCollectDao.countByUserIdAndCollectType(userId,type); + public Optional findByUserId(String userId) { + return userCollectDao.findById(userId); } }