1.retire article in music

main
Gary 10 months ago
parent 80e2c33310
commit 177e77d4b8

@ -46,6 +46,12 @@ public class DateUtil {
return getSdf(pattern).format(date);
}
public static String format(LocalDateTime localDateTime, String pattern) {
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
Date date = Date.from(instant);
return getSdf(pattern).format(date);
}
public static Date parse(String dateStr, String pattern) {
try {
return getSdf(pattern).parse(dateStr);

@ -4,9 +4,9 @@ import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -26,9 +26,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import com.luoo.music.pojo.Article;
import com.luoo.music.pojo.Journal;
import com.luoo.music.pojo.Tag;
import com.luoo.music.service.ArticleService;
import com.luoo.music.service.JournalService;
import com.luoo.music.service.UserCollectService;
import annotation.GlobalInterceptor;
@ -60,7 +60,7 @@ public class JournalController {
@Autowired
private UserCollectService userCollectService;
@Autowired
private ArticleService articleService;
private JournalService journalService;
@Autowired
private TagDao tagDao;
@ -106,7 +106,7 @@ public class JournalController {
queryReq.setPageNum(1);
queryReq.setPageSize(10);
}
Page<Article> pageList = articleService.queryPage(queryReq);
Page<Journal> pageList = journalService.queryPage(queryReq);
Set<String> journalCollectSet = null == user ? Collections.emptySet()
: userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.JOURNAL);
List<JournalRespDTO> list = pageList.stream().map(a -> getJournalRespDTO(a, journalCollectSet))
@ -129,7 +129,7 @@ public class JournalController {
if (objectIds.isEmpty()) {
return Result.success(new PageResult<JournalRespDTO>(0L, Collections.emptyList()));
}
List<Article> pageList = articleService.orderByField(objectIds);
List<Journal> pageList = journalService.orderByField(objectIds);
Set<String> journalCollectSet = objectIds.isEmpty() ? Collections.emptySet() : new HashSet<>(objectIds);
List<JournalRespDTO> list = pageList.stream().map(a -> getJournalRespDTO(a, journalCollectSet))
.collect(Collectors.toList());
@ -143,24 +143,24 @@ public class JournalController {
@RequestHeader(value = "Authorization", required = false) String authorization,
@PathVariable @VerifyParam(required = true) String id) {
UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
Article journal = articleService.findById(id);
Journal journal = journalService.findById(id);
Set<String> journalCollectSet = null == user ? Collections.emptySet()
: userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.JOURNAL);
return Result.success(getJournalRespDTO(journal, journalCollectSet));
}
private JournalRespDTO getJournalRespDTO(Article article, Set<String> journalCollectSet) {
private JournalRespDTO getJournalRespDTO(Journal journal, Set<String> journalCollectSet) {
JournalRespDTO journalRespDTO = new JournalRespDTO();
journalRespDTO.setId(article.getId());
journalRespDTO.setJournalNo(article.getVolid());
journalRespDTO.setTitle(article.getTitle());
journalRespDTO.setImage(Constants.MUSIC_RESOURCE_PREFIX + article.getImage());
journalRespDTO.setDate(getEditDate(article));
journalRespDTO.setId(journal.getId());
journalRespDTO.setJournalNo(journal.getJournalNo());
journalRespDTO.setTitle(journal.getTitle());
journalRespDTO.setImage(Constants.MUSIC_RESOURCE_PREFIX + journal.getImage());
journalRespDTO.setDate(getEditDate(journal));
journalRespDTO.setHaveCollect(journalCollectSet.contains(journalRespDTO.getId()));
journalRespDTO.setIpLocation("广东");
journalRespDTO.setTags(getTags(journalRespDTO.getJournalNo()));
String content = article.getContent();
String content = journal.getContent();
if (StringTools.isEmpty(content)) {
Poem poem = RandomSource.languageSource().randomTangPoem();
journalRespDTO.setEditor(poem.getAuthor());
@ -184,8 +184,8 @@ public class JournalController {
return tagDao.random(limit).stream().map(Tag::getNameCh).sorted().collect(Collectors.toList());
}
private String getEditDate(Article article) {
Date date = null == article.getUpdatetime() ? article.getCreatetime() : article.getUpdatetime();
private String getEditDate(Journal journal) {
LocalDateTime date = null == journal.getUpdateTime() ? journal.getCreateTime() : journal.getUpdateTime();
return DateUtil.format(date, DateTimePatternEnum.YYYY_DOT_MM_DOT_DD.getPattern());
}

@ -1,9 +1,9 @@
package com.luoo.music.controller;
import com.luoo.music.dto.response.SongRespDTO;
import com.luoo.music.pojo.Song;
import com.luoo.music.service.ArticleService;
import com.luoo.music.service.SongService;
import com.luoo.music.pojo.JournalSong;
import com.luoo.music.service.JournalService;
import com.luoo.music.service.JournalSongService;
import com.luoo.music.service.UserCollectService;
import annotation.GlobalInterceptor;
@ -35,9 +35,9 @@ public class SongController {
@Autowired
private UserCollectService userCollectService;
@Autowired
private SongService songService;
private JournalSongService journalSongService;
@Autowired
private ArticleService articleService;
private JournalService journalService;
@Autowired
private JwtUtil jwtUtil;
@ -47,8 +47,8 @@ public class SongController {
* @return
*/
@GetMapping
public Result<List<Song>> findAll() {
return Result.success(songService.findAll());
public Result<List<JournalSong>> findAll() {
return Result.success(journalSongService.findAll());
}
@ApiOperation(value = "1.根据期刊号查询歌曲信息", notes = "若为游客期刊号须在最新10期内")
@ -61,7 +61,7 @@ public class SongController {
if (null == user && !isLatest10(journalNo)) {
return Result.unauthorized(null);
}
List<Song> songs = songService.findByVolid(journalNo);
List<JournalSong> songs = journalSongService.findByVolid(journalNo);
Set<String> songCollectSet = null == user ? Collections.emptySet()
: userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet))
@ -70,7 +70,7 @@ public class SongController {
}
private boolean isLatest10(String journalNo) {
return articleService.isLatest10(journalNo);
return journalService.isLatest10(journalNo);
}
@ApiOperation(value = "2.查询收藏歌曲信息")
@ -88,7 +88,7 @@ public class SongController {
if(objectIds.isEmpty()) {
return Result.success(new PageResult<SongRespDTO>(0L, Collections.emptyList()));
}
List<Song> songs = songService.orderByField(objectIds);
List<JournalSong> songs = journalSongService.orderByField(objectIds);
Set<String> songCollectSet = objectIds.isEmpty() ? Collections.emptySet() : new HashSet<>(objectIds);
List<SongRespDTO> results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet))
@ -103,7 +103,7 @@ public class SongController {
public Result<List<SongRespDTO>> random(
@RequestHeader(value = "Authorization", required = false) String authorization,
@PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) {
List<Song> songs = songService.random(limit);
List<JournalSong> songs = journalSongService.random(limit);
UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
Set<String> songCollectSet = null == user ? Collections.emptySet()
: userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG);
@ -118,15 +118,15 @@ public class SongController {
public Result<SongRespDTO> findById(@RequestHeader(value = "Authorization", required = false) String authorization,
@PathVariable @VerifyParam(required = true) String id) {
UserLoginDto user = jwtUtil.getUserLoginDto(authorization);
Song song = songService.findById(id);
JournalSong song = journalSongService.findById(id);
Set<String> songCollectSet = null == user ? Collections.emptySet()
: userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG);
return Result.success(getSongRespDTO(song, songCollectSet));
}
private SongRespDTO getSongRespDTO(Song song, Set<String> songCollectSet) {
private SongRespDTO getSongRespDTO(JournalSong song, Set<String> songCollectSet) {
SongRespDTO songRespDTO = new SongRespDTO();
songRespDTO.setJournalNo(song.getVolid());
songRespDTO.setJournalNo(song.getJournalNo());
songRespDTO.setAlbum(song.getAlbum());
songRespDTO.setArtist(song.getArtist());
songRespDTO.setId(song.getId());
@ -134,7 +134,7 @@ public class SongController {
songRespDTO.setSrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl());
songRespDTO.setPic(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".jpg"));
songRespDTO.setLrc(Constants.MUSIC_RESOURCE_PREFIX + song.getUrl().replace(".mp3", ".lyric"));
songRespDTO.setSongNo(song.getSongno());
songRespDTO.setSongNo(song.getSongNo());
songRespDTO.setHaveCollect(songCollectSet.contains(songRespDTO.getId()));
return songRespDTO;
}

@ -1,34 +0,0 @@
package com.luoo.music.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import com.luoo.music.pojo.Article;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
/**
* 访
* @author Administrator
*
*/
public interface ArticleDao extends JpaRepository<Article,String>,JpaSpecificationExecutor<Article>{
@Modifying
@Query(value = "update tb_article set state=1 where id=?",nativeQuery = true)
public void updateState(String id);
@Modifying
@Query(value = "update tb_article set thumbup=thumbup+1 where id=?",nativeQuery = true)
public void addThumbup(String id);
public Article findArticleByVolid(String volid);
@Query(value = "select * from tb_article where id in ?1 order by field(id,?1)", nativeQuery = true)
public List<Article> orderByField(List<String> idList);
}

@ -39,5 +39,8 @@ public interface JournalDao extends JpaRepository<Journal,String>,JpaSpecificati
@Transactional
@Query("UPDATE Journal j SET j.state = :state WHERE j.id = :id")
int updateJournalState(@Param("id") String id, @Param("state") String state);
@Query(value = "select * from tb_journal where id in ?1 order by field(id,?1)", nativeQuery = true)
List<Journal> orderByField(List<String> objectIds);
}

@ -40,4 +40,10 @@ public interface JournalSongDao extends JpaRepository<JournalSong,String>, JpaSp
@Transactional
@Query("DELETE FROM JournalSong js WHERE js.songId = :songId")
int deleteBySongId(@Param("songId") String songId);
@Query(value = "select * from tb_journal_song order by rand() limit ?1 ", nativeQuery = true)
List<JournalSong> random(int limit);
@Query(value = "select * from tb_journal_song where id in ?1 order by field(id,?1)", nativeQuery = true)
public List<JournalSong> orderByField(List<String> idList);
}

@ -1,192 +0,0 @@
package com.luoo.music.pojo;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
/**
*
* @author Administrator
*
*/
@Entity
@Table(name="tb_article")
public class Article implements Serializable{
@Id
private String id;//ID
private String columnid;//专栏ID
private String userid;//用户ID
private String volid;//剘刊号
private String title;//标题
private String content;//文章正文
private String image;//文章封面
private java.util.Date createtime;//发表日期
private java.util.Date updatetime;//修改日期
private String ispublic;//是否公开
private String istop;//是否置顶
private Integer visits;//浏览量
private Integer thumbup;//点赞数
private Integer comment;//评论数
private String state;//审核状态
private String channelid;//所属频道
private String url;//URL
private String type;//类型
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getColumnid() {
return columnid;
}
public void setColumnid(String columnid) {
this.columnid = columnid;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getVolid() {
return volid;
}
public void setVolid(String volid) {
this.volid = volid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public java.util.Date getCreatetime() {
return createtime;
}
public void setCreatetime(java.util.Date createtime) {
this.createtime = createtime;
}
public java.util.Date getUpdatetime() {
return updatetime;
}
public void setUpdatetime(java.util.Date updatetime) {
this.updatetime = updatetime;
}
public String getIspublic() {
return ispublic;
}
public void setIspublic(String ispublic) {
this.ispublic = ispublic;
}
public String getIstop() {
return istop;
}
public void setIstop(String istop) {
this.istop = istop;
}
public Integer getVisits() {
return visits;
}
public void setVisits(Integer visits) {
this.visits = visits;
}
public Integer getThumbup() {
return thumbup;
}
public void setThumbup(Integer thumbup) {
this.thumbup = thumbup;
}
public Integer getComment() {
return comment;
}
public void setComment(Integer comment) {
this.comment = comment;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getChannelid() {
return channelid;
}
public void setChannelid(String channelid) {
this.channelid = channelid;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (obj instanceof Article) {
Article vo = (Article) obj;
if (vo.title.equals(this.title) && vo.volid.equals(this.volid)) {
return true;
}
}
return false;
}
@Override
public int hashCode() {
return this.getTitle().hashCode()*this.getVolid().hashCode();
}
}

@ -2,6 +2,8 @@ package com.luoo.music.pojo;
import lombok.Data;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@ -13,9 +15,10 @@ import javax.persistence.Table;
@Data
@Entity
@Table(name="tb_journal_song")
public class JournalSong {
public class JournalSong implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Id
private String id;
/**

@ -1,249 +0,0 @@
package com.luoo.music.service;
import java.util.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import util.IdWorker;
import com.luoo.music.dao.ArticleDao;
import com.luoo.music.dto.request.JournalQueryReq;
import com.luoo.music.pojo.Article;
/**
*
*
* @author Administrator
*
*/
@Service
public class ArticleService {
@Autowired
private ArticleDao articleDao;
@Autowired
private IdWorker idWorker;
@Autowired
private RedisTemplate redisTemplate;
public void updateState(String id) {
articleDao.updateState(id);
}
public void addThumbup(String id){
articleDao.addThumbup(id);
}
/**
*
* @return
*/
public List<Article> findAll() {
return articleDao.findAll();
}
/**
* +
* @param whereMap
* @param page
* @param size
* @return
*/
public Page<Article> findSearch(Map whereMap, int page, int size) {
Specification<Article> specification = createSpecification(whereMap);
PageRequest pageRequest = PageRequest.of(page-1, size);
return articleDao.findAll(specification, pageRequest);
}
/**
*
* @param whereMap
* @return
*/
public List<Article> findSearch(Map whereMap) {
Specification<Article> specification = createSpecification(whereMap);
return articleDao.findAll(specification);
}
/**
* ID
* @param id
* @return
*/
public Article findById(String id) {
Article article = (Article) redisTemplate.opsForValue().get("article_"+id);
if (article==null) {
article =articleDao.findById(id).get();
redisTemplate.opsForValue().set("article_"+id,article);
}
return article;
}
/**
*
* @param article
*/
public void add(Article article) {
article.setId( idWorker.nextId()+"" );
article.setVisits(0);
article.setThumbup(0);
article.setComment(0);
article.setCreatetime(new Date());
article.setUpdatetime(new Date());
articleDao.save(article);
}
/**
*
* @param article
*/
public void update(Article article) {
redisTemplate.delete("article_"+article.getId());
articleDao.save(article);
}
/**
*
* @param id
*/
public void deleteById(String id) {
redisTemplate.delete("article_"+id);
articleDao.deleteById(id);
}
/**
*
* @param searchMap
* @return
*/
private Specification<Article> createSpecification(Map searchMap) {
return new Specification<Article>() {
@Override
public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicateList = new ArrayList<Predicate>();
// ID
if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
}
// 专栏ID
if (searchMap.get("columnid")!=null && !"".equals(searchMap.get("columnid"))) {
predicateList.add(cb.like(root.get("columnid").as(String.class), "%"+(String)searchMap.get("columnid")+"%"));
}
// 用户ID
if (searchMap.get("userid")!=null && !"".equals(searchMap.get("userid"))) {
predicateList.add(cb.like(root.get("userid").as(String.class), "%"+(String)searchMap.get("userid")+"%"));
}
// 剘刊号
if (searchMap.get("volid")!=null && !"".equals(searchMap.get("volid"))) {
predicateList.add(cb.like(root.get("volid").as(String.class), "%"+(String)searchMap.get("volid")+"%"));
}
// 标题
if (searchMap.get("title")!=null && !"".equals(searchMap.get("title"))) {
predicateList.add(cb.like(root.get("title").as(String.class), "%"+(String)searchMap.get("title")+"%"));
}
// 文章正文
if (searchMap.get("content")!=null && !"".equals(searchMap.get("content"))) {
predicateList.add(cb.like(root.get("content").as(String.class), "%"+(String)searchMap.get("content")+"%"));
}
// 文章封面
if (searchMap.get("image")!=null && !"".equals(searchMap.get("image"))) {
predicateList.add(cb.like(root.get("image").as(String.class), "%"+(String)searchMap.get("image")+"%"));
}
// 是否公开
if (searchMap.get("ispublic")!=null && !"".equals(searchMap.get("ispublic"))) {
predicateList.add(cb.like(root.get("ispublic").as(String.class), "%"+(String)searchMap.get("ispublic")+"%"));
}
// 是否置顶
if (searchMap.get("istop")!=null && !"".equals(searchMap.get("istop"))) {
predicateList.add(cb.like(root.get("istop").as(String.class), "%"+(String)searchMap.get("istop")+"%"));
}
// 审核状态
if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
predicateList.add(cb.like(root.get("state").as(String.class), "%"+(String)searchMap.get("state")+"%"));
}
// 所属频道
if (searchMap.get("channelid")!=null && !"".equals(searchMap.get("channelid"))) {
predicateList.add(cb.like(root.get("channelid").as(String.class), "%"+(String)searchMap.get("channelid")+"%"));
}
// URL
if (searchMap.get("url")!=null && !"".equals(searchMap.get("url"))) {
predicateList.add(cb.like(root.get("url").as(String.class), "%"+(String)searchMap.get("url")+"%"));
}
// 类型
if (searchMap.get("type")!=null && !"".equals(searchMap.get("type"))) {
predicateList.add(cb.like(root.get("type").as(String.class), "%"+(String)searchMap.get("type")+"%"));
}
query.orderBy(cb.desc(root.get("volid").as(Integer.class)));
return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
}
};
}
public Article findByVolid(String volid) {
return articleDao.findArticleByVolid(volid);
}
public Page<Article> queryPage(JournalQueryReq queryReq) {
Specification<Article> specification = createArticleSpecification(queryReq);
PageRequest pageRequest = PageRequest.of(queryReq.getPageNum()-1, queryReq.getPageSize());
return articleDao.findAll(specification, pageRequest);
}
/**
*
* as article do not support style and langauge, only sort the result
*/
private Specification<Article> createArticleSpecification(JournalQueryReq queryReq) {
return new Specification<Article>() {
private static final long serialVersionUID = 1L;
@Override
public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicateList = new ArrayList<Predicate>();
query.orderBy(cb.desc(root.get("volid").as(Integer.class)));
return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
}
};
}
public boolean isLatest10(String journalNo) {
// TODO Auto-generated method stub
return true;
}
public List<Article> orderByField(List<String> objectIds) {
return articleDao.orderByField(objectIds);
}
}

@ -0,0 +1,183 @@
package com.luoo.music.service;
import java.util.*;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import util.IdWorker;
import com.luoo.music.dao.JournalDao;
import com.luoo.music.dto.request.JournalQueryReq;
import com.luoo.music.pojo.Journal;
/**
*
*
* @author Administrator
*
*/
@Service
public class JournalService {
@Autowired
private JournalDao journalDao;
@Autowired
private IdWorker idWorker;
@Autowired
private RedisTemplate redisTemplate;
/*
* public void updateState(String id) { journalDao.updateState(id); }
*
*
* public void addThumbup(String id){ journalDao.addThumbup(id); }
*/
/**
*
* @return
*/
public List<Journal> findAll() {
return journalDao.findAll();
}
/**
* +
* @param whereMap
* @param page
* @param size
* @return
*/
public Page<Journal> findSearch(Map whereMap, int page, int size) {
Specification<Journal> specification = createSpecification(whereMap);
PageRequest pageRequest = PageRequest.of(page-1, size);
return journalDao.findAll(specification, pageRequest);
}
/**
*
* @param whereMap
* @return
*/
public List<Journal> findSearch(Map whereMap) {
Specification<Journal> specification = createSpecification(whereMap);
return journalDao.findAll(specification);
}
/**
* ID
* @param id
* @return
*/
public Journal findById(String id) {
Journal journal = (Journal) redisTemplate.opsForValue().get("journal_"+id);
if (journal==null) {
journal =journalDao.findById(id).get();
redisTemplate.opsForValue().set("journal_"+id,journal);
}
return journal;
}
/**
*
* @param searchMap
* @return
*/
private Specification<Journal> createSpecification(Map searchMap) {
return new Specification<Journal>() {
@Override
public Predicate toPredicate(Root<Journal> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicateList = new ArrayList<Predicate>();
// ID
if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
}
// 用户ID
if (searchMap.get("userId")!=null && !"".equals(searchMap.get("userId"))) {
predicateList.add(cb.like(root.get("userId").as(String.class), "%"+(String)searchMap.get("userId")+"%"));
}
// 剘刊号
if (searchMap.get("journalNo")!=null && !"".equals(searchMap.get("journalNo"))) {
predicateList.add(cb.like(root.get("journalNo").as(String.class), "%"+(String)searchMap.get("journalNo")+"%"));
}
// 标题
if (searchMap.get("title")!=null && !"".equals(searchMap.get("title"))) {
predicateList.add(cb.like(root.get("title").as(String.class), "%"+(String)searchMap.get("title")+"%"));
}
// 文章正文
if (searchMap.get("content")!=null && !"".equals(searchMap.get("content"))) {
predicateList.add(cb.like(root.get("content").as(String.class), "%"+(String)searchMap.get("content")+"%"));
}
// 文章封面
if (searchMap.get("image")!=null && !"".equals(searchMap.get("image"))) {
predicateList.add(cb.like(root.get("image").as(String.class), "%"+(String)searchMap.get("image")+"%"));
}
// 是否公开
if (searchMap.get("isPublish")!=null && !"".equals(searchMap.get("isPublish"))) {
predicateList.add(cb.like(root.get("isPublish").as(String.class), "%"+(String)searchMap.get("isPublish")+"%"));
}
// 审核状态
if (searchMap.get("state")!=null && !"".equals(searchMap.get("state"))) {
predicateList.add(cb.like(root.get("state").as(String.class), "%"+(String)searchMap.get("state")+"%"));
}
query.orderBy(cb.desc(root.get("journalNo").as(Integer.class)));
return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
}
};
}
public Page<Journal> queryPage(JournalQueryReq queryReq) {
Specification<Journal> specification = createJournalSpecification(queryReq);
PageRequest pageRequest = PageRequest.of(queryReq.getPageNum()-1, queryReq.getPageSize());
return journalDao.findAll(specification, pageRequest);
}
/**
*
* as journal do not support style and langauge, only sort the result
*/
private Specification<Journal> createJournalSpecification(JournalQueryReq queryReq) {
return new Specification<Journal>() {
private static final long serialVersionUID = 1L;
@Override
public Predicate toPredicate(Root<Journal> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
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()]));
}
};
}
public boolean isLatest10(String journalNo) {
return true;
}
public List<Journal> orderByField(List<String> objectIds) {
return journalDao.orderByField(objectIds);
}
}

@ -0,0 +1,155 @@
package com.luoo.music.service;
import com.luoo.music.dao.JournalSongDao;
import com.luoo.music.pojo.JournalSong;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
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 util.IdWorker;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class JournalSongService {
@Autowired
private JournalSongDao journalSongDao;
@Autowired
private IdWorker idWorker;
/**
*
*/
public List<JournalSong> findAll(){
return journalSongDao.findAll();
}
/**
* +
*/
public Page<JournalSong> findSearch(Map whereMap, int page, int size) {
Specification<JournalSong> specification = createSpecification(whereMap);
PageRequest pageRequest = PageRequest.of(page-1, size);
return journalSongDao.findAll(specification, pageRequest);
}
/**
*
* @param whereMap
* @return
*/
public List<JournalSong> findSearch(Map whereMap) {
Specification<JournalSong> specification = createSpecification(whereMap);
return journalSongDao.findAll(specification);
}
/**
* ID
* @param id
* @return
*/
@Cacheable(value = "journalSong",key = "#id")
public JournalSong findById(String id) {
return journalSongDao.findById(id).get();
}
/**
*
* @param song
*/
public void add(JournalSong song) {
song.setId( idWorker.nextId()+"" );
journalSongDao.save(song);
}
/**
*
* @param song
*/
@CacheEvict(value = "journalSong",key = "#journalSong.id")
public void update(JournalSong song) {
journalSongDao.save(song);
}
/**
*
* @param id
*/
@CacheEvict(value = "journalSong",key = "#id")
public void deleteById(String id) {
journalSongDao.deleteById(id);
}
/**
*
* @param searchMap
* @return
*/
private Specification<JournalSong> createSpecification(Map searchMap) {
return new Specification<JournalSong>() {
@Override
public Predicate toPredicate(Root<JournalSong> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicateList = new ArrayList<Predicate>();
// ID
if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
}
// 专栏ID
if (searchMap.get("name")!=null && !"".equals(searchMap.get("name"))) {
predicateList.add(cb.like(root.get("name").as(String.class), "%"+(String)searchMap.get("name")+"%"));
}
// 用户ID
if (searchMap.get("artist")!=null && !"".equals(searchMap.get("artist"))) {
predicateList.add(cb.like(root.get("artist").as(String.class), "%"+(String)searchMap.get("artist")+"%"));
}
// 剘刊号
if (searchMap.get("journalNo")!=null && !"".equals(searchMap.get("journalNo"))) {
predicateList.add(cb.like(root.get("journalNo").as(String.class), "%"+(String)searchMap.get("journalNo")+"%"));
}
// 标题
if (searchMap.get("album")!=null && !"".equals(searchMap.get("album"))) {
predicateList.add(cb.like(root.get("album").as(String.class), "%"+(String)searchMap.get("album")+"%"));
}
// 文章正文
if (searchMap.get("url")!=null && !"".equals(searchMap.get("url"))) {
predicateList.add(cb.like(root.get("url").as(String.class), "%"+(String)searchMap.get("url")+"%"));
}
query.orderBy(cb.desc(root.get("songNo").as(Integer.class)));
return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
}
};
}
public List<JournalSong> findByVolid(String journalNo){
return journalSongDao.findByJournalNo(journalNo);
}
public List<JournalSong> random(Integer limit) {
return journalSongDao.random(limit);
}
public List<JournalSong> orderByField(List<String> objectIds) {
return journalSongDao.orderByField(objectIds);
}
}
Loading…
Cancel
Save