parent
80e2c33310
commit
177e77d4b8
@ -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);
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
@ -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…
Reference in new issue