From 0ca110f4dfa14193ba8fe76ddd54b12054866c86 Mon Sep 17 00:00:00 2001 From: Gary Date: Sun, 28 Jan 2024 02:12:41 +0800 Subject: [PATCH] 1.remove unsed code in SongController 2.implement collect/follow/fans/blackList list --- .../src/main/java/enums/CollectTypeEnum.java | 2 +- .../src/main/java/enums/UserRelationEnum.java | 40 ++++ .../luoo/music/controller/SongController.java | 91 --------- .../luoo/user/controller/MyController.java | 174 +++++++++++++----- .../controller/UserCollectController.java | 12 +- .../java/com/luoo/user/dao/UserInfoDao.java | 4 + .../luoo/user/dto/response/UserRespDTO.java | 11 +- .../java/com/luoo/user/pojo/UserCollect.java | 9 +- .../luoo/user/service/UserCollectService.java | 110 ++++++++++- .../luoo/user/service/UserInfoService.java | 2 +- .../main/java/com/luoo/user/util/IpUtil.java | 7 + 11 files changed, 299 insertions(+), 163 deletions(-) create mode 100644 luoo_common/src/main/java/enums/UserRelationEnum.java create mode 100644 luoo_user/src/main/java/com/luoo/user/util/IpUtil.java diff --git a/luoo_common/src/main/java/enums/CollectTypeEnum.java b/luoo_common/src/main/java/enums/CollectTypeEnum.java index 3e7c5c0..be8dc3d 100644 --- a/luoo_common/src/main/java/enums/CollectTypeEnum.java +++ b/luoo_common/src/main/java/enums/CollectTypeEnum.java @@ -1,7 +1,7 @@ package enums; public enum CollectTypeEnum { - SONG(0, "歌曲"), JOURNAL(1, "期刊"), FOLLOW(2, "关注"), BLACK_LIST(3, "黑名单"), THUMB_UP(4, "点赞"); + SONG(0, "歌曲"), JOURNAL(1, "期刊"), FOLLOWS(2, "关注"), BLACK_LIST(3, "黑名单"), FANS(4, "粉丝"), THUMB_UP(5, "点赞"); private Integer type; private String description; diff --git a/luoo_common/src/main/java/enums/UserRelationEnum.java b/luoo_common/src/main/java/enums/UserRelationEnum.java new file mode 100644 index 0000000..d862ec2 --- /dev/null +++ b/luoo_common/src/main/java/enums/UserRelationEnum.java @@ -0,0 +1,40 @@ +package enums; + + +public enum UserRelationEnum { + NOT_FOLLOW(1, "未关注"), + FOLLOW(2, "已关注"), + BOTH_FOLLOW(3, "互相关注"), + SELF_BLACK_LIST(4, "他人在自己黑名单"), + OTHER_BLACK_LIST(5, "自己在他人黑名单"); + + + private Integer status; + private String desc; + + UserRelationEnum(Integer status, String desc) { + this.status = status; + this.desc = desc; + } + + public static UserRelationEnum getByStatus(Integer status) { + for (UserRelationEnum item : UserRelationEnum.values()) { + if (item.getStatus().equals(status)) { + return item; + } + } + return null; + } + + public Integer getStatus() { + return status; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } +} 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 ca90f21..c4878b9 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,8 +1,6 @@ package com.luoo.music.controller; -import com.luoo.music.dto.response.JournalRespDTO; import com.luoo.music.dto.response.SongRespDTO; -import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Song; import com.luoo.music.service.ArticleService; import com.luoo.music.service.SongService; @@ -22,13 +20,11 @@ import io.swagger.annotations.ApiOperation; import util.JwtUtil; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -139,91 +135,4 @@ public class SongController { songRespDTO.setHaveCollect(songCollectSet.contains(songRespDTO.getId())); return songRespDTO; } - - /* *//** - * 根据ID查询 - * - * @param id ID - * @return - *//* - * @GetMapping("/{id}") public Result findById(@PathVariable String id) { - * return Result.success(songService.findById(id)); } - */ - - /** - * 分页+多条件查询 - * - * @param searchMap 查询条件封装 - * @param page 页码 - * @param size 页大小 - * @return 分页结果 - */ - /* - * @PostMapping("/search/{page}/{size}") public Result> - * findSearch(@RequestBody Map searchMap, @PathVariable int page, - * - * @PathVariable int size) { Page pageList = - * songService.findSearch(searchMap, page, size); return Result.success(new - * PageResult(pageList.getTotalElements(), pageList.getContent())); } - * - * @PostMapping("/init") public Result init(@RequestBody Map map) { - * List data = (List) map.get("data"); System.out.println(data.size()); - * Song song = new Song(); Set
set = new HashSet
(); - * - * int temp = 0; int num = 0; for (Map dataMap : data) { Article article = new - * Article(); article.setVolid(dataMap.get("id") + ""); - * article.setTitle(dataMap.get("title") + ""); String avatar = "0000" + - * dataMap.get("id"); String substring = avatar.substring(avatar.length() - 5); - * System.out.println(substring); article.setImage(substring + "/00.jpg"); if - * (temp == (int) dataMap.get("id")) { num = num + 1; } else { num = 0; } temp = - * (int) dataMap.get("id"); String numstr = "0000" + (num + 1); - * System.out.println(numstr.substring(numstr.length() - 2)); - * - * song.setAlbum(dataMap.get("album") + ""); - * song.setArtist(dataMap.get("artist") + ""); song.setName(dataMap.get("name") - * + ""); song.setVolid(dataMap.get("id") + ""); song.setUrl(substring + "/" + - * numstr.substring(numstr.length() - 2) + ".mp3"); song.setSongno(num + 1); - * - * songService.add(song); set.add(article); } System.out.println(set.size()); - * - * for (Article pojo : set) { // articleService.add(pojo); } return - * Result.success(); } - * - *//** - * 根据条件查询 - * - * @param searchMap - * @return - */ - /* - * @PostMapping("/search") public Result> findSearch(@RequestBody Map - * searchMap) { return Result.success(songService.findSearch(searchMap)); } - * - *//** - * 增加 - * - * @param song - */ - /* - * @PostMapping public Result add(@RequestBody Song song) { - * songService.add(song); return Result.success(); } - * - *//** - * 修改 - * - * @param song - */ - /* - * @PutMapping("/{id}") public Result update(@RequestBody Song - * song, @PathVariable String id) { song.setId(id); songService.update(song); - * return Result.success(); } - * - *//** - * 删除 - * - * @param id - *//* - * @DeleteMapping("/{id}") public Result delete(@PathVariable String id) { - * songService.deleteById(id); return Result.success(); } - */ } 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 e4e9a0b..d94f9cb 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 @@ -1,25 +1,20 @@ package com.luoo.user.controller; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Paths; +import java.util.Collections; import java.util.Date; -import java.util.EnumMap; +import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; 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.apache.commons.lang3.tuple.Pair; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -28,8 +23,6 @@ import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import constants.Constants; @@ -40,14 +33,17 @@ import com.luoo.user.dto.response.UserRespDTO; import com.luoo.user.pojo.UserInfo; import com.luoo.user.service.S3Service; import com.luoo.user.service.UserInfoService; +import com.luoo.user.util.IpUtil; import annotation.GlobalInterceptor; import annotation.VerifyParam; +import api.PageResult; import api.Result; import api.StatusCode; import dto.UserLoginDto; import enums.CollectTypeEnum; import enums.DateTimePatternEnum; +import enums.UserRelationEnum; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; @@ -79,42 +75,8 @@ public class MyController extends BaseController { @GlobalInterceptor(checkAppUserLogin = true) public Result getUserInfo(@RequestHeader(value = "Authorization", required = false) String authorization) { UserLoginDto userLoginDto = getUserLoginDto(authorization); - if (null == userLoginDto) { - return Result.unauthorized(null); - } - UserRespDTO userRespDTO = new UserRespDTO(); UserInfo user = userInfoService.findById(userLoginDto.getUserId()); - - BeanUtils.copyProperties(user, userRespDTO); - // EnumMap - // map=userCollectService.getUserCollectTypeMap(user.getId()); -// int fansCount=userCollectService.getFansCount(user.getId()); -// int followCount=userCollectService.getCount(user.getId(),CollectTypeEnum.FOLLOW.getType()); -// int thumbUpCount=userCollectService.getCount(user.getId(),CollectTypeEnum.THUMB_UP.getType()); -// int songCount=userCollectService.getCount(user.getId(),CollectTypeEnum.SONG.getType()); -// int journalCount=userCollectService.getCount(user.getId(),CollectTypeEnum.JOURNAL.getType()); -// -// userRespDTO.setFollowCount(followCount); -// userRespDTO.setFansCount(fansCount); -// userRespDTO.setThumbUpCount(thumbUpCount); -// userRespDTO.setCommentReplyCount(0); -// userRespDTO.setSongCount(songCount); -// userRespDTO.setJournalCount(journalCount); - if (null != userRespDTO.getAvatar()) { - userRespDTO.setAvatar(Constants.RESOURCE_PREFIX + userRespDTO.getAvatar()); - } - - 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()) { - userRespDTO.setBirthDay( - DateUtil.format(user.getBirthday(), DateTimePatternEnum.YYYY_DOT_MM_DOT_DD.getPattern())); - } + UserRespDTO userRespDTO = getUserRespDTO(user,true,Collections.emptySet()); return Result.success(userRespDTO); } @@ -180,11 +142,129 @@ public class MyController extends BaseController { if (null == user) { return Result.failed(StatusCode.USER_INVALID_USER_ID); } + + UserRespDTO userRespDTO = getUserRespDTO(user,false,Collections.emptySet()); + userRespDTO.setIpLocation(IpUtil.getIpLocation(user.getLastLoginIp())); + + UserLoginDto userLoginDto = getUserLoginDto(authorization); + + Optional loginOptional = userCollectService.findByUserId(userLoginDto.getUserId()); + if (loginOptional.isPresent()) { + UserCollect loginCollect = loginOptional.get(); + Set loginBlackList=new HashSet<>(loginCollect.getBlackList()); + if(loginBlackList.contains(userId)) { + userRespDTO.setRelation(UserRelationEnum.SELF_BLACK_LIST.getStatus()); + return Result.success(userRespDTO); + } + } + Optional otherOptional = userCollectService.findByUserId(userId); + if (otherOptional.isPresent()) { + UserCollect otherCollect = otherOptional.get(); + Set otherBlackList=new HashSet<>(otherCollect.getBlackList()); + if(otherBlackList.contains(userLoginDto.getUserId())) { + userRespDTO.setRelation(UserRelationEnum.OTHER_BLACK_LIST.getStatus()); + return Result.success(userRespDTO); + } + + Set follows=new HashSet<>(otherCollect.getFollows()); + Set fans=new HashSet<>(otherCollect.getFans()); + userRespDTO.setFollowCount(follows.size()); + userRespDTO.setFansCount(fans.size()); + boolean isFollow=fans.contains(userLoginDto.getUserId()); + boolean isFan=follows.contains(userLoginDto.getUserId()); + if(isFollow&&isFan) { + userRespDTO.setRelation(UserRelationEnum.BOTH_FOLLOW.getStatus()); + return Result.success(userRespDTO); + }else if(isFollow&&!isFan) { + userRespDTO.setRelation(UserRelationEnum.FOLLOW.getStatus()); + return Result.success(userRespDTO); + } + } + userRespDTO.setRelation(UserRelationEnum.NOT_FOLLOW.getStatus()); + return Result.success(userRespDTO); + } + + @ApiOperation(value = "5.查询黑名单") + @ApiImplicitParams({ + @ApiImplicitParam(name = "pageNum", value = "分页: 页码,以1开始", required = true), + @ApiImplicitParam(name = "pageSize", value = "分页: 每页数量", required = true) }) + @GetMapping("/blackList/{pageNum}/{pageSize}") + @GlobalInterceptor(checkAppUserLogin = true) + public Result> getBlackList(@RequestHeader(value = "Authorization", required = false) String authorization, + @PathVariable @VerifyParam(required = true) Integer pageNum, + @PathVariable @VerifyParam(required = true) Integer pageSize) { + UserLoginDto userLoginDto = getUserLoginDto(authorization); + return getCollectedUserInfo(userLoginDto.getUserId(), pageNum, pageSize, CollectTypeEnum.BLACK_LIST); + } + + + @ApiOperation(value = "6.查询关注人信息") + @ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "用户id", required = true), + @ApiImplicitParam(name = "pageNum", value = "分页: 页码,以1开始", required = true), + @ApiImplicitParam(name = "pageSize", value = "分页: 每页数量", required = true) }) + @GetMapping("/follows/{userId}/{pageNum}/{pageSize}") + @GlobalInterceptor(checkAppUserLogin = true) + public Result> getFollows(@RequestHeader(value = "Authorization", required = false) String authorization, + @PathVariable @VerifyParam(required = true) String userId, + @PathVariable @VerifyParam(required = true) Integer pageNum, + @PathVariable @VerifyParam(required = true) Integer pageSize) { + + return getCollectedUserInfo(userId, pageNum, pageSize, CollectTypeEnum.FOLLOWS); + } + + @ApiOperation(value = "7.查询粉丝信息") + @ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "用户id", required = true), + @ApiImplicitParam(name = "pageNum", value = "分页: 页码,以1开始", required = true), + @ApiImplicitParam(name = "pageSize", value = "分页: 每页数量", required = true) }) + @GetMapping("/fans/{userId}/{pageNum}/{pageSize}") + @GlobalInterceptor(checkAppUserLogin = true) + public Result> getFans(@RequestHeader(value = "Authorization", required = false) String authorization, + @PathVariable @VerifyParam(required = true) String userId, + @PathVariable @VerifyParam(required = true) Integer pageNum, + @PathVariable @VerifyParam(required = true) Integer pageSize) { + return getCollectedUserInfo(userId, pageNum, pageSize, CollectTypeEnum.FANS); + } + + private Result> getCollectedUserInfo(String userId, Integer pageNum, Integer pageSize, + CollectTypeEnum collectTypeEnum) { + Pair,Set> pair = userCollectService.getCollectListWithBothFollowSet(userId, pageNum, pageSize, collectTypeEnum); + List objectIds = pair.getKey(); + if(objectIds.isEmpty()) { + return Result.success(new PageResult(0L, Collections.emptyList())); + } + List userInfos = userInfoService.orderByField(objectIds); + + List results = userInfos.stream().map(s -> getUserRespDTO(s,false,pair.getRight())) + .collect(Collectors.toList()); + + return Result.success(new PageResult(Long.valueOf(results.size()), results)); + } + + private UserRespDTO getUserRespDTO(UserInfo user,boolean withCount, Set bothFollowSet) { UserRespDTO userRespDTO = new UserRespDTO(); BeanUtils.copyProperties(user, userRespDTO); if (null != userRespDTO.getAvatar()) { userRespDTO.setAvatar(Constants.RESOURCE_PREFIX + userRespDTO.getAvatar()); } - return Result.success(userRespDTO); + if (null != user.getBirthday()) { + userRespDTO.setBirthDay( + DateUtil.format(user.getBirthday(), DateTimePatternEnum.YYYY_DOT_MM_DOT_DD.getPattern())); + } + if(!bothFollowSet.isEmpty()&&bothFollowSet.contains(userRespDTO.getId())) { + userRespDTO.setRelation(UserRelationEnum.BOTH_FOLLOW.getStatus()); + } + if(!withCount) { + return userRespDTO; + } + Optional optional = userCollectService.findByUserId(userRespDTO.getId()); + if (optional.isPresent()) { + UserCollect userCollect = optional.get(); + userRespDTO.setSongCount(userCollect.getSongs().size()); + userRespDTO.setJournalCount(userCollect.getJournals().size()); + userRespDTO.setFansCount(userCollect.getFans().size()); + userRespDTO.setFollowCount(userCollect.getFollows().size()); + } + + return userRespDTO; } } 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 d693331..0a5347b 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 @@ -23,12 +23,12 @@ public class UserCollectController { 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:期刊", required = true) }) + @ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊/关注某用户/某用户列入黑名单的id", required = true), + @ApiImplicitParam(name = "collectType", value = "0:歌曲,1:期刊,2:关注,3:黑名单", required = true) }) @PostMapping @GlobalInterceptor(checkAppUserLogin = true) public Result addCollect(@RequestHeader(value = "Authorization", required = false) String authorization, @@ -40,12 +40,12 @@ public class UserCollectController { } //@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:期刊", required = true) }) + @ApiImplicitParam(name = "objectId", value = "喜欢歌曲/收藏期刊/关注某用户/某用户列入黑名单/粉丝的id", required = true), + @ApiImplicitParam(name = "collectType", value = "0:歌曲,1:期刊,2:关注,3:黑名单,4:粉丝", required = true) }) @DeleteMapping @GlobalInterceptor(checkAppUserLogin = true) public Result cancelCollect(@RequestHeader(value = "Authorization", required = false) String authorization, diff --git a/luoo_user/src/main/java/com/luoo/user/dao/UserInfoDao.java b/luoo_user/src/main/java/com/luoo/user/dao/UserInfoDao.java index 49dc10a..fdd09b1 100644 --- a/luoo_user/src/main/java/com/luoo/user/dao/UserInfoDao.java +++ b/luoo_user/src/main/java/com/luoo/user/dao/UserInfoDao.java @@ -1,6 +1,7 @@ package com.luoo.user.dao; import java.util.List; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; @@ -31,4 +32,7 @@ public interface UserInfoDao extends JpaRepository, JpaSpecifi @Query(value = "select new client.vo.SimpleUser(id, nickName) from UserInfo where id in ?1 order by field(id,?1)") public List getSimpleUserOrderByField(List idList); + + @Query(value = "select * from tb_user_info where id = ?1", nativeQuery = true) + public UserInfo getById(String id); } diff --git a/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java b/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java index 348f507..471630e 100644 --- a/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java +++ b/luoo_user/src/main/java/com/luoo/user/dto/response/UserRespDTO.java @@ -1,13 +1,8 @@ package com.luoo.user.dto.response; -import com.luoo.user.dto.UserCollectJournalDto; -import com.luoo.user.dto.UserCollectSongDto; import io.swagger.annotations.ApiModelProperty; import lombok.Data; -import java.util.List; -import java.util.Set; - @Data public class UserRespDTO { @ApiModelProperty(value = "ID") @@ -30,12 +25,14 @@ public class UserRespDTO { private int thumbUpCount; @ApiModelProperty(value = "喜欢歌曲数") private int songCount; - @ApiModelProperty(value = "收藏期刊数") private int journalCount; - @ApiModelProperty(value = "获得评论数") private int commentReplyCount; @ApiModelProperty(value = "生日,格式为: yyyy.MM.dd") private String birthDay; + @ApiModelProperty(value = "与他人关系,1:未关注,2 :已关注,3:互相关注, 4:他人在自己黑名单,5:自己在他人黑名单", example = "1") + private int relation; + @ApiModelProperty(value = "IP 归属地,精确到省份", example = "北京") + private String ipLocation; } 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 d33140d..acbaba7 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 @@ -32,6 +32,13 @@ public class UserCollect implements Serializable { private Integer collectType; private LinkedList journals; - + private LinkedList songs; + + private LinkedList fans; + + private LinkedList follows; + + private LinkedList blackList; + } 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 f83b574..5b6c046 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 @@ -1,11 +1,16 @@ package com.luoo.user.service; +import java.util.Collections; import java.util.Date; import java.util.EnumMap; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -32,28 +37,58 @@ public class UserCollectService { if (null == collectTypeEnum) { throw new BizException(StatusCode.VALIDATE_FAILED); } - UserCollect userCollect=null; Optional dbCollect = userCollectDao.findById(userId); - if (dbCollect.isPresent()) { - userCollect=dbCollect.get(); - }else { - userCollect = new UserCollect(); - userCollect.setUserId(userId); - userCollect.setJournals(new LinkedList<>()); - userCollect.setSongs(new LinkedList<>()); - } + UserCollect userCollect=dbCollect.isPresent()?dbCollect.get():createUserCollect(userId); switch (collectTypeEnum) { case SONG: addOrRemove(isAdd,userCollect.getSongs(),objectId); break; case JOURNAL: addOrRemove(isAdd,userCollect.getJournals(),objectId); + break; + case FOLLOWS: + Optional followsDbCollect = userCollectDao.findById(objectId); + UserCollect followsUserCollect=followsDbCollect.isPresent()?followsDbCollect.get():createUserCollect(objectId); + addOrRemove(isAdd,userCollect.getFollows(),objectId,followsUserCollect.getFans(),userId); + userCollectDao.save(followsUserCollect); + break; + case FANS: + Optional fansDbCollect = userCollectDao.findById(objectId); + UserCollect fansUserCollect=fansDbCollect.isPresent()?fansDbCollect.get():createUserCollect(objectId); + addOrRemove(isAdd,userCollect.getFans(),objectId,fansUserCollect.getFollows(),userId); + userCollectDao.save(fansUserCollect); + break; + case BLACK_LIST: + addOrRemove(isAdd,userCollect.getBlackList(),objectId); + break; default: break; } userCollectDao.save(userCollect); } + private void addOrRemove(boolean isAdd, LinkedList follows, String objectId, LinkedList fans, + String userId) { + if(isAdd) { + follows.addFirst(objectId); + fans.addFirst(userId); + }else{ + follows.removeFirstOccurrence(objectId); + fans.removeFirstOccurrence(userId); + } + } + + private UserCollect createUserCollect(String userId) { + UserCollect userCollect = new UserCollect(); + userCollect.setUserId(userId); + userCollect.setJournals(new LinkedList<>()); + userCollect.setSongs(new LinkedList<>()); + userCollect.setFollows(new LinkedList<>()); + userCollect.setFans(new LinkedList<>()); + userCollect.setBlackList(new LinkedList<>()); + return userCollect; + } + private void addOrRemove(boolean isAdd, LinkedList list, String objectId) { if(isAdd) { list.addFirst(objectId); @@ -70,4 +105,61 @@ public class UserCollectService { public Optional findByUserId(String userId) { return userCollectDao.findById(userId); } + + 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 FOLLOWS: + return getPageResult(pageNum, pageSize, userCollect.getFollows()); + case FANS: + return getPageResult(pageNum, pageSize, userCollect.getFans()); + case BLACK_LIST: + return getPageResult(pageNum, pageSize, userCollect.getBlackList()); + default: + return Collections.emptyList(); + } + } + private List getPageResult(Integer pageNum, Integer pageSize, LinkedList objectIds) { + int end=pageNum * pageSize; + if (null != pageNum && null != pageSize && pageNum > 0 && pageSize > 0&&end<=objectIds.size()) { + return objectIds.subList((pageNum - 1) * pageSize, end); + } + return objectIds; + } + + public Pair, Set> getCollectListWithBothFollowSet(String userId, Integer pageNum, Integer pageSize, + CollectTypeEnum collectTypeEnum) { + if (null == userId) { + return Pair.of(Collections.emptyList(), Collections.emptySet()); + } + Optional optional = userCollectDao.findById(userId); + if (!optional.isPresent()) { + return Pair.of(Collections.emptyList(), Collections.emptySet()); + } + UserCollect userCollect = optional.get(); + switch (collectTypeEnum) { + case FOLLOWS: + List follows=getPageResult(pageNum, pageSize, userCollect.getFollows()); + Set fans=new HashSet<>(userCollect.getFans()); + Set bothFollowSet=follows.stream().filter(fans::contains).collect(Collectors.toSet()); + return Pair.of(follows, bothFollowSet); + case FANS: + List fanList=getPageResult(pageNum, pageSize, userCollect.getFans()); + Set followList=new HashSet<>(userCollect.getFollows()); + Set bothFanSet=fanList.stream().filter(followList::contains).collect(Collectors.toSet()); + return Pair.of(fanList, bothFanSet); + case BLACK_LIST: + return Pair.of(getPageResult(pageNum, pageSize, userCollect.getBlackList()), Collections.emptySet()); + default: + return Pair.of(Collections.emptyList(), Collections.emptySet()); + } + } } diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java index f902e59..cc13754 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java @@ -114,7 +114,7 @@ public class UserInfoService { * @return */ public UserInfo findById(String id) { - return userInfoDao.findById(id).get(); + return userInfoDao.getById(id); } diff --git a/luoo_user/src/main/java/com/luoo/user/util/IpUtil.java b/luoo_user/src/main/java/com/luoo/user/util/IpUtil.java new file mode 100644 index 0000000..dbf1577 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/util/IpUtil.java @@ -0,0 +1,7 @@ +package com.luoo.user.util; + +public class IpUtil { + public static String getIpLocation(String ip) { + return "中国"; + } +}