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

    <resultMap type="EmsRecordAlarmRule" id="EmsRecordAlarmRuleResult">
        <result property="objId"    column="obj_id"    />
        <result property="ruleId"    column="rule_id"    />
        <result property="ruleName"    column="rule_name"    />
        <result property="monitorId"    column="monitor_id"    />
        <result property="monitorName"    column="monitor_name"    />
        <result property="monitorCode"    column="monitor_code"    />
        <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="nickName"    column="nick_name"    />
    </resultMap>

    <sql id="selectEmsRecordAlarmRuleVo">
        SELECT
	        RAL.obj_id,
	        RAL.rule_id,
	        RAL.rule_name,
	        RAL.monitor_id,
	        RAL.collect_time,
	        RAL.energy_type,
	        RAL.trigger_rule,
	        RAL.trigger_number,
	        RAL.time_range,
	        RAL.device_offline_time,
	        RAL.notify_user,
	        RAL.cause,
	        RAL.create_by,
	        RAL.create_time,
	        RAL.update_by,
	        RAL.update_time,
	        BMI.monitor_code,
	        BMI.monitor_name,
	        SUSER.nick_name
    FROM
	ems_record_alarm_rule as RAL
	left join ems_base_monitor_info as BMI on RAL.monitor_id = BMI.obj_id
	left join sys_user as SUSER on RAL.notify_user = SUSER.user_id
	</sql>

    <select id="selectEmsRecordAlarmRuleList" parameterType="EmsRecordAlarmRule" resultMap="EmsRecordAlarmRuleResult">
        <include refid="selectEmsRecordAlarmRuleVo"/>
        <where>
            <if test="ruleId != null  and ruleId != ''"> and RAL.rule_id = #{ruleId}</if>
            <if test="ruleName != null  and ruleName != ''"> and RAL.rule_name like concat('%', #{ruleName}, '%')</if>
            <if test="monitorId != null  and monitorId != ''"> and RAL.monitor_id = #{monitorId}</if>
            <if test="collectTime != null "> and RAL.collect_time = #{collectTime}</if>
            <if test="energyType != null "> and RAL.energy_type = #{energyType}</if>
            <if test="triggerRule != null "> and RAL.trigger_rule = #{triggerRule}</if>
            <if test="triggerNumber != null "> and RAL.trigger_number = #{triggerNumber}</if>
            <if test="timeRange != null "> and RAL.time_range = #{timeRange}</if>
            <if test="deviceOfflineTime != null "> and RAL.device_offline_time = #{deviceOfflineTime}</if>
            <if test="notifyUser != null  and notifyUser != ''"> and RAL.notify_user = #{notifyUser}</if>
            <if test="cause != null  and cause != ''"> and RAL.cause = #{cause}</if>
        </where>
    </select>

    <select id="selectEmsRecordAlarmRuleByObjId" parameterType="Long" resultMap="EmsRecordAlarmRuleResult">
        <include refid="selectEmsRecordAlarmRuleVo"/>
        where RAL.obj_id = #{objId}
    </select>

    <insert id="insertEmsRecordAlarmRule" parameterType="EmsRecordAlarmRule" useGeneratedKeys="true" keyProperty="objId">
        insert into ems_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>
         </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>
         </trim>
    </insert>

    <update id="updateEmsRecordAlarmRule" parameterType="EmsRecordAlarmRule">
        update ems_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>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteEmsRecordAlarmRuleByObjId" parameterType="Long">
        delete from ems_record_alarm_rule where obj_id = #{objId}
    </delete>

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