From 177e77d4b84abf83c7ea8a1f7286b955a7260bbd Mon Sep 17 00:00:00 2001 From: Gary Date: Mon, 29 Jan 2024 23:36:32 +0800 Subject: [PATCH] 1.retire article in music --- luoo_common/src/main/java/util/DateUtil.java | 6 + .../music/controller/JournalController.java | 32 +-- .../luoo/music/controller/SongController.java | 30 +-- .../java/com/luoo/music/dao/ArticleDao.java | 34 --- .../java/com/luoo/music/dao/JournalDao.java | 3 + .../com/luoo/music/dao/JournalSongDao.java | 6 + .../java/com/luoo/music/pojo/Article.java | 192 -------------- .../java/com/luoo/music/pojo/JournalSong.java | 7 +- .../luoo/music/service/ArticleService.java | 249 ------------------ .../luoo/music/service/JournalService.java | 183 +++++++++++++ .../music/service/JournalSongService.java | 155 +++++++++++ 11 files changed, 389 insertions(+), 508 deletions(-) delete mode 100644 luoo_music/src/main/java/com/luoo/music/dao/ArticleDao.java delete mode 100644 luoo_music/src/main/java/com/luoo/music/pojo/Article.java delete mode 100644 luoo_music/src/main/java/com/luoo/music/service/ArticleService.java create mode 100644 luoo_music/src/main/java/com/luoo/music/service/JournalService.java create mode 100644 luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java diff --git a/luoo_common/src/main/java/util/DateUtil.java b/luoo_common/src/main/java/util/DateUtil.java index e14a530..3959f4e 100644 --- a/luoo_common/src/main/java/util/DateUtil.java +++ b/luoo_common/src/main/java/util/DateUtil.java @@ -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); 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 c58f815..d1b3780 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 @@ -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
pageList = articleService.queryPage(queryReq); + Page pageList = journalService.queryPage(queryReq); Set journalCollectSet = null == user ? Collections.emptySet() : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.JOURNAL); List list = pageList.stream().map(a -> getJournalRespDTO(a, journalCollectSet)) @@ -129,7 +129,7 @@ public class JournalController { if (objectIds.isEmpty()) { return Result.success(new PageResult(0L, Collections.emptyList())); } - List
pageList = articleService.orderByField(objectIds); + List pageList = journalService.orderByField(objectIds); Set journalCollectSet = objectIds.isEmpty() ? Collections.emptySet() : new HashSet<>(objectIds); List 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 journalCollectSet = null == user ? Collections.emptySet() : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.JOURNAL); return Result.success(getJournalRespDTO(journal, journalCollectSet)); } - private JournalRespDTO getJournalRespDTO(Article article, Set journalCollectSet) { + private JournalRespDTO getJournalRespDTO(Journal journal, Set 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()); } diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java index ae8515d..02ed3fc 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java @@ -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> findAll() { - return Result.success(songService.findAll()); + public Result> 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 songs = songService.findByVolid(journalNo); + List songs = journalSongService.findByVolid(journalNo); Set songCollectSet = null == user ? Collections.emptySet() : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); List 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(0L, Collections.emptyList())); } - List songs = songService.orderByField(objectIds); + List songs = journalSongService.orderByField(objectIds); Set songCollectSet = objectIds.isEmpty() ? Collections.emptySet() : new HashSet<>(objectIds); List results = songs.stream().map(s -> getSongRespDTO(s, songCollectSet)) @@ -103,7 +103,7 @@ public class SongController { public Result> random( @RequestHeader(value = "Authorization", required = false) String authorization, @PathVariable @VerifyParam(required = true, regex = VerifyRegexEnum.RANDOM_SONG_LIMIT) Integer limit) { - List songs = songService.random(limit); + List songs = journalSongService.random(limit); UserLoginDto user = jwtUtil.getUserLoginDto(authorization); Set songCollectSet = null == user ? Collections.emptySet() : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); @@ -118,15 +118,15 @@ public class SongController { public Result 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 songCollectSet = null == user ? Collections.emptySet() : userCollectService.getCollectSet(user.getUserId(), CollectTypeEnum.SONG); return Result.success(getSongRespDTO(song, songCollectSet)); } - private SongRespDTO getSongRespDTO(Song song, Set songCollectSet) { + private SongRespDTO getSongRespDTO(JournalSong song, Set 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; } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/ArticleDao.java b/luoo_music/src/main/java/com/luoo/music/dao/ArticleDao.java deleted file mode 100644 index 3ef1705..0000000 --- a/luoo_music/src/main/java/com/luoo/music/dao/ArticleDao.java +++ /dev/null @@ -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,JpaSpecificationExecutor
{ - - @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
orderByField(List idList); - -} diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java index 1babe1f..9bac3a9 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalDao.java @@ -39,5 +39,8 @@ public interface JournalDao extends JpaRepository,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 orderByField(List objectIds); } diff --git a/luoo_music/src/main/java/com/luoo/music/dao/JournalSongDao.java b/luoo_music/src/main/java/com/luoo/music/dao/JournalSongDao.java index c65d41d..62ce2bd 100644 --- a/luoo_music/src/main/java/com/luoo/music/dao/JournalSongDao.java +++ b/luoo_music/src/main/java/com/luoo/music/dao/JournalSongDao.java @@ -40,4 +40,10 @@ public interface JournalSongDao extends JpaRepository, 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 random(int limit); + + @Query(value = "select * from tb_journal_song where id in ?1 order by field(id,?1)", nativeQuery = true) + public List orderByField(List idList); } diff --git a/luoo_music/src/main/java/com/luoo/music/pojo/Article.java b/luoo_music/src/main/java/com/luoo/music/pojo/Article.java deleted file mode 100644 index 14bb346..0000000 --- a/luoo_music/src/main/java/com/luoo/music/pojo/Article.java +++ /dev/null @@ -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(); - } -} diff --git a/luoo_music/src/main/java/com/luoo/music/pojo/JournalSong.java b/luoo_music/src/main/java/com/luoo/music/pojo/JournalSong.java index 50181c8..2bf3522 100644 --- a/luoo_music/src/main/java/com/luoo/music/pojo/JournalSong.java +++ b/luoo_music/src/main/java/com/luoo/music/pojo/JournalSong.java @@ -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; /** diff --git a/luoo_music/src/main/java/com/luoo/music/service/ArticleService.java b/luoo_music/src/main/java/com/luoo/music/service/ArticleService.java deleted file mode 100644 index 67a77c4..0000000 --- a/luoo_music/src/main/java/com/luoo/music/service/ArticleService.java +++ /dev/null @@ -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
findAll() { - return articleDao.findAll(); - } - - - /** - * 条件查询+分页 - * @param whereMap - * @param page - * @param size - * @return - */ - public Page
findSearch(Map whereMap, int page, int size) { - Specification
specification = createSpecification(whereMap); - PageRequest pageRequest = PageRequest.of(page-1, size); - return articleDao.findAll(specification, pageRequest); - } - - - /** - * 条件查询 - * @param whereMap - * @return - */ - public List
findSearch(Map whereMap) { - Specification
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
createSpecification(Map searchMap) { - - return new Specification
() { - - @Override - public Predicate toPredicate(Root
root, CriteriaQuery query, CriteriaBuilder cb) { - List predicateList = new ArrayList(); - // 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
queryPage(JournalQueryReq queryReq) { - Specification
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
createArticleSpecification(JournalQueryReq queryReq) { - return new Specification
() { - private static final long serialVersionUID = 1L; - @Override - public Predicate toPredicate(Root
root, CriteriaQuery query, CriteriaBuilder cb) { - List predicateList = new ArrayList(); - 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
orderByField(List objectIds) { - return articleDao.orderByField(objectIds); - } -} 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 new file mode 100644 index 0000000..c34bb89 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalService.java @@ -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 findAll() { + return journalDao.findAll(); + } + + + /** + * 条件查询+分页 + * @param whereMap + * @param page + * @param size + * @return + */ + public Page findSearch(Map whereMap, int page, int size) { + Specification specification = createSpecification(whereMap); + PageRequest pageRequest = PageRequest.of(page-1, size); + return journalDao.findAll(specification, pageRequest); + } + + + /** + * 条件查询 + * @param whereMap + * @return + */ + public List findSearch(Map whereMap) { + Specification 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 createSpecification(Map searchMap) { + + return new Specification() { + + @Override + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { + List predicateList = new ArrayList(); + // 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 queryPage(JournalQueryReq queryReq) { + Specification 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 createJournalSpecification(JournalQueryReq queryReq) { + return new Specification() { + private static final long serialVersionUID = 1L; + @Override + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { + List predicateList = new ArrayList(); + 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 orderByField(List objectIds) { + return journalDao.orderByField(objectIds); + } +} diff --git a/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java b/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java new file mode 100644 index 0000000..86f2be4 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/service/JournalSongService.java @@ -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 findAll(){ + return journalSongDao.findAll(); + } + + /** + * 条件查询+分页 + */ + + public Page findSearch(Map whereMap, int page, int size) { + Specification specification = createSpecification(whereMap); + PageRequest pageRequest = PageRequest.of(page-1, size); + return journalSongDao.findAll(specification, pageRequest); + } + + + /** + * 条件查询 + * @param whereMap + * @return + */ + public List findSearch(Map whereMap) { + Specification 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 createSpecification(Map searchMap) { + + return new Specification() { + + @Override + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { + List predicateList = new ArrayList(); + // 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 findByVolid(String journalNo){ + return journalSongDao.findByJournalNo(journalNo); + } + + public List random(Integer limit) { + return journalSongDao.random(limit); + } + + public List orderByField(List objectIds) { + return journalSongDao.orderByField(objectIds); + } +}