1.implement recomend journal interface

main
Gary 10 months ago
parent 8d84c5aec3
commit 8167f62b08

@ -1,8 +1,10 @@
package com.luoo.music.controller; package com.luoo.music.controller;
import java.util.Arrays;
import java.util.Collections; 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.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -84,7 +86,8 @@ public class JournalController {
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() Set<String> journalCollectSet = null == user ? Collections.emptySet()
: userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL); : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL);
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao)) Map<String,List<String>> tagMap=tagService.getTagMap(ids);
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao,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));
} }
@ -107,7 +110,8 @@ public class JournalController {
} }
List<Journal> pageList = journalService.orderByField(objectIds); List<Journal> pageList = journalService.orderByField(objectIds);
Set<String> journalCollectSet = new HashSet<>(objectIds); Set<String> journalCollectSet = new HashSet<>(objectIds);
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao)) Map<String,List<String>> tagMap=tagService.getTagMap(objectIds);
List<JournalRespDTO> list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao,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));
} }
@ -123,7 +127,8 @@ public class JournalController {
return Result.unauthorized(null); return Result.unauthorized(null);
} }
Journal journal = journalService.findById(id); Journal journal = journalService.findById(id);
JournalRespDTO journalRespDTO=JournalMapper.getJournalRespDTO(journal, Collections.emptySet(),mongoTemplate,redisTemplate,commentDao); Map<String,List<String>> tagMap=tagService.getTagMap(Arrays.asList(id));
JournalRespDTO journalRespDTO=JournalMapper.getJournalRespDTO(journal, Collections.emptySet(),mongoTemplate,redisTemplate,commentDao,tagMap);
boolean isCollect=null == user ? false:userCollectInfoService.isCollect(user.getUserId(),id, CollectTypeEnum.JOURNAL); boolean isCollect=null == user ? false:userCollectInfoService.isCollect(user.getUserId(),id, CollectTypeEnum.JOURNAL);
journalRespDTO.setHaveCollect(isCollect); journalRespDTO.setHaveCollect(isCollect);
return Result.success(journalRespDTO); return Result.success(journalRespDTO);
@ -139,7 +144,7 @@ public class JournalController {
if (null == user && !journalService.isLatest10ByJournalId(id)) { if (null == user && !journalService.isLatest10ByJournalId(id)) {
return Result.unauthorized(null); return Result.unauthorized(null);
} }
return Result.success(CommentMapper.getCommentDTO(id,mongoTemplate,commentDao)); return Result.success(CommentMapper.getCommentDTO(id,mongoTemplate,commentDao,redisTemplate));
} }
@ApiOperation(value = "4.获取期刊筛选条件") @ApiOperation(value = "4.获取期刊筛选条件")
@ -154,6 +159,29 @@ public class JournalController {
journalFilterDTO.setStyleList(tagService.getStyleList().stream().map(this::getTagDTO).collect(Collectors.toList())); journalFilterDTO.setStyleList(tagService.getStyleList().stream().map(this::getTagDTO).collect(Collectors.toList()));
return Result.success(journalFilterDTO); return Result.success(journalFilterDTO);
} }
@ApiOperation(value = "5.根据当前期刊的标签推荐6期类似期刊")
@GetMapping("/recommend/{id}")
@GlobalInterceptor
public Result<List<JournalRespDTO>> recommendById(
@RequestHeader(value = "Authorization", required = false) String authorization,
@PathVariable @VerifyParam(required = true) String id) {
UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
if (null == user && !journalService.isLatest10ByJournalId(id)) {
return Result.unauthorized(null);
}
List<Journal> journals = journalService.recommendById(id);
List<String> ids=journals.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);
List<JournalRespDTO> list = journals.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,redisTemplate,commentDao,tagMap))
.collect(Collectors.toList());
return Result.success(list);
}
private TagDTO getTagDTO(Tag tag) { private TagDTO getTagDTO(Tag tag) {
TagDTO tagDTO=new TagDTO(); TagDTO tagDTO=new TagDTO();
tagDTO.setName(tag.getNameCh()); tagDTO.setName(tag.getNameCh());

@ -94,14 +94,17 @@ public class SearchController {
@ApiOperation(value = "2.搜索期刊/歌曲", notes = "根据关键词模糊搜索,如 期刊号,歌曲名 等") @ApiOperation(value = "2.搜索期刊/歌曲", notes = "根据关键词模糊搜索,如 期刊号,歌曲名 等")
@ApiImplicitParams({ @ApiImplicitParam(name = "keyword", value = "搜索关键词", required = true) }) @ApiImplicitParams({ @ApiImplicitParam(name = "keyword", value = "搜索关键词", required = true) })
@GetMapping("/fuzzy/{keyword}") @GetMapping("/fuzzy/{keyword}")
@GlobalInterceptor @GlobalInterceptor(checkAppUserLogin = true)
public Result<SearchResultDTO> fuzzySearch( public Result<SearchResultDTO> fuzzySearch(
@RequestHeader(value = "Authorization", required = false) String authorization, @RequestHeader(value = "Authorization", required = false) String authorization,
@PathVariable @VerifyParam(required = true) String keyword) { @PathVariable @VerifyParam(required = true) String keyword) {
SearchResultDTO searchResultDTO = new SearchResultDTO(); SearchResultDTO searchResultDTO = new SearchResultDTO();
List<Journal> journals = journalService.fuzzySearch(keyword); List<Journal> journals = journalService.fuzzySearch(keyword);
List<JournalSong> songs = journalSongService.fuzzySearch(keyword); List<JournalSong> songs = journalSongService.fuzzySearch(keyword);
searchResultDTO.setJournals(journals.stream().map(JournalMapper::getJournalRespDTO).collect(Collectors.toList())); List<String> ids=journals.stream().map(Journal::getId).collect(Collectors.toList());
Map<String,List<String>> tagMap=tagService.getTagMap(ids);
searchResultDTO.setJournals(journals.stream().map(j->JournalMapper.getJournalRespDTO(j,tagMap)).collect(Collectors.toList()));
searchResultDTO.setSongs(songs.stream().map(SongMapper::getSongRespDTO).collect(Collectors.toList())); searchResultDTO.setSongs(songs.stream().map(SongMapper::getSongRespDTO).collect(Collectors.toList()));
return Result.success(searchResultDTO); return Result.success(searchResultDTO);
} }

@ -66,5 +66,8 @@ public interface JournalDao extends JpaRepository<Journal,String>,JpaSpecificati
@Query(value = "select max(ABS(journal_no)) FROM tb_journal", nativeQuery = true) @Query(value = "select max(ABS(journal_no)) FROM tb_journal", nativeQuery = true)
int getMaxJournalNo(); int getMaxJournalNo();
@Query(value = "select id from tb_journal where id not in ?2 order by rand() limit ?1", nativeQuery = true)
List<String> random(int limit, List<String> recommendIds);
} }

@ -31,4 +31,8 @@ public interface JournalTagDao extends JpaRepository<JournalTag,String>, JpaSpec
@Query(value = "select journal_id FROM tb_journal_tag where tag_id=?1", nativeQuery = true) @Query(value = "select journal_id FROM tb_journal_tag where tag_id=?1", nativeQuery = true)
List<String> getJournalIdByTagId(String tagId); List<String> getJournalIdByTagId(String tagId);
@Query(value = "select journal_id from tb_journal_tag where tag_id in (select tag_id from tb_journal_tag where journal_id=?1) and not journal_id=?1 order by rand() limit 6", nativeQuery = true)
List<String> recommendByJournalId(String journalId);
} }

