Merge remote-tracking branch 'origin/main'

main
wangqing 10 months ago
commit af51520fdf

@ -6,6 +6,10 @@ public class Constants {
public static final String REDIS_KEY_IMAGE_CHECK_CODE = "redis_key_image_check_code_"; public static final String REDIS_KEY_IMAGE_CHECK_CODE = "redis_key_image_check_code_";
public static final String REDIS_KEY_MOBILE_CHECK_CODE = "redis_key_mobile_check_code_"; public static final String REDIS_KEY_MOBILE_CHECK_CODE = "redis_key_mobile_check_code_";
public static final String REDIS_KEY_USER_COLLECT_JOURNAL = "redis_key_user_collect_journal_";
public static final String REDIS_KEY_PAGE_JOURNAL_RESPONSE_DTO = "redis_key_page_journal_response_dto_";
public static final String TOKEN_PREFIX = "Bearer "; public static final String TOKEN_PREFIX = "Bearer ";
public static final int TOKEN_PREFIX_LENGTH = TOKEN_PREFIX.length(); public static final int TOKEN_PREFIX_LENGTH = TOKEN_PREFIX.length();

@ -2,6 +2,7 @@ package com.luoo.music.controller;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -22,6 +23,7 @@ import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Journal;
import com.luoo.music.pojo.Tag; import com.luoo.music.pojo.Tag;
@ -32,9 +34,11 @@ import annotation.GlobalInterceptor;
import annotation.VerifyParam; import annotation.VerifyParam;
import api.PageResult; import api.PageResult;
import api.Result; import api.Result;
import constants.Constants;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.CollectTypeEnum; import enums.CollectTypeEnum;
import util.JwtUtil; import util.JwtUtil;
import util.StringTools;
/** /**
* *
@ -69,6 +73,7 @@ public class JournalController {
private RedisTemplate redisTemplate; private RedisTemplate redisTemplate;
private static final int JOURNAL_FILTER_NUMBER_RANGE=100; private static final int JOURNAL_FILTER_NUMBER_RANGE=100;
private static final Map<String,JournalRespDTO> journalRespDTOMap=new HashMap<>();
@ApiOperation(value = "1.查询期刊信息", notes = "若authorization为空或authorization校验失败默认返回最新的10期期刊筛选条件对游客不可用") @ApiOperation(value = "1.查询期刊信息", notes = "若authorization为空或authorization校验失败默认返回最新的10期期刊筛选条件对游客不可用")
@GetMapping("/list") @GetMapping("/list")
@ -82,15 +87,50 @@ public class JournalController {
queryReq.setPageNum(1); queryReq.setPageNum(1);
queryReq.setPageSize(10); queryReq.setPageSize(10);
} }
Set<String> journalCollectSet = null == user ? Collections.emptySet():getJournalCollectSet(user.getUserId());
if(StringTools.isEmpty(queryReq.getCategoryId())&&StringTools.isEmpty(queryReq.getJournalNoRange())) {
List<JournalRespDTO> cacheList = getPagableJournalRespDTO(queryReq,journalCollectSet);
List<String> ids=cacheList.stream().map(JournalRespDTO::getId).collect(Collectors.toList());
Map<String,List<String>> tagMap=tagService.getTagMap(ids);
List<JournalRespDTO> list = cacheList.stream().map(j->updateCollectAndTag(j,journalCollectSet,tagMap)).collect(Collectors.toList());
return Result.success(new PageResult<JournalRespDTO>(Long.valueOf(list.size()), list));
}
List<Journal> pageList = journalService.queryPage(queryReq); List<Journal> pageList = journalService.queryPage(queryReq);
List<String> ids=pageList.stream().map(Journal::getId).collect(Collectors.toList()); List<String> ids=pageList.stream().map(Journal::getId).collect(Collectors.toList());
Set<String> journalCollectSet = null == user ? Collections.emptySet()
: userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL);
Map<String,List<String>> tagMap=tagService.getTagMap(ids); Map<String,List<String>> tagMap=tagService.getTagMap(ids);
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao,tagMap)) List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,tagMap)).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 JournalRespDTO updateCollectAndTag(JournalRespDTO journalRespDTO,Set<String> journalCollectSet, Map<String, List<String>> tagMap) {
journalRespDTO.setTags(tagMap.getOrDefault(journalRespDTO.getId(), Collections.emptyList()));
journalRespDTO.setHaveCollect(journalCollectSet.contains(journalRespDTO.getId()));
return journalRespDTO;
}
private List<JournalRespDTO> getPagableJournalRespDTO(JournalQueryReq queryReq,Set<String> journalCollectSet) {
String redisPageJournalResponseDtoKey=Constants.REDIS_KEY_PAGE_JOURNAL_RESPONSE_DTO+queryReq.getPageNum()+"_"+queryReq.getPageSize();
List<JournalRespDTO> journalRespDTOs = (List<JournalRespDTO>)redisTemplate.opsForValue().get(redisPageJournalResponseDtoKey);
if(null==journalRespDTOs) {
List<Journal> pageList = journalService.queryPage(queryReq);
List<String> ids=pageList.stream().map(Journal::getId).collect(Collectors.toList());
Map<String,List<String>> tagMap=tagService.getTagMap(ids);
journalRespDTOs = pageList.stream().map(journal -> JournalMapper.getJournalRespDTO(journal, journalCollectSet,mongoTemplate,redisTemplate,commentDao,tagMap)).collect(Collectors.toList());
redisTemplate.opsForValue().set(redisPageJournalResponseDtoKey,journalRespDTOs);
}
return journalRespDTOs;
}
private Set<String> getJournalCollectSet(String userId) {
Set<String> set=(Set<String>) redisTemplate.opsForValue().get(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+userId);
if(null==set) {
set=userCollectInfoService.getCollectSet(userId, CollectTypeEnum.JOURNAL);
redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+userId,set);
}
return set;
}
@ApiOperation(value = "2.查询收藏期刊信息") @ApiOperation(value = "2.查询收藏期刊信息")
@ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "用户id", required = true), @ApiImplicitParams({ @ApiImplicitParam(name = "userId", value = "用户id", required = true),

@ -30,4 +30,7 @@ public interface TagDao extends JpaRepository<Tag, String>, JpaSpecificationExec
@Query(value = "select journal_id,name_ch from tb_journal_tag,tb_tag_info where journal_id in ?1 and tb_journal_tag.tag_id =tb_tag_info.id ;", nativeQuery = true) @Query(value = "select journal_id,name_ch from tb_journal_tag,tb_tag_info where journal_id in ?1 and tb_journal_tag.tag_id =tb_tag_info.id ;", nativeQuery = true)
List<Object[]> getJounralId2TagPairs(List<String> journalIds); List<Object[]> getJounralId2TagPairs(List<String> journalIds);
@Query(value = "select name_ch from tb_journal_tag,tb_tag_info where journal_id = ?1 and tb_journal_tag.tag_id =tb_tag_info.id ;", nativeQuery = true)
List<String> getTagNameChByJournalId(String journalId);
} }

@ -79,7 +79,9 @@ public class JournalMapper {
} }
return journalRespDTO; return journalRespDTO;
} }
public static List<String> getTags(String journalId,String journalNo,Map<String, List<String>> tagMap){
return tagMap.getOrDefault(journalId, getTags(journalNo));
}
public static JournalRespDTO getJournalRespDTO(Journal journal, Set<String> journalCollectSet, public static JournalRespDTO getJournalRespDTO(Journal journal, Set<String> journalCollectSet,
MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao, MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao,
Map<String, List<String>> tagMap) { Map<String, List<String>> tagMap) {

@ -1,5 +1,6 @@
package com.luoo.music.dto.response; package com.luoo.music.dto.response;
import java.io.Serializable;
import java.util.List; import java.util.List;
import com.luoo.music.pojo.Comment; import com.luoo.music.pojo.Comment;
@ -7,7 +8,8 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@Data @Data
public class JournalRespDTO { public class JournalRespDTO implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "ID") @ApiModelProperty(value = "ID")
private String id; private String id;
@ApiModelProperty(value = "剘刊号") @ApiModelProperty(value = "剘刊号")

@ -36,5 +36,8 @@ public class TagService {
return pairs.stream().collect(Collectors.groupingBy(s -> s[0].toString(), return pairs.stream().collect(Collectors.groupingBy(s -> s[0].toString(),
Collectors.mapping(s -> s[1].toString(), Collectors.toList()))); Collectors.mapping(s -> s[1].toString(), Collectors.toList())));
} }
public List<String> getTagNameChByJournalId(String journalId){
return tagDao.getTagNameChByJournalId(journalId);
}
} }

@ -38,6 +38,14 @@ public class UserCollectInfoService {
return userCollectInfos.isEmpty() ? Collections.emptySet() : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet()); return userCollectInfos.isEmpty() ? Collections.emptySet() : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet());
} }
public Set<String> getCollectSet(String userId, CollectTypeEnum collectTypeEnum) {
Criteria criteria = Criteria.where("userId").is(userId).and("collectType")
.is(collectTypeEnum.getType());
Query query = new Query(criteria);
List<UserCollectInfo> userCollectInfos = mongoTemplate.find(query, UserCollectInfo.class);
return userCollectInfos.isEmpty() ? Collections.emptySet() : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet());
}
public boolean isCollect(String userId, String objectId, CollectTypeEnum collectTypeEnum) { public boolean isCollect(String userId, String objectId, CollectTypeEnum collectTypeEnum) {
Criteria criteria = Criteria.where("userId").is(userId).and("objectId").is(objectId).and("collectType") Criteria criteria = Criteria.where("userId").is(userId).and("objectId").is(objectId).and("collectType")
.is(collectTypeEnum.getType()); .is(collectTypeEnum.getType());

@ -21,6 +21,7 @@ import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update; import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.*; import java.util.*;
import java.util.function.Function; import java.util.function.Function;
@ -32,13 +33,22 @@ public class UserCollectInfoService {
private RabbitTemplate rabbitTemplate; private RabbitTemplate rabbitTemplate;
@Autowired @Autowired
private MongoTemplate mongoTemplate; private MongoTemplate mongoTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Autowired @Autowired
private UserCollectInfoDao userCollectInfoDao; private UserCollectInfoDao userCollectInfoDao;
@Autowired @Autowired
private UserInfoDao userInfoDao; private UserInfoDao userInfoDao;
public Set<String> getCollectSet(String userId, CollectTypeEnum collectTypeEnum) {
Criteria criteria = Criteria.where("userId").is(userId).and("collectType")
.is(collectTypeEnum.getType());
Query query = new Query(criteria);
List<UserCollectInfo> userCollectInfos = mongoTemplate.find(query, UserCollectInfo.class);
return userCollectInfos.isEmpty() ? Collections.emptySet() : userCollectInfos.stream().map(UserCollectInfo::getObjectId).collect(Collectors.toSet());
}
public void saveCollect(String userId, String objectId, Integer collectType) { public void saveCollect(String userId, String objectId, Integer collectType) {
//自己不关注自己,自己不把自己列入黑名单 //自己不关注自己,自己不把自己列入黑名单
if(objectId.equals(userId)) { if(objectId.equals(userId)) {
@ -263,6 +273,8 @@ public class UserCollectInfoService {
private void addJournalCount(UserInfo user) { private void addJournalCount(UserInfo user) {
user.setJournalCount(user.getJournalCount() + 1); user.setJournalCount(user.getJournalCount() + 1);
userInfoDao.save(user); userInfoDao.save(user);
//更新redis 中的缓存
redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+user.getId(),getCollectSet(user.getId(), CollectTypeEnum.JOURNAL));
} }
private void addSongCount(UserInfo user) { private void addSongCount(UserInfo user) {
@ -273,6 +285,8 @@ public class UserCollectInfoService {
private void reduceJournalCount(UserInfo user) { private void reduceJournalCount(UserInfo user) {
user.setJournalCount(user.getJournalCount() - 1); user.setJournalCount(user.getJournalCount() - 1);
userInfoDao.save(user); userInfoDao.save(user);
//更新redis 中的缓存
redisTemplate.opsForValue().set(Constants.REDIS_KEY_USER_COLLECT_JOURNAL+user.getId(),getCollectSet(user.getId(), CollectTypeEnum.JOURNAL));
} }
private void reduceSongCount(UserInfo user) { private void reduceSongCount(UserInfo user) {

Loading…
Cancel
Save