<?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.ruoyi.mall.mapper.MallMemberSearchHistoryMapper">
    
    <resultMap type="MallMemberSearchHistory" id="MallMemberSearchHistoryResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="goodName"    column="good_name"    />
        <result property="createAt"    column="create_at"    />
    </resultMap>
    <resultMap type="com.ruoyi.mall.domain.MallMemberSearchHistoryDto" id="MallMemberSearchHistoryDtoResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="goodName"    column="good_name"    />
        <result property="createAt"    column="create_at"    />
        <result property="userName"    column="userName"    />
        <result property="phone"    column="phone"    />
        <result property="salesmanName"    column="salesmanName"    />
    </resultMap>

    <sql id="selectMallMemberSearchHistoryVo">
        select id, user_id, good_name, create_at from mall_member_search_history
    </sql>

    <select id="selectMallMemberSearchHistoryList" parameterType="MallMemberSearchHistoryDto" resultMap="MallMemberSearchHistoryDtoResult">
        select mmsh.id, mmsh.user_id,mmu.name as userName,mmu.phone as phone,ms.name as salesmanName, mmsh.good_name, mmsh.create_at from mall_member_search_history mmsh
        left join mall_member_user mmu on mmsh.user_id =mmu.id
        left join mall_salesman ms on mmu.salesman_id =ms.id
        <where>
            <if test="userId != null "> and mmsh.user_id = #{userId}</if>
            <if test="userName != null "> and mmu.name like concat('%', #{userName}, '%')</if>
            <if test="goodName != null  and goodName != ''"> and mmsh.good_name like concat('%', #{goodName}, '%')</if>
            <if test="createAt != null  and createAt != ''"> and mmsh.create_at = #{createAt}</if>
        </where>
        order by create_at desc
    </select>
    
    <select id="selectMallMemberSearchHistoryById" parameterType="Long" resultMap="MallMemberSearchHistoryResult">
        <include refid="selectMallMemberSearchHistoryVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertMallMemberSearchHistory" parameterType="MallMemberSearchHistory" useGeneratedKeys="true" keyProperty="id">
        insert into mall_member_search_history
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="goodName != null">good_name,</if>
            <if test="createAt != null">create_at,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="goodName != null">#{goodName},</if>
            <if test="createAt != null">#{createAt},</if>
         </trim>
    </insert>

    <update id="updateMallMemberSearchHistory" parameterType="MallMemberSearchHistory">
        update mall_member_search_history
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="goodName != null">good_name = #{goodName},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMallMemberSearchHistoryById" parameterType="Long">
        delete from mall_member_search_history where id = #{id}
    </delete>

    <delete id="deleteMallMemberSearchHistoryByIds" parameterType="String">
        delete from mall_member_search_history where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteMallMemberSearchHistoryByUserId" parameterType="long">
        delete from mall_member_search_history where user_id = #{userId}
    </delete>
</mapper>