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