<?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.app.mapper.HookMapper">
    
    <resultMap type="Hook" id="HookResult">
        <result property="id"    column="id"    />
        <result property="hookName"    column="hook_name"    />
        <result property="hookWeight"    column="hook_weight"    />
        <result property="defaultFlag"    column="default_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectHookVo">
        select id, hook_name, hook_weight,default_flag, create_time, create_by, update_time, update_by, del_flag from hook
    </sql>

    <select id="selectHookList" parameterType="Hook" resultMap="HookResult">
        <include refid="selectHookVo"/>
        <where>
            del_flag = '0'
            <if test="hookName != null  and hookName != ''"> and hook_name like concat('%', #{hookName}, '%')</if>
            <if test="hookWeight != null "> and hook_weight = #{hookWeight}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectHookById" parameterType="Long" resultMap="HookResult">
        <include refid="selectHookVo"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="checkHookNameUnique" parameterType="String" resultMap="HookResult">
        select id, hook_name from hook where hook_name = #{hookName} and del_flag = '0' limit 1
    </select>

    <insert id="insertHook" parameterType="Hook" useGeneratedKeys="true" keyProperty="id">
        insert into hook
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="hookName != null and hookName != ''">hook_name,</if>
            <if test="hookWeight != null">hook_weight,</if>
            <if test="defaultFlag != null">default_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            del_flag
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="hookName != null and hookName != ''">#{hookName},</if>
            <if test="hookWeight != null">#{hookWeight},</if>
            <if test="defaultFlag != null">#{defaultFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            '0'
         </trim>
    </insert>

    <update id="updateHook" parameterType="Hook">
        update hook
        <trim prefix="SET" suffixOverrides=",">
            <if test="hookName != null and hookName != ''">hook_name = #{hookName},</if>
            <if test="hookWeight != null">hook_weight = #{hookWeight},</if>
            <if test="defaultFlag != null">default_flag = #{defaultFlag},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <!--设置所有为非默认-->
    <update id="updateHookDefault" parameterType="Hook">
        update hook set default_flag = 0 where default_flag = 1
    </update>

    <delete id="deleteHookById" parameterType="Long">
        update hook set del_flag = '2' where id = #{id}
    </delete>

    <delete id="deleteHookByIds" parameterType="String">
        update hook set del_flag = '2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>