<?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.ruoyi.frequency.comment.mapper.CommentMapper">

    <resultMap type="com.ruoyi.frequency.comment.entity.Comment" id="CommentResult">
        <result property="id"    column="id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="type"    column="type"    />
        <result property="userType"    column="user_type"    />
        <result property="userId"    column="user_id"    />
        <result property="linkId"    column="link_id"    />
        <result property="topId"    column="top_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="parentUserType"    column="parent_user_type"    />
        <result property="parentUserId"    column="parent_user_id"    />
        <result property="content"    column="content"    />
        <result property="likeNum"    column="like_num"    />
        <result property="commentNum"    column="comment_num"    />
        <result property="image"    column="image"    />
        <result property="remark"    column="remark"    />
    </resultMap>



    <sql id="selectCommentVo">
        select
            t.id ,
            t.del_flag ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.type ,
            t.user_type ,
            t.user_id ,
            t.link_id ,
            t.top_id ,
            t.parent_id ,
            t.parent_user_type ,
            t.parent_user_id ,
            t.content ,
            t.like_num ,
            t.comment_num ,
            t.image ,
            t.remark
        from t_comment AS t
    </sql>

    <select id="selectCommentList" parameterType="com.ruoyi.frequency.comment.entity.Comment" resultMap="CommentResult">
        <include refid="selectCommentVo"/>
        <where> AND t.del_flag != 2
            <if test="type != null "> and t.type = #{type}</if>
            <if test="userType != null "> and t.user_type = #{userType}</if>
            <if test="userId != null "> and t.user_id = #{userId}</if>
            <if test="linkId != null "> and t.link_id = #{linkId}</if>
            <if test="topId != null "> and t.top_id = #{topId}</if>
            <if test="parentId != null "> and t.parent_id = #{parentId}</if>
            <if test="parentUserType != null "> and t.parent_user_type = #{parentUserType}</if>
            <if test="parentUserId != null "> and t.parent_user_id = #{parentUserId}</if>
            <if test="content != null  and content != ''"> and t.content = #{content}</if>
            <if test="likeNum != null "> and t.like_num = #{likeNum}</if>
            <if test="commentNum != null "> and t.comment_num = #{commentNum}</if>
            <if test="image != null  and image != ''"> and t.image = #{image}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
        </where>
        order by t.create_time asc
    </select>

    <select id="selectCommentById" parameterType="Long" resultMap="CommentResult">
        <include refid="selectCommentVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertComment" parameterType="com.ruoyi.frequency.comment.entity.Comment">
        insert into t_comment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="type != null">type,</if>
            <if test="userType != null">user_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="linkId != null">link_id,</if>
            <if test="topId != null">top_id,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="parentUserType != null">parent_user_type,</if>
            <if test="parentUserId != null">parent_user_id,</if>
            <if test="content != null">content,</if>
            <if test="likeNum != null">like_num,</if>
            <if test="commentNum != null">comment_num,</if>
            <if test="image != null">image,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="type != null">#{type},</if>
            <if test="userType != null">#{userType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="linkId != null">#{linkId},</if>
            <if test="topId != null">#{topId},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="parentUserType != null">#{parentUserType},</if>
            <if test="parentUserId != null">#{parentUserId},</if>
            <if test="content != null">#{content},</if>
            <if test="likeNum != null">#{likeNum},</if>
            <if test="commentNum != null">#{commentNum},</if>
            <if test="image != null">#{image},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateComment" parameterType="com.ruoyi.frequency.comment.entity.Comment">
        update t_comment
        <trim prefix="SET" suffixOverrides=",">
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="type != null">type = #{type},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="linkId != null">link_id = #{linkId},</if>
            <if test="topId != null">top_id = #{topId},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="parentUserType != null">parent_user_type = #{parentUserType},</if>
            <if test="parentUserId != null">parent_user_id = #{parentUserId},</if>
            <if test="content != null">content = #{content},</if>
            <if test="likeNum != null">like_num = like_num + #{likeNum},</if>
            <if test="commentNum != null">comment_num = comment_num + #{commentNum},</if>
            <if test="image != null">image = #{image},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
        <if test="likeNum != null">and like_num + #{likeNum} >= 0</if>
        <if test="commentNum != null">and  comment_num + #{commentNum} >= 0</if>
    </update>

    <select id="getCommentResp" resultType="com.ruoyi.controller.resp.CommentResp">
        SELECT
            t.id,
            t.top_id,
            t.create_time,
            t.user_type,
            t.user_id,
            t.parent_id,
            t.parent_user_type,
            t.parent_user_id,
            t.content,
            t.like_num,
            t.comment_num,
            CASE
                WHEN (
                         SELECT
                             COUNT( 1 )
                         FROM
                             t_like fl
                         WHERE
                             fl.del_flag = 1
                           AND fl.type = 2
                           AND fl.user_id = #{userVo.userId}
                           AND fl.user_type = #{userVo.userType}
                           AND fl.link_id = t.id
                     ) = 0 THEN
                    1 ELSE 2
                END isLike,
            CASE
                WHEN #{userVo.userType} = t.user_type
                    AND #{userVo.userId} = t.user_id THEN
                    2 ELSE 1
                END isSelf
        FROM
            t_comment AS t
        WHERE
            t.id = #{id}
    </select>

    <select id="commentPage" resultType="com.ruoyi.controller.resp.CommentPageResp">
        SELECT
        t.id,
        t.top_id,
        t.create_time,
        t.user_type,
        t.user_id,
        t.parent_id,
        t.parent_user_type,
        t.parent_user_id,
        t.image,
        t.content,
        t.like_num,
        t.comment_num,
        CASE
        WHEN (
        SELECT
        COUNT( 1 )
        FROM
        t_like fl
        WHERE
        fl.del_flag = 1
        AND fl.type = 2
        AND fl.user_id = #{userVo.userId}
        AND fl.user_type = #{userVo.userType}
        AND fl.link_id = t.id
        ) = 0 THEN
        1 ELSE 2
        END isLike,
        CASE
        WHEN #{userVo.userType} = t.user_type
        AND #{userVo.userId} = t.user_id THEN
        2 ELSE 1
        END isSelf,
        IF( p.comment_id is not null and t.id = p.comment_id ,2,1) isSelectedComment
        FROM
        t_comment AS t
        LEFT JOIN t_comment t4 ON t.parent_id = t4.id
        LEFT JOIN t_news p ON t.link_id = p.id and t.type = 1
        WHERE
        t.del_flag = 1
        AND t.link_id = #{req.linkId}
        <if test="req.parentId!=null and req.parentId!=''">
            AND t.parent_id != 0
            AND t.top_id = #{req.parentId}
        </if>
        <if test="req.parentId==null or req.parentId ==''">
            AND t.parent_id = 0
        </if>

        <if test="req.blockUserList!=null and req.blockUserList.size() > 0">
            AND (
            (t.user_type = 1 and t.user_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 1,#{userVo.userId},'')
            </foreach>)
            or
            (t.user_type = 2 and t.user_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 2,#{userVo.userId},'')
            </foreach>)
            or
            (t.user_type = 4 and t.user_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 4,#{userVo.userId},'')
            </foreach>)
            )
        </if>
        <if test="req.shieldUserList!=null and req.shieldUserList.size() > 0">
            AND (
            (t.user_type = 1 and t.user_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 1,#{userVo.userId},'')
            </foreach>)
            or
            (t.user_type = 2 and t.user_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 2,#{userVo.userId},'')
            </foreach>)
            or
            (t.user_type = 4 and t.user_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 4,#{userVo.userId},'')
            </foreach>)
            )
        </if>
        ORDER BY
        <choose>
            <when test="req.sortType != null and req.sortType != ''">
                <if test="req.sortType == 1">
                    <if test="req.isAsc == 1">
                        t.create_time
                    </if>
                    <if test="req.isAsc == 2">
                        t.create_time DESC
                    </if>
                </if>
                <if test="req.sortType == 2">
                    <if test="req.isAsc == 1">
                        t.like_num
                    </if>
                    <if test="req.isAsc == 2">
                        t.like_num DESC
                    </if>
                </if>
            </when>
            <otherwise>
                t.like_num DESC ,t.create_time
            </otherwise>
        </choose>

    </select>

    <select id="commentList" resultType="com.ruoyi.controller.resp.CommentPageResp">
        SELECT
        t.id,
        t.top_id,
        t.create_time,
        t.user_type,
        t.user_id,
        t.parent_id,
        t.parent_user_type,
        t.parent_user_id,
        t.image,
        t.content,
        t.like_num,
        t.comment_num,
        CASE
        WHEN (
        SELECT
        COUNT( 1 )
        FROM
        t_like fl
        WHERE
        fl.del_flag = 1
        AND fl.type = 2
        AND fl.user_id = #{userVo.userId}
        AND fl.user_type = #{userVo.userType}
        AND fl.link_id = t.id
        ) = 0 THEN
        1 ELSE 2
        END isLike,
        CASE
        WHEN #{userVo.userType} = t.user_type
        AND #{userVo.userId} = t.user_id THEN
        2 ELSE 1
        END isSelf,
        IF( p.comment_id is not null and t.id = p.comment_id ,2,1) isSelectedComment
        FROM
        t_comment AS t
        LEFT JOIN t_comment t4 ON t.parent_id = t4.id
        LEFT JOIN t_news p ON t.link_id = p.id and t.type = 1
        WHERE
        t.del_flag = 1
        AND t.link_id = #{req.linkId}
        <if test="req.parentId!=null and req.parentId!=''">
            AND t.parent_id != 0
            AND t.top_id = #{req.parentId}
        </if>
        <if test="req.parentId==null or req.parentId ==''">
            AND t.parent_id != 0
        </if>

        <if test="req.blockUserList!=null and req.blockUserList.size() > 0">
            AND (
            (t.user_type = 1 and t.user_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 1,#{userVo.userId},'')
            </foreach>)
            or
            (t.user_type = 2 and t.user_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 2,#{userVo.userId},'')
            </foreach>)
            or
            (t.user_type = 4 and t.user_id not in
            <foreach collection="req.blockUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 4,#{userVo.userId},'')
            </foreach>)
            )
        </if>
        <if test="req.shieldUserList!=null and req.shieldUserList.size() > 0">
            AND (
            (t.user_type = 1 and t.user_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 1,#{userVo.userId},'')
            </foreach>)
            or
            (t.user_type = 2 and t.user_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 2,#{userVo.userId},'')
            </foreach>)
            or
            (t.user_type = 4 and t.user_id not in
            <foreach collection="req.shieldUserList" item="userVo" open="(" close=")" separator=",">
                IF(#{userVo.userType} = 4,#{userVo.userId},'')
            </foreach>)
            )
        </if>
        AND ( SELECT COUNT(*) FROM t_works_comment AS sub WHERE sub.parent_id = t.parent_id AND sub.like_num >= t.like_num AND sub.create_time &lt;= t.create_time ) &lt;= 10
        AND t.parent_id in
        <foreach item="id" collection="list" open="(" separator="," close=")">
            #{id}
        </foreach>
        ORDER BY
        t.like_num DESC ,t.create_time
    </select>

    <select id="selectCommentNumList" resultType="com.ruoyi.controller.resp.CommentNum">
        SELECT
        t.top_id link_id,
        COUNT(t.id) commentNum
        FROM
        t_comment AS t
        LEFT JOIN t_comment t4 ON t.parent_id = t4.id
        WHERE
        t.del_flag = 1
        AND t.parent_id != 0
        AND t.top_id in
        <foreach item="id" collection="commentIds" open="(" separator="," close=")">
            #{id}
        </foreach>
        AND (
        (t.user_type = 1 and t.user_id  in
        <foreach collection="userVoList" item="userVo" open="(" close=")" separator=",">
            IF(#{userVo.userType} = 1,#{userVo.userId},'')
        </foreach>)
        or
        (t.user_type = 2 and t.user_id  in
        <foreach collection="userVoList" item="userVo" open="(" close=")" separator=",">
            IF(#{userVo.userType} = 2,#{userVo.userId},'')
        </foreach>)
        or
        (t.user_type = 4 and t.user_id  in
        <foreach collection="userVoList" item="userVo" open="(" close=")" separator=",">
            IF(#{userVo.userType} = 4,#{userVo.userId},'')
        </foreach>)
        )
        GROUP BY t.top_id
    </select>

    <select id="commentDialoguePage" resultType="com.ruoyi.controller.resp.CommentPageResp">
        (SELECT
        t.id,
        1 sort,
        t.create_time,
        t.user_type,
        t.user_id,
        t.parent_id,
        t.parent_user_type,
        t.parent_user_id,
        t.image,
        t.content,
        t.like_num,
        t.comment_num,
        CASE
        WHEN (
        SELECT
        COUNT( 1 )
        FROM
        t_like fl
        WHERE
        fl.del_flag = 1
        AND fl.type = 2
        AND fl.user_id = #{userVo.userId}
        AND fl.user_type = #{userVo.userType}
        AND fl.link_id = t.id
        ) = 0 THEN
        1 ELSE 2
        END isLike,
        CASE
        WHEN #{userVo.userType} = t.user_type
        AND #{userVo.userId} = t.user_id THEN
        2 ELSE 1
        END isSelf,
        IF( p.comment_id is not null and t.id = p.comment_id ,2,1) isSelectedComment
        FROM
        t_comment AS t
        LEFT JOIN t_comment t4 ON t.parent_id = t4.id
        LEFT JOIN t_news p ON t.link_id = p.id and t.type = 1
        WHERE
        t.del_flag = 1
        AND t.id = #{req.commentId}
        <if test="req.parentId!=null and req.parentId!=''">
            AND t.parent_id != 0
            AND t.top_id = #{req.parentId}
        </if>
        )
        UNION ALL
        (SELECT
        t.id,
        2 sort,
        t.create_time,
        t.user_type,
        t.user_id,
        t.parent_id,
        t.parent_user_type,
        t.parent_user_id,
        t.image,
        t.content,
        t.like_num,
        t.comment_num,
        CASE
        WHEN (
        SELECT
        COUNT( 1 )
        FROM
        t_like fl
        WHERE
        fl.del_flag = 1
        AND fl.type = 2
        AND fl.user_id = #{userVo.userId}
        AND fl.user_type = #{userVo.userType}
        AND fl.link_id = t.id
        ) = 0 THEN
        1 ELSE 2
        END isLike,
        CASE
        WHEN #{userVo.userType} = t.user_type
        AND #{userVo.userId} = t.user_id THEN
        2 ELSE 1
        END isSelf,
        IF( p.comment_id is not null and t.id = p.comment_id ,2,1) isSelectedComment
        FROM
        t_comment AS t
        LEFT JOIN t_comment t4 ON t.parent_id = t4.id
        LEFT JOIN t_news p ON t.link_id = p.id
        LEFT JOIN t_comment c ON c.id = #{req.commentId}
        WHERE
        t.del_flag = 1
        AND t.id != #{req.commentId}
        AND (
        (t.user_type = c.user_type and t.user_id = c.user_id )
        or
        (t.user_type = c.parent_user_type and t.user_id = c.parent_user_id )
        or
        (t.parent_user_type = c.user_type and t.parent_user_id = c.user_id )
        or
        (t.parent_user_type = c.parent_user_type and t.parent_user_id = c.parent_user_id )
        )
        <if test="req.parentId!=null and req.parentId!=''">
            AND t.parent_id != 0
            AND t.top_id = #{req.parentId}
        </if>
        )
        ORDER BY
        <choose>
            <when test="req.sortType != null and req.sortType != ''">
                <if test="req.sortType == 1">
                    <if test="req.isAsc == 1">
                        sort,create_time
                    </if>
                    <if test="req.isAsc == 2">
                        sort,create_time DESC
                    </if>
                </if>
                <if test="req.sortType == 2">
                    <if test="req.isAsc == 1">
                        sort,like_num
                    </if>
                    <if test="req.isAsc == 2">
                        sort, like_num DESC
                    </if>
                </if>
            </when>
            <otherwise>
                sort, like_num DESC ,create_time
            </otherwise>
        </choose>
    </select>

    <update id="updateCommentBatch" parameterType="com.ruoyi.frequency.comment.entity.Comment">
        <foreach item="item" index="index" collection="list" separator=";">
            update t_comment
            <trim prefix="SET" suffixOverrides=",">
                <if test="item.likeNum != null">like_num = like_num + #{item.likeNum},</if>
                <if test="item.commentNum != null">comment_num = comment_num + #{item.commentNum},</if>
            </trim>
            where id = #{item.id}
            <if test="item.likeNum != null">and like_num + #{item.likeNum}>=0</if>
            <if test="item.commentNum != null">and comment_num + #{item.commentNum} >= 0</if>
        </foreach>
    </update>
</mapper>