@ -1,6 +1,7 @@
package com.luoo.music.dao; package com.luoo.music.dao;
import com.luoo.music.pojo.Tag; import com.luoo.music.pojo.Tag;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
@ -26,4 +27,7 @@ public interface TagDao extends JpaRepository<Tag, String>, JpaSpecificationExec
@Query(value = "select * from tb_tag_info where level=1", nativeQuery = true) @Query(value = "select * from tb_tag_info where level=1", nativeQuery = true)
List<Tag> getLevel1Tags(); List<Tag> getLevel1Tags();
@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);
} }

@ -1,11 +1,14 @@
package com.luoo.music.dto.mapper; package com.luoo.music.dto.mapper;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults; import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.redis.core.RedisTemplate;
import com.luoo.music.dao.CommentDao; import com.luoo.music.dao.CommentDao;
import com.luoo.music.dto.response.CommentDTO; import com.luoo.music.dto.response.CommentDTO;
@ -13,33 +16,52 @@ import com.luoo.music.dto.response.TotalCommentVo;
import com.luoo.music.pojo.Comment; import com.luoo.music.pojo.Comment;
public class CommentMapper { public class CommentMapper {
public static CommentDTO getCommentDTO(String journalId, MongoTemplate mongoTemplate, CommentDao commentDao) { public static CommentDTO getCommentDTO(String journalId, MongoTemplate mongoTemplate, CommentDao commentDao,RedisTemplate redisTemplate) {
/**
* 5...
*/
CommentDTO commentDTO=new CommentDTO(); CommentDTO commentDTO=new CommentDTO();
List<Comment> commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId); List<Comment> commentList = (List<Comment>) redisTemplate.opsForValue().get("JOURNAL_TOP5_COMMENT___"+journalId);
commentDTO.setCommentList(commentList); if (null!= commentList) {
commentDTO.setCommentList(commentList);
} else {
commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journalId);
commentDTO.setCommentList(commentList);
redisTemplate.opsForValue().set("JOURNAL_TOP5_COMMENT___"+journalId,commentList,6, TimeUnit.HOURS);
}
/** /**
* *
*/ */
Criteria criteria = Criteria.where("journalId").is(journalId);
Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件 String totalString = (String) redisTemplate.opsForValue().get("JOURNAL_TATAL_COMMENT_COUNT___"+journalId);
Aggregation.group().sum("commentCount").as("totalComment")); if (StringUtils.isNotBlank(totalString)) {
AggregationResults<TotalCommentVo> results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class); commentDTO.setTotalCommentReply(totalString);
TotalCommentVo totalCommentVo = results.getUniqueMappedResult(); }else {
commentDTO.setTotalCommentReply("0"); totalString = "0";
List<Comment> list = commentDao.findByJournalId(journalId); Criteria criteria = Criteria.where("journalId").is(journalId);
int total = 0; Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件
if (null != list && list.size() > 0) { Aggregation.group().sum("commentCount").as("totalComment"));
total = list.size(); AggregationResults<TotalCommentVo> results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class);
} TotalCommentVo totalCommentVo = results.getUniqueMappedResult();
if (null != totalCommentVo) { commentDTO.setTotalCommentReply("0");
total = total + totalCommentVo.getTotalComment(); List<Comment> list = commentDao.findByJournalId(journalId);
commentDTO.setTotalCommentReply(total + ""); int total = 0;
if (total > 99) { if (null != list && list.size() > 0) {
commentDTO.setTotalCommentReply("99+"); total = list.size();
}
if (null != totalCommentVo) {
total = total + totalCommentVo.getTotalComment();
totalString = total +"";
if (total > 99) {
totalString = "99+";
}
} }
commentDTO.setTotalCommentReply(totalString);
redisTemplate.opsForValue().set("JOURNAL_TATAL_COMMENT_COUNT___"+journalId,totalString,6,TimeUnit.HOURS);
} }
return commentDTO; return commentDTO;
} }
} }

