<?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.tongran.rocketmq.mapper.RmAlarmRuleMapper">
    
    <resultMap type="RmAlarmRule" id="RmAlarmRuleResult">
        <result property="id"    column="id"    />
        <result property="alarmPolicyId"    column="alarm_policy_id"    />
        <result property="ruleName"    column="rule_name"    />
        <result property="metricKey"    column="metric_key"    />
        <result property="operator"    column="operator"    />
        <result property="threshold"    column="threshold"    />
        <result property="resourceType"    column="resource_type"    />
        <result property="status"    column="status"    />
        <result property="portWhitelist"    column="port_whitelist"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectRmAlarmRuleVo">
        select id, alarm_policy_id, rule_name, metric_key, operator, threshold, resource_type, status, port_whitelist, create_time, update_time, create_by, update_by from rm_alarm_rule
    </sql>

    <select id="selectRmAlarmRuleList" parameterType="RmAlarmRule" resultMap="RmAlarmRuleResult">
        <include refid="selectRmAlarmRuleVo"/>
        <where>  
            <if test="alarmPolicyId != null "> and alarm_policy_id = #{alarmPolicyId}</if>
            <if test="ruleName != null  and ruleName != ''"> and rule_name like concat('%', #{ruleName}, '%')</if>
            <if test="metricKey != null  and metricKey != ''"> and metric_key = #{metricKey}</if>
            <if test="operator != null  and operator != ''"> and operator = #{operator}</if>
            <if test="threshold != null "> and threshold = #{threshold}</if>
            <if test="resourceType != null  and resourceType != ''"> and resource_type = #{resourceType}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="portWhitelist != null  and portWhitelist != ''"> and port_whitelist = #{portWhitelist}</if>
        </where>
    </select>
    
    <select id="selectRmAlarmRuleById" parameterType="Long" resultMap="RmAlarmRuleResult">
        <include refid="selectRmAlarmRuleVo"/>
        where id = #{id}
    </select>

    <insert id="insertRmAlarmRule" parameterType="RmAlarmRule" useGeneratedKeys="true" keyProperty="id">
        insert into rm_alarm_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="alarmPolicyId != null">alarm_policy_id,</if>
            <if test="ruleName != null">rule_name,</if>
            <if test="metricKey != null">metric_key,</if>
            <if test="operator != null">operator,</if>
            <if test="threshold != null">threshold,</if>
            <if test="resourceType != null">resource_type,</if>
            <if test="status != null">status,</if>
            <if test="portWhitelist != null">port_whitelist,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="alarmPolicyId != null">#{alarmPolicyId},</if>
            <if test="ruleName != null">#{ruleName},</if>
            <if test="metricKey != null">#{metricKey},</if>
            <if test="operator != null">#{operator},</if>
            <if test="threshold != null">#{threshold},</if>
            <if test="resourceType != null">#{resourceType},</if>
            <if test="status != null">#{status},</if>
            <if test="portWhitelist != null">#{portWhitelist},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
         </trim>
    </insert>

    <update id="updateRmAlarmRule" parameterType="RmAlarmRule">
        update rm_alarm_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="alarmPolicyId != null">alarm_policy_id = #{alarmPolicyId},</if>
            <if test="ruleName != null">rule_name = #{ruleName},</if>
            <if test="metricKey != null">metric_key = #{metricKey},</if>
            <if test="operator != null">operator = #{operator},</if>
            <if test="threshold != null">threshold = #{threshold},</if>
            <if test="resourceType != null">resource_type = #{resourceType},</if>
            <if test="status != null">status = #{status},</if>
            <if test="portWhitelist != null">port_whitelist = #{portWhitelist},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRmAlarmRuleById" parameterType="Long">
        delete from rm_alarm_rule where id = #{id}
    </delete>

    <delete id="deleteRmAlarmRuleByIds" parameterType="String">
        delete from rm_alarm_rule where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteRmAlarmRuleByPolicyId" parameterType="Long">
        delete from rm_alarm_rule where  alarm_policy_id #{id}
    </delete>
    <!-- 批量插入告警规则 -->
    <insert id="batchInsertRmAlarmRule" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO rm_alarm_rule
        (
        alarm_policy_id,
        rule_name,
        metric_key,
        operator,
        threshold,
        resource_type,
        status,
        port_whitelist,
        create_time,
        update_time,
        create_by,
        update_by
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.alarmPolicyId},
            #{item.ruleName},
            #{item.metricKey},
            #{item.operator},
            #{item.threshold},
            #{item.resourceType},
            #{item.status},
            #{item.portWhitelist},
            #{item.createTime},
            #{item.updateTime},
            #{item.createBy},
            #{item.updateBy}
            )
        </foreach>
    </insert>
</mapper>