1.modify UserCollect model

main
Gary 10 months ago
parent 580e31241f
commit 2be18b3fd4

@ -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<Article> pageList = articleService.queryPage(queryReq);
List<String> collectIds = getCollectIds(pageList, user, CollectTypeEnum.JOURNAL.getType());
Set<String> journalCollectSet = getCollectSet(collectIds);
Set<String> journalCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId());
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 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 Set<String> getCollectSet(String userId) {
Optional<UserCollect> optional=userCollectDao.findById(userId);
if(optional.isPresent()) {
return new HashSet<>(optional.get().getJournals());
}
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;
return Collections.emptySet();
}
private JournalRespDTO getArticleRespDTO(Article article, Set<String> journalCollectSet) {

@ -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<Song> songs = songService.findByVolid(journalNo);
List<String> collectIds = getCollectIds(songs, user, CollectTypeEnum.SONG.getType());
Set<String> songCollectSet = getCollectSet(collectIds);
Set<String> songCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId());
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList());
return Result.success(results);
}
private Set<String> getCollectSet(String userId) {
Optional<UserCollect> 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<List<SongRespDTO>> random(
@RequestHeader(value = "token", required = false) String token,
@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, Collections.emptySet())).collect(Collectors.toList());
UserLoginDto user = jwtUtil.getUserLoginDto(token);
Set<String> songCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId());
List<SongRespDTO> 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<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
*

@ -7,5 +7,4 @@ import com.luoo.music.pojo.UserCollect;
public interface UserCollectDao extends MongoRepository<UserCollect, String> {
public long countByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType);
}

@ -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<String> journals;
private LinkedList<String> songs;
}

@ -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<UserRespDTO> 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<UserCollect> 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()) {

@ -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<Void> 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<Void> 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();
}

@ -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<UserCollect, String> {
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<UserCollectCount> countByUserIdAndGroupByCollectType(String userId);
public int countByObjectIdAndCollectType(String objectId, Integer collectType);
public int countByUserIdAndCollectType(String userId, Integer collectType);
}

@ -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<String> journals;
private LinkedList<String> songs;
}

@ -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<UserCollect> dbCollect = userCollectDao.findById(collectId);
UserCollect userCollect=null;
Optional<UserCollect> dbCollect = userCollectDao.findById(userId);
if (dbCollect.isPresent()) {
return;
}
UserCollect userCollect = new UserCollect();
userCollect.setCollectId(collectId);
userCollect=dbCollect.get();
}else {
userCollect = new UserCollect();
userCollect.setUserId(userId);
userCollect.setCollectTime(new Date());
userCollect.setObjectId(objectId);
userCollect.setCollectType(collectType);
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;
}
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<String> list, String objectId) {
if(isAdd) {
list.addFirst(objectId);
}else{
list.removeFirstOccurrence(objectId);
}
public EnumMap<CollectTypeEnum,Long> getUserCollectTypeMap(String userId) {
EnumMap<CollectTypeEnum,Long> userCollectTypeMap=new EnumMap<>(CollectTypeEnum.class);
/*
* List<UserCollectCount>
* 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<UserCollect> findByUserId(String userId) {
return userCollectDao.findById(userId);
}
}

Loading…
Cancel
Save