<?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.platform.dao.ApiSearchHistoryMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.platform.entity.SearchHistoryVo" id="searchHistoryMap">
        <result property="id" column="id"/>
        <result property="keyword" column="keyword"/>
        <result property="from" column="from"/>
        <result property="add_time" column="add_time"/>
        <result property="user_id" column="user_id"/>
    </resultMap>

    <select id="queryObject" resultMap="searchHistoryMap">
		select * from nideshop_search_history where id = #{value}
	</select>

    <select id="queryList" resultMap="searchHistoryMap">
        select
        <if test="fields != null and fields != ''">
            ${fields}
        </if>
        <if test="fields == null or fields == ''">
            *
        </if>
        from nideshop_search_history
        <where>
            <if test="user_id != null">
                and user_id = #{user_id}
            </if>
        </where>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
		select count(*) from nideshop_search_history 
	</select>

    <insert id="save" parameterType="com.platform.entity.SearchHistoryVo" useGeneratedKeys="true" keyProperty="id">
		insert into nideshop_search_history
		(
			`keyword`, 
			`from`, 
			`add_time`, 
			`user_id`
		)
		values
		(
			#{keyword}, 
			#{from}, 
			#{add_time}, 
			#{user_id}
		)
	</insert>

    <update id="update" parameterType="com.platform.entity.SearchHistoryVo">
        update nideshop_search_history
        <set>
            <if test="keyword != null">`keyword` = #{keyword},</if>
            <if test="from != null">`from` = #{from},</if>
            <if test="add_time != null">`add_time` = #{add_time},</if>
            <if test="user_id != null">`user_id` = #{user_id}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from nideshop_search_history where id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from nideshop_search_history where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteByUserId">
		delete from nideshop_search_history where  user_id = #{user_id}
	</delete>
</mapper>