<?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.kmall.admin.dao.SearchHistoryDao">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kmall.admin.entity.SearchHistoryEntity" id="searchHistoryMap">
        <result property="id" column="id"/>
        <result property="keyword" column="keyword"/>
        <result property="from" column="from"/>
        <result property="addTime" column="add_time"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.admin.entity.SearchHistoryEntity">
		select * from mall_search_history where id = #{value}
	</select>

    <select id="queryList" resultMap="searchHistoryMap">
        select distinct a.* ,b.nickname as user_name
        from mall_search_history a
        left join mall_user b on a.user_id = b.id
        LEFT JOIN mall_merch_user d ON a.user_id=d.user_id
        where 1=1
        <if test="storeId != null">
            and d.store_id = #{storeId}
        </if>
        <if test="merchSn != null and merchSn.trim() != ''">
            and d.merch_sn = #{merchSn}
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
		select count(distinct a.id)
		from mall_search_history a
        LEFT JOIN mall_merch_user d ON a.user_id=d.user_id
        where 1=1
        <if test="storeId != null">
            and d.store_id = #{storeId}
        </if>
        <if test="merchSn != null and merchSn.trim() != ''">
            and d.merch_sn = #{merchSn}
        </if>
	</select>

    <insert id="save" parameterType="com.kmall.admin.entity.SearchHistoryEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_search_history
		(
			`keyword`, 
			`from`, 
			`add_time`, 
			`user_id`
		)
		values
		(
			#{keyword}, 
			#{from}, 
			#{addTime}, 
			#{userId}
		)
	</insert>

    <update id="update" parameterType="com.kmall.admin.entity.SearchHistoryEntity">
        update mall_search_history
        <set>
            <if test="keyword != null">`keyword` = #{keyword},</if>
            <if test="from != null">`from` = #{from},</if>
            <if test="addTime != null">`add_time` = #{addTime},</if>
            <if test="userId != null">`user_id` = #{userId}</if>
        </set>
        where id = #{id}
    </update>

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

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

</mapper>