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

	<resultMap id="BaseResultMap" type="com.pcloud.book.book.entity.SearchRecord">
		<id column="id" property="id" jdbcType="BIGINT" />
		<result column="content" property="content" jdbcType="VARCHAR" />
		<result column="book_id" property="bookId" jdbcType="BIGINT" />
		<result column="agent_id" property="agentId" jdbcType="BIGINT" />
		<result column="wx_id" property="wxId" jdbcType="VARCHAR" />
		<result column="hot_book_id" property="hotBookId" jdbcType="BIGINT" />
		<result column="user_delete" property="userDelete" jdbcType="BOOLEAN" />
		<result column="create_time" property="createTime" jdbcType="INTEGER" />
	</resultMap>

	<sql id="Base_Column_List">
		id,content,book_id,agent_id,wx_id,hot_book_id,user_delete,create_time
	</sql>

	<insert id="insert" parameterType="com.pcloud.book.book.entity.SearchRecord" useGeneratedKeys="true"
			keyProperty="id">
		insert into search_record
		<trim prefix="(" suffix=")" suffixOverrides=",">
			content,
			book_id,
			agent_id,
			wx_id,
			hot_book_id,
			create_time
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			#{content,jdbcType=VARCHAR},
			#{bookId,jdbcType=BIGINT},
			#{agentId,jdbcType=BIGINT},
			#{wxId,jdbcType=VARCHAR},
			#{hotBookId,jdbcType=BIGINT},
			NOW()
		</trim>
	</insert>

	<select id="getSearchList" parameterType="map" resultType="com.pcloud.book.book.vo.SearchBookVO">
		select
			content,
			book_id bookId,
			agent_id agentId,
			count(1) searchCount
		from search_record
		where 1=1
		<if test="content!=null">
			and content like CONCAT('%', #{content}, '%')
		</if>
		group by
		content,book_id,agent_id
	</select>

	<select id="getSearchRecordList" parameterType="map" resultMap="BaseResultMap">
		select <include refid="Base_Column_List"/>
		from search_record
		where content=#{content}
		<if test="bookId!=null">
			and book_id=#{bookId}
		</if>
		<if test="agentId!=null">
			and agent_id=#{agentId}
		</if>
	</select>

	<select id="getUserSearchList4H5" parameterType="map" resultMap="BaseResultMap">
		select <include refid="Base_Column_List"/>
		from search_record
		where wx_id=#{wxId}
		and user_delete=0
		group by content
		order by create_time desc
	</select>

	<delete id="deleteByWxId" parameterType="string" >
		delete from search_record
		where wx_id=#{wxId}
	</delete>

	<select id="getRecent5WxIds" parameterType="long" resultType="string">
		select distinct wx_id
		from search_record
		where book_id=#{bookId}
		limit 5
	</select>

	<update id="updateUserDeleteByWxId" parameterType="map" >
		update search_record
		set user_delete=#{userDelete}
		where wx_id=#{wxId}
	</update>

</mapper>