<?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.hw.ems.record.mapper.RecordAlarmRuleMapper">

    <resultMap type="RecordAlarmRule" id="RecordAlarmRuleResult">
        <result property="objId"    column="objId"    />
        <result property="ruleId"    column="rule_id"    />
        <result property="ruleName"    column="rule_name"    />
        <result property="monitorId"    column="monitor_id"    />
        <result property="collectTime"    column="collect_time"    />
        <result property="energyType"    column="energy_type"    />
        <result property="triggerRule"    column="trigger_rule"    />
        <result property="triggerNumber"    column="trigger_number"    />
        <result property="timeRange"    column="time_range"    />
        <result property="deviceOfflineTime"    column="device_offline_time"    />
        <result property="notifyUser"    column="notify_user"    />
        <result property="cause"    column="cause"    />
        <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="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
    </resultMap>

    <sql id="selectRecordAlarmRuleVo">
        select objId, rule_id, rule_name, monitor_id, collect_time, energy_type, trigger_rule, trigger_number, time_range, device_offline_time, notify_user, cause, create_by, create_time, update_by, update_time, dept_id, user_id from record_alarm_rule
    </sql>

    <select id="selectRecordAlarmRuleList" parameterType="RecordAlarmRule" resultMap="RecordAlarmRuleResult">
        <include refid="selectRecordAlarmRuleVo"/>
        <where>
            <if test="ruleId != null  and ruleId != ''"> and rule_id = #{ruleId}</if>
            <if test="ruleName != null  and ruleName != ''"> and rule_name like concat('%', #{ruleName}, '%')</if>
            <if test="monitorId != null  and monitorId != ''"> and monitor_id = #{monitorId}</if>
            <if test="collectTime != null "> and collect_time = #{collectTime}</if>
            <if test="energyType != null "> and energy_type = #{energyType}</if>
            <if test="triggerRule != null "> and trigger_rule = #{triggerRule}</if>
            <if test="triggerNumber != null "> and trigger_number = #{triggerNumber}</if>
            <if test="timeRange != null "> and time_range = #{timeRange}</if>
            <if test="deviceOfflineTime != null "> and device_offline_time = #{deviceOfflineTime}</if>
            <if test="notifyUser != null  and notifyUser != ''"> and notify_user = #{notifyUser}</if>
            <if test="cause != null  and cause != ''"> and cause = #{cause}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
        </where>
    </select>

    <select id="selectRecordAlarmRuleByObjId" parameterType="Long" resultMap="RecordAlarmRuleResult">
        <include refid="selectRecordAlarmRuleVo"/>
        where objId = #{objId}
    </select>

    <insert id="insertRecordAlarmRule" parameterType="RecordAlarmRule" useGeneratedKeys="true" keyProperty="objId">
        insert into record_alarm_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="ruleId != null">rule_id,</if>
            <if test="ruleName != null">rule_name,</if>
            <if test="monitorId != null">monitor_id,</if>
            <if test="collectTime != null">collect_time,</if>
            <if test="energyType != null">energy_type,</if>
            <if test="triggerRule != null">trigger_rule,</if>
            <if test="triggerNumber != null">trigger_number,</if>
            <if test="timeRange != null">time_range,</if>
            <if test="deviceOfflineTime != null">device_offline_time,</if>
            <if test="notifyUser != null">notify_user,</if>
            <if test="cause != null">cause,</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="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="ruleId != null">#{ruleId},</if>
            <if test="ruleName != null">#{ruleName},</if>
            <if test="monitorId != null">#{monitorId},</if>
            <if test="collectTime != null">#{collectTime},</if>
            <if test="energyType != null">#{energyType},</if>
            <if test="triggerRule != null">#{triggerRule},</if>
            <if test="triggerNumber != null">#{triggerNumber},</if>
            <if test="timeRange != null">#{timeRange},</if>
            <if test="deviceOfflineTime != null">#{deviceOfflineTime},</if>
            <if test="notifyUser != null">#{notifyUser},</if>
            <if test="cause != null">#{cause},</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="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
         </trim>
    </insert>

    <update id="updateRecordAlarmRule" parameterType="RecordAlarmRule">
        update record_alarm_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="ruleId != null">rule_id = #{ruleId},</if>
            <if test="ruleName != null">rule_name = #{ruleName},</if>
            <if test="monitorId != null">monitor_id = #{monitorId},</if>
            <if test="collectTime != null">collect_time = #{collectTime},</if>
            <if test="energyType != null">energy_type = #{energyType},</if>
            <if test="triggerRule != null">trigger_rule = #{triggerRule},</if>
            <if test="triggerNumber != null">trigger_number = #{triggerNumber},</if>
            <if test="timeRange != null">time_range = #{timeRange},</if>
            <if test="deviceOfflineTime != null">device_offline_time = #{deviceOfflineTime},</if>
            <if test="notifyUser != null">notify_user = #{notifyUser},</if>
            <if test="cause != null">cause = #{cause},</if>
            <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="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
        </trim>
        where objId = #{objId}
    </update>

    <delete id="deleteRecordAlarmRuleByObjId" parameterType="Long">
        delete from record_alarm_rule where objId = #{objId}
    </delete>

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