<?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.nbclass.activity.mapper.CommentMapper">

    <resultMap id="Content" type="com.nbclass.activity.model.Content">
        <id column="id" property="id"/>
        <result column="createtime" property="createtime"/>
        <result column="updatetime" property="updatetime"/>
        <result column="type" property="type"/>
        <result column="isvalid" property="isvalid"/>
        <result column="fromid" property="fromid"/>
        <result column="title" property="title"/>
        <result column="desct" property="desct"/>
        <result column="viewno" property="viewno"/>
        <result column="praiseno" property="praiseno"/>
        <result column="collection_number" property="collectionNumber"/>
        <result column="listicon" property="listicon"/>
        <result column="qrcode" property="qrcode"/>
        <result column="sort" property="sort"/>
        <result column="url" property="url"/>
        <result column="content" property="content"/>
        <result column="content_text" property="content_text"/>
        <result column="attachment" property="attachment"/>
        <result column="TotalScore" property="TotalScore"/>
        <result column="userTotal" property="userTotal"/>

        <collection property="tagList" ofType="com.nbclass.activity.model.ContentTags" column="id" select="getTags">
            <id column="id" property="id"/>
            <result column="tagName" property="tagName"/>
        </collection>

        <collection property="commentators" ofType="com.nbclass.activity.model.Comment" column="id" select="getComments">
            <id column="id" property="id"/>
            <result column="content_id" property="contentId"/>
            <result column="user_id" property="userId"/>
            <result column="reply_id" property="replyId"/>
            <result column="comment_id" property="commentId"/>
            <result column="comment_content" property="commentContent"/>
            <result column="create_time" property="createTime"/>
            <result column="praiseno" property="praiseNo"/>
            <result column="reply_number" property="replyNumber"/>
        </collection>

        <!--<collection property="commentators" ofType="com.nbclass.activity.model.Comment" column="id" select="getComments">
            <id column="id" property="id"/>
            <result column="content_id" property="contentId"/>
            <result column="user_id" property="userId"/>
            <result column="reply_id" property="replyId"/>
            <result column="comment_id" property="commentId"/>
            <result column="comment_content" property="commentContent"/>
            <result column="create_time" property="createTime"/>
            <result column="praiseno" property="praiseNo"/>
        </collection>-->
    </resultMap>

    <resultMap id="Comment" type="com.nbclass.activity.model.Comment">
        <id column="id" property="id"/>
        <result column="content_id" property="contentId"/>
        <result column="user_id" property="userId"/>
        <result column="reply_id" property="replyId"/>
        <result column="comment_content" property="commentContent"/>
        <result column="create_time" property="createTime"/>
        <result column="praiseno" property="praiseNo"/>
        <result column="reply_number" property="replyNumber"/>
        <result column="nickname" property="nickname"/>
        <result column="headimgurl" property="headimgurl"/>
        <result column="praiseId" property="praiseId"/>
        <collection property="respondents" ofType="com.nbclass.activity.model.Comment" column="prop1=content_id,prop2=id,userId=user_id" select="getCommentReply" >
            <id column="id" property="id"/>
            <result column="content_id" property="contentId"/>
            <result column="user_id" property="userId"/>
            <result column="reply_id" property="replyId"/>
            <result column="comment_id" property="commentId"/>
            <result column="comment_content" property="commentContent"/>
            <result column="create_time" property="createTime"/>
            <result column="praiseno" property="praiseNo"/>
        </collection>
    </resultMap>

    <resultMap id="Comment2" type="com.nbclass.activity.model.Comment">
        <id column="id" property="id"/>
        <result column="content_id" property="contentId"/>
        <result column="user_id" property="userId"/>
        <result column="reply_id" property="replyId"/>
        <result column="comment_content" property="commentContent"/>
        <result column="create_time" property="createTime"/>
        <result column="praiseno" property="praiseNo"/>
        <result column="reply_number" property="replyNumber"/>
        <result column="nickname" property="nickname"/>
        <result column="headimgurl" property="headimgurl"/>
        <result column="praiseId" property="praiseId"/>
        <collection property="respondents" ofType="com.nbclass.activity.model.Comment" column="prop1=content_id,prop2=id,userId=user_id" select="getCommentReply2" >
            <id column="id" property="id"/>
            <result column="content_id" property="contentId"/>
            <result column="user_id" property="userId"/>
            <result column="reply_id" property="replyId"/>
            <result column="comment_id" property="commentId"/>
            <result column="comment_content" property="commentContent"/>
            <result column="create_time" property="createTime"/>
            <result column="praiseno" property="praiseNo"/>
        </collection>
    </resultMap>

    <resultMap id="Report" type="com.nbclass.activity.model.Report">
        <id column="id" property="id"/>
        <result column="comment_id" property="commentId"/>
        <result column="report_id" property="reportId"/>
        <result column="report_content" property="reportContent"/>
        <result column="report_type" property="reportType"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
    </resultMap>

    <resultMap id="CommentBean" type="com.nbclass.activity.model.Comment">
        <id column="id" property="id"/>
        <result column="content_id" property="contentId"/>
        <result column="user_id" property="userId"/>
        <result column="reply_id" property="replyId"/>
        <result column="comment_content" property="commentContent"/>
        <result column="create_time" property="createTime"/>
        <result column="praiseno" property="praiseNo"/>
        <result column="collection_number" property="collectionNumber"/>
        <result column="reply_number" property="replyNumber"/>
        <result column="nickname" property="nickname"/>
        <result column="headimgurl" property="headimgurl"/>
        <result column="praiseId" property="praiseId"/>
    </resultMap>

    <resultMap id="FabulousList" type="com.nbclass.activity.model.Comment">
        <id column="id" property="id"/>
        <result column="comment_content" property="commentContent"/>
        <result column="praiseno" property="praiseNo"/>
        <result column="user_id" property="userId"/>
        <collection property="userList" ofType="com.nbclass.activity.model.WeiXiUser" column="commentId=id,userId=user_id" select="getUserFabulousList">
            <result column="nickname" property="nickname"/>
            <result column="headimgurl" property="headimgurl"/>
        </collection>
    </resultMap>

    <resultMap id="BaseResultMap" type="com.nbclass.activity.model.Content">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="praiseno" property="praiseno"/>
        <result column="listicon" property="listicon"/>
        <result column="comments" property="comments"/>
        <result column="userTotal" property="userTotal"/>
        <result column="TotalScore" property="TotalScore"/>

        <collection property="tagList" ofType="com.nbclass.activity.model.ContentTags" column="id" select="getTagList2">
            <id column="id" property="id"/>
            <result column="tagName" property="tagName"/>
        </collection>

    </resultMap>

    <select id="getComment" resultMap="Content">
        SELECT
            c.*,
            IFNULL(SUM(s.score),0) AS TotalScore,
            count( s.user_id ) as userTotal
        FROM
            content c
        LEFT JOIN scoring_table s ON c.id = s.content_id
        WHERE
            c.id = #{contentId}
    </select>

    <select id="getCommentReplyList" resultMap="Comment">
        SELECT
            c.*,
            u.nickname,
            u.headimgurl,
            IFNULL(p.praise_id,0)  as praiseId
        FROM
            comment c
                LEFT JOIN content c2 ON c.content_id = c2.id
                LEFT JOIN praise p ON c.id = p.comment_id and p.praise_id = #{userId}
                LEFT JOIN weixi_user u ON u.id = c.user_id
        WHERE
            c.id = #{commentId} and c.comment_id = 0
        ORDER BY
            create_time DESC
    </select>

    <select id="getComments" resultMap="Comment2">
        SELECT
            c.*,
            u.nickname,
            u.headimgurl,
            IFNULL(p.praise_id,0)  as praiseId
        FROM
            comment c
        LEFT JOIN content c2 ON c.content_id = c2.id
        LEFT JOIN praise p ON c.id = p.comment_id and p.praise_id = #{userId}
        LEFT JOIN weixi_user u ON u.id = c.user_id
        WHERE
            c.content_id = #{contentId} and c.comment_id = 0
        ORDER BY
            create_time DESC
    </select>

    <!--<select id="getComments" resultMap="Comment">
        SELECT
            c.*,
            u.nickname,
            u.headimgurl,
            IFNULL(p.praise_id,0)  as praiseId
        FROM
            comment c
        LEFT JOIN content c2 ON c.content_id = c2.id
        LEFT JOIN praise p ON c.id = p.comment_id and p.praise_id = #{userId}
        LEFT JOIN weixi_user u ON u.id = c.user_id
        WHERE
            c.content_id = #{contentId} and c.comment_id = 0
        ORDER BY
            create_time DESC
    </select>-->

    <select id="getCommentReply" resultMap="Comment">
        SELECT
            c.*,
            u.nickname,
            u.headimgurl,
            IFNULL(p.praise_id,0)  as praiseId
        FROM
            `comment` c
        LEFT JOIN content c2 ON c.content_id = c2.id
        LEFT JOIN praise p ON c.id = p.comment_id and p.praise_id = #{userId}
        LEFT JOIN weixi_user u ON u.id = c.user_id
        WHERE
            c.content_id = #{prop1} and c.comment_id = #{prop2}
        order by create_time desc
    </select>

    <select id="getCommentReply2" resultMap="CommentBean">
        SELECT
            c.*,
            u.nickname,
            u.headimgurl,
            IFNULL(p.praise_id,0)  as praiseId
        FROM
            `comment` c
                LEFT JOIN content c2 ON c.content_id = c2.id
                LEFT JOIN praise p ON c.id = p.comment_id and p.praise_id = #{userId}
                LEFT JOIN weixi_user u ON u.id = c.user_id
        WHERE
            c.content_id = #{prop1} AND c.comment_id != 0 AND c.comment_id != 0 AND c.comment_id = #{prop2}
        order by c.create_time desc limit 2
    </select>

    <select id="getTags" resultType="com.nbclass.activity.model.ContentTags">
        SELECT
            item.dictid as id,
            item.NAME AS tagName
        FROM
            content_tags c
                INNER JOIN data_dict_item item ON c.tid = item.id
        WHERE
            c.cid = #{id}
    </select>

    <insert id="saveComment">
        insert into comment
        (
        content_id,
        user_id,
        comment_content,
        create_time
        )
        values
        (
        #{contentId},
        #{userId},
        #{commentContent},
        now()
        )
    </insert>

    <select id="getCommentUserId" resultMap="Comment">
        select * from comment where id = #{commentId}
    </select>

    <insert id="savePraiseNo">
        insert into praise
        (
         comment_id,
         praise_id,
         user_id,
         create_time
        )
        values
        (
        #{commentId},
        #{praiseId},
        #{userId},
        now()
        )
    </insert>

    <update id="updatePraiseno">
        update comment set
        <if test="commentId!=null">
            praiseno = praiseno + 1
        </if>
        where id = #{commentId}
    </update>

    <update id="updateContentPraiseNo">
        update content set
        <if test="contentId!=null">
            praiseno = praiseno+1
        </if>
        where id = #{contentId}
    </update>

    <insert id="saveUserReply">
        insert into comment
        (
        content_id,
        user_id,
        reply_id,
        comment_id,
        comment_content,
        create_time
        )
        values
        (
        #{contentId},
        #{userId},
        #{replyId},
        #{commentId},
        #{commentContent},
        now()
        )
    </insert>

    <insert id="saveReport">
        insert into report
        (
        comment_id,
        report_id,
        report_content,
        report_type,
        create_time
        )
        values
        (
        #{commentId},
        #{reportId},
        #{reportContent},
        #{reportType},
        now()
        )
    </insert>

    <select id="getReport" resultMap="Report">
        SELECT
            r.*
        FROM
            report r
                LEFT JOIN `comment` c ON r.comment_id = c.id
        WHERE
            c.user_id = #{userId}
    </select>

    <insert id="saveLabelLogger">
        insert into label_logger
        (
         content_id,
         user_id,
         logger,
         create_time
        )
        values
        (
        #{contentId},
        #{userId},
        #{logger},
        now()
        )
    </insert>

    <insert id="saveFavorites">
        insert into favorites_folder
        (
         name,
         user_id,
         create_time
        )
        values
        (
         #{name},
         #{userId},
         now()
        )
    </insert>

    <select id="getUserFolder" resultType="com.nbclass.activity.model.FavoritesFolder">
        SELECT
            f.id,
            f.name,
            f.total_collections,
            IFNULL(r.content_id,0) as contentId
        FROM
            favorites_folder f
                LEFT JOIN folder_resources r ON f.id = r.folder_id and r.content_id = #{commentId}
        WHERE
            f.user_id = #{userId}
    </select>

    <insert id="saveFolder">
        insert into folder_resources
        (
         folder_id,
         content_id,
         create_time
        )
        values
        (
        #{folderId},
        #{contentId},
        now()
        )
    </insert>

    <update id="saveFolderTotal">
        update favorites_folder set
        <if test="folderId!=null">
            total_collections = total_collections+1
        </if>
        where id = #{folderId}
    </update>

    <select id="getFolderExistence" resultType="int">
        select count(*) from folder_resources where folder_id = #{folderId} and content_id = #{contentId}
    </select>

    <select id="getFavoritesName" resultType="int">
        select count(*) from favorites_folder where name = #{name}
    </select>

    <delete id="deleteFolderResources">
        delete from folder_resources where folder_id = #{folderId} and content_id = #{contentId}
    </delete>

    <select id="getMyCollection" resultMap="BaseResultMap">
        SELECT
            c.id,
            c.title,
            c.praiseno,
            c.listicon,
            c.comments,
            IFNULL(SUM(s.score),0) AS TotalScore,
            count( s.user_id ) as userTotal
        FROM
            favorites_folder f
                LEFT JOIN folder_resources r ON f.id = r.folder_id
                LEFT JOIN content c ON r.content_id = c.id
                LEFT JOIN scoring_table s ON c.id = s.content_id
        WHERE
            f.user_id = #{userId}
          AND f.id = #{folderId}
        order by r.create_time desc
    </select>

    <select id="getTagList2" resultType="com.nbclass.activity.model.ContentTags">
        SELECT
            dict.id,
            item.`name` AS tagName
        FROM
            content_tags tags
                LEFT JOIN data_dict_item item ON tags.tid = item.id
                INNER JOIN data_dict dict ON dict.id = item.dictid
                AND dict.id IN ( 23, 24)
        WHERE
            tags.cid = #{id}
    </select>

    <select id="getUserFolderList" resultType="com.nbclass.activity.model.FavoritesFolder">
        select id,name from favorites_folder where user_id = #{userId}
    </select>

    <delete id="deletePraise">
        delete from praise where comment_id = #{commentId} and praise_id = #{userId}
    </delete>

    <update id="updatePraiseNoONE">
        update comment set
        <if test="commentId!=null">
            praiseno = praiseno - 1
        </if>
        where id = #{commentId}
    </update>

    <update id="updateReplyNumberONE">
        update comment set
        <if test="commentId!=null">
            reply_number = reply_number + 1
        </if>
        where id = #{commentId}
    </update>

    <select id="getReplyList" resultMap="CommentBean">
        SELECT
            c.id,
            c.content_id,
            c.comment_content,
            c.create_time,
            u.nickname,
            u.headimgurl,
            IFNULL(p.praise_id,0) as praiseId
        FROM
            `comment` c
        LEFT JOIN praise p ON c.id = p.comment_id and praise_id = #{userId}
        LEFT JOIN weixi_user u ON c.reply_id = u.id
        WHERE
            c.user_id = #{userId}
          AND reply_id != 0
        ORDER BY
            c.create_time DESC
    </select>

    <select id="getFabulousList" resultMap="FabulousList">
        SELECT
            c.id,
            c.comment_content,
            c.praiseno,
            c.user_id
        FROM
            `comment` c
        order by c.create_time desc
    </select>

    <select id="getUserFabulousList" resultType="com.nbclass.activity.model.WeiXiUser">
        SELECT
            u.nickname,
            u.headimgurl
        FROM
            weixi_user u
                LEFT JOIN praise p ON u.id = p.praise_id
        WHERE
            p.comment_id = #{commentId}
          AND p.user_id = #{userId}
        ORDER BY p.create_time desc
        LIMIT 2
    </select>

    <update id="updateCollectionNumber">
        update content set
        <if test="contentId != null">
            collection_number = collection_number + 1
        </if>
        where id = #{contentId}
    </update>

    <update id="updateCollectionNumberReduce">
        update content set
        <if test="contentId != null">
            collection_number = collection_number - 1
        </if>
        where id = #{contentId}
    </update>

    <update id="updateLabelFeedback">
        update content set
        <if test="contentId != null">
            label_feedback = label_feedback + 1
        </if>
        where id = #{contentId}
    </update>

    <select id="getLabelLogger" resultType="com.nbclass.activity.model.LabelLogger">
        SELECT
            l.id,
            l.logger,
            l.content_id as contentId,
            l.create_time as createTime,
            l.`status`,
            u.nickname as name
        FROM
            label_logger l
        LEFT JOIN content c ON l.content_id = c.id
        LEFT JOIN weixi_user u ON l.user_id = u.id
        where 1=1
            <if test="id!=null">
                and l.content_id = #{id}
            </if>
        order by l.create_time desc
    </select>

    <insert id="saveScoring">
        insert into scoring_table
        (
         content_id,
         score,
         user_id,
         create_time
        )
        values
        (
        #{contentId},
        #{score},
        #{userId},
        now()
        )
    </insert>

    <update id="updateLogger">
        update label_logger set
        <if test="status!=null">
            status = #{status}
        </if>
        where id = #{id}
    </update>

</mapper>
