<?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.slodon.b2b2c.dao.read.video.SvideoCommentReadMapper">
  <resultMap id="resultMap" type="com.slodon.b2b2c.video.pojo.SvideoComment">
    <id column="comment_id" property="commentId" />
    <result column="video_id" property="videoId" />
    <result column="video_image" property="videoImage" />
    <result column="from_author_id" property="fromAuthorId" />
    <result column="from_author_name" property="fromAuthorName" />
    <result column="member_avatar" property="memberAvatar" />
    <result column="to_author_id" property="toAuthorId" />
    <result column="to_author_name" property="toAuthorName" />
    <result column="like_num" property="likeNum" />
    <result column="create_time" property="createTime" />
    <result column="is_read" property="isRead" />
    <result column="content" property="content" />
  </resultMap>
  <!--按照主键值进行操作-->
  <sql id="pkWhere">
    WHERE `comment_id` = #{primaryKey}
  </sql>
  <!--操作条件-->
  <sql id="whereCondition">
    <if test="example != null">
      <trim prefix="WHERE" prefixOverrides="AND|OR">
        <if test="example.commentIdNotEquals != null">
          AND `comment_id` != #{example.commentIdNotEquals}
        </if>
        <if test="example.commentIdIn != null">
          AND `comment_id` in (${example.commentIdIn})
        </if>
        <if test="example.videoId != null">
          AND `video_id` = #{example.videoId}
        </if>
        <if test="example.videoImage != null">
          AND `video_image` = #{example.videoImage}
        </if>
        <if test="example.fromAuthorId != null">
          AND `from_author_id` = #{example.fromAuthorId}
        </if>
        <if test="example.fromAuthorName != null">
          AND `from_author_name` = #{example.fromAuthorName}
        </if>
        <if test="example.fromAuthorNameLike != null">
          AND `from_author_name` like concat('%',#{example.fromAuthorNameLike},'%')
        </if>
        <if test="example.memberAvatar != null">
          AND `member_avatar` = #{example.memberAvatar}
        </if>
        <if test="example.toAuthorId != null">
          AND `to_author_id` = #{example.toAuthorId}
        </if>
        <if test="example.toAuthorName != null">
          AND `to_author_name` = #{example.toAuthorName}
        </if>
        <if test="example.toAuthorNameLike != null">
          AND `to_author_name` like concat('%',#{example.toAuthorNameLike},'%')
        </if>
        <if test="example.likeNum != null">
          AND `like_num` = #{example.likeNum}
        </if>
        <if test="example.createTimeAfter != null">
          AND `create_time` <![CDATA[ >= ]]> #{example.createTimeAfter}
        </if>
        <if test="example.createTimeBefore != null">
          AND `create_time` <![CDATA[ <= ]]> #{example.createTimeBefore}
        </if>
        <if test="example.isRead != null">
          AND `is_read` = #{example.isRead}
        </if>
        <if test="example.content != null">
          AND `content` = #{example.content}
        </if>
        <if test="example.contentLike != null">
          AND `content` like concat('%',#{example.contentLike},'%')
        </if>
      </trim>
    </if>
  </sql>
  <!--排序条件-->
  <sql id="orderBy">
    ORDER BY `comment_id` DESC
  </sql>
  <sql id="orderByOther">
    order by ${example.orderBy}
  </sql>
  <!--分组条件-->
  <sql id="groupBy">
    group by ${example.groupBy}
  </sql>
  <!--分页条件-->
  <sql id="limit">
    <if test="size != null and size &gt; 0">
      limit #{startRow},#{size}
    </if>
  </sql>
  <!--查询符合条件的记录数-->
  <select id="countByExample" parameterType="com.slodon.b2b2c.video.example.SvideoCommentExample" resultType="java.lang.Integer">
    SELECT
      COUNT(*)
    FROM `svideo_comment`
    <include refid="whereCondition" />
  </select>
  <!--根据主键查询记录-->
  <select id="getByPrimaryKey" resultMap="resultMap">
    SELECT
      *
    FROM `svideo_comment`
    <include refid="pkWhere" />
  </select>
  <!--查询符合条件的记录(所有字段)-->
  <select id="listByExample" resultMap="resultMap">
    SELECT
      *
    FROM `svideo_comment`
    <include refid="whereCondition" />
    <if test="example.groupBy != null">
      <include refid="groupBy" />
    </if>
    <choose>
      <when test="example.orderBy != null">
        <include refid="orderByOther" />
      </when>
      <otherwise>
        <include refid="orderBy" />
      </otherwise>
    </choose>
  </select>
  <!--分页查询符合条件的记录(所有字段)-->
  <select id="listPageByExample" resultMap="resultMap">
    SELECT
      *
    FROM `svideo_comment`
    <include refid="whereCondition" />
    <if test="example.groupBy != null">
      <include refid="groupBy" />
    </if>
    <choose>
      <when test="example.orderBy != null">
        <include refid="orderByOther" />
      </when>
      <otherwise>
        <include refid="orderBy" />
      </otherwise>
    </choose>
    <include refid="limit" />
  </select>
  <!--查询符合条件的记录(指定字段)-->
  <select id="listFieldsByExample" resultMap="resultMap">
    SELECT
      ${fields}
    FROM `svideo_comment`
    <include refid="whereCondition" />
    <if test="example.groupBy != null">
      <include refid="groupBy" />
    </if>
    <choose>
      <when test="example.orderBy != null">
        <include refid="orderByOther" />
      </when>
      <otherwise>
        <include refid="orderBy" />
      </otherwise>
    </choose>
  </select>
  <!--分页查询符合条件的记录(指定字段)-->
  <select id="listFieldsPageByExample" resultMap="resultMap">
    SELECT
      ${fields}
    FROM `svideo_comment`
    <include refid="whereCondition" />
    <if test="example.groupBy != null">
      <include refid="groupBy" />
    </if>
    <choose>
      <when test="example.orderBy != null">
        <include refid="orderByOther" />
      </when>
      <otherwise>
        <include refid="orderBy" />
      </otherwise>
    </choose>
    <include refid="limit" />
  </select>

  <select id="getMessageCount" resultType="java.lang.Integer">
    SELECT
        count( * )
    FROM
        ( SELECT create_time FROM svideo_comment WHERE is_read = 0 AND to_author_id = #{example.toAuthorId}
    UNION
        SELECT create_time FROM svideo_reply WHERE is_read = 0 AND to_author_id = #{example.toAuthorId} ) AS message
  </select>

  <select id="getMessageList" resultType="com.slodon.b2b2c.vo.video.VideoMessageVO">
    SELECT
    comment_id commentId,
    0 replyId,
    from_author_id authorId,
    from_author_name authorName,
    member_avatar memberAvatar,
    video_id videoId,
    video_image videoImage,
    content,
    create_time createTime,
    is_read isRead,
    "comment" msgType
    FROM svideo_comment
    WHERE is_read = 0 AND to_author_id = #{example.toAuthorId}
    UNION
    SELECT
    comment_id commentId,
    reply_id replyId,
    from_author_id authorId,
    from_author_name authorName,
    member_avatar memberAvatar,
    video_id videoId,
    video_image videoImage,
    content,
    create_time createTime,
    is_read isRead,
    "reply" msgType
    FROM svideo_reply
    WHERE is_read = 0 AND to_author_id = #{example.toAuthorId}
    ORDER BY createTime
    <include refid="limit"/>
  </select>
</mapper>