<?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.forum.dao.ForumDao">

    <resultMap id="forum" type="Forum">
        <id property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="content" column="content"/>
        <result property="flag" column="flag"/>
        <result property="views" column="views"/>
        <result property="updateTime" column="update_time"/>
        <result property="avatars" column="avatars"/>
        <result property="commentabled" column="commentabled"/>
        <result property="description" column="description"/>
        <result property="createTime" column="create_time"/>
        <result property="userId" column="user_id"/>
        <result property="like" column="like"/>
        <result property="tagIds" column="tag_ids"/>
        <association property="user" javaType="User">
            <id property="id" column="uid"/>
            <result property="nickname" column="nickname"/>
            <result property="username" column="username"/>
            <result property="email" column="email"/>
            <result property="avatar" column="avatar"/>
        </association>
        <collection property="tags" ofType="Tag">
            <id property="id" column="tagid"/>
            <result property="name" column="tagname"/>
        </collection>
<!--        <collection property="tagsId" ofType="tagIds">-->
<!--            <result property="tag_id" column="tag_id"/>-->
<!--        </collection>-->
    </resultMap>

    <resultMap id="ForumAndIds" type="forum">
        <id property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="updateTime" column="update_time"/>
<!--        <collection property="tagsId" ofType="tagIds">-->
<!--            <result property="tag_id" column="tag_id"/>-->
<!--        </collection>-->
    </resultMap>

    <delete id="deleteForum">
        delete from forum where id = #{id}
    </delete>

    <select id="getIndexForum" resultMap="forum">  /*主页帖子展示*/
        select b.id, b.title, b.avatars,b.flag, b.views, b.create_time, b.description,b.like,
        a.username, a.avatar
        from forum b,user a
        where  a.id = b.user_id order by b.create_time desc
    </select>

    <select id="getForumTadIds" resultMap="forum">  /*主页帖子展示*/
       select b.id, b.title, b.avatars, b.views, b.update_time, b.description,b.like,
        a.username, a.avatar,
        bt.tag_id
        from user a,forum b
        left join forum_tags bt on b.id=bt.forum_id
         where  a.id = b.user_id
--         order by b.update_time desc



-- 0.0.
-- 0
    </select>


    <select id="getAllForum" resultMap="forum">  /*后台帖子展示*/
         select b.id, b.title, b.create_time, b.commentabled
        from forum b  order by b.create_time desc
    </select>


    <select id="getByTagId" resultMap="forum">
        select b.id, b.title,b.flag, b.avatars, b.views, b.update_time, b.description,
        t1.name tagname, t1.id tagid,
        a.username, a.avatar
        from forum b,  user a, forum_tags tb, tag t1
        where  a.id = b.user_id and tb.forum_id = b.id and tb.tag_id = t1.id and t1.id = #{tagId}
        order by b.update_time desc
    </select>


    <select id="getForum" resultMap="forum"> /*后台展示帖子*/
        select b.id, b.flag, b.title, b.content,
         b.tag_ids, b.avatars, b.description, b.commentabled
        from forum b  where  b.id = #{id};
    </select>

    <select id="getDetailedForum" resultMap="forum">  /*帖子详情*/
        select b.id, b.avatars, b.flag, b.title, b.content, b.views,
        b.commentabled,b.like,a.username, a.avatar,tag.id tagid, tag.name tagname
        from forum b, user a, tag tag, forum_tags tb
        where b.user_id = a.id and tb.forum_id = b.id and tb.tag_id = tag.id and  b.id = #{id}
    </select>

    <select id="getSearchForum" resultMap="forum">
        <bind name="pattern" value="'%' + query + '%'"/>
        select b.id, b.title, b.avatars, b.views, b.create_time, b.description,b.like,
        a.username, a.avatar
        from forum b, user a
        where a.id = b.user_id and (b.title like #{pattern} or b.content like #{pattern})
        order by b.create_time desc
    </select>

    <select id="searchAllForum" parameterType="Forum" resultMap="forum">
        <bind name="pattern" value="'%' + title + '%'"/>
        /*模糊查询*/
        select b.id, b.title, b.update_time, b.published, t.id, t.name
        from forum b where b.title like #{pattern}

    </select>

    <update id="updateForum" parameterType="Forum">
        update forum set flag = #{flag} ,
        title = #{title}, content = #{content}, tag_ids = #{tagIds},
        avatars = #{avatars} , description = #{description}  ,
        commentabled = #{commentabled} ,update_time = #{updateTime} where id = #{id};
    </update>
    <update id="updateView">
        update forum b set b.views=b.views+1 where id=#{id};
    </update>
    <!--useGeneratedKeys="true"；使用自增主键获取主键值策略
        keyProperty；指定对应的主键属性，也就是mybatis获取到主键值以后，将这个值封装给javaBean的哪个属性
    -->
    <insert id="saveForum" parameterType="Forum" useGeneratedKeys="true" keyProperty="id">
        insert into forum (title, content, avatars, flag,
        views, commentabled, create_time, update_time,  tag_ids, user_id, description)
        values (#{title}, #{content}, #{avatars}, #{flag}, #{views}, #{commentabled}, #{createTime},
        #{updateTime}, #{tagIds}, #{userId}, #{description});
    </insert>

    <insert id="saveForumAndTag" parameterType="ForumAndTag">
        insert into forum_tags (tag_id, forum_id) values (#{tagId},#{forumId});
    </insert>


    <select id="findGroupYear" resultType="String">
        select DATE_FORMAT(b.updateTime, '%Y') from forum b
    </select>

<!--    查找所有月份月份-->
    <select id="findGroupMonth" resultType="String">
        select DATE_FORMAT(b.update_time, '%M') from forum b order by b.update_time desc
    </select>


<!--    把所有文章下的标签id查询出来-->
    <select id="findForumAndTagsByYear" resultMap="ForumAndIds">
     select b.title, b.id,b.update_time,
            bt.tag_id
            from forum b
            left join forum_tags bt on b.id=bt.forum_id
          where DATE_FORMAT(b.update_time, "%Y") = #{year} order by b.update_time desc;
    </select>



<!--    按年份+月份查找-->
    <select id="findByMonth" resultMap="forum">
        select b.title, b.update_time, b.id, b.flag
        from forum b
        where DATE_FORMAT(b.update_time, "%Y%M") = #{month}
    </select>

<!--最热推荐-->
    <select id="findForumByRank" resultType="rankForum">
        select b.title,b.id from forum b order by b.views desc limit 0,3
    </select>
<!--最新推荐-->
    <select id="getNewForum" resultType="rankForum">
        select b.title,b.id from forum b order by b.create_time desc limit 0,3
    </select>



    <select id="getLike"  resultType="Long">
        select b.like from forum b where b.id=#{id}
    </select>


    <update id="addLike" >
    update forum b set b.like=b.like+1 where b.id=#{id}
    </update>


    <select id="countView" resultType="java.lang.Integer">
        select count(views) from forum
    </select>



    <delete id="cancelLike">
         update forum b set b.like=b.like-1 where b.id=#{forumId}
    </delete>

</mapper>
