From f5a2b242ffc43d367d7b25bf30c709cd7aa22f20 Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 6 Feb 2024 15:02:33 +0800 Subject: [PATCH 1/2] 1.add search interafce --- .../music/controller/JournalController.java | 17 +-- .../music/controller/SearchController.java | 100 ++++++++++++++++++ .../com/luoo/music/dao/JournalTagDao.java | 4 +- .../main/java/com/luoo/music/dao/TagDao.java | 11 +- .../music/dto/request/JournalQueryReq.java | 6 +- .../music/dto/response/JournalFilterDTO.java | 4 +- .../music/dto/response/SearchCategoryDTO.java | 18 ++++ .../com/luoo/music/dto/response/TagDTO.java | 12 +++ .../main/java/com/luoo/music/pojo/Tag.java | 21 ++-- .../luoo/music/service/JournalService.java | 7 +- .../com/luoo/music/service/TagService.java | 9 +- 11 files changed, 178 insertions(+), 31 deletions(-) create mode 100644 luoo_music/src/main/java/com/luoo/music/controller/SearchController.java create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/response/SearchCategoryDTO.java create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/response/TagDTO.java diff --git a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java index 32af9e7..dc0de6b 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java @@ -23,7 +23,7 @@ import com.luoo.music.dao.TagDao; import com.luoo.music.dto.request.JournalQueryReq; import com.luoo.music.dto.response.JournalFilterDTO; import com.luoo.music.dto.response.JournalRespDTO; - +import com.luoo.music.dto.response.TagDTO; import com.luoo.music.dto.response.TotalCommentVo; import com.luoo.music.pojo.Comment; import io.swagger.annotations.*; @@ -121,8 +121,7 @@ public class JournalController { @VerifyParam JournalQueryReq queryReq) { UserLoginDto user = jwtUtil.getUserLoginDto(authorization); if (null == user) { - queryReq.setLanguage(null); - queryReq.setStyle(null); + queryReq.setCategoryId(null); queryReq.setPageNum(1); queryReq.setPageSize(10); } @@ -179,11 +178,17 @@ public class JournalController { @RequestHeader(value = "Authorization", required = false) String authorization) { JournalFilterDTO journalFilterDTO=new JournalFilterDTO(); journalFilterDTO.setJournalNoList(journalService.getJournalNoList(JOURNAL_FILTER_NUMBER_RANGE)); - journalFilterDTO.setLanguageList(tagService.getLanguageList()); - journalFilterDTO.setStyleList(tagService.getStyleList()); + + journalFilterDTO.setLanguageList(tagService.getLanguageList().stream().map(this::getTagDto).collect(Collectors.toList())); + journalFilterDTO.setStyleList(tagService.getStyleList().stream().map(this::getTagDto).collect(Collectors.toList())); return Result.success(journalFilterDTO); } - + private TagDTO getTagDto(Tag tag) { + TagDTO tagDTO=new TagDTO(); + tagDTO.setName(tag.getNameCh()); + tagDTO.setId(tag.getId()); + return tagDTO; + } private JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet) { JournalRespDTO journalRespDTO = new JournalRespDTO(); journalRespDTO.setId(journal.getId()); diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java new file mode 100644 index 0000000..8ba89a4 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java @@ -0,0 +1,100 @@ +package com.luoo.music.controller; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.time.LocalDateTime; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import javax.annotation.PostConstruct; + +import com.apifan.common.random.RandomSource; +import com.apifan.common.random.entity.Poem; +import com.luoo.music.dao.CommentDao; +import com.luoo.music.dao.TagDao; +import com.luoo.music.dto.request.JournalQueryReq; +import com.luoo.music.dto.response.JournalFilterDTO; +import com.luoo.music.dto.response.JournalRespDTO; +import com.luoo.music.dto.response.SearchCategoryDTO; +import com.luoo.music.dto.response.TotalCommentVo; +import com.luoo.music.pojo.Comment; +import io.swagger.annotations.*; + +import org.springframework.beans.BeanUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +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 org.springframework.web.bind.annotation.*; +import com.luoo.music.pojo.Journal; +import com.luoo.music.pojo.Tag; +import com.luoo.music.service.JournalService; +import com.luoo.music.service.JournalSongService; +import com.luoo.music.service.TagService; +import com.luoo.music.service.UserCollectInfoService; +import annotation.GlobalInterceptor; +import annotation.VerifyParam; +import api.PageResult; +import api.Result; +import constants.Constants; +import dto.UserLoginDto; +import enums.CollectTypeEnum; +import enums.DateTimePatternEnum; +import lombok.SneakyThrows; +import util.DateUtil; +import util.JwtUtil; +import util.StringTools; + +/** + * 控制器层 + * + * @author Administrator + * + */ +@RestController +@CrossOrigin +@Api(tags = "雀跃APP搜索 APIs") +@RequestMapping("/search") +public class SearchController { + @Autowired + private UserCollectInfoService userCollectInfoService; + + @Autowired + private JournalController journalController; + + @Autowired + private JournalService journalService; + + @Autowired + private JournalSongService journalSongService; + + @Autowired + private TagService tagService; + + + @ApiOperation(value = "1.查询搜索大类", notes = "如 ‘民谣’,‘电子’") + @GetMapping("/category") + @GlobalInterceptor + public Result> getCategory() { + List tags=tagService.getLevel1Tags(); + List searchCategoryDTOs=tags.stream().map(this::getSearchCategoryDTO).collect(Collectors.toList()); + return Result.success(searchCategoryDTOs); + } + private SearchCategoryDTO getSearchCategoryDTO(Tag tag) { + SearchCategoryDTO searchCategoryDTO=new SearchCategoryDTO(); + BeanUtils.copyProperties(tag, searchCategoryDTO); + return searchCategoryDTO; + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java index b055f7c..471a2b1 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalTagDao.java @@ -29,6 +29,6 @@ public interface JournalTagDao extends JpaRepository, JpaSpec @Query("DELETE FROM JournalTag jt WHERE jt.journalId = :journalId") int deleteByJournalId(@Param("journalId") String journalId); - @Query(value = "select journal_id FROM tb_journal_tag where tag_id=(select id from tb_tag_info where level=?1 and name_ch=?2 limit 1)", nativeQuery = true) - List getJournalIdByLevelAndChName(int level,String chName); + @Query(value = "select journal_id FROM tb_journal_tag where tag_id=?1", nativeQuery = true) + List getJournalIdByTagId(String tagId); } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java b/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java index c888f53..fc47281 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/TagDao.java @@ -18,9 +18,12 @@ public interface TagDao extends JpaRepository, JpaSpecificationExec @Query(value = "select * from tb_tag_info where parent_id='' order by rand() limit ?1 ", nativeQuery = true) List random(int limit); - @Query(value = "select name_ch from tb_tag_info where parent_id=(select id from tb_tag_info where name_ch='语言' and level=1 limit 1) ", nativeQuery = true) - List getLanguageList(); + @Query(value = "select * from tb_tag_info where parent_id=(select id from tb_tag_info where name_ch='语言' and level=1 limit 1) ", nativeQuery = true) + List getLanguageList(); - @Query(value = "select name_ch from tb_tag_info where level=1 and not name_ch='语言' ", nativeQuery = true) - List getStyleList(); + @Query(value = "select * from tb_tag_info where level=1 and not name_ch='语言' ", nativeQuery = true) + List getStyleList(); + + @Query(value = "select * from tb_tag_info where level=1", nativeQuery = true) + List getLevel1Tags(); } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java b/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java index e2c21f7..aa3410f 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/JournalQueryReq.java @@ -12,10 +12,8 @@ import java.io.Serializable; @ApiModel public class JournalQueryReq implements Serializable { private static final long serialVersionUID = -1198060864891902188L; - @ApiModelProperty(value = "筛选条件:风格", example = "民谣") - private String style; - @ApiModelProperty(value = "筛选条件:语言", example = "国语") - private String language; + @ApiModelProperty(value = "筛选条件:类目Id", example = "民谣/国语 id") + private String categoryId; @ApiModelProperty(value = "筛选条件:期刊号范围", example = "900~800") private String journalNoRange; @ApiModelProperty(value = "分页: 页码,以1开始,即获取最新的几期", example = "1") diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/JournalFilterDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/JournalFilterDTO.java index 05de373..4f55bd4 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/response/JournalFilterDTO.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/JournalFilterDTO.java @@ -8,9 +8,9 @@ import lombok.Data; @Data public class JournalFilterDTO { @ApiModelProperty(value = "风格筛选条件") - private List styleList; + private List styleList; @ApiModelProperty(value = "语言筛选条件") - private List languageList; + private List languageList; @ApiModelProperty(value = "期刊号筛选条件") private List journalNoList; } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/SearchCategoryDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/SearchCategoryDTO.java new file mode 100644 index 0000000..32e0c91 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/SearchCategoryDTO.java @@ -0,0 +1,18 @@ +package com.luoo.music.dto.response; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +public class SearchCategoryDTO { + @ApiModelProperty(value = "ID") + private String id; + @ApiModelProperty(value = "中文名") + private String nameCh; + @ApiModelProperty(value = "英文名") + private String nameEn; + @ApiModelProperty(value = "图片路径") + private String image; + @ApiModelProperty(value = "说明文案") + private String description; +} diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/TagDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/TagDTO.java new file mode 100644 index 0000000..63bed1f --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/TagDTO.java @@ -0,0 +1,12 @@ +package com.luoo.music.dto.response; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +public class TagDTO { + @ApiModelProperty(value = "ID") + private String id; + @ApiModelProperty(value = "中文名") + private String name; +} diff --git a/luoo_music/src/main/java/com/luoo/music/pojo/Tag.java b/luoo_music/src/main/java/com/luoo/music/pojo/Tag.java index e10cd50..c9ac810 100644 --- a/luoo_music/src/main/java/com/luoo/music/pojo/Tag.java +++ b/luoo_music/src/main/java/com/luoo/music/pojo/Tag.java @@ -1,7 +1,7 @@ package com.luoo.music.pojo; -import lombok.Data; +import lombok.*; import org.hibernate.proxy.HibernateProxy; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedDate; @@ -15,11 +15,10 @@ import java.io.Serializable; import java.time.LocalDateTime; import java.util.Objects; -/** - * 标签信息表 - * @author locust - */ -@Data +@Getter +@Setter +@ToString +@RequiredArgsConstructor @Entity @Table(name = "tb_tag_info") @EntityListeners(AuditingEntityListener.class) @@ -54,6 +53,16 @@ public class Tag implements Serializable { */ private Integer state; + /** + * 说明文案 + */ + private String description; + + /** + * 图片路径 + */ + private String image; + /** * 创建人用户ID */ diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java index 7c65d4b..57aa4e7 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java @@ -205,11 +205,8 @@ public class JournalService { private List getJournalIds(JournalQueryReq queryReq) { - if(StringUtils.isNotBlank(queryReq.getLanguage())){ - return journalTagDao.getJournalIdByLevelAndChName(2,queryReq.getLanguage()); - } - if(StringUtils.isNotBlank(queryReq.getStyle())){ - return journalTagDao.getJournalIdByLevelAndChName(1,queryReq.getStyle()); + if(StringUtils.isNotBlank(queryReq.getCategoryId())){ + return journalTagDao.getJournalIdByTagId(queryReq.getCategoryId()); } return Collections.emptyList(); } diff --git a/luoo_music/src/main/java/com/luoo/music/service/TagService.java b/luoo_music/src/main/java/com/luoo/music/service/TagService.java index d85e5b3..d0587c8 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/TagService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/TagService.java @@ -6,17 +6,22 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.luoo.music.dao.TagDao; +import com.luoo.music.pojo.Tag; @Service public class TagService { @Autowired private TagDao tagDao; - public List getLanguageList() { + public List getLanguageList() { return tagDao.getLanguageList(); } - public List getStyleList() { + public List getStyleList() { return tagDao.getStyleList(); } + public List getLevel1Tags() { + return tagDao.getLevel1Tags(); + } + } From 0087e35a6347ed80d6bd162be2c7e380f590253a Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 6 Feb 2024 15:53:15 +0800 Subject: [PATCH 2/2] 1.add fuzzySearch interface --- .../music/controller/JournalController.java | 140 +----------------- .../music/controller/SearchController.java | 38 +++-- .../luoo/music/controller/SongController.java | 25 +--- .../luoo/music/dto/mapper/JournalMapper.java | 135 +++++++++++++++++ .../com/luoo/music/dto/mapper/SongMapper.java | 30 ++++ .../music/dto/response/SearchResultDTO.java | 14 ++ .../luoo/music/service/JournalService.java | 6 + .../music/service/JournalSongService.java | 6 + 8 files changed, 230 insertions(+), 164 deletions(-) create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/mapper/SongMapper.java create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/response/SearchResultDTO.java diff --git a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java index dc0de6b..1e2571e 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/JournalController.java @@ -1,40 +1,22 @@ package com.luoo.music.controller; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.time.LocalDateTime; -import java.util.Arrays; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import java.util.stream.Collectors; -import javax.annotation.PostConstruct; - -import com.apifan.common.random.RandomSource; -import com.apifan.common.random.entity.Poem; import com.luoo.music.dao.CommentDao; -import com.luoo.music.dao.TagDao; +import com.luoo.music.dto.mapper.JournalMapper; import com.luoo.music.dto.request.JournalQueryReq; import com.luoo.music.dto.response.JournalFilterDTO; import com.luoo.music.dto.response.JournalRespDTO; import com.luoo.music.dto.response.TagDTO; -import com.luoo.music.dto.response.TotalCommentVo; -import com.luoo.music.pojo.Comment; import io.swagger.annotations.*; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.core.io.ClassPathResource; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; 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 org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.Tag; @@ -45,14 +27,9 @@ import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.PageResult; import api.Result; -import constants.Constants; import dto.UserLoginDto; import enums.CollectTypeEnum; -import enums.DateTimePatternEnum; -import lombok.SneakyThrows; -import util.DateUtil; import util.JwtUtil; -import util.StringTools; /** * 控制器层 @@ -71,16 +48,12 @@ public class JournalController { @Autowired private JournalService journalService; - @Autowired - private TagDao tagDao; - @Autowired private TagService tagService; @Autowired private JwtUtil jwtUtil; - @Autowired private MongoTemplate mongoTemplate; @@ -88,30 +61,6 @@ public class JournalController { private CommentDao commentDao; private static final int JOURNAL_FILTER_NUMBER_RANGE=100; - // mock data - private static final String JOURNAL_TAG_FILE_PATH = "journalTags.txt"; - private Map> journalTagMap = new HashMap<>(); - private static final String[] EDITOR = new String[] { "左岸以西", "落在低处" }; - - @PostConstruct - @SneakyThrows - private void init() { - getLines(JOURNAL_TAG_FILE_PATH).forEach(s -> { - String[] segs = s.split("\\|"); - List tags = Arrays.stream(segs[1].split(",")).collect(Collectors.toList()); - journalTagMap.put(segs[0], tags); - }); - } - - private static List getLines(String filePath) { - try (InputStream is = new ClassPathResource(filePath).getInputStream(); - BufferedReader reader = new BufferedReader(new InputStreamReader(is));) { - return reader.lines().collect(Collectors.toList()); - } catch (IOException e) { - e.printStackTrace(); - } - return Collections.emptyList(); - } @ApiOperation(value = "1.查询期刊信息", notes = "若authorization为空或authorization校验失败,默认返回最新的10期,期刊筛选条件对游客不可用") @GetMapping("/list") @@ -129,7 +78,7 @@ public class JournalController { List ids=pageList.stream().map(Journal::getId).collect(Collectors.toList()); Set journalCollectSet = null == user ? Collections.emptySet() : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL); - List list = pageList.stream().map(a -> getJournalRespDTO(a, journalCollectSet)) + List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,commentDao)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); } @@ -152,7 +101,7 @@ public class JournalController { } List pageList = journalService.orderByField(objectIds); Set journalCollectSet = new HashSet<>(objectIds); - List list = pageList.stream().map(a -> getJournalRespDTO(a, journalCollectSet)) + List list = pageList.stream().map(a -> JournalMapper.getJournalRespDTO(a, journalCollectSet,mongoTemplate,commentDao)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(list.size()), list)); } @@ -165,7 +114,7 @@ public class JournalController { @PathVariable @VerifyParam(required = true) String id) { UserLoginDto user = jwtUtil.getUserLoginDto(authorization); Journal journal = journalService.findById(id); - JournalRespDTO journalRespDTO=getJournalRespDTO(journal, Collections.emptySet()); + JournalRespDTO journalRespDTO=JournalMapper.getJournalRespDTO(journal, Collections.emptySet(),mongoTemplate,commentDao); boolean isCollect=null == user ? false:userCollectInfoService.isCollect(user.getUserId(),id, CollectTypeEnum.JOURNAL); journalRespDTO.setHaveCollect(isCollect); return Result.success(journalRespDTO); @@ -179,91 +128,16 @@ public class JournalController { JournalFilterDTO journalFilterDTO=new JournalFilterDTO(); journalFilterDTO.setJournalNoList(journalService.getJournalNoList(JOURNAL_FILTER_NUMBER_RANGE)); - journalFilterDTO.setLanguageList(tagService.getLanguageList().stream().map(this::getTagDto).collect(Collectors.toList())); - journalFilterDTO.setStyleList(tagService.getStyleList().stream().map(this::getTagDto).collect(Collectors.toList())); + journalFilterDTO.setLanguageList(tagService.getLanguageList().stream().map(this::getTagDTO).collect(Collectors.toList())); + journalFilterDTO.setStyleList(tagService.getStyleList().stream().map(this::getTagDTO).collect(Collectors.toList())); return Result.success(journalFilterDTO); } - private TagDTO getTagDto(Tag tag) { + private TagDTO getTagDTO(Tag tag) { TagDTO tagDTO=new TagDTO(); tagDTO.setName(tag.getNameCh()); tagDTO.setId(tag.getId()); return tagDTO; } - private JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet) { - JournalRespDTO journalRespDTO = new JournalRespDTO(); - journalRespDTO.setId(journal.getId()); - journalRespDTO.setJournalNo(journal.getJournalNo()); - journalRespDTO.setTitle(journal.getTitle()); - journalRespDTO.setImage(Constants.MUSIC_RESOURCE_PREFIX + journal.getImage()); - journalRespDTO.setDate(getEditDate(journal)); - journalRespDTO.setHaveCollect(journalCollectSet.contains(journalRespDTO.getId())); - journalRespDTO.setIpLocation("广东"); - - journalRespDTO.setTags(getTags(journalRespDTO.getJournalNo())); - String content = journal.getContent(); - if (StringTools.isEmpty(content)) { - Poem poem = RandomSource.languageSource().randomTangPoem(); - journalRespDTO.setEditor(poem.getAuthor()); - journalRespDTO.setContent(Arrays.stream(poem.getContent()).collect(Collectors.joining("\r\n"))); - } else { - journalRespDTO.setContent(content); - - journalRespDTO.setEditor(getEditor(journal.getUserName())); - } - - /** - *显示期刊点赞最多5条评论,该五条评论随机显示,每次进入刷新新,超过部分显示为“...”点击评论进入期刊详情页并自动定位到该评论,点击用户头像同上,若为空数据则显示“暂无评论,快去抢沙发吧! - */ - - List commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journal.getId()); - - journalRespDTO.setCommentList(commentList); - - /** - * 获取期刊总评论数 一级评论数加上父评论的总回复数 - */ - Criteria criteria = Criteria.where("journalId").is(journal.getId()); - Aggregation agg = Aggregation.newAggregation( - Aggregation.match(criteria),//匹配条件 - Aggregation.group().sum("commentCount").as("totalComment") - ); - AggregationResults results = mongoTemplate.aggregate(agg,"comment", TotalCommentVo.class); - TotalCommentVo totalCommentVo = results.getUniqueMappedResult(); - journalRespDTO.setTotalCommentReply("0"); - List list =commentDao.findByJournalId(journal.getId()); - int total = 0; - if(null != list && list.size()>0) { - total = list.size(); - } - if (null != totalCommentVo) { - total = total + totalCommentVo.getTotalComment(); - journalRespDTO.setTotalCommentReply(total+""); - if (total>99) { - journalRespDTO.setTotalCommentReply("99+"); - } - } - - return journalRespDTO; - } - - private String getEditor(String userName) { - return StringTools.isEmpty(userName)?EDITOR[RandomSource.numberSource().randomInt(0, EDITOR.length)]:userName; - } - - private List getTags(String journalNo) { - return journalTagMap.computeIfAbsent(journalNo, a -> getTags()); - } - - private List getTags() { - int limit = RandomSource.numberSource().randomInt(1, 3); - return tagDao.random(limit).stream().map(Tag::getNameCh).sorted().collect(Collectors.toList()); - } - - private String getEditDate(Journal journal) { - LocalDateTime date = null == journal.getUpdateTime() ? journal.getCreateTime() : journal.getUpdateTime(); - return DateUtil.format(date, DateTimePatternEnum.YYYY_DOT_MM_DOT_DD.getPattern()); - } - @ApiOperation(value = "统计期刊的浏览数", notes = "APP端打开期刊调用一次统计接口") @RequestMapping(value="/stat/visits/{id}", method= RequestMethod.PUT) public Result statVisitsCount(@ApiParam(value = "期刊ID", required = true) @PathVariable String id) { diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java index 8ba89a4..88fb9fa 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SearchController.java @@ -20,10 +20,13 @@ import com.apifan.common.random.RandomSource; import com.apifan.common.random.entity.Poem; import com.luoo.music.dao.CommentDao; import com.luoo.music.dao.TagDao; +import com.luoo.music.dto.mapper.JournalMapper; +import com.luoo.music.dto.mapper.SongMapper; import com.luoo.music.dto.request.JournalQueryReq; import com.luoo.music.dto.response.JournalFilterDTO; import com.luoo.music.dto.response.JournalRespDTO; import com.luoo.music.dto.response.SearchCategoryDTO; +import com.luoo.music.dto.response.SearchResultDTO; import com.luoo.music.dto.response.TotalCommentVo; import com.luoo.music.pojo.Comment; import io.swagger.annotations.*; @@ -39,6 +42,7 @@ import org.springframework.data.mongodb.core.aggregation.AggregationResults; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Journal; +import com.luoo.music.pojo.JournalSong; import com.luoo.music.pojo.Tag; import com.luoo.music.service.JournalService; import com.luoo.music.service.JournalSongService; @@ -68,32 +72,42 @@ import util.StringTools; @Api(tags = "雀跃APP搜索 APIs") @RequestMapping("/search") public class SearchController { - @Autowired - private UserCollectInfoService userCollectInfoService; - - @Autowired - private JournalController journalController; - @Autowired private JournalService journalService; - + @Autowired private JournalSongService journalSongService; - + @Autowired private TagService tagService; - @ApiOperation(value = "1.查询搜索大类", notes = "如 ‘民谣’,‘电子’") @GetMapping("/category") @GlobalInterceptor public Result> getCategory() { - List tags=tagService.getLevel1Tags(); - List searchCategoryDTOs=tags.stream().map(this::getSearchCategoryDTO).collect(Collectors.toList()); + List tags = tagService.getLevel1Tags(); + List searchCategoryDTOs = tags.stream().map(this::getSearchCategoryDTO) + .collect(Collectors.toList()); return Result.success(searchCategoryDTOs); } + + @ApiOperation(value = "2.搜索期刊/歌曲", notes = "根据关键词模糊搜索,如 期刊号,歌曲名 等") + @ApiImplicitParams({ @ApiImplicitParam(name = "keyword", value = "搜索关键词", required = true) }) + @GetMapping("/fuzzy/{keyword}") + @GlobalInterceptor + public Result fuzzySearch( + @RequestHeader(value = "Authorization", required = false) String authorization, + @PathVariable @VerifyParam(required = true) String keyword) { + SearchResultDTO searchResultDTO = new SearchResultDTO(); + List journals = journalService.fuzzySearch(keyword); + List songs = journalSongService.fuzzySearch(keyword); + searchResultDTO.setJournals(journals.stream().map(JournalMapper::getJournalRespDTO).collect(Collectors.toList())); + searchResultDTO.setSongs(songs.stream().map(SongMapper::getSongRespDTO).collect(Collectors.toList())); + return Result.success(searchResultDTO); + } + private SearchCategoryDTO getSearchCategoryDTO(Tag tag) { - SearchCategoryDTO searchCategoryDTO=new SearchCategoryDTO(); + SearchCategoryDTO searchCategoryDTO = new SearchCategoryDTO(); BeanUtils.copyProperties(tag, searchCategoryDTO); return searchCategoryDTO; } 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 70c44fe..ae9bdb2 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,5 +1,6 @@ package com.luoo.music.controller; +import com.luoo.music.dto.mapper.SongMapper; import com.luoo.music.dto.response.SongRespDTO; import com.luoo.music.pojo.JournalSong; import com.luoo.music.service.JournalService; @@ -9,7 +10,6 @@ import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.PageResult; import api.Result; -import constants.Constants; import dto.UserLoginDto; import enums.CollectTypeEnum; import enums.VerifyRegexEnum; @@ -68,7 +68,7 @@ public class SongController { List ids=songs.stream().map(JournalSong::getId).collect(Collectors.toList()); Set songCollectSet = null == user ? Collections.emptySet() : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.SONG); - List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)) + List results = songs.stream().map(s -> SongMapper.getSongRespDTO(s, songCollectSet)) .collect(Collectors.toList()); return Result.success(results); } @@ -97,7 +97,7 @@ public class SongController { List songs = journalSongService.orderByField(objectIds); Set songCollectSet = new HashSet<>(objectIds); - List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)) + List results = songs.stream().map(s -> SongMapper.getSongRespDTO(s, songCollectSet)) .collect(Collectors.toList()); return Result.success(new PageResult(Long.valueOf(results.size()), results)); } @@ -116,7 +116,7 @@ public class SongController { Set songCollectSet = null == user ? Collections.emptySet() : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.SONG); - List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)) + List results = songs.stream().map(s -> SongMapper.getSongRespDTO(s, songCollectSet)) .collect(Collectors.toList()); return Result.success(results); } @@ -129,24 +129,11 @@ public class SongController { UserLoginDto user = jwtUtil.getUserLoginDto(authorization); JournalSong song = journalSongService.findById(id); - SongRespDTO songRespDTO=getSongRespDTO(song, Collections.emptySet()); + SongRespDTO songRespDTO=SongMapper.getSongRespDTO(song); boolean isCollect=null == user ? false:userCollectInfoService.isCollect(user.getUserId(),id, CollectTypeEnum.SONG); songRespDTO.setHaveCollect(isCollect); return Result.success(songRespDTO); } - private SongRespDTO getSongRespDTO(JournalSong song, Set songCollectSet) { - SongRespDTO songRespDTO = new SongRespDTO(); - songRespDTO.setJournalNo(song.getJournalNo()); - songRespDTO.setAlbum(song.getAlbum()); - songRespDTO.setArtist(song.getArtist()); - songRespDTO.setId(song.getId()); - songRespDTO.setTitle(song.getName()); - songRespDTO.setSrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl()); - songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".jpg")); - songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".lyric")); - songRespDTO.setSongNo(song.getSongNo()); - songRespDTO.setHaveCollect(songCollectSet.contains(songRespDTO.getId())); - return songRespDTO; - } + } diff --git a/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java b/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java new file mode 100644 index 0000000..0a3304c --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/mapper/JournalMapper.java @@ -0,0 +1,135 @@ +package com.luoo.music.dto.mapper; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.time.LocalDateTime; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import org.springframework.core.io.ClassPathResource; +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.entity.Poem; +import com.luoo.music.dao.CommentDao; +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 constants.Constants; +import enums.DateTimePatternEnum; +import util.DateUtil; +import util.StringTools; + +public class JournalMapper { + // mock data + private static final String JOURNAL_TAG_FILE_PATH = "journalTags.txt"; + private static final Map> journalTagMap = new HashMap<>(); + private static final String[] EDITOR = new String[] { "左岸以西", "落在低处" }; + + static { + getLines(JOURNAL_TAG_FILE_PATH).forEach(s -> { + String[] segs = s.split("\\|"); + List tags = Arrays.stream(segs[1].split(",")).collect(Collectors.toList()); + journalTagMap.put(segs[0], tags); + }); + } + + private static List getLines(String filePath) { + try (InputStream is = new ClassPathResource(filePath).getInputStream(); + BufferedReader reader = new BufferedReader(new InputStreamReader(is));) { + return reader.lines().collect(Collectors.toList()); + } catch (IOException e) { + e.printStackTrace(); + } + return Collections.emptyList(); + } + + public static JournalRespDTO getJournalRespDTO(Journal journal) { + return getJournalRespDTO(journal,Collections.emptySet()); + } + + public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet) { + JournalRespDTO journalRespDTO = new JournalRespDTO(); + journalRespDTO.setId(journal.getId()); + journalRespDTO.setJournalNo(journal.getJournalNo()); + journalRespDTO.setTitle(journal.getTitle()); + journalRespDTO.setImage(Constants.MUSIC_RESOURCE_PREFIX + journal.getImage()); + journalRespDTO.setDate(getEditDate(journal)); + journalRespDTO.setHaveCollect(journalCollectSet.contains(journalRespDTO.getId())); + journalRespDTO.setIpLocation("广东"); + + journalRespDTO.setTags(getTags(journalRespDTO.getJournalNo())); + String content = journal.getContent(); + if (StringTools.isEmpty(content)) { + Poem poem = RandomSource.languageSource().randomTangPoem(); + journalRespDTO.setEditor(poem.getAuthor()); + journalRespDTO.setContent(Arrays.stream(poem.getContent()).collect(Collectors.joining("\r\n"))); + } else { + journalRespDTO.setContent(content); + + journalRespDTO.setEditor(getEditor(journal.getUserName())); + } + return journalRespDTO; + } + public static JournalRespDTO getJournalRespDTO(Journal journal, Set journalCollectSet, + MongoTemplate mongoTemplate, CommentDao commentDao) { + JournalRespDTO journalRespDTO = getJournalRespDTO(journal,journalCollectSet); + + /** + * 显示期刊点赞最多5条评论,该五条评论随机显示,每次进入刷新新,超过部分显示为“...”点击评论进入期刊详情页并自动定位到该评论,点击用户头像同上,若为空数据则显示“暂无评论,快去抢沙发吧! + */ + + List commentList = commentDao.findTop5ByJournalIdOrderByThumbupCountDesc(journal.getId()); + + journalRespDTO.setCommentList(commentList); + + /** + * 获取期刊总评论数 一级评论数加上父评论的总回复数 + */ + Criteria criteria = Criteria.where("journalId").is(journal.getId()); + Aggregation agg = Aggregation.newAggregation(Aggregation.match(criteria), // 匹配条件 + Aggregation.group().sum("commentCount").as("totalComment")); + AggregationResults results = mongoTemplate.aggregate(agg, "comment", TotalCommentVo.class); + TotalCommentVo totalCommentVo = results.getUniqueMappedResult(); + journalRespDTO.setTotalCommentReply("0"); + List list = commentDao.findByJournalId(journal.getId()); + int total = 0; + if (null != list && list.size() > 0) { + total = list.size(); + } + if (null != totalCommentVo) { + total = total + totalCommentVo.getTotalComment(); + journalRespDTO.setTotalCommentReply(total + ""); + if (total > 99) { + journalRespDTO.setTotalCommentReply("99+"); + } + } + + return journalRespDTO; + } + + private static String getEditor(String userName) { + return StringTools.isEmpty(userName) ? EDITOR[RandomSource.numberSource().randomInt(0, EDITOR.length)] + : userName; + } + + private static List getTags(String journalNo) { + return journalTagMap.getOrDefault(journalNo, Collections.emptyList()); + } + + private static String getEditDate(Journal journal) { + LocalDateTime date = null == journal.getUpdateTime() ? journal.getCreateTime() : journal.getUpdateTime(); + return DateUtil.format(date, DateTimePatternEnum.YYYY_DOT_MM_DOT_DD.getPattern()); + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/dto/mapper/SongMapper.java b/luoo_music/src/main/java/com/luoo/music/dto/mapper/SongMapper.java new file mode 100644 index 0000000..61a2c52 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/mapper/SongMapper.java @@ -0,0 +1,30 @@ +package com.luoo.music.dto.mapper; + +import java.util.Collections; +import java.util.Set; + +import com.luoo.music.dto.response.SongRespDTO; +import com.luoo.music.pojo.JournalSong; + +import constants.Constants; + +public class SongMapper { + public static SongRespDTO getSongRespDTO(JournalSong song, Set songCollectSet) { + SongRespDTO songRespDTO = new SongRespDTO(); + songRespDTO.setJournalNo(song.getJournalNo()); + songRespDTO.setAlbum(song.getAlbum()); + songRespDTO.setArtist(song.getArtist()); + songRespDTO.setId(song.getId()); + songRespDTO.setTitle(song.getName()); + songRespDTO.setSrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl()); + songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".jpg")); + songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".lyric")); + songRespDTO.setSongNo(song.getSongNo()); + songRespDTO.setHaveCollect(songCollectSet.contains(songRespDTO.getId())); + return songRespDTO; + } + + public static SongRespDTO getSongRespDTO(JournalSong song) { + return getSongRespDTO(song, Collections.emptySet()); + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/SearchResultDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/SearchResultDTO.java new file mode 100644 index 0000000..a18d5c4 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/SearchResultDTO.java @@ -0,0 +1,14 @@ +package com.luoo.music.dto.response; + +import java.util.List; + +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +public class SearchResultDTO { + @ApiModelProperty(value = "歌曲") + private List songs; + @ApiModelProperty(value = "期刊") + private List journals; +} diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java index 57aa4e7..1816eaa 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java @@ -275,4 +275,10 @@ public class JournalService { JOURNAL_RANGE_MAP.put(maxJournalNo, journalNoList); return journalNoList; } + + + public List fuzzySearch(String keyword) { + // TODO Auto-generated method stub + return Collections.emptyList(); + } } diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java index 86f2be4..ba297cd 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java @@ -17,6 +17,7 @@ import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -152,4 +153,9 @@ public class JournalSongService { public List orderByField(List objectIds) { return journalSongDao.orderByField(objectIds); } + + public List fuzzySearch(String keyword) { + // TODO Auto-generated method stub + return Collections.emptyList(); + } }