<?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.muyu.platform.mapper.WarnRuleMapper">
    
    <resultMap type="WarnRule" id="WarnRuleResult">
        <result property="id"    column="id"    />
        <result property="ruleName"    column="rule_name"    />
        <result property="strategyId"    column="strategy_id"    />
        <result property="msgTypeId"    column="msg_type_id"    />
        <result property="slideTime"    column="slide_time"    />
        <result property="slideFrequency"    column="slide_frequency"    />
        <result property="maxValue"    column="max_value"    />
        <result property="minValue"    column="min_value"    />
    </resultMap>

    <sql id="selectWarnRuleVo">
        select id, rule_name, strategy_id, msg_type_id, slide_time, slide_frequency, max_value, min_value from warn_rule
    </sql>

    <select id="selectWarnRuleList" parameterType="WarnRule" resultMap="WarnRuleResult">
        <include refid="selectWarnRuleVo"/>
        <where>  
            <if test="ruleName != null  and ruleName != ''"> and rule_name like concat('%', #{ruleName}, '%')</if>
            <if test="strategyId != null "> and strategy_id = #{strategyId}</if>
            <if test="msgTypeId != null "> and msg_type_id = #{msgTypeId}</if>
            <if test="slideTime != null "> and slide_time = #{slideTime}</if>
            <if test="slideFrequency != null "> and slide_frequency = #{slideFrequency}</if>
            <if test="maxValue != null "> and max_value = #{maxValue}</if>
            <if test="minValue != null "> and min_value = #{minValue}</if>
        </where>
    </select>
    
    <select id="selectWarnRuleById" parameterType="Long" resultMap="WarnRuleResult">
        <include refid="selectWarnRuleVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertWarnRule" parameterType="WarnRule" useGeneratedKeys="true" keyProperty="id">
        insert into warn_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="ruleName != null">rule_name,</if>
            <if test="strategyId != null">strategy_id,</if>
            <if test="msgTypeId != null">msg_type_id,</if>
            <if test="slideTime != null">slide_time,</if>
            <if test="slideFrequency != null">slide_frequency,</if>
            <if test="maxValue != null">max_value,</if>
            <if test="minValue != null">min_value,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="ruleName != null">#{ruleName},</if>
            <if test="strategyId != null">#{strategyId},</if>
            <if test="msgTypeId != null">#{msgTypeId},</if>
            <if test="slideTime != null">#{slideTime},</if>
            <if test="slideFrequency != null">#{slideFrequency},</if>
            <if test="maxValue != null">#{maxValue},</if>
            <if test="minValue != null">#{minValue},</if>
         </trim>
    </insert>

    <update id="updateWarnRule" parameterType="WarnRule">
        update warn_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="ruleName != null">rule_name = #{ruleName},</if>
            <if test="strategyId != null">strategy_id = #{strategyId},</if>
            <if test="msgTypeId != null">msg_type_id = #{msgTypeId},</if>
            <if test="slideTime != null">slide_time = #{slideTime},</if>
            <if test="slideFrequency != null">slide_frequency = #{slideFrequency},</if>
            <if test="maxValue != null">max_value = #{maxValue},</if>
            <if test="minValue != null">min_value = #{minValue},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWarnRuleById" parameterType="Long">
        delete from warn_rule where id = #{id}
    </delete>

    <delete id="deleteWarnRuleByIds" parameterType="String">
        delete from warn_rule where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>