From df98d5aef78e0394dace4219545c5a646768ade2 Mon Sep 17 00:00:00 2001 From: Gary Date: Wed, 21 Feb 2024 23:43:28 +0800 Subject: [PATCH] 1.seperate journal/song fuzzy search --- .../music/controller/SearchController.java | 45 +++++++++++++------ .../music/dto/request/FuzzySearchReq.java | 10 ++--- .../luoo/music/service/JournalService.java | 7 ++- .../music/service/JournalSongService.java | 5 +-- 4 files changed, 42 insertions(+), 25 deletions(-) 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 947f7e7..64e2557 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 @@ -18,6 +18,8 @@ import io.swagger.annotations.*; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Journal; import com.luoo.music.pojo.JournalSong; @@ -28,6 +30,7 @@ import com.luoo.music.service.SearchService; import com.luoo.music.service.TagService; import annotation.GlobalInterceptor; import annotation.VerifyParam; +import api.PageResult; import api.Result; import constants.Constants; @@ -65,25 +68,41 @@ public class SearchController { return Result.success(searchCategoryDTOs); } - @ApiOperation(value = "2.搜索期刊/歌曲", notes = "根据关键词模糊搜索,如 期刊号,歌曲名 等, limit 默认查10条") - @GetMapping("/fuzzy") + @ApiOperation(value = "2.1 搜索歌曲", notes = "根据关键词模糊搜索歌曲") + @GetMapping("/fuzzy/song") @GlobalInterceptor(checkAppUserLogin = true) - public Result fuzzySearch( + public Result> fuzzySearchSong( @RequestHeader(value = "Authorization", required = false) String authorization, @VerifyParam(required = true) FuzzySearchReq query) throws InterruptedException, ExecutionException { - SearchResultDTO searchResultDTO = new SearchResultDTO(); - int limit = getLimit(query.getLimit()); + PageRequest pageRequest = PageRequest.of(getPageNum(query.getPageNum()), getPageSize(query.getPageSize())); - CompletableFuture> cfa = CompletableFuture.supplyAsync(() -> journalService.fuzzySearch(query.getKeyword(),limit).stream().map(JournalMapper::getJournalRespDTO).collect(Collectors.toList())); - CompletableFuture> cfb = CompletableFuture.supplyAsync(() -> journalSongService.fuzzySearch(query.getKeyword(),limit).stream().map(SongMapper::getSongRespDTO).collect(Collectors.toList())); - List journals = cfa.get(); - List songs = cfb.get(); - - searchResultDTO.setJournals(journals); - searchResultDTO.setSongs(songs); - return Result.success(searchResultDTO); + Page pageResults=journalSongService.fuzzySearch(query.getKeyword(),pageRequest); + List list = pageResults.stream().map(SongMapper::getSongRespDTO) + .collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()), list)); + } + + private int getPageSize(Integer pageSize) { + return null==pageSize||0==pageSize?10:pageSize; + } + + private int getPageNum(Integer pageNum) { + return null==pageNum?0:pageNum-1; } + @ApiOperation(value = "2.2 搜索期刊", notes = "根据关键词模糊搜索期刊") + @GetMapping("/fuzzy/journal") + @GlobalInterceptor(checkAppUserLogin = true) + public Result> fuzzySearchJournal( + @RequestHeader(value = "Authorization", required = false) String authorization, + @VerifyParam(required = true) FuzzySearchReq query) throws InterruptedException, ExecutionException { + PageRequest pageRequest = PageRequest.of(getPageNum(query.getPageNum()), getPageSize(query.getPageSize())); + Page pageResults=journalService.fuzzySearch(query.getKeyword(),pageRequest); + List list = pageResults.stream().map(JournalMapper::getJournalRespDTO) + .collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()), list)); + } + @ApiOperation(value = "3.搜索自动补全", notes = "limit 默认查10条") @GetMapping("/autoComplete") @GlobalInterceptor(checkAppUserLogin = true) diff --git a/luoo_music/src/main/java/com/luoo/music/dto/request/FuzzySearchReq.java b/luoo_music/src/main/java/com/luoo/music/dto/request/FuzzySearchReq.java index f771416..60c69b1 100644 --- a/luoo_music/src/main/java/com/luoo/music/dto/request/FuzzySearchReq.java +++ b/luoo_music/src/main/java/com/luoo/music/dto/request/FuzzySearchReq.java @@ -7,9 +7,6 @@ import java.io.Serializable; import annotation.VerifyParam; -/** - * 搜索自动补全请求参数 - */ @Data @ApiModel public class FuzzySearchReq implements Serializable { @@ -18,6 +15,9 @@ public class FuzzySearchReq implements Serializable { @ApiModelProperty(value = "关键词", required = true) private String keyword; @VerifyParam(required = false) - @ApiModelProperty(value = "默认返10条", example = "10") - private Integer limit; + @ApiModelProperty(value = "分页: 页码", example = "1") + private Integer pageNum; + @VerifyParam(required = false) + @ApiModelProperty(value = "分页: 每页数量", example = "10") + private Integer pageSize; } 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 b63c741..ca29f41 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 @@ -284,10 +284,9 @@ public class JournalService { } - public List fuzzySearch(String keyword, int limit) { - PageRequest pageRequest = PageRequest.of(0, limit); + public Page fuzzySearch(String keyword, PageRequest pageRequest) { Specification specification = fuzzySpecification(keyword); - return journalDao.findAll(specification,pageRequest).getContent(); + return journalDao.findAll(specification,pageRequest); } private Specification fuzzySpecification(String keyword) { return new Specification() { @@ -298,7 +297,7 @@ public class JournalService { predicateList.add(criteriaBuilder.like(root.get("journalNo").as(String.class),"%"+keyword+"%")); predicateList.add(criteriaBuilder.like(root.get("title").as(String.class),"%"+keyword+"%")); - query.orderBy(criteriaBuilder.desc(root.get("journalNo").as(Integer.class))); + query.orderBy(criteriaBuilder.asc(root.get("journalNo").as(Integer.class))); return criteriaBuilder.or( predicateList.toArray(new Predicate[predicateList.size()])); } 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 e80889b..730ff2e 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 @@ -157,10 +157,9 @@ public class JournalSongService { return journalSongDao.orderByField(objectIds); } - public List fuzzySearch(String keyword, int limit) { - PageRequest pageRequest = PageRequest.of(0, limit); + public Page fuzzySearch(String keyword, PageRequest pageRequest) { Specification specification = fuzzySpecification(keyword); - return journalSongDao.findAll(specification,pageRequest).getContent(); + return journalSongDao.findAll(specification,pageRequest); } private Specification fuzzySpecification(String keyword) {