<?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.cab.dao.ContentFollowMapper" >
  <resultMap id="BaseResultMap" type="com.cab.model.ContentFollow" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="follow_time" property="followTime" jdbcType="TIMESTAMP" />
    <result column="comment_id" property="commentId" jdbcType="BIGINT" />
    <result column="content" property="content" jdbcType="VARCHAR" />
    <association property="fromUser" column="from_id" javaType="com.cab.model.User" resultMap="com.cab.dao.UserMapper.ContentFollowFromJoinMap"/>
    <association property="toUser" column="to_id" javaType="com.cab.model.User" resultMap="com.cab.dao.UserMapper.ContentFollowToJoinMap"/>    
  </resultMap>
  <sql id="Base_Column_List" >
    CF.id, from_id, to_id, follow_time, comment_id, content
  </sql>
  
  <select id="selectList" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />, 
    U1.name as name_from, U1.avatar as avatar_from, 
    U2.name as name_to, U2.avatar as avatar_to
    from cab_content_follows CF inner join cab_users U1 on CF.from_id = U1.id 
    left join cab_users U2 on CF.to_id = U2.id
    where CF.id IN <foreach item="item" index="index" collection="list" open="(" separator="," close=")">#{item}</foreach>
  </select>
  
  <select id="selectByCommentId" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />, 
    U1.name as name_from, U1.avatar as avatar_from, 
    U2.name as name_to, U2.avatar as avatar_to
    from cab_content_follows CF inner join cab_users U1 on CF.from_id = U1.id 
    left join cab_users U2 on CF.to_id = U2.id
    where comment_id = #{0} order by follow_time desc 
  </select>
  <!-- 查询回复的数量 -->
  <select id="getFollowCountByCommentId" parameterType="java.lang.Long" resultType="java.lang.Long">
  	select count(id) from cab_content_follows where comment_id=#{0}
  </select>
  <select id="selectByCommentIdCount" resultType="java.lang.Long" >
    select count(id) from cab_content_follows where comment_id = #{0} 
  </select>
  
  <select id="selectIdListByCommentIdList" resultType="java.lang.String">
    select GROUP_CONCAT(CAST(id AS char)) from 
    (select * from cab_content_follows ORDER BY follow_time DESC) AS TEMP 
    GROUP BY comment_id HAVING comment_id IN <foreach item="item" index="index" collection="list" open="(" separator="," close=")">#{item}</foreach>
  </select>
  
  <insert id="insertSelective" parameterType="com.cab.model.ContentFollow" useGeneratedKeys="true" keyProperty="id" >
    insert into cab_content_follows
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="fromId != null" >
        from_id,
      </if>
      <if test="toId != null" >
        to_id,
      </if>
      <if test="followTime != null" >
        follow_time,
      </if>
      <if test="commentId != null" >
        comment_id,
      </if>
      <if test="content != null" >
        content,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      </if>
      <if test="fromId != null" >
        #{fromId,jdbcType=BIGINT},
      </if>
      <if test="toId != null" >
        #{toId,jdbcType=BIGINT},
      </if>
      <if test="followTime != null" >
        #{followTime,jdbcType=TIMESTAMP},
      </if>
      <if test="commentId != null" >
        #{commentId,jdbcType=BIGINT},
      </if>
      <if test="content != null" >
        #{content,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
</mapper>