<?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" />
    <result column="first_classify" property="firstClassify" jdbcType="BIGINT" />
    <result column="second_classify" property="secondClassify" jdbcType="BIGINT" />
    <result column="third_classify" property="thirdClassify" jdbcType="BIGINT"/>
    <result column="grade_label_id" property="gradeLabelId" jdbcType="BIGINT" />
    <result column="subject_label_id" property="subjectLabelId" jdbcType="BIGINT" />
    <result column="relate_agent_id" property="relateAgentId" jdbcType="BIGINT" />
  </resultMap>

    <resultMap id="BaseResultMap4DTO" type="com.pcloud.book.applet.dto.AppletBooklistDTO" >
        <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="first_classify" property="firstClassify" jdbcType="BIGINT" />
        <result column="second_classify" property="secondClassify" jdbcType="BIGINT" />
        <result column="third_classify" property="thirdClassify" jdbcType="BIGINT"/>
        <result column="grade_label_id" property="gradeLabelId" jdbcType="BIGINT" />
        <result column="subject_label_id" property="subjectLabelId" jdbcType="BIGINT" />
    </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, first_classify, second_classify, third_classify, grade_label_id, subject_label_id, relate_agent_id
  </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, first_classify, second_classify, third_classify, grade_label_id, subject_label_id,
     relate_agent_id
      )
    values (
      #{booklistName}, #{userName}, #{digest}, #{displayPic}, #{introduceType}, #{introduceContent}, #{introduceUrl},
      #{classifyId}, NOW(), NOW(), #{firstClassify,jdbcType=BIGINT}, #{secondClassify,jdbcType=BIGINT},#{thirdClassify},
      #{gradeLabelId,jdbcType=BIGINT}, #{subjectLabelId,jdbcType=BIGINT}, #{relateAgentId}
      )
  </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>
        first_classify = #{firstClassify,jdbcType=BIGINT},
        second_classify = #{secondClassify,jdbcType=BIGINT},
        third_classify = #{thirdClassify},
        grade_label_id = #{gradeLabelId,jdbcType=BIGINT},
        subject_label_id = #{subjectLabelId,jdbcType=BIGINT},
        relate_agent_id = #{relateAgentId},
      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,
        b.first_classify firstClassify,
        b.second_classify secondClassify,
        b.third_classify thirdClassify,
        b.grade_label_id gradeLabelId,
        b.subject_label_id subjectLabelId,
        b.relate_agent_id relateAgentId
       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 and b.is_delete = 0
       <if test="name != null">
         AND b.booklist_name LIKE CONCAT("%", #{name} ,"%")
       </if>
       <if test="classifyId >0">
          AND b.classify_id= #{classifyId}
       </if>
      <if test="firstClassify!=null">
          AND b.first_classify =#{firstClassify}
      </if>
      <if test="secondClassify!=null">
          AND b.second_classify =#{secondClassify}
      </if>
      <if test="thirdClassify != null">
          and b.third_classify = #{thirdClassify}
      </if>
      <if test="gradeLabelId != null">
          AND b.grade_label_id = #{gradeLabelId}
      </if>
      <if test="subjectLabelId != null">
          AND b.subject_label_id = #{subjectLabelId}
      </if>
      <if test="relateAgentId != null">
          AND b.relate_agent_id = #{relateAgentId}
      </if>
       <choose>
           <when test="agentId != null">
               and a.agent_id = #{agentId}
           </when>
           <otherwise>
               and a.agent_id = 0
           </otherwise>
       </choose>
       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} and is_delete = 0
  </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,
    A.third_TEMPLET_ID thirdTempletId,
    c.seq seq,
    B.ISBN isbn,
    CONCAT('BK',A.BOOK_ID) uniqueNumber,
    B.REMARK remark,
    B.BOOK_PRICE bookPrice
	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} and ap.is_delete = 0
    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 and  b.is_delete = 0
        <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,
            A.third_TEMPLET_ID thirdTempletId,
            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>


    <select id="getByLabel" 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
        FROM
            applet_booklist b
        LEFT JOIN applet_booklist_config c ON b.id = c.booklist_id
        WHERE 1 = 1
        <if test="firstClassify!=null">
            AND b.first_classify =#{firstClassify}
        </if>
        <if test="secondClassify!=null">
            AND b.second_classify =#{secondClassify}
        </if>
        <if test="thirdClassify != null">
            and b.third_classify = #{thirdClassify}
        </if>
        <if test="gradeLabelId != null">
            AND b.grade_label_id = #{gradeLabelId}
        </if>
        <if test="subjectLabelId != null">
            AND b.subject_label_id = #{subjectLabelId}
        </if>
        <if test="relateAgentId != null">
            AND b.relate_agent_id = #{relateAgentId}
        </if>
        GROUP BY
            b.id
        HAVING
            COUNT(c.id) > 0
        ORDER BY b.create_time DESC
        <if test="limit != null">
            LIMIT #{limit}
        </if>
    </select>




    <update id="updateDeleteState" parameterType="long">
        update
        applet_booklist
        set
        is_delete = 1
        where
        id = #{id}
    </update>

    <select id="getBookListMap" parameterType="list" resultMap="BaseResultMap4DTO">
        SELECT
        b.id id,
        b.booklist_name,
        b.user_name,
        COUNT(c.id) bookCounts
        FROM applet_booklist b
        LEFT JOIN applet_booklist_config c ON b.id=c.booklist_id
        WHERE
        b.is_delete = 0
        and
        b.id in
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
            #{item}
        </foreach>
        group by b.id
    </select>

    <select id="getBooklistIdByBookId4Library" parameterType="long" resultType="long">
        SELECT
            booklist_id
        FROM
            applet_booklist_labrary
        WHERE
            book_id = #{bookId}
        LIMIT 1
    </select>

    <select id="getBookCount" parameterType="long" resultType="integer">
        SELECT COUNT(1) FROM applet_booklist_config WHERE booklist_id = #{booklistId}
    </select>
</mapper>