From 18b2ce25da5aa77db6432896f66274fc01ef4863 Mon Sep 17 00:00:00 2001 From: Gary Date: Tue, 6 Feb 2024 13:52:28 +0800 Subject: [PATCH] 1.implement journal filter --- .../music/controller/JournalController.java | 3 +- .../com/luoo/music/dao/JournalTagDao.java | 3 + .../music/dto/request/JournalQueryReq.java | 6 +- .../luoo/music/service/JournalService.java | 67 +++++++++++++++---- .../music/service/JournalSongServiceTest.java | 10 +++ 5 files changed, 72 insertions(+), 17 deletions(-) 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 229ff1a..32af9e7 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 @@ -29,7 +29,6 @@ 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.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.mongodb.core.MongoTemplate; @@ -127,7 +126,7 @@ public class JournalController { queryReq.setPageNum(1); queryReq.setPageSize(10); } - Page pageList = journalService.queryPage(queryReq); + List pageList = journalService.queryPage(queryReq); List ids=pageList.stream().map(Journal::getId).collect(Collectors.toList()); Set journalCollectSet = null == user ? Collections.emptySet() : userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.JOURNAL); 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 059ea83..b055f7c 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 @@ -28,4 +28,7 @@ public interface JournalTagDao extends JpaRepository, JpaSpec @Transactional @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); } 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 bb36450..e2c21f7 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,12 @@ import java.io.Serializable; @ApiModel public class JournalQueryReq implements Serializable { private static final long serialVersionUID = -1198060864891902188L; - @ApiModelProperty(value = "筛选条件:风格") + @ApiModelProperty(value = "筛选条件:风格", example = "民谣") private String style; - @ApiModelProperty(value = "筛选条件:语言") + @ApiModelProperty(value = "筛选条件:语言", example = "国语") private String language; + @ApiModelProperty(value = "筛选条件:期刊号范围", example = "900~800") + private String journalNoRange; @ApiModelProperty(value = "分页: 页码,以1开始,即获取最新的几期", example = "1") private Integer pageNum = 1; @ApiModelProperty(value = "分页: 每页数量", example = "10") 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 c634140..7c65d4b 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 @@ -1,5 +1,6 @@ package com.luoo.music.service; +import java.time.LocalDateTime; import java.util.*; import java.util.stream.IntStream; @@ -9,6 +10,9 @@ import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import api.Result; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -19,6 +23,8 @@ import org.springframework.stereotype.Service; import util.IdWorker; import com.luoo.music.dao.JournalDao; +import com.luoo.music.dao.JournalTagDao; +import com.luoo.music.dao.TagDao; import com.luoo.music.dto.request.JournalQueryReq; import com.luoo.music.pojo.Journal; @@ -36,8 +42,8 @@ public class JournalService { @Autowired private IdWorker idWorker; - - + @Autowired + private JournalTagDao journalTagDao; @Autowired private RedisTemplate redisTemplate; @@ -152,29 +158,64 @@ public class JournalService { } - public Page queryPage(JournalQueryReq queryReq) { - Specification specification = createJournalSpecification(queryReq); + public List queryPage(JournalQueryReq queryReq) { + List ids=getJournalIds(queryReq); + Pair journalNoRange=getJournalNoRange(queryReq.getJournalNoRange()); + Specification specification = createJournalSpecification(ids,journalNoRange); + if(null==queryReq.getPageNum()||queryReq.getPageNum()<1||null==queryReq.getPageSize()||queryReq.getPageSize()<1) { + return journalDao.findAll(specification); + } PageRequest pageRequest = PageRequest.of(queryReq.getPageNum()-1, queryReq.getPageSize()); - return journalDao.findAll(specification, pageRequest); + return journalDao.findAll(specification, pageRequest).getContent(); } - /** - * - * as journal do not support style and langauge, only sort the result - */ - private Specification createJournalSpecification(JournalQueryReq queryReq) { + private Specification createJournalSpecification(List ids, Pair journalNoRange) { return new Specification() { private static final long serialVersionUID = 1L; @Override - public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { List predicateList = new ArrayList(); - query.orderBy(cb.desc(root.get("journalNo").as(Integer.class))); - return cb.and( predicateList.toArray(new Predicate[predicateList.size()])); + if(!ids.isEmpty()) { + predicateList.add(criteriaBuilder.in(root.get("id")).value(ids)); + } + if(Objects.nonNull(journalNoRange)) { + predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("journalNo").as(Integer.class), + journalNoRange.getLeft())); + + predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("journalNo").as(Integer.class), + journalNoRange.getRight())); + + } + query.orderBy(criteriaBuilder.desc(root.get("journalNo").as(Integer.class))); + return criteriaBuilder.and( predicateList.toArray(new Predicate[predicateList.size()])); } }; + } + + private Pair getJournalNoRange(String range) { + if(StringUtils.isNotBlank(range)){ + String[] segs=range.split("~"); + int start=Integer.valueOf(segs[0]); + int end=Integer.valueOf(segs[1]); + return start 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()); + } + return Collections.emptyList(); + } + + + public boolean isLatest10(String journalNo) { return null!=journalDao.isLatest10(journalNo); } diff --git a/luoo_music/src/test/java/com/luoo/music/service/JournalSongServiceTest.java b/luoo_music/src/test/java/com/luoo/music/service/JournalSongServiceTest.java index dc7d11a..e47c1d0 100644 --- a/luoo_music/src/test/java/com/luoo/music/service/JournalSongServiceTest.java +++ b/luoo_music/src/test/java/com/luoo/music/service/JournalSongServiceTest.java @@ -10,6 +10,16 @@ import org.junit.Test; public class JournalSongServiceTest { + @Test + public void testJournalNoRange() { + String range="900~800"; + String[] segs=range.split("~"); + int start=Integer.valueOf(segs[0]); + int end=Integer.valueOf(segs[1]); + System.out.println(start); + System.out.println(end); + } + @Test public void testJournalNoList() { getJournalNoList(100).forEach(System.out::println);