<?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.greate.community.dao.CommentMapper">

    <sql id = "selectFields">
        id, user_id, entity_type, entity_id, target_id, content, status, create_time
    </sql>

    <sql id = "insertFields">
        user_id, entity_type, entity_id, target_id, content, status, create_time
    </sql>

    <select id = "selectCommentById" resultType="Comment">
        select <include refid="selectFields"></include>
        from comment
        where id = #{id}
    </select>

    <!--分页查询评论-->
    <!--不查询禁用的评论, 按照创建时间升序排序-->
    <select id = "selectCommentByEntity" resultType="Comment">
        select <include refid="selectFields"></include>
        from comment
        where status = 0
        and entity_type = #{entityType}
        and entity_id = #{entityId}
        order by create_time asc
        limit #{offset}, #{limit}
    </select>

    <!--查询评论的个数-->
    <select id = "selectCountByEntity" resultType="int">
        select count(id)
        from comment
        where status = 0
        and entity_type = #{entityType}
        and entity_id = #{entityId}
    </select>

    <!--添加评论-->
    <insert id = "insertComment" parameterType="Comment">
        insert into comment(<include refid="insertFields"></include>)
        values(#{userId}, #{entityType}, #{entityId}, #{targetId}, #{content}, #{status}, #{createTime})
    </insert>

    <!--分页查询某个用户的评论/回复列表-->
    <select id = "selectCommentByUserId" resultType="Comment">
        select <include refid="selectFields"></include>
        from comment
        where status = 0
        and user_id = #{userId}
        order by create_time desc
        limit #{offset}, #{limit}
    </select>

    <!--查询某个用户的评论/回复数量-->
    <select id = "selectCommentCountByUserId" resultType="int">
        select count(id)
        from comment
        where status = 0
        and user_id = #{userId}
    </select>
</mapper>