<?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.applet.dao.impl.AppletUserBookcaseDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletUserBookcase" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="wechat_user_id" property="wechatUserId" jdbcType="BIGINT" />
    <result column="book_id" property="bookId" jdbcType="BIGINT" />
    <result column="channel_id" property="channelId" jdbcType="BIGINT" />
    <result column="adviser_id" property="adviserId" jdbcType="BIGINT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="read_type" property="readType" jdbcType="TINYINT" />
    <result column="rights_setting_id" property="rightsSettingId" jdbcType="BIGINT" />
    <result column="book_group_id" property="bookGroupId" jdbcType="BIGINT" />
  </resultMap>

  <sql id="Base_Column_List" >
    id, wechat_user_id, book_id, channel_id, adviser_id, create_time,read_type, rights_setting_id
  </sql>


    <sql id="Base_Column_List_own" >
    a.id, a.wechat_user_id, a.book_id, a.channel_id, a.adviser_id, a.create_time,read_type, a.rights_setting_id
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.applet.entity.AppletUserBookcase" useGeneratedKeys="true" keyProperty="id">
    insert into applet_user_bookcase (
      wechat_user_id, book_id, channel_id, adviser_id, create_time, read_type, rights_setting_id
      )
    values (
      #{wechatUserId}, #{bookId}, #{channelId}, #{adviserId}, NOW(), #{readType}, #{rightsSettingId}
      )
    on duplicate key
    update
    create_time = now()
  </insert>


    <insert id="batchInsert" parameterType="com.pcloud.book.applet.entity.AppletUserBookcase" useGeneratedKeys="true" keyProperty="id">
    insert into applet_user_bookcase (
      wechat_user_id, book_id, channel_id, adviser_id, create_time, read_type, rights_setting_id
      )
    values
    <foreach collection="list" item="item" index="index" separator=",">
        (
        #{item.wechatUserId}, #{item.bookId}, #{item.channelId}, #{item.adviserId}, NOW(), #{item.readType}, #{item.rightsSettingId}
        )
    </foreach>
    on duplicate key
    update
    create_time = now()
  </insert>

  <select id="listByWechatUserId" parameterType="long" resultType="com.pcloud.book.applet.dto.AppletUserBookcaseDTO">
    SELECT
        c.id id,
        c.book_id bookId,
        c.channel_id channelId,
        c.adviser_id adviserId,
        b.BOOK_NAME bookName,
        b.COVER_IMG coverImg,
        c.create_time createTime,
		b.AUTHOR author,
		b.PUBLISH publish,
		a.SECOND_TEMPLET_ID secondTempletId,
		a.TEMPLET_ID templetId,
		d.join_group_type joinGroupType,
		d.id bookGroupId,
		b.ISBN isbn,
		a.GRA_LABEL_ID gradeLabelId,
        a.SUB_LABEL_ID subjectLabelId,
        a.vol_label_id volLabelId,
        IF(d.join_group_type=1 OR d.related_book_group_id>0,1,0) hasGroup,
        r.id rightsSettingId,
        r.count rightsSettingCount
    FROM applet_user_bookcase c
    LEFT JOIN BOOK_ADVISER a ON c.book_id=a.BOOK_ID AND c.adviser_id=a.ADVISER_ID
    AND c.channel_id=a.CHANNEL_ID AND a.IS_DELETE = 0
	INNER JOIN BOOK b ON c.BOOK_ID = b.BOOK_ID AND b.IS_DELETE = 0
	LEFT JOIN book_group d ON d.BOOK_ID = a.BOOK_ID and d.create_user=c.adviser_id and d.channel_id=c.channel_id AND d.IS_DELETE = 0
	LEFT JOIN rights_setting r ON c.rights_setting_id = r.id AND d.join_group_type=4 and r.show_state = 1  and r.rights_setting_type = 2
    WHERE c.wechat_user_id=#{wechatUserId}
    ORDER BY c.create_time DESC, d.id desc
  </select>

  <insert id="insertClickRecord" parameterType="com.pcloud.book.applet.entity.AppletUserClickRecord">
    insert into applet_user_click_record (
      id,wechat_user_id, book_id, from_id, from_type, channel_id, adviser_id, create_time
      )
    values (
      #{id}, #{wechatUserId}, #{bookId}, #{fromId}, #{fromType}, #{channelId}, #{adviserId}, NOW()
      )
  </insert>

  <select id="getUserClickServerCount" parameterType="map" resultType="Integer">
        SELECT COUNT(DISTINCT from_id)
        FROM applet_user_click_record
        WHERE wechat_user_id=#{wechatUserId}
        AND book_id = #{bookId}
        AND channel_id=#{channelId}
        AND adviser_id=#{adviserId}
        AND from_id in
        <foreach collection="list" index="i" item="item" open="(" separator="," close=")">
              ${item}
        </foreach>
        AND from_type = #{fromType}
  </select>

    <select id="getMinClickTime" resultType="string" parameterType="map">
        SELECT
        DATE_FORMAT(MIN(create_time),'%Y-%m-%d')
        FROM applet_user_click_record
        WHERE wechat_user_id=#{wechatUserId}
        AND book_id = #{bookId}
        AND channel_id=#{channelId}
        AND adviser_id=#{adviserId}
        AND from_id in
        <foreach collection="list" index="i" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
        AND from_type = #{fromType}
    </select>

    <delete id="deleteByBookIdUserId" parameterType="com.pcloud.book.applet.entity.AppletUserBookcase">
        DELETE FROM applet_user_bookcase
        WHERE wechat_user_id=#{wechatUserId}
        AND book_id=#{bookId}
        AND channel_id=#{channelId}
        AND adviser_id=#{adviserId}
    </delete>

    <select id="getUserLastGroupBookRe" parameterType="long" resultType="com.pcloud.book.applet.dto.UserLastBookReDTO">
       SELECT
            t.book_id bookId,
            t.adviser_id adviserId,
            t.channel_id channelId,
            t.read_type readType
        FROM
            applet_user_bookcase t
        INNER JOIN book_group t1 ON t.book_id = t1.book_id
        AND t1.join_group_type = 4
        AND t1.is_delete = 0
        WHERE
            t.channel_id >= 1
        AND t.adviser_id >= 1
        AND t.wechat_user_id = #{wechatUserId}
        ORDER BY
            t.create_time DESC, t1.id desc
        LIMIT 1
    </select>

    <select id="getUserLastRe" parameterType="long" resultType="com.pcloud.book.applet.dto.UserLastBookReDTO" >
        SELECT
            t.book_id bookId,
            t.adviser_id adviserId,
            t.channel_id channelId,
            t.read_type readType
        FROM
            applet_user_bookcase t
        WHERE
            t.channel_id >= 1
        AND t.adviser_id >= 1
        AND t.wechat_user_id = #{wechatUserId}
        ORDER BY
            t.create_time DESC
        LIMIT 1
    </select>

    <delete id="deleteByIds" parameterType="list">
        DELETE FROM applet_user_bookcase
        WHERE id in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
            ${item}
        </foreach>
    </delete>

    <update id="updateUserReadType" parameterType="com.pcloud.book.applet.entity.AppletUserBookcase">
      update
      applet_user_bookcase
      set
      read_type = #{readType}
      WHERE
      wechat_user_id=#{wechatUserId}
      AND book_id=#{bookId}
      AND channel_id=#{channelId}
      AND adviser_id=#{adviserId}
    </update>

  <select id="getUserReadType" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from
    applet_user_bookcase
    where
    wechat_user_id=#{wechatUserId}
    AND book_id=#{bookId}
    AND channel_id=#{channelId}
    AND adviser_id=#{adviserId}
    limit 1
  </select>

    <select id="getListByUserId" parameterType="long" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List_own"/>, t1.id book_group_id
        FROM
        applet_user_bookcase a
        left JOIN book_group t1 ON a.book_id = t1.book_id
        AND t1.join_group_type = 4
        AND t1.is_delete = 0
        WHERE
        wechat_user_id = #{wechatUserId}
        group by a.book_id, a.channel_id, a.adviser_id
        ORDER BY
        create_time DESC, book_group_id desc
    </select>

    <select id="getBookcaseIdListByUser" resultType="long" parameterType="long">
        SELECT DISTINCT id
        FROM applet_user_bookcase
        WHERE wechat_user_id = #{wechatUserId}
    </select>

    <update id="updateRightsSettingId" parameterType="com.pcloud.book.applet.entity.AppletUserBookcase">
      update
      applet_user_bookcase
      set
      rights_setting_id = #{rightsSettingId}
      WHERE
      book_id=#{bookId}
      AND channel_id=#{channelId}
      AND adviser_id=#{adviserId}
    </update>

    <select id="getBookUserCountList" resultType="com.pcloud.book.applet.dto.AppletUserBookcaseDTO" parameterType="map">
     SELECT book_id bookId, adviser_id adviserId, channel_id channelId, COUNT(1) bookUserCount
      FROM applet_user_bookcase
      WHERE  1=1
      <if test="bookIds != null and bookIds.size>0">
          AND book_id in
          <foreach collection="bookIds" open="(" close=")" separator="," index="index" item="item">
              #{item}
          </foreach>
      </if>
     GROUP BY book_id, adviser_id, channel_id
    </select>

    <select id="getBookIdsByIds" resultType="long" parameterType="map">
        SELECT DISTINCT book_id
        FROM applet_user_bookcase
        WHERE id in
        <foreach collection="ids" open="(" close=")" separator="," index="index" item="item">
            #{item}
        </foreach>
    </select>

    <select id="getUserBookInfoByWechatUserId" parameterType="long" resultType="com.pcloud.book.applet.dto.AppletUserBookcaseDTO">
    SELECT
        c.id id,
        c.book_id bookId,
        c.channel_id channelId,
        c.adviser_id adviserId,
        b.BOOK_NAME bookName,
        b.COVER_IMG coverImg,
        c.create_time createTime,
		b.AUTHOR author,
		b.PUBLISH publish,
		a.SECOND_TEMPLET_ID secondTempletId,
		a.TEMPLET_ID templetId,
		d.join_group_type joinGroupType,
		d.id bookGroupId,
		b.ISBN isbn,
		a.GRA_LABEL_ID gradeLabelId,
        a.SUB_LABEL_ID subjectLabelId,
        a.vol_label_id volLabelId,
        IF(d.join_group_type=1 OR d.related_book_group_id>0,1,0) hasGroup,
        c.rights_setting_id rightsSettingId,
        r.count rightsSettingCount,
        d.related_book_group_id relatedBookGroupId
    FROM applet_user_bookcase c
    LEFT JOIN BOOK_ADVISER a ON c.book_id=a.BOOK_ID AND c.adviser_id=a.ADVISER_ID
    AND c.channel_id=a.CHANNEL_ID AND a.IS_DELETE = 0
	INNER JOIN BOOK b ON c.BOOK_ID = b.BOOK_ID AND b.IS_DELETE = 0
	LEFT JOIN book_group d ON d.BOOK_ID = a.BOOK_ID and d.create_user=c.adviser_id and d.channel_id=c.channel_id AND d.IS_DELETE = 0
	LEFT JOIN rights_setting r ON c.rights_setting_id = r.id AND d.join_group_type=4
    WHERE  c.book_id = #{bookId} AND c.channel_id = #{channelId} AND c.adviser_id = #{adviserId}
    ORDER BY c.create_time DESC
    LIMIT 1
  </select>

    <select id="getUserReadBook" parameterType="long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from applet_user_bookcase where wechat_user_id = #{wechatUserId} order by create_time desc limit 1
    </select>

    <select id="getBookClassifyInfo" parameterType="map" resultType="com.pcloud.book.rightsSetting.entity.BaseTempletClassify">
        SELECT b.TEMPLET_ID        firstClassify,
               b.SECOND_TEMPLET_ID secondClassify,
               b.GRA_LABEL_ID      gradeLabelId,
               b.SUB_LABEL_ID      subjectLabelId,
               b.vol_label_id      volLabelId
        FROM applet_user_bookcase a
                 LEFT JOIN book_adviser b ON a.book_id = b.BOOK_ID
            AND a.channel_id = b.CHANNEL_ID
            AND a.adviser_id = b.ADVISER_ID
        WHERE b.IS_DELETE = 0
          AND a.book_id = #{bookId}
          AND a.channel_id = #{channelId}
          AND a.adviser_id = #{adviserId}
        ORDER BY a.create_time DESC
        LIMIT 1
    </select>

    <select id="getLastReadBookInfo" parameterType="map" resultType="com.pcloud.book.rightsSetting.entity.BaseTempletClassify">
        SELECT b.TEMPLET_ID        firstClassify,
               b.SECOND_TEMPLET_ID secondClassify,
               b.GRA_LABEL_ID      gradeLabelId,
               b.SUB_LABEL_ID      subjectLabelId,
               b.vol_label_id      volLabelId
        FROM applet_user_bookcase a
                 LEFT JOIN book_adviser b ON a.book_id = b.BOOK_ID
            AND a.channel_id = b.CHANNEL_ID
            AND a.adviser_id = b.ADVISER_ID
        WHERE b.IS_DELETE = 0
          AND a.wechat_user_id = #{wechatUserId}
        ORDER BY a.create_time DESC
        LIMIT 1
    </select>

</mapper>