@ -11,22 +11,15 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import com.apifan.common.random.RandomSource; import com.apifan.common.random.RandomSource;
import com.apifan.common.random.entity.Poem; import com.apifan.common.random.entity.Poem;
import com.luoo.music.dao.CommentDao; import com.luoo.music.dao.CommentDao;
import com.luoo.music.dto.response.CommentDTO;
import com.luoo.music.dto.response.JournalRespDTO; import com.luoo.music.dto.response.JournalRespDTO;
import com.luoo.music.dto.response.TotalCommentVo;
import com.luoo.music.pojo.Comment;
import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Journal;
import constants.Constants; import constants.Constants;
import enums.DateTimePatternEnum; import enums.DateTimePatternEnum;
@ -58,11 +51,12 @@ public class JournalMapper {
return Collections.emptyList(); return Collections.emptyList();
} }
public static JournalRespDTO getJournalRespDTO(Journal journal) { public static JournalRespDTO getJournalRespDTO(Journal journal, Map<String, List<String>> tagMap) {
return getJournalRespDTO(journal,Collections.emptySet()); return getJournalRespDTO(journal, Collections.emptySet(), tagMap);
} }
public static JournalRespDTO getJournalRespDTO(Journal journal, Set<String> journalCollectSet) { public static JournalRespDTO getJournalRespDTO(Journal journal, Set<String> journalCollectSet,
Map<String, List<String>> tagMap) {
JournalRespDTO journalRespDTO = new JournalRespDTO(); JournalRespDTO journalRespDTO = new JournalRespDTO();
journalRespDTO.setId(journal.getId()); journalRespDTO.setId(journal.getId());
journalRespDTO.setJournalNo(journal.getJournalNo()); journalRespDTO.setJournalNo(journal.getJournalNo());
@ -72,7 +66,7 @@ public class JournalMapper {
journalRespDTO.setHaveCollect(journalCollectSet.contains(journalRespDTO.getId())); journalRespDTO.setHaveCollect(journalCollectSet.contains(journalRespDTO.getId()));
journalRespDTO.setIpLocation("广东"); journalRespDTO.setIpLocation("广东");
journalRespDTO.setTags(getTags(journalRespDTO.getJournalNo())); journalRespDTO.setTags(tagMap.getOrDefault(journalRespDTO.getId(), getTags(journalRespDTO.getJournalNo())));
String content = journal.getContent(); String content = journal.getContent();
if (StringTools.isEmpty(content)) { if (StringTools.isEmpty(content)) {
Poem poem = RandomSource.languageSource().randomTangPoem(); Poem poem = RandomSource.languageSource().randomTangPoem();
@ -85,58 +79,15 @@ public class JournalMapper {
} }
return journalRespDTO; return journalRespDTO;
} }
public static JournalRespDTO getJournalRespDTO(Journal journal, Set<String> journalCollectSet,
MongoTemplate mongoTemplate, RedisTemplate redisTemplate,CommentDao commentDao) {
JournalRespDTO journalRespDTO = getJournalRespDTO(journal,journalCollectSet);
/**
* 5...
*/
List<Comment> commentList = (List<Comment>) redisTemplate.opsForValue().get("JOURNAL_TOP5_COMMENT___"+journal.getId());
if (null!= commentList) {
journalRespDTO.setCommentList(commentList);
} else {
commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journal.getId());
journalRespDTO.setCommentList(commentList);
redisTemplate.opsForValue().set("JOURNAL_TOP5_COMMENT___"+journal.getId(),commentList,6, TimeUnit.HOURS);
}
// List<Comment> commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journal.getId());
//
// journalRespDTO.setCommentList(commentList);
/**
*
*/
String totalString = (String) redisTemplate.opsForValue().get("JOURNAL_TATAL_COMMENT_COUNT___"+journal.getId());
if (StringUtils.isNotBlank(totalString)) {
journalRespDTO.setTotalCommentReply(totalString);
}else {
totalString = "0";
Criteria criteria = Criteria.where("journalId").is(journal.getId());
Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件
Aggregation.group().sum("commentCount").as("totalComment"));
AggregationResults<TotalCommentVo> results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class);
TotalCommentVo totalCommentVo = results.getUniqueMappedResult();
journalRespDTO.setTotalCommentReply("0");
List<Comment> list = commentDao.findByJournalId(journal.getId());
int total = 0;
if (null != list && list.size() > 0) {
total = list.size();
}
if (null != totalCommentVo) {
total = total + totalCommentVo.getTotalComment();
totalString = total +"";
if (total > 99) {
totalString = "99+";
}
}
journalRespDTO.setTotalCommentReply(totalString);
redisTemplate.opsForValue().set("JOURNAL_TATAL_COMMENT_COUNT___"+journal.getId(),totalString,6,TimeUnit.HOURS);
}
public static JournalRespDTO getJournalRespDTO(Journal journal, Set<String> journalCollectSet,
MongoTemplate mongoTemplate, RedisTemplate redisTemplate, CommentDao commentDao,
Map<String, List<String>> tagMap) {
JournalRespDTO journalRespDTO = getJournalRespDTO(journal, journalCollectSet, tagMap);
CommentDTO commentDTO = CommentMapper.getCommentDTO(journalRespDTO.getId(), mongoTemplate, commentDao,
redisTemplate);
journalRespDTO.setCommentList(commentDTO.getCommentList());
journalRespDTO.setTotalCommentReply(commentDTO.getTotalCommentReply());
return journalRespDTO; return journalRespDTO;
} }

