package cn.reghao.tnb.search.app.blog.db.repository;

import cn.reghao.tnb.search.app.blog.model.po.Article;
import cn.reghao.tnb.search.app.blog.model.vo.QuestionCount;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author reghao
 * @date 2024-01-28 13:17:47
 */
public interface ArticleRepository extends JpaRepository<Article, Integer>, JpaSpecificationExecutor<Article> {
    int countByCategoryIdAndPostType(int categoryId, int postType);
    Page<Article> findByCategoryIdAndPostType(int categoryId, int postType, Pageable pageable);
    Page<Article> findByPublishedIsTrueAndPostType(int postType, Pageable pageable);
    Article findByArticleId(String articleId);
    @Query("select new cn.reghao.tnb.search.app.blog.model.vo.QuestionCount(article.categoryId, count(article))\n" +
            "from Article article\n" +
            "where article.postType=2\n" +
            "group by article.categoryId \n" +
            "order by count(article) desc")
    List<QuestionCount> findQuestionCountByGroup();
}
