<?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.HotBookSaleDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.applet.entity.HotBookSale">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="book_id" property="bookId" jdbcType="INTEGER"/>
        <result column="seq_num" property="seqNum" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, book_id, seq_num, create_time        
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM hot_book_sale
        WHERE id = #{id}
    </select>

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM hot_book_sale
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO hot_book_sale(
            book_id, 
            seq_num, 
            create_time
        ) VALUES (
            #{bookId, jdbcType=INTEGER}, 
            #{seqNum, jdbcType=INTEGER}, 
            now()
        )
    </insert>

    <insert id="batchInsert">
        INSERT INTO hot_book_sale (
        book_id,
        seq_num,
        create_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.bookId, jdbcType=INTEGER},
            #{item.seqNum, jdbcType=INTEGER},
            #{item.createTime, jdbcType=TIMESTAMP}
            )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE hot_book_sale
        <set>
            <if test="bookId != null">
                book_id = #{bookId},
            </if>
            <if test="seqNum != null">
                seq_num = #{seqNum},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        DELETE FROM hot_book_sale where id = #{id}
    </delete>

    <select id="getMaxSeqNum" resultType="integer">
		select ifnull(max(seq_num),0)  from hot_book_sale
	</select>

    <select id="getHotBookList" parameterType="map" resultType="com.pcloud.book.applet.entity.HotBookSale">
		select
			h.id,
			h.book_id bookId,
			h.seq_num seqNum,
			h.create_time createTime,
			b.BOOK_NAME bookName,
			b.COVER_IMG coverImg,
			b.author,
			b.publish,
			a.ADVISER_ID adviserId,
			a.channel_id channelId,
			count(r.id) searchCount
		from hot_book_sale h
		LEFT JOIN search_record_sale r
		on h.id=r.hot_book_id
		INNER JOIN book b
		on h.book_id=b.BOOK_ID
		INNER JOIN book_adviser a
		ON h.book_id=a.BOOK_ID
		group by h.id
		order by h.seq_num
	</select>

    <update id="updateSeqNum" parameterType="map" >
		update hot_book_sale
		set seq_num=#{seqNum}
		where id=#{id}
	</update>


</mapper>