<?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="wechat_user_id" property="wechatUserId" jdbcType="BIGINT" />
		<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,wechat_user_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,
			wechat_user_id,
			hot_book_id,
			create_time
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
			#{content,jdbcType=VARCHAR},
			#{bookId,jdbcType=BIGINT},
			#{agentId,jdbcType=BIGINT},
			#{wxId,jdbcType=VARCHAR},
			#{wechatUserId,jdbcType=BIGINT},
			#{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>


	<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>

	<select id="getBookSearchTop" parameterType="map" resultType="com.pcloud.book.book.vo.BookSearchTopVO">
		select
		content bookName,
		book_id bookId,
		agent_id agentId
		from search_record
		where book_id is not null
		and agent_id is not null
		group by book_id,agent_id
		order by count(1) desc
		limit #{top}
	</select>

	<update id="updateUserDeleteByWechatUserId" parameterType="map" >
		update search_record
		set user_delete=#{userDelete}
		where wechat_user_id=#{wechatUserId}
	</update>

	<select id="getBookSearchCount" parameterType="map" resultType="Integer">
		select ifnull(count(1),0) from(
		SELECT
		b.BOOK_ID bookId
		FROM
		(select book_id bookId from
		search_record
		where wechat_user_id>0 and book_id>0
		GROUP BY book_id)c
		INNER JOIN
		book_adviser a on c.bookId=a.book_id
		INNER JOIN book b ON b.BOOK_ID = a.BOOK_ID AND b.IS_DELETE = 0
		WHERE
		1=1
		<if test="keyword !=null">
			AND (b.BOOK_NAME LIKE CONCAT('%', #{keyword}, '%') or b.ISBN LIKE
			CONCAT(#{keyword},'%'))
		</if>
		GROUP BY b.BOOK_ID
		)t
	</select>

	<select id="getBookSearchIds" parameterType="map" resultType="com.pcloud.book.book.dto.AdviserBookInfoDTO">
		SELECT
		b.BOOK_ID bookId,
		b.ISBN isbn,
		b.BOOK_NAME bookName,
		a.ADVISER_ID adviserId
		FROM
      (select book_id bookId from
		search_record
		where wechat_user_id>0 and book_id>0
    GROUP BY book_id)c
       INNER JOIN
        book_adviser a on c.bookId=a.book_id
		INNER JOIN book b ON b.BOOK_ID = a.BOOK_ID AND b.IS_DELETE = 0
		WHERE
		1=1
		<if test="keyword !=null">
			AND (b.BOOK_NAME LIKE CONCAT('%', #{keyword}, '%') or  b.ISBN LIKE CONCAT(#{keyword},'%'))
		</if>
		group by b.BOOK_ID
		order by count(1) desc
		limit #{pageNum}, #{numPerPage}
	</select>

	<select id="getWechatUserIds" parameterType="map" resultType="Long">
		select distinct wechat_user_id
		from search_record
		where book_id=#{bookId}
		and wechat_user_id>0
	</select>
</mapper>