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

	<resultMap id="BaseResultMap" type="com.pcloud.book.book.entity.SearchRecordDis">
		<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="search_count" property="searchCount" jdbcType="INTEGER" />
		<result column="create_time" property="createTime" jdbcType="INTEGER" />
		<result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
	</resultMap>

	<resultMap id="SearchRecordResultMap" 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,search_count,create_time,update_time
	</sql>

	<insert id="insert" parameterType="com.pcloud.book.book.entity.SearchRecordDis" useGeneratedKeys="true"
			keyProperty="id">
		insert into search_record_dis
		<trim prefix="(" suffix=")" suffixOverrides=",">
			content,
			book_id,
			agent_id,
			wx_id,
			wechat_user_id,
			hot_book_id,
			create_time,
			update_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(),
			NOW()
		</trim>
	</insert>

	<select id="getByCondition" parameterType="map" resultMap="BaseResultMap">
		select <include refid="Base_Column_List"/>
		from search_record_dis
		where user_delete=0
		and content=#{content}
		<if test="wechatUserId!=null">
			and wechat_user_id=#{wechatUserId}
		</if>
		<if test="wxId!=null">
			and wx_id=#{wxId}
		</if>
		limit 1
	</select>

	<update id="update" parameterType="com.pcloud.book.book.entity.SearchRecordDis">
		update search_record_dis
		<set>
			<if test="content != null">
				content = #{content},
			</if>
			<if test="bookId != null">
				book_id = #{bookId},
			</if>
			<if test="agentId != null">
				agent_id = #{agentId},
			</if>
			<if test="wxId != null">
				wx_id = #{wxId},
			</if>
			<if test="wechatUserId != null">
				wechat_user_id = #{wechatUserId},
			</if>
			<if test="hotBookId != null">
				hot_book_id = #{hotBookId},
			</if>
			<if test="userDelete != null">
				user_delete = #{userDelete},
			</if>
				search_count=search_count+1,
				update_time=now()
		</set>
		where id = #{id,jdbcType=BIGINT}
	</update>

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

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

	<select id="getUserSearchList4H5" parameterType="map" resultMap="SearchRecordResultMap">
		select
		<include refid="Base_Column_List"/>
		FROM
		search_record_dis
		WHERE
		user_delete = 0
		<if test="wxId!=null">
			and wx_id = #{wxId}
		</if>
		<if test="wechatUserId!=null">
			and wechat_user_id = #{wechatUserId}
		</if>
		ORDER by update_time DESC
	</select>

	<select id="countUserSearchList4H5" parameterType="map" resultType="integer">
		select
		count(1)
		FROM
		search_record_dis
		WHERE
		user_delete = 0
		<if test="wxId!=null">
			and wx_id = #{wxId}
		</if>
		<if test="wechatUserId!=null">
			and wechat_user_id = #{wechatUserId}
		</if>
	</select>

</mapper>