<?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
			t1.id,
			t1.book_id bookId,
			t1.seq_num seqNum,
			t1.create_time createTime,
			t2.book_name bookName,
			t3.adviser_id adviserId,
			count(t4.id) searchCount
		from hot_book t1
		left join search_record t4
		on t1.id=t4.hot_book_id
		left join book t2
		on t1.book_id=t2.book_id
		left join book_adviser t3
		on t1.book_id=t3.book_id
		group by t1.id
		order by seq_num
	</select>

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