1.implement journal filter

main
Gary 10 months ago
parent 640cc5f51f
commit 18b2ce25da

@ -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<Journal> pageList = journalService.queryPage(queryReq);
List<Journal> pageList = journalService.queryPage(queryReq);
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);

@ -28,4 +28,7 @@ public interface JournalTagDao extends JpaRepository<JournalTag,String>, 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<String> getJournalIdByLevelAndChName(int level,String chName);
}

@ -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")

@ -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<Journal> queryPage(JournalQueryReq queryReq) {
Specification<Journal> specification = createJournalSpecification(queryReq);
public List<Journal> queryPage(JournalQueryReq queryReq) {
List<String> ids=getJournalIds(queryReq);
Pair<Integer,Integer> journalNoRange=getJournalNoRange(queryReq.getJournalNoRange());
Specification<Journal> 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<Journal> createJournalSpecification(JournalQueryReq queryReq) {
private Specification<Journal> createJournalSpecification(List<String> ids, Pair<Integer, Integer> journalNoRange) {
return new Specification<Journal>() {
private static final long serialVersionUID = 1L;
@Override
public Predicate toPredicate(Root<Journal> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
public Predicate toPredicate(Root<Journal> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicateList = new ArrayList<Predicate>();
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<Integer, Integer> 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<end?Pair.of(start, end):Pair.of(end, start);
}
return null;
}
private List<String> 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);
}

@ -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);

Loading…
Cancel
Save