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.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; 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.dao.UserCollectDao;
import com.luoo.music.dto.request.JournalQueryReq; import com.luoo.music.dto.request.JournalQueryReq;
import com.luoo.music.dto.response.JournalRespDTO; import com.luoo.music.dto.response.JournalRespDTO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Article;
import com.luoo.music.pojo.UserCollect;
import com.luoo.music.service.ArticleService; import com.luoo.music.service.ArticleService;
import annotation.GlobalInterceptor; import annotation.GlobalInterceptor;
@ -27,7 +24,6 @@ import api.PageResult;
import api.Result; import api.Result;
import constants.Constants; import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.CollectTypeEnum;
import enums.DateTimePatternEnum; import enums.DateTimePatternEnum;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import util.DateUtil; import util.DateUtil;
@ -63,31 +59,18 @@ public class JournalController {
queryReq.setPageSize(10); queryReq.setPageSize(10);
} }
Page<Article> pageList = articleService.queryPage(queryReq); Page<Article> pageList = articleService.queryPage(queryReq);
List<String> collectIds = getCollectIds(pageList, user, CollectTypeEnum.JOURNAL.getType()); Set<String> journalCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId());
Set<String> journalCollectSet = getCollectSet(collectIds);
List<JournalRespDTO> list = pageList.stream().map(a -> getArticleRespDTO(a, journalCollectSet)) List<JournalRespDTO> list = pageList.stream().map(a -> getArticleRespDTO(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));
} }
private Set<String> getCollectSet(List<String> collectIds) { private Set<String> getCollectSet(String userId) {
if (collectIds.isEmpty()) { Optional<UserCollect> optional=userCollectDao.findById(userId);
return Collections.emptySet(); if(optional.isPresent()) {
return new HashSet<>(optional.get().getJournals());
} }
Set<String> set = new HashSet<>(); return Collections.emptySet();
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) { private JournalRespDTO getArticleRespDTO(Article article, Set<String> journalCollectSet) {

@ -1,10 +1,10 @@
package com.luoo.music.controller; package com.luoo.music.controller;
import com.luoo.music.client.UserClient;
import com.luoo.music.dao.UserCollectDao; import com.luoo.music.dao.UserCollectDao;
import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.dto.response.SongRespDTO;
import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Article;
import com.luoo.music.pojo.Song; import com.luoo.music.pojo.Song;
import com.luoo.music.pojo.UserCollect;
import com.luoo.music.service.ArticleService; import com.luoo.music.service.ArticleService;
import com.luoo.music.service.SongService; import com.luoo.music.service.SongService;
@ -14,7 +14,6 @@ import api.PageResult;
import api.Result; import api.Result;
import constants.Constants; import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.CollectTypeEnum;
import enums.VerifyRegexEnum; import enums.VerifyRegexEnum;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
@ -29,6 +28,7 @@ import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -65,12 +65,19 @@ public class SongController {
return Result.unauthorized(null); return Result.unauthorized(null);
} }
List<Song> songs = songService.findByVolid(journalNo); List<Song> songs = songService.findByVolid(journalNo);
List<String> collectIds = getCollectIds(songs, user, CollectTypeEnum.SONG.getType()); Set<String> songCollectSet = null==user?Collections.emptySet():getCollectSet(user.getUserId());
Set<String> songCollectSet = getCollectSet(collectIds);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList()); List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)).collect(Collectors.toList());
return Result.success(results); 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) { private boolean isLatest10(String journalNo) {
return articleService.isLatest10(journalNo); return articleService.isLatest10(journalNo);
} }
@ -80,9 +87,12 @@ public class SongController {
@GetMapping("/random/{limit}") @GetMapping("/random/{limit}")
@GlobalInterceptor @GlobalInterceptor
public Result<List<SongRespDTO>> random( public Result<List<SongRespDTO>> random(
@RequestHeader(value = "token", required = false) String token,
@PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { @PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) {
List<Song> songs = songService.random(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); return Result.success(results);
} }
@ -101,24 +111,6 @@ public class SongController {
return songRespDTO; 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 * ID
* *

@ -7,5 +7,4 @@ import com.luoo.music.pojo.UserCollect;
public interface UserCollectDao extends MongoRepository<UserCollect, String> { 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.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.Date; import java.util.LinkedList;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable; import java.io.Serializable;
/** /**
@ -19,16 +15,10 @@ import java.io.Serializable;
@Setter @Setter
public class UserCollect implements Serializable { public class UserCollect implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/**
* ID
*/
@Id
private String collectId;
/** /**
* ID * ID
*/ */
@Id
private String userId; private String userId;
/** /**
@ -41,10 +31,7 @@ public class UserCollect implements Serializable {
*/ */
private Integer collectType; private Integer collectType;
/** private LinkedList<String> journals;
*
*/ private LinkedList<String> songs;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date collectTime;
} }

@ -8,10 +8,14 @@ import java.nio.file.Paths;
import java.util.Date; import java.util.Date;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.Set; import java.util.Set;
import com.luoo.user.pojo.UserCollect;
import com.luoo.user.pojo.UserCollectInfo; import com.luoo.user.pojo.UserCollectInfo;
import com.luoo.user.service.UserCollectInfoService; import com.luoo.user.service.UserCollectInfoService;
import com.luoo.user.service.UserCollectService;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -63,8 +67,8 @@ public class MyController extends BaseController{
@Autowired @Autowired
private IdWorker idWorker; private IdWorker idWorker;
@Autowired @Autowired
private UserCollectInfoService userCollectInfoService; // private UserCollectInfoService userCollectInfoService;
// private UserCollectService userCollectService; private UserCollectService userCollectService;
public static String UPLOAD_DIRECTORY = "user/avatar/"; public static String UPLOAD_DIRECTORY = "user/avatar/";
@ -72,7 +76,7 @@ public class MyController extends BaseController{
@ApiOperation(value = "1.获取个人信息", notes = "游客无法获取个人信息") @ApiOperation(value = "1.获取个人信息", notes = "游客无法获取个人信息")
@GetMapping("/getUserInfo") @GetMapping("/getUserInfo")
@GlobalInterceptor @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); UserLoginDto userLoginDto = getUserLoginDto(token);
if (null == userLoginDto) { if (null == userLoginDto) {
return Result.unauthorized(null); return Result.unauthorized(null);
@ -97,19 +101,12 @@ public class MyController extends BaseController{
if(null!=userRespDTO.getAvatar()) { if(null!=userRespDTO.getAvatar()) {
userRespDTO.setAvatar(Constants.RESOURCE_PREFIX+userRespDTO.getAvatar()); userRespDTO.setAvatar(Constants.RESOURCE_PREFIX+userRespDTO.getAvatar());
} }
UserCollectInfo userCollectInfo = userCollectInfoService.findByUserId(userLoginDto.getUserId());
if(null!=userCollectInfo) { Optional<UserCollect> optional=userCollectService.findByUserId(userLoginDto.getUserId());
Set songSet =userCollectInfo.getSongSet(); if(optional.isPresent()) {
Set journalSet = userCollectInfo.getJournalSet(); UserCollect userCollect=optional.get();
userRespDTO.setJournalSet(journalSet); userRespDTO.setSongCount(userCollect.getSongs().size());
userRespDTO.setSongSet(songSet); userRespDTO.setJournalCount(userCollect.getJournals().size());
if(null != songSet){
userRespDTO.setSongCount(songSet.size());
}
if(null != journalSet) {
userRespDTO.setJournalCount(journalSet.size());
}
} }
if(null!=user.getBirthday()) { if(null!=user.getBirthday()) {

@ -22,10 +22,13 @@ public class UserCollectController {
@Autowired @Autowired
private UserCollectService userCollectService; private UserCollectService userCollectService;
@ApiOperation(value = "1.喜欢歌曲/收藏期刊/关注/黑名单/点赞") //@ApiOperation(value = "1.喜欢歌曲/收藏期刊/关注/黑名单/点赞")
@ApiOperation(value = "1.喜欢歌曲/收藏期刊")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊/关注某用户/某用户列入黑名单/点赞评论的id", required = true), //@ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊/关注某用户/某用户列入黑名单/点赞评论的id", required = true),
@ApiImplicitParam(name = "collectType", value = "0:歌曲1:期刊2:关注3:黑名单4:点赞", 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") @PostMapping("/addCollect")
@GlobalInterceptor(checkLogin = true) @GlobalInterceptor(checkLogin = true)
public Result<Void> addCollect(@RequestHeader(value = "token", required = false) String token, public Result<Void> addCollect(@RequestHeader(value = "token", required = false) String token,
@ -36,17 +39,20 @@ public class UserCollectController {
return Result.success(); return Result.success();
} }
@ApiOperation(value = "2.取消 喜欢歌曲/收藏期刊/关注/黑名单/点赞") //@ApiOperation(value = "2.取消 喜欢歌曲/收藏期刊/关注/黑名单/点赞")
@ApiOperation(value = "2.取消 喜欢歌曲/收藏期刊")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊/关注某用户/某用户列入黑名单/点赞评论的id", required = true), //@ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊/关注某用户/某用户列入黑名单/点赞评论的id", required = true),
@ApiImplicitParam(name = "collectType", value = "0:歌曲1:期刊2:关注3:黑名单4:点赞", 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") @DeleteMapping("/cancelCollect")
@GlobalInterceptor(checkLogin = true) @GlobalInterceptor(checkLogin = true)
public Result<Void> cancelCollect(@RequestHeader(value = "token", required = false) String token, public Result<Void> cancelCollect(@RequestHeader(value = "token", required = false) String token,
@VerifyParam(required = true) @RequestParam("objectId") String objectId, @VerifyParam(required = true) @RequestParam("objectId") String objectId,
@VerifyParam(required = true) @RequestParam("collectType") Integer collectType) { @VerifyParam(required = true) @RequestParam("collectType") Integer collectType) {
UserLoginDto loginDto = jwtUtil.getUserLoginDto(token); UserLoginDto loginDto = jwtUtil.getUserLoginDto(token);
userCollectService.deleteUserCollectByUserIdAndObjectIdAndCollectType(loginDto.getUserId(), objectId, userCollectService.deleteByUserIdAndObjectIdAndCollectType(loginDto.getUserId(), objectId,
collectType); collectType);
return Result.success(); return Result.success();
} }

@ -1,20 +1,9 @@
package com.luoo.user.dao; package com.luoo.user.dao;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import com.luoo.user.dto.UserCollectCount;
import com.luoo.user.pojo.UserCollect; import com.luoo.user.pojo.UserCollect;
public interface UserCollectDao extends MongoRepository<UserCollect, String> { 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.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.Date; import java.util.LinkedList;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable; import java.io.Serializable;
/** /**
@ -19,16 +15,10 @@ import java.io.Serializable;
@Setter @Setter
public class UserCollect implements Serializable { public class UserCollect implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
/**
* ID
*/
@Id
private String collectId;
/** /**
* ID * ID
*/ */
@Id
private String userId; private String userId;
/** /**
@ -41,10 +31,7 @@ public class UserCollect implements Serializable {
*/ */
private Integer collectType; private Integer collectType;
/** private LinkedList<String> journals;
*
*/ private LinkedList<String> songs;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date collectTime;
} }

@ -2,6 +2,7 @@ package com.luoo.user.service;
import java.util.Date; import java.util.Date;
import java.util.EnumMap; import java.util.EnumMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -15,6 +16,7 @@ import com.luoo.user.pojo.UserCollect;
import api.StatusCode; import api.StatusCode;
import enums.CollectTypeEnum; import enums.CollectTypeEnum;
import enums.CollectTypeEnum.*;
import exception.BizException; import exception.BizException;
@Service @Service
@ -22,48 +24,50 @@ public class UserCollectService {
@Autowired @Autowired
private UserCollectDao userCollectDao; private UserCollectDao userCollectDao;
public void saveCollect(String userId, String objectId, Integer collectType) { 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); CollectTypeEnum collectTypeEnum = CollectTypeEnum.getByType(collectType);
if (null == collectTypeEnum) { if (null == collectTypeEnum) {
throw new BizException(StatusCode.VALIDATE_FAILED); throw new BizException(StatusCode.VALIDATE_FAILED);
} }
String collectId=userId+"_"+objectId+"_"+collectType; UserCollect userCollect=null;
Optional<UserCollect> dbCollect = userCollectDao.findById(collectId); Optional<UserCollect> dbCollect = userCollectDao.findById(userId);
if (dbCollect.isPresent()) { 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); userCollectDao.save(userCollect);
} }
public void deleteUserCollectByUserIdAndObjectIdAndCollectType(String userId, String objectId, private void addOrRemove(boolean isAdd, LinkedList<String> list, String objectId) {
Integer collectType) { if(isAdd) {
String collectId=userId+"_"+objectId+"_"+collectType; list.addFirst(objectId);
userCollectDao.deleteById(collectId); }else{
list.removeFirstOccurrence(objectId);
}
} }
public EnumMap<CollectTypeEnum,Long> getUserCollectTypeMap(String userId) { public void deleteByUserIdAndObjectIdAndCollectType(String userId, String objectId,
EnumMap<CollectTypeEnum,Long> userCollectTypeMap=new EnumMap<>(CollectTypeEnum.class); Integer collectType) {
/* addOrRemove(userId,objectId,collectType,false);
* 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 int getCount(String userId, Integer type) { public Optional<UserCollect> findByUserId(String userId) {
return userCollectDao.countByUserIdAndCollectType(userId,type); return userCollectDao.findById(userId);
} }
} }

Loading…
Cancel
Save