1.add count info by multi search, need to improve by one search

main
Gary 10 months ago
parent ff07475aa3
commit 8521863255

@ -6,6 +6,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Date; import java.util.Date;
import java.util.EnumMap;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -26,6 +27,7 @@ import controller.BaseController;
import com.luoo.user.dto.response.UserRespDTO; import com.luoo.user.dto.response.UserRespDTO;
import com.luoo.user.pojo.UserInfo; import com.luoo.user.pojo.UserInfo;
import com.luoo.user.service.S3Service; import com.luoo.user.service.S3Service;
import com.luoo.user.service.UserCollectService;
import com.luoo.user.service.UserInfoService; import com.luoo.user.service.UserInfoService;
import annotation.GlobalInterceptor; import annotation.GlobalInterceptor;
@ -33,6 +35,7 @@ import annotation.VerifyParam;
import api.Result; import api.Result;
import api.StatusCode; import api.StatusCode;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.CollectTypeEnum;
import enums.DateTimePatternEnum; import enums.DateTimePatternEnum;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
@ -52,6 +55,9 @@ public class MyController extends BaseController{
@Autowired @Autowired
private UserInfoService userInfoService; private UserInfoService userInfoService;
@Autowired
private UserCollectService userCollectService;
public static String UPLOAD_DIRECTORY = "user/avatar/"; public static String UPLOAD_DIRECTORY = "user/avatar/";
@ApiOperation(value = "1.获取个人信息", notes = "游客无法获取个人信息") @ApiOperation(value = "1.获取个人信息", notes = "游客无法获取个人信息")
@ -65,6 +71,20 @@ public class MyController extends BaseController{
UserRespDTO userRespDTO = new UserRespDTO(); UserRespDTO userRespDTO = new UserRespDTO();
UserInfo user = userInfoService.findById(userLoginDto.getUserId()); UserInfo user = userInfoService.findById(userLoginDto.getUserId());
BeanUtils.copyProperties(user, userRespDTO); BeanUtils.copyProperties(user, userRespDTO);
//EnumMap<CollectTypeEnum,Long> 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); return Result.success(userRespDTO);
} }

@ -1,11 +1,20 @@
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 UserCollect findByUserIdAndObjectIdAndCollectType(String userId, String objectId, Integer collectType);
public long deleteByUserIdAndObjectIdAndCollectType(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 long countByObjectIdAndCollectType(String objectId, Integer collectType);
public long countByUserIdAndCollectType(String userId, Integer collectType);
} }

@ -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;
}

@ -17,4 +17,16 @@ public class UserRespDTO {
private String signature; private String signature;
@ApiModelProperty(value = "用户标识,如“贡献者”,“音乐人”") @ApiModelProperty(value = "用户标识,如“贡献者”,“音乐人”")
private String badge; 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;
} }

@ -1,6 +1,8 @@
package com.luoo.user.service; package com.luoo.user.service;
import java.util.Date; import java.util.Date;
import java.util.EnumMap;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -8,6 +10,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.luoo.user.dao.UserCollectDao; import com.luoo.user.dao.UserCollectDao;
import com.luoo.user.dto.UserCollectCount;
import com.luoo.user.pojo.UserCollect; import com.luoo.user.pojo.UserCollect;
import api.StatusCode; import api.StatusCode;
@ -24,7 +27,7 @@ public class UserCollectService {
throw new BizException(StatusCode.VALIDATE_FAILED); throw new BizException(StatusCode.VALIDATE_FAILED);
} }
String collectId=userId+"_"+objectId+"_"+collectType; String collectId=userId+"_"+objectId+"_"+collectType;
Optional<UserCollect> dbCollect = userCollectDao.findById(collectId);//.findByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); Optional<UserCollect> dbCollect = userCollectDao.findById(collectId);
if (dbCollect.isPresent()) { if (dbCollect.isPresent()) {
return; return;
} }
@ -41,6 +44,26 @@ public class UserCollectService {
Integer collectType) { Integer collectType) {
String collectId=userId+"_"+objectId+"_"+collectType; String collectId=userId+"_"+objectId+"_"+collectType;
userCollectDao.deleteById(collectId); userCollectDao.deleteById(collectId);
//userCollectDao.deleteByUserIdAndObjectIdAndCollectType(userId, objectId, collectType); }
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 Long getFansCount(String userId) {
return userCollectDao.countByObjectIdAndCollectType(userId,CollectTypeEnum.FOLLOW.getType());
}
public Long getCount(String userId, Integer type) {
return userCollectDao.countByUserIdAndCollectType(userId,type);
} }
} }

Loading…
Cancel
Save