1.add random song by topic

main
Gary 3 months ago
parent 5ad3d47065
commit c0ae79ab2e

@ -2,6 +2,9 @@ package com.luoo.music.controller;
import com.luoo.music.dto.mapper.SongMapper;
import com.luoo.music.dto.request.CollectQueryReq;
import com.luoo.music.dto.request.FuzzySearchReq;
import com.luoo.music.dto.request.RandomTopicReq;
import com.luoo.music.dto.response.RandomTopicDTO;
import com.luoo.music.dto.response.SongRespDTO;
import com.luoo.music.pojo.JournalSong;
import com.luoo.music.service.JournalService;
@ -165,6 +168,34 @@ public class SongController {
return Result.success(results);
}
@ApiOperation(value = "3.1 获取随机播放歌曲主题")
@GetMapping("/randomTopic")
public Result<List<RandomTopicDTO>> randomTopic() {
return Result.success(journalSongService.getRandomTopic());
}
@ApiOperation(value = "3.2 按主题随机播放歌曲", notes = "雀乐主题FM")
@GetMapping("/random")
@GlobalInterceptor
public Result<List<SongRespDTO>> randomTopicSong(
@RequestHeader(value = "Authorization", required = false) String authorization,
@VerifyParam(required = true) RandomTopicReq query) {
if(!journalSongService.isValidTopic(query.getTopicId())) {
return random(authorization,query.getLimit());
}
List<JournalSong> songs = journalSongService.randomSongByTopic(query);
UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
List<String> ids=songs.stream().map(JournalSong::getJournalNoSongId).collect(Collectors.toList());
Set<String> songCollectSet = null == user ? Collections.emptySet()
: userCollectInfoService.getCollectSet(user.getUserId(),ids, CollectTypeEnum.SONG);
List<SongRespDTO> results = songs.stream().map(s -> SongMapper.getSongRespDTO(s, songCollectSet))
.collect(Collectors.toList());
return Result.success(results);
}
@ApiOperation(value = "4.根据歌曲id查询歌曲信息")
@GetMapping("/{id}")
@GlobalInterceptor

@ -97,5 +97,8 @@ public interface JournalDao extends JpaRepository<Journal,String>,JpaSpecificati
@Query(value = "select tbl.* from (select tbl1.* from tb_journal tbl1 where state='1' and is_publish='1' order by user_collect_count DESC LIMIT 50) tbl order by rand() limit 8", nativeQuery = true)
List<Journal> recommend();
@Query(value = "select j.journal_no from indie_music.tb_journal as j, indie_music.tb_journal_tag as jt where j.id=jt.journal_id and jt.tag_id in (select id from indie_music.tb_tag_info where name_ch in ?1)", nativeQuery = true)
List<Integer> getJournalNoByTags(List<String> tags);
}

@ -10,6 +10,7 @@ import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author locust
@ -65,4 +66,7 @@ public interface JournalSongDao extends JpaRepository<JournalSong,String>, JpaSp
@Query(value = "select * from tb_journal_song tjs where journal_no = (select journal_no from tb_journal where id=?1) order by song_no", nativeQuery = true)
List<JournalSong> findByJournalId(String journalId);
@Query(value = "select * from tb_journal_song where journal_no in ?1 order by rand() limit ?2 ", nativeQuery = true)
List<JournalSong> random(List<Integer> journalNos, Integer limit);
}

@ -0,0 +1,24 @@
package com.luoo.music.dto.request;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import annotation.VerifyParam;
import enums.VerifyRegexEnum;
/**
*
*/
@Data
@ApiModel
public class RandomTopicReq implements Serializable {
private static final long serialVersionUID = 1L;
@VerifyParam(required = false)
@ApiModelProperty(value = "主题id", required = false)
private Integer topicId;
@VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT)
@ApiModelProperty(value = "随机歌曲数最少1首最多30首", example = "10")
private Integer limit;
}

@ -0,0 +1,14 @@
package com.luoo.music.dto.response;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class RandomTopicDTO implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
}

