<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.quinn.mapper.BlogMapper">

    <select id="getTopBlog" resultType="com.quinn.pojo.Blog">
        select bid,title,(star + views) as views from
        (select bid,title,(select count(1) from qn_star where topic_id = bid and topic_category = 'BLOG') as star,views
        from qn_blog where sort = 0) t
        order by (views + star) desc limit 7
    </select>

    <select id="getBlogWithUserOrderBySortCount" resultType="int">
        select count(1)
        from qn_blog a
        where (case when a.category_id = '1' then a.author_id = #{userId} else 1 end)
    </select>

    <select id="getBlogWithUserOrderBySort" resultType="com.quinn.pojo.BlogWithUser">
        select a.id,a.bid,a.title,a.sort,a.views,a.author_id,a.category_id,a.category_name,a.gmt_create,a.gmt_update
        ,b.username,b.avatar,
        (select count(1) from qn_star c where c.topic_id = a.bid and c.topic_category = 'BLOG') as star
        from qn_blog a,qn_user b
        where a.author_id = b.uid
        and (case when a.category_id = '1' then a.author_id = #{userId} else 1 end)
        order by a.sort desc,a.gmt_create desc
        limit #{myPageParam.pageNum},#{myPageParam.size}
    </select>

    <select id="getMyBlogs" resultType="com.quinn.pojo.BlogWithUser">
        select a.id,a.bid,a.title,a.sort,a.views,a.author_id,a.category_id,a.category_name,a.gmt_create,a.gmt_update,
        (select count(1) from qn_star c where c.topic_id = a.bid and c.topic_category = 'BLOG') as star
        from qn_blog a
        where a.author_id = #{userId} order by a.gmt_create desc
        limit #{myPageParam.pageNum},#{myPageParam.size}
    </select>

</mapper>
