<?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.system.mapper.CommentIndexMapper">

    <resultMap type="CommentIndex" id="CommentIndexResult">
        <result property="commentId" column="comment_id"/>
        <result property="communityId" column="community_id"/>
        <result property="communityType" column="community_type"/>
        <result property="communityTitle"    column="community_title"    />
        <result property="isRoot" column="is_root"/>
        <result property="parentId" column="parent_id"/>
        <result property="userId" column="user_id"/>
        <result property="nickName" column="nick_name"/>
        <result property="avatar" column="avatar"/>
        <result property="toUserId" column="to_user_id"/>
        <result property="toNickName" column="to_nick_name"/>
        <result property="toAvatar" column="to_avatar"/>
        <result property="toRoot" column="to_root"/>
        <result property="contentId" column="content_id"/>
        <result property="commentContent" column="comment_content"/>
        <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="remark" column="remark"/>
    </resultMap>

    <resultMap type="com.ruoyi.system.domain.communityNews.vo.CommentIndexShVo" id="CommentIndexShVoResult">
        <result property="commentId" column="comment_id"/>
        <result property="communityId" column="community_id"/>
        <result property="communityType" column="community_type"/>
        <result property="communityTitle"    column="community_title"    />
        <result property="isRoot" column="is_root"/>
        <result property="parentId" column="parent_id"/>
        <result property="userId" column="user_id"/>
        <result property="nickName" column="nick_name"/>
        <result property="avatar" column="avatar"/>
        <result property="toUserId" column="to_user_id"/>
        <result property="toNickName" column="to_nick_name"/>
        <result property="toAvatar" column="to_avatar"/>
        <result property="contentId" column="content_id"/>
        <result property="commentContent" column="comment_content"/>
        <result property="status"    column="status"    />
        <result property="type"    column="type"    />
    </resultMap>
    <!--回复列表返回给前端的格式-->
    <resultMap type="com.ruoyi.system.domain.communityNews.vo.CommentIndexVo" id="CommentIndexVoResult">
        <id property="id" column="ci_id"/>
        <result property="isRoot" column="ci_is_root"/>
        <result property="content" column="ci_content"/>
        <result property="likeCount" column="ci_like_count"/>
        <result property="isLike" column="ci_is_like"/>
        <result property="createTime" column="ci_create_time"/>
        <result property="childrenListCount" column="ci_children_list_count"/>
        <association property="commentUser" javaType="com.ruoyi.system.domain.communityNews.vo.CommentUser"
                     resultMap="CommentUserOneResult"/>
        <collection property="childrenList" javaType="java.util.List" resultMap="ChildrenListResult"/>
    </resultMap>
    <resultMap id="ChildrenListResult" type="com.ruoyi.system.domain.communityNews.vo.CommentChildrenVo">
        <result property="id" column="cl_id"/>
        <result property="isRoot" column="cl_is_root"/>
        <result property="content" column="cl_content"/>
        <result property="likeCount" column="cl_like_count"/>
        <result property="isLike" column="cl_is_like"/>
        <result property="createTime" column="cl_create_time"/>
        <result property="toRoot" column="cl_to_root"/>
        <association property="commentUser" javaType="com.ruoyi.system.domain.communityNews.vo.CommentUser"
                     resultMap="CommentUserTwoResult"/>
        <association property="targetUser" javaType="com.ruoyi.system.domain.communityNews.vo.TargetUser"
                     resultMap="TargetUserVoResult"/>
    </resultMap>
    <!--一级评论对象评论人对象-->
    <resultMap id="CommentUserOneResult" type="com.ruoyi.system.domain.communityNews.vo.CommentUser">
        <id property="id" column="cu_one_id"/>
        <result property="nickName" column="cu_one_nick_name"/>
        <result property="avatar" column="cu_one_avatar"/>
    </resultMap>
    <!--子集评论评论人对象-->
    <resultMap id="CommentUserTwoResult" type="com.ruoyi.system.domain.communityNews.vo.CommentUser">
        <id property="id" column="cu_two_id"/>
        <result property="nickName" column="cu_two_nick_name"/>
        <result property="avatar" column="cu_two_avatar"/>
    </resultMap>
    <resultMap id="TargetUserVoResult" type="com.ruoyi.system.domain.communityNews.vo.TargetUser">
        <id property="id" column="tu_id"/>
        <result property="nickName" column="tu_nick_name"/>
        <result property="avatar" column="tu_avatar"/>
    </resultMap>


    <sql id="selectCommentIndexVo">
        select comment_id,
               community_id,
               community_type,
               community_title,
               is_root,
               parent_id,
               user_id,
               nick_name,
               avatar,
               to_user_id,
               to_nick_name,
               to_avatar,
               to_root,
               content_id,
               comment_content,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from comment_index
    </sql>

    <select id="selectCommentIndexList" parameterType="CommentIndex" resultMap="CommentIndexResult">
        <include refid="selectCommentIndexVo"/>
        <where>
            del_flag = 'N'
            <if test="communityId != null ">and community_id = #{communityId}</if>
            <if test="communityType != null  and communityType != ''">and community_type = #{communityType}</if>
            <if test="isRoot != null ">and is_root = #{isRoot}</if>
            <if test="parentId != null ">and parent_id = #{parentId}</if>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="nickName != null  and nickName != ''">and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="avatar != null  and avatar != ''">and avatar = #{avatar}</if>
            <if test="toUserId != null ">and to_user_id = #{toUserId}</if>
            <if test="toNickName != null  and toNickName != ''">and to_nick_name like concat('%', #{toNickName}, '%')
            </if>
            <if test="toAvatar != null  and toAvatar != ''">and to_avatar = #{toAvatar}</if>
            <if test="contentId != null ">and content_id = #{contentId}</if>
            <if test="commentContent != null  and commentContent != ''">and comment_content = #{commentContent}</if>
        </where>
        order by create_time DESC
    </select>

    <select id="selectCommentIndexByCommentId" parameterType="Long" resultMap="CommentIndexResult">
        <include refid="selectCommentIndexVo"/>
        where comment_id = #{commentId}
    </select>


    <select id="getRootComment" parameterType="CommentIndex" resultMap="CommentIndexVoResult">
        SELECT
            i.comment_id AS ci_id,
            i.is_root AS ci_is_root,
            i.create_time AS ci_create_time,
            c.comment_content AS ci_content,
            i.user_id AS cu_one_id,
            i.nick_name AS cu_one_nick_name,
            i.avatar AS cu_one_avatar,
            c.like_count AS ci_like_count,
            ifnull(children.ci_children_list_count,0) AS ci_children_list_count
        FROM
            comment_index i
                LEFT JOIN comment_content c ON i.content_id = c.content_id
                LEFT JOIN (
                SELECT
                    count( 1 ) AS ci_children_list_count,
                    i.parent_id AS ci_parent_id
                FROM
                    comment_index i
                        LEFT JOIN comment_content c ON i.content_id = c.content_id
        <where>
                    c.STATUS = '2'
                  AND i.is_root = 2
                  AND c.del_flag = 'N'
                  AND i.del_flag = 'N'
            <if test="communityId != null ">and i.community_id = #{communityId}</if>
        </where>
                 GROUP BY i.parent_id
            ) AS children ON children.ci_parent_id = i.comment_id
        <where>
            c.STATUS = '2'
          AND i.is_root = 1
          AND c.del_flag = 'N'
          AND i.del_flag = 'N'
        <if test="communityId != null ">and i.community_id = #{communityId}</if>
        </where>
        ORDER BY
            ci_create_time,
            ci_like_count DESC
    </select>
    <select id="getParentComment" parameterType="CommentIndex" resultMap="ChildrenListResult">
        SELECT
        i.comment_id AS cl_id,
        i.is_root AS cl_is_root,
        i.user_id AS cu_two_id,
        i.nick_name AS cu_two_nick_name,
        i.avatar AS cu_two_avatar,
        i.to_user_id AS tu_id,
        i.to_nick_name AS tu_nick_name,
        i.to_avatar AS tu_avatar,
        i.to_root AS cl_to_root,
        i.create_time AS cl_create_time,
        c.comment_content AS cl_content,
        c.like_count AS cl_like_count
        FROM
        comment_index i
        LEFT JOIN comment_content c ON i.content_id = c.content_id
        <where>
            c.STATUS = '2'
            AND i.is_root = 2
            AND c.del_flag = 'N'
            AND i.del_flag = 'N'
            <if test="parentId != null ">and i.parent_id = #{parentId}</if>
        </where>
        order by cl_create_time ,cl_like_count DESC
    </select>
    <select id="commentSh" parameterType="com.ruoyi.system.domain.communityNews.vo.CommentIndexShVo" resultMap="CommentIndexShVoResult">
        SELECT
            i.comment_id,
            i.community_id,
            i.community_type,
            i.community_title,
            i.is_root,
            i.parent_id,
            i.user_id,
            i.nick_name,
            i.avatar,
            i.to_user_id,
            i.to_nick_name,
            i.to_avatar,
            i.content_id,
            i.comment_content,
            c.status,
            c.type
        FROM
            comment_index i
                LEFT JOIN comment_content c ON i.content_id = c.content_id
        <where>
            i.del_flag = 'N'
            <if test="communityId != null ">and i.community_id = #{communityId}</if>
            <if test="communityType != null  and communityType != ''">and i.community_type = #{communityType}</if>
            <if test="isRoot != null ">and i.is_root = #{isRoot}</if>
            <if test="parentId != null ">and i.parent_id = #{parentId}</if>
            <if test="userId != null ">and i.user_id = #{userId}</if>
            <if test="nickName != null  and nickName != ''">and i.nick_name like concat('%', #{nickName}, '%')</if>
            <if test="avatar != null  and avatar != ''">and i.avatar = #{avatar}</if>
            <if test="toUserId != null ">and i.to_user_id = #{toUserId}</if>
            <if test="toNickName != null  and toNickName != ''">and i.to_nick_name like concat('%', #{toNickName}, '%')
            </if>
            <if test="toAvatar != null  and toAvatar != ''">and i.to_avatar = #{toAvatar}</if>
            <if test="status != null and status != ''"> and c.status = #{status}</if>
            <if test="type != null "> and c.type = #{type}</if>
        </where>
    </select>
    <select id="selectCommentIdListByCommentId" resultMap="CommentIndexResult">
        SELECT
            i.comment_id,
            i.community_id,
            i.community_type,
            i.community_title,
            i.is_root,
            i.parent_id,
            i.user_id,
            i.nick_name,
            i.avatar,
            i.to_user_id,
            i.to_nick_name,
            i.to_avatar,
            i.content_id,
            i.comment_content
        FROM
            comment_index i
                LEFT JOIN comment_content c ON i.comment_id = c.comment_id
        WHERE
            i.parent_id = #{commentId}
          AND i.del_flag = 'N'
          AND c.`status` = 2
    </select>


    <insert id="insertCommentIndex" parameterType="CommentIndex" useGeneratedKeys="true" keyProperty="commentId">
        insert into comment_index
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="communityId != null">community_id,</if>
            <if test="communityType != null and communityType != ''">community_type,</if>
            <if test="communityTitle != null and communityTitle != ''">community_title,</if>
            <if test="isRoot != null">is_root,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="avatar != null">avatar,</if>
            <if test="toUserId != null">to_user_id,</if>
            <if test="toNickName != null">to_nick_name,</if>
            <if test="toAvatar != null">to_avatar,</if>
            <if test="toRoot != null">to_root,</if>
            <if test="contentId != null">content_id,</if>
            <if test="commentContent != null">comment_content,</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="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="communityId != null">#{communityId},</if>
            <if test="communityType != null and communityType != ''">#{communityType},</if>
            <if test="communityTitle != null and communityTitle != ''">#{communityTitle},</if>
            <if test="isRoot != null">#{isRoot},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="toUserId != null">#{toUserId},</if>
            <if test="toNickName != null">#{toNickName},</if>
            <if test="toAvatar != null">#{toAvatar},</if>
            <if test="toRoot != null">#{toRoot},</if>
            <if test="contentId != null">#{contentId},</if>
            <if test="commentContent != null">#{commentContent},</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="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateCommentIndex" parameterType="CommentIndex">
        update comment_index
        <trim prefix="SET" suffixOverrides=",">
            <if test="communityId != null">community_id = #{communityId},</if>
            <if test="communityType != null and communityType != ''">community_type = #{communityType},</if>
            <if test="communityTitle != null and communityTitle != ''">community_title = #{communityTitle},</if>
            <if test="isRoot != null">is_root = #{isRoot},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="toUserId != null">to_user_id = #{toUserId},</if>
            <if test="toNickName != null">to_nick_name = #{toNickName},</if>
            <if test="toAvatar != null">to_avatar = #{toAvatar},</if>
            <if test="contentId != null">content_id = #{contentId},</if>
            <if test="commentContent != null">comment_content = #{commentContent},</if>
            <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="remark != null">remark = #{remark},</if>
        </trim>
        where comment_id = #{commentId}
    </update>

    <delete id="deleteCommentIndexByCommentId" parameterType="Long">
        delete
        from comment_index
        where comment_id = #{commentId}
    </delete>
    <update id="updateCommentIndexByCommentId" parameterType="CommentIndex">
        update comment_index set del_flag = 'Y'
        where comment_id = #{commentId}
    </update>
    <update id="updateCommentIndexByParentId" parameterType="CommentIndex">
        update comment_index set del_flag = 'Y'
        where parent_id = #{commentId}
    </update>

    <delete id="deleteCommentIndexByCommentIds" parameterType="String">
        delete from comment_index where comment_id in
        <foreach item="commentId" collection="array" open="(" separator="," close=")">
            #{commentId}
        </foreach>
    </delete>
</mapper>