@ -9,6 +9,8 @@ import org.springframework.stereotype.Component;
import com.luoo.music.dao.JournalDao;
import com.luoo.music.dto.request.JournalQueryReq;
import com.luoo.music.pojo.Journal;
import com.luoo.music.service.JournalSongService;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@ -22,12 +24,16 @@ public class CleanJournalCache {
@Autowired
private JournalDao journalDao;
@Autowired
private JournalSongService journalSongService;
@Async
public void cleanCache() {
try {
cacheChannel.evict("default", "journal_filter");
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_PAGE);
cacheChannel.clear(Constants.J2CACHE_REGION_JOURNAL_QUERY_COUNT);
journalSongService.updateTopicMap();
}catch(Exception e) {
e.printStackTrace();
}
@ -47,6 +53,7 @@ public class CleanJournalCache {
String journalNo=optional.get().getJournalNo();
cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_SONG_LIST, journalNo);
cacheChannel.evict(constants.Constants.J2CACHE_REGION_JOURNAL_NO,journalNo);
journalSongService.updateTopicMap();
}catch(Exception e) {
e.printStackTrace();
}

@ -1,37 +1,84 @@
package com.luoo.music.service;
import com.luoo.music.dao.JournalDao;
import com.luoo.music.dao.JournalSongDao;
import com.luoo.music.dto.mapper.SongMapper;
import com.luoo.music.dto.request.RandomTopicReq;
import com.luoo.music.dto.response.RandomTopicDTO;
import com.luoo.music.dto.response.SongRespDTO;
import com.luoo.music.pojo.JournalSong;
import constants.Constants;
import lombok.extern.slf4j.Slf4j;
import net.oschina.j2cache.CacheChannel;
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.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
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;
@Service
@Slf4j
public class JournalSongService {
@Autowired
private JournalSongDao journalSongDao;
@Autowired
private JournalDao journalDao;
@Autowired
private CacheChannel cacheChannel;
private List<RandomTopicDTO> randomTopicDTOs = new ArrayList<>();
private Map<Integer,List<Integer>> topicJournalMap = new HashMap<>();
private static final String TOPIC_FILE_PATH="topic.txt";
@PostConstruct
private void init() {
getLines(TOPIC_FILE_PATH).forEach(s->{
String[] segs=s.split("\\|");
Integer id=Integer.valueOf(segs[0]);
String name=segs[1];
List<String> tags=Arrays.stream(segs[2].split(",")).collect(Collectors.toList());
List<Integer> journalNos=getJournalNoByTags(tags);
randomTopicDTOs.add(new RandomTopicDTO(id,name));
topicJournalMap.put(id, journalNos);
});
}
private List<Integer> getJournalNoByTags(List<String> tags) {
return journalDao.getJournalNoByTags(tags);
}
private static List<String> 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) {
log.error(e.getMessage());
}
return Collections.emptyList();
}
/**
*
*/
@ -172,4 +219,26 @@ public class JournalSongService {
}
};
}
public List<RandomTopicDTO> getRandomTopic() {
return randomTopicDTOs;
}
public boolean isValidTopic(Integer topicId) {
return null!=topicId&&topicJournalMap.containsKey(topicId);
}
public List<JournalSong> randomSongByTopic(RandomTopicReq query) {
return journalSongDao.random(topicJournalMap.get(query.getTopicId()), query.getLimit());
}
public void updateTopicMap() {
getLines(TOPIC_FILE_PATH).forEach(s->{
String[] segs=s.split("\\|");
Integer id=Integer.valueOf(segs[0]);
List<String> tags=Arrays.stream(segs[2].split(",")).collect(Collectors.toList());
List<Integer> journalNos=getJournalNoByTags(tags);
topicJournalMap.put(id, journalNos);
});
}
}

@ -0,0 +1,5 @@
1|力量之源|金属,朋克,说唱,摇滚
2|靡靡之音|民谣,流行,爵士,乡村,蓝调,雷鬼
3|情绪之下|后摇,世界音乐,古典,原声,口哨,轻音乐
4|黑暗之舞|暗潮,仙音,新民谣,黑暗民谣
5|节奏之魂|电子
Loading…
Cancel
Save