<?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.book.dao.impl.HotBookDaoImpl">

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

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

	<insert id="insert" parameterType="com.pcloud.book.book.entity.HotBook" useGeneratedKeys="true"
			keyProperty="id">
		insert into hot_book
		<trim prefix="(" suffix=")" suffixOverrides=",">
			book_id,
			seq_num,
			create_time
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			#{bookId,jdbcType=BIGINT},
			#{seqNum,jdbcType=INTEGER},
			NOW()
		</trim>
	</insert>

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

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

	<select id="getHotBookList" parameterType="map" resultType="com.pcloud.book.book.entity.HotBook">
		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,
			a.TEMPLET_ID templetId
		from hot_book h
		LEFT JOIN search_record 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
		INNER JOIN
		applet_book_classify_relation
		group by h.id
		order by h.seq_num
	</select>

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