package com.tensquare.article.service;
import com.tensquare.article.dao.ArticleDao;
import com.tensquare.article.pojo.Article;
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 org.springframework.transaction.annotation.Transactional;
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;
import java.util.concurrent.TimeUnit;
@Service
@Transactional
public class ArticleService {
@Autowired
private ArticleDao articleDao;
@Autowired
private IdWorker idWorker;
//reids注解
@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, 10, TimeUnit.SECONDS);//过期时间
// }
// return article;
// }
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,10,TimeUnit.SECONDS);//设置10秒过期
}
return article;
}
/**
* 增加
* @param article
*/
public void add(Article article) {
article.setId( idWorker.nextId()+"" );
articleDao.save(article);
}
/**
* 修改
* @param article
*/
public void update(Article article) {
redisTemplate.delete("article_"+article.getId());
articleDao.save(article);
}
/**
* 删除
*/
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("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")+"%"));
}
return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
}
};
}
}