<?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.pcloud.book.reading.dao.impl.ReadingUserDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.reading.entity.ReadingUser" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="activity_id" property="activityId" jdbcType="BIGINT" />
    <result column="wx_user_id" property="wxUserId" jdbcType="VARCHAR" />
    <result column="alt_id" property="altId" jdbcType="VARCHAR" />
    <result column="join_state" property="joinState" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="create_user" property="createUser" jdbcType="BIGINT" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, activity_id, wx_user_id, alt_id, join_state, create_time, create_user
  </sql>

  <insert id="batchInsert" useGeneratedKeys="true" parameterType="java.util.List">
    insert into reading_user (activity_id, wx_user_id, alt_id, join_state,
    create_time, create_user)
    values
    <foreach collection="list" item="item" index="index" separator="," >
      (#{item.activityId,jdbcType=BIGINT}, #{item.wxUserId,jdbcType=VARCHAR}, #{item.altId,jdbcType=VARCHAR},0,
      NOW(), #{item.createUser,jdbcType=BIGINT})
    </foreach>
  </insert>

  <select id="getByActivityId" resultMap="BaseResultMap" parameterType="map">
    SELECT <include refid="Base_Column_List"/>
    FROM reading_user
    WHERE activity_id = #{activityId}
    <if test="joinState != null">
     AND join_state = #{joinState}
    </if>
  </select>

  <update id="updateJoinState" parameterType="map">
    UPDATE reading_user
    SET join_state = #{joinState}
    where wx_user_id = #{wxUserId} and activity_id = #{activityId}
  </update>

  <!--根据活动id集合获取活动人数-->
  <select id="getUserCountsByActivityIds" parameterType="map" resultType="com.pcloud.book.reading.dto.ActivityUserCountDTO">
    SELECT
    t.activity_id activityId,
    COUNT(1) `count`
    FROM
    reading_user t
    where activity_id in
    <foreach collection="activityIds" item="item" open="(" separator="," close=")">
      ${item}
    </foreach>
    GROUP BY
    t.activity_id
  </select>

  <select id="getWxUserIdByActivityIds" parameterType="map" resultType="string">
    SELECT DISTINCT
    wx_user_id
    FROM
    reading_user
    WHERE
    1=1
    <if test="activityIds != null and activityIds.size>0">
     AND activity_id IN
      <foreach collection="activityIds" item="item" open="(" separator="," close=")">
        ${item}
      </foreach>
    </if>
  </select>

  <!--批量获取活动读者书籍-->
  <select id="getBooksByActivityIds" parameterType="map" resultType="com.pcloud.book.reading.dto.ActivityUserBookDTO">
        SELECT
        t.activity_id activityId,
        t3.BOOK_ID bookId,
        t3.BOOK_NAME bookName
    FROM
        reading_user t
    INNER JOIN book_group_cipher_user t1 ON t.wx_user_id = t1.wx_user_id
    INNER JOIN book_group t2 ON t1.book_group_id = t2.id
    AND t2.is_delete = 0
    INNER JOIN book t3 ON t2.book_id = t3.BOOK_ID
    AND t3.IS_DELETE = 0
    WHERE
        t.activity_id IN
        <foreach collection="activityIds" item="item" open="(" separator="," close=")">
          ${item}
        </foreach>
    GROUP BY
      t.activity_id,
      t3.BOOK_ID
  </select>

</mapper>