package com.yihu.editor.service;


import com.yihu.editor.dao.ArticleDao;
import com.yihu.editor.entity.SystemDictItem;
import com.yihu.editor.entity.YdfHealthEduArticle;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/11 0011.
 */
@Service
@Transactional
public class ArticleService {

    @Autowired
    private ArticleDao articleDao;

    public Page<YdfHealthEduArticle> findFeedback(int page, int rows, int type, int identity, int status) {
        if (page <= 0) {
            page = 1;
        }

        if (rows <= 0) {
            rows = 15;
        }
        // 排序
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        // 分页信息
        PageRequest pageRequest = new PageRequest(page - 1, rows, sort);
        // 设置查询条件
        Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
        if (type!=-1) {
            filters.put("type", new SearchFilter("type", SearchFilter.Operator.EQ, type));
        }
        if (identity!=-1) {
            filters.put("identity", new SearchFilter("identity", SearchFilter.Operator.EQ, identity));
        }
        if (status!=-1) {
            filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, status));
        }
        // 未删除
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.GT, "-1"));
        Specification<YdfHealthEduArticle> spec = DynamicSpecifications.bySearchFilter(filters.values(), YdfHealthEduArticle.class);
        return articleDao.findAll(spec, pageRequest);
    }

    public void save(YdfHealthEduArticle article){
        articleDao.save(article);
    }

    public List<SystemDictItem> findHealthEduArticleTypeList() {
        return articleDao.findByDict("4");
    }

    public void delete(String id) {
        articleDao.delete(id);
    }

    public YdfHealthEduArticle findById(String id) {
        return articleDao.findById(id);
    }
}
