<?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.frequency.hosearch.mapper.HotSearchMapper">

    <resultMap type="com.ruoyi.frequency.hosearch.entity.HotSearch" id="HotSearchResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="searchTerm"    column="search_term"    />
        <result property="successTimes"    column="success_times"    />
        <result property="failTimes"    column="fail_times"    />
    </resultMap>



    <sql id="selectHotSearchVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.search_term ,
            t.success_times ,
            t.fail_times
        from t_hot_search AS t

    </sql>

    <select id="selectHotSearchList" parameterType="com.ruoyi.frequency.hosearch.entity.HotSearch" resultMap="HotSearchResult">
        <include refid="selectHotSearchVo"/>
        <where> AND t.del_flag != 2
            <if test="searchTerm != null  and searchTerm != ''"> and t.search_term = #{searchTerm}</if>
            <if test="successTimes != null "> and t.success_times = #{successTimes}</if>
            <if test="failTimes != null "> and t.fail_times = #{failTimes}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectHotSearchById" parameterType="Long" resultMap="HotSearchResult">
        <include refid="selectHotSearchVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertHotSearch" parameterType="com.ruoyi.frequency.hosearch.entity.HotSearch">
        insert into t_hot_search
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="searchTerm != null">search_term,</if>
            <if test="successTimes != null">success_times,</if>
            <if test="failTimes != null">fail_times,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="searchTerm != null">#{searchTerm},</if>
            <if test="successTimes != null">#{successTimes},</if>
            <if test="failTimes != null">#{failTimes},</if>
        </trim>
    </insert>

    <update id="updateHotSearch" parameterType="com.ruoyi.frequency.hosearch.entity.HotSearch">
        update t_hot_search
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="searchTerm != null">search_term = #{searchTerm},</if>
            <if test="successTimes != null">success_times = #{successTimes},</if>
            <if test="failTimes != null">fail_times = #{failTimes},</if>
        </trim>
        where id = #{id}
    </update>

    <select id="hotSearchList" resultType="com.ruoyi.frequency.hosearch.entity.HotSearch">
        select
        t.id ,
        t.create_by ,
        t.create_time ,
        t.update_by ,
        t.update_time ,
        t.del_flag ,
        t.search_term ,
        t.success_times ,
        t.fail_times
        from t_hot_search AS t
        <where>  t.del_flag = 1
            <if test="req.keyword != null  and req.keyword != ''">
                and (
                t.search_term like concat('%',#{req.keyword},'%')
                or
                #{req.keyword} like concat('%',t.search_term,'%')
                )
            </if>
        </where>
        order by t.success_times desc,t.create_time desc
    </select>

    <update id="addHotSearch" parameterType="com.ruoyi.frequency.hosearch.entity.HotSearch">
        insert into t_hot_search
        (id, create_by, create_time, update_by, update_time, search_term, success_times, fail_times)
        values
        (#{item.id},#{item.updateBy},#{item.updateTime},#{item.updateBy},#{item.updateTime},#{item.searchTerm},#{item.successTimes}, #{item.failTimes})
        ON DUPLICATE  KEY UPDATE
        <choose>
            <when test="isSuccess">
                success_times = success_times + 1,
            </when>
            <otherwise>
                fail_times = fail_times + 1,
            </otherwise>
        </choose>
        update_time = #{item.updateTime},
        update_by = #{item.updateBy};
    </update>
</mapper>