@ -300,4 +300,17 @@ public class JournalService {
public boolean isLatest10ByJournalId(String journalId) { public boolean isLatest10ByJournalId(String journalId) {
return null!=journalDao.isLatest10ByJournalId(journalId); return null!=journalDao.isLatest10ByJournalId(journalId);
} }
public List<Journal> recommendById(String id) {
List<String> recommendIds=journalTagDao.recommendByJournalId(id);
List<String> extraIds=Collections.emptyList();
if(recommendIds.size()<6) {
extraIds=journalDao.random(6-recommendIds.size(),recommendIds);
}
if(!extraIds.isEmpty()) {
recommendIds.addAll(extraIds);
}
return journalDao.orderByField(recommendIds);
}
} }

@ -1,6 +1,8 @@
package com.luoo.music.service; package com.luoo.music.service;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -12,6 +14,7 @@ import com.luoo.music.pojo.Tag;
public class TagService { public class TagService {
@Autowired @Autowired
private TagDao tagDao; private TagDao tagDao;
public List<Tag> getLanguageList() { public List<Tag> getLanguageList() {
return tagDao.getLanguageList(); return tagDao.getLanguageList();
} }
@ -24,4 +27,10 @@ public class TagService {
return tagDao.getLevel1Tags(); return tagDao.getLevel1Tags();
} }
public Map<String, List<String>> getTagMap(List<String> journalIds) {
List<Object[]> pairs = tagDao.getJounralId2TagPairs(journalIds);
return pairs.stream().collect(Collectors.groupingBy(s -> s[0].toString(),
Collectors.mapping(s -> s[1].toString(), Collectors.toList())));
}
} }

Loading…
Cancel
Save