<?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.AppletBooklistDaoImpl" >
  <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.AppletBooklist" >
    <id column="id" property="id" jdbcType="BIGINT"/>
    <result column="booklist_name" property="booklistName" jdbcType="VARCHAR" />
    <result column="user_name" property="userName" jdbcType="VARCHAR" />
    <result column="digest" property="digest" jdbcType="VARCHAR" />
    <result column="display_pic" property="displayPic" jdbcType="VARCHAR" />
    <result column="introduce_type" property="introduceType" jdbcType="INTEGER" />
    <result column="introduce_content" property="introduceContent" jdbcType="VARCHAR" />
    <result column="introduce_url" property="introduceUrl" jdbcType="VARCHAR" />
    <result column="classify_id" property="classifyId" jdbcType="BIGINT" />
    <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
    <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
  </resultMap>

  <sql id="Base_Column_List" >
        id, booklist_name, user_name, digest, display_pic, introduce_type, introduce_content, introduce_url,
         classify_id, create_time, update_time
  </sql>

  <insert id="insert" parameterType="com.pcloud.book.applet.entity.AppletBooklist" useGeneratedKeys="true" keyProperty="id">
     insert into applet_booklist (
     booklist_name, user_name, digest, display_pic, introduce_type, introduce_content, introduce_url,
     classify_id, create_time, update_time
      )
    values (
      #{booklistName}, #{userName}, #{digest}, #{displayPic}, #{introduceType}, #{introduceContent}, #{introduceUrl},
      #{classifyId}, NOW(), NOW()
      )
  </insert>

  <update id="update" parameterType="com.pcloud.book.applet.entity.AppletBooklist">
    update applet_booklist
    <set>
      <if test="booklistName != null">
        booklist_name = #{booklistName},
      </if>
      <if test="userName != null">
        user_name = #{userName},
      </if>
      <if test="digest != null">
        digest = #{digest},
      </if>
      <if test="displayPic != null">
        display_pic = #{displayPic},
      </if>
      <if test="introduceType != null">
        introduce_type = #{introduceType},
      </if>
      <if test="introduceContent != null">
        introduce_content = #{introduceContent},
      </if>
      <if test="introduceUrl != null">
        introduce_url = #{introduceUrl},
      </if>
      <if test="classifyId != null">
        classify_id = #{classifyId},
      </if>
      update_time=NOW()
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="getById" parameterType="long" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/>
    from applet_booklist
    where id=#{id}
  </select>

  <delete id="deleteById" parameterType="long">
    delete from applet_booklist
    where id=#{id}
  </delete>

  <delete id="deleteConfigByBooklistId" parameterType="long">
    delete from applet_booklist_config WHERE booklist_id = #{booklistId}
  </delete>

  <insert id="addBooklistConfig" parameterType="list" useGeneratedKeys="true" keyProperty="id">
    insert into applet_booklist_config (
    booklist_id, book_id, adviser_id, channel_id,seq, create_time)
    values
    <foreach collection="list" item="item" index="index" separator=",">
    ( #{item.booklistId}, #{item.bookId}, #{item.adviserId}, #{item.channelId}, #{item.seq}, NOW() )
    </foreach>
  </insert>

  <select id="listBooklist" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletBooklistDTO">
      SELECT
        b.id id,
        b.booklist_name booklistName,
        b.user_name userName,
        b.display_pic displayPic,
        COUNT(c.id) bookCounts,
        a.id classifyId,
        a.classify_name classifyName
       FROM applet_booklist b
       LEFT JOIN applet_booklist_config c ON b.id=c.booklist_id
       LEFT JOIN applet_booklist_classify a ON b.classify_id=a.id
       WHERE 1=1
       <if test="name != null">
         AND b.booklist_name LIKE CONCAT("%", #{name} ,"%")
       </if>
       <if test="classifyId >0">
          AND b.classify_id= #{classifyId}
       </if>
       GROUP BY b.id
       order by b.create_time desc
  </select>

  <select id="getByClassifyId" parameterType="long" resultMap="BaseResultMap">
    SELECT <include refid="Base_Column_List"/> FROM applet_booklist WHERE classify_id=#{classifyId}
  </select>

  <update id="updateBookSeq" parameterType="map">
    update applet_booklist_config
    set seq = #{seq}
    where id = #{id}
  </update>

  <select id="listPageBook4AppletBooklist" parameterType="map" resultType="com.pcloud.book.applet.dto.BookDTO4Booklist">
   SELECT
	c.id id,
    c.booklist_id booklistId,
    ap.booklist_name booklistName,
    A.BOOK_ID bookId,
    A.CHANNEL_ID channelId,
    A.ADVISER_ID adviserId,
    B.BOOK_NAME bookName,
    B.AUTHOR author,
    B.PUBLISH publish,
    B.COVER_IMG coverImg,
    A.TEMPLET_ID templetId,
    A.SECOND_TEMPLET_ID secondTempletId,
    c.seq seq,
    B.ISBN isbn,
    CONCAT('BK',A.BOOK_ID) uniqueNumber
	FROM
    applet_booklist_config c
    LEFT JOIN applet_booklist ap ON c.booklist_id=ap.id
    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
	WHERE
	c.booklist_id=#{booklistId}
    ORDER BY c.seq ASC
  </select>

    <select id="listBooklist4Wechat" parameterType="map" resultType="com.pcloud.book.applet.dto.AppletBooklistDTO">
        SELECT
        b.id id,
        b.booklist_name booklistName,
        b.user_name userName,
        COUNT(c.id) bookCounts
        FROM applet_booklist b
        LEFT JOIN applet_booklist_config c ON b.id=c.booklist_id
        WHERE 1=1
        <if test="classifyId != null">
            AND b.classify_id= #{classifyId}
        </if>
        <if test="classifyIdList != null">
            AND b.classify_id in
            <foreach collection="classifyIdList" item="item" open="(" separator="," close=")">
                ${item}
            </foreach>
        </if>
        GROUP BY b.id
        HAVING COUNT(c.id)>0
        <choose>
            <when test="order>0 and order == 1">
                ORDER BY RAND()
            </when>
            <otherwise>
                order by b.create_time desc
            </otherwise>
        </choose>
    </select>

    <select id="getBookBaseByBooklistId" parameterType="map" resultType="com.pcloud.book.applet.dto.BookDTO4Booklist">
        SELECT
        c.book_id bookId,
        c.channel_id channelId,
        c.adviser_id adviserId,
        b.BOOK_NAME bookName,
        b.COVER_IMG coverImg
        FROM applet_booklist_config c
        INNER JOIN book b ON c.book_id=b.BOOK_ID
        WHERE c.booklist_id=#{booklistId}
        ORDER BY c.seq ASC
        <if test="pageNum>=0 and numPerPage>0">
            limit #{pageNum}, #{numPerPage}
        </if>
    </select>

    <select id="getConfigByBooklistIds" parameterType="map" resultType="com.pcloud.book.applet.dto.BookDTO4Booklist">
        SELECT
        booklist_id booklistId,
        book_id bookId,
        adviser_id adviserId,
        channel_id channelId
        FROM applet_booklist_config
        WHERE booklist_id in
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
         #{item}
        </foreach>
    </select>

    <select id="getByBookIdAdviserChannel" parameterType="map" resultType="com.pcloud.book.applet.dto.BookDTO4Booklist">
        SELECT
            A.BOOK_ID bookId,
            A.CHANNEL_ID channelId,
            A.ADVISER_ID adviserId,
            B.BOOK_NAME bookName,
            B.AUTHOR author,
            B.PUBLISH publish,
            B.COVER_IMG coverImg,
            A.TEMPLET_ID templetId,
            A.SECOND_TEMPLET_ID secondTempletId,
            B.ISBN isbn,
            CONCAT('BK',A.BOOK_ID) uniqueNumber
            FROM
            BOOK_ADVISER A
            INNER JOIN BOOK B ON A.BOOK_ID = B.BOOK_ID AND A.IS_DELETE = 0 AND B.IS_DELETE = 0
            WHERE
            A.BOOK_ID=#{bookId}
            AND A.ADVISER_ID=#{adviserId}
            AND A.CHANNEL_ID=#{channelId}
    </select>
</mapper>