From 852186325514f82adf26f05cce66e2d3e452ceae Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 23 Jan 2024 01:12:42 +0800 Subject: [PATCH] 1.add count info by multi search, need to improve by one search --- .../luoo/user/controller/MyController.java | 20 ++++++++++++++ .../com/luoo/user/dao/UserCollectDao.java | 9 +++++++ .../com/luoo/user/dto/UserCollectCount.java | 11 ++++++++ .../luoo/user/dto/response/UserRespDTO.java | 12 +++++++++ .../luoo/user/service/UserCollectService.java | 27 +++++++++++++++++-- 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 luoo_user/src/main/java/com/luoo/user/dto/UserCollectCount.java 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 791f05c..e08f66c 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 @@ -6,6 +6,7 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Paths; import java.util.Date; +import java.util.EnumMap; import org.apache.commons.io.IOUtils; import org.springframework.beans.BeanUtils; @@ -26,6 +27,7 @@ import controller.BaseController; import com.luoo.user.dto.response.UserRespDTO; import com.luoo.user.pojo.UserInfo; import com.luoo.user.service.S3Service; +import com.luoo.user.service.UserCollectService; import com.luoo.user.service.UserInfoService; import annotation.GlobalInterceptor; @@ -33,6 +35,7 @@ import annotation.VerifyParam; import api.Result; import api.StatusCode; import dto.UserLoginDto; +import enums.CollectTypeEnum; import enums.DateTimePatternEnum; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; @@ -52,6 +55,9 @@ public class MyController extends BaseController{ @Autowired private UserInfoService userInfoService; + @Autowired + private UserCollectService userCollectService; + public static String UPLOAD_DIRECTORY = "user/avatar/"; @ApiOperation(value = "1.获取个人信息", notes = "游客无法获取个人信息") @@ -65,6 +71,20 @@ public class MyController extends BaseController{ UserRespDTO userRespDTO = new UserRespDTO(); UserInfo user = userInfoService.findById(userLoginDto.getUserId()); BeanUtils.copyProperties(user, userRespDTO); + //EnumMap map=userCollectService.getUserCollectTypeMap(user.getId()); + Long fansCount=userCollectService.getFansCount(user.getId()); + Long followCount=userCollectService.getCount(user.getId(),CollectTypeEnum.FOLLOW.getType()); + Long thumbUpCount=userCollectService.getCount(user.getId(),CollectTypeEnum.THUMB_UP.getType()); + Long songCount=userCollectService.getCount(user.getId(),CollectTypeEnum.SONG.getType()); + Long journalCount=userCollectService.getCount(user.getId(),CollectTypeEnum.JOURNAL.getType()); + + userRespDTO.setFollowCount(followCount); + userRespDTO.setFansCount(fansCount); + userRespDTO.setThumbUpCount(thumbUpCount); + userRespDTO.setCommentReplyCount(0L); + userRespDTO.setSongCount(songCount); + userRespDTO.setJournalCount(journalCount); + return Result.success(userRespDTO); } 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 45a7036..bce5f94 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,11 +1,20 @@ 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 long countByObjectIdAndCollectType(String objectId, Integer collectType); + public long countByUserIdAndCollectType(String userId, Integer collectType); } diff --git a/luoo_user/src/main/java/com/luoo/user/dto/UserCollectCount.java b/luoo_user/src/main/java/com/luoo/user/dto/UserCollectCount.java new file mode 100644 index 0000000..ddadf05 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dto/UserCollectCount.java @@ -0,0 +1,11 @@ +package com.luoo.user.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class UserCollectCount { + private int collectType; + private long count; +} 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 32b12d5..0a4b350 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 @@ -17,4 +17,16 @@ public class UserRespDTO { private String signature; @ApiModelProperty(value = "用户标识,如“贡献者”,“音乐人”") private String badge; + @ApiModelProperty(value = "关注数") + private Long followCount; + @ApiModelProperty(value = "粉丝数") + private Long fansCount; + @ApiModelProperty(value = "获赞数") + private Long thumbUpCount; + @ApiModelProperty(value = "喜欢歌曲数") + private Long songCount; + @ApiModelProperty(value = "收藏期刊数") + private Long journalCount; + @ApiModelProperty(value = "获得评论数") + private Long commentReplyCount; } 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 9a24388..15b1c34 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,6 +1,8 @@ package com.luoo.user.service; import java.util.Date; +import java.util.EnumMap; +import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; @@ -8,6 +10,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.luoo.user.dao.UserCollectDao; +import com.luoo.user.dto.UserCollectCount; import com.luoo.user.pojo.UserCollect; import api.StatusCode; @@ -24,7 +27,7 @@ public class UserCollectService { throw new BizException(StatusCode.VALIDATE_FAILED); } String collectId=userId+"_"+objectId+"_"+collectType; - Optional dbCollect = userCollectDao.findById(collectId);//.findByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); + Optional dbCollect = userCollectDao.findById(collectId); if (dbCollect.isPresent()) { return; } @@ -41,6 +44,26 @@ public class UserCollectService { Integer collectType) { String collectId=userId+"_"+objectId+"_"+collectType; userCollectDao.deleteById(collectId); - //userCollectDao.deleteByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); + } + + 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 Long getFansCount(String userId) { + return userCollectDao.countByObjectIdAndCollectType(userId,CollectTypeEnum.FOLLOW.getType()); + } + + public Long getCount(String userId, Integer type) { + return userCollectDao.countByUserIdAndCollectType(userId,type); } }