<?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.mapper.AlarmRuleMapper">

    <resultMap type="com.ruoyi.ems.domain.AlarmRule" id="AlarmRuleResult">
        <id property="id" column="id"/>
        <result property="ruleCode" column="rule_code"/>
        <result property="ruleName" column="rule_name"/>
        <result property="areaCode" column="area_code"/>
        <result property="areaName" column="area_name"/>
        <result property="targetType" column="target_type"/>
        <result property="targetScope" column="target_scope"/>
        <result property="deviceModel" column="device_model"/>
        <result property="deviceModelName" column="device_model_name"/>
        <result property="attrKey" column="attr_key"/>
        <result property="attrName" column="attr_name"/>
        <result property="checkType" column="check_type"/>
        <result property="operator" column="operator"/>
        <result property="thresholdValue" column="threshold_value"/>
        <result property="thresholdMin" column="threshold_min"/>
        <result property="thresholdMax" column="threshold_max"/>
        <result property="alarmLevel" column="alarm_level"/>
        <result property="alarmCode" column="alarm_code"/>
        <result property="alarmMsgTemplate" column="alarm_msg_template"/>
        <result property="recoveryCheck" column="recovery_check"/>
        <result property="recoveryThreshold" column="recovery_threshold"/>
        <result property="triggerCount" column="trigger_count"/>
        <result property="triggerInterval" column="trigger_interval"/>
        <result property="coolDown" column="cool_down"/>
        <result property="enabled" column="enabled"/>
        <result property="useForInspection" column="use_for_inspection"/>
        <result property="useForRealtime" column="use_for_realtime"/>
        <result property="subsystemCode" column="subsystem_code"/>
        <result property="subsystemName" column="subsystem_name"/>
        <result property="ruleOrder" column="rule_order"/>
        <result property="description" column="description"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectVo">
        SELECT r.id, r.rule_code, r.rule_name, r.area_code, a.area_name,
               r.target_type, r.target_scope, r.device_model, m.model_name AS device_model_name,
               r.attr_key, r.attr_name, r.check_type, r.operator,
               r.threshold_value, r.threshold_min, r.threshold_max,
               r.alarm_level, r.alarm_code, r.alarm_msg_template,
               r.recovery_check, r.recovery_threshold, r.trigger_count, r.trigger_interval, r.cool_down,
               r.enabled, r.use_for_inspection, r.use_for_realtime,
               r.subsystem_code, s.system_name AS subsystem_name,
               r.rule_order, r.description, r.create_by, r.create_time, r.update_by, r.update_time
        FROM adm_op_alarm_rule r
                 LEFT JOIN adm_area a ON r.area_code = a.area_code
                 LEFT JOIN adm_ems_obj_model m ON r.device_model = m.model_code
                 LEFT JOIN adm_ems_subsystem s ON r.subsystem_code = s.system_code
    </sql>

    <select id="selectById" parameterType="Long" resultMap="AlarmRuleResult">
        <include refid="selectVo"/>
        WHERE r.id = #{id}
    </select>

    <select id="selectByRuleCode" parameterType="String" resultMap="AlarmRuleResult">
        <include refid="selectVo"/>
        WHERE r.rule_code = #{ruleCode}
    </select>

    <select id="selectList" parameterType="com.ruoyi.ems.domain.AlarmRule" resultMap="AlarmRuleResult">
        <include refid="selectVo"/>
        <where>
            <if test="ruleCode != null and ruleCode != ''">AND r.rule_code = #{ruleCode}</if>
            <if test="ruleName != null and ruleName != ''">AND r.rule_name LIKE CONCAT('%', #{ruleName}, '%')</if>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">AND r.area_code = #{areaCode}</if>
            <if test="targetType != null">AND r.target_type = #{targetType}</if>
            <if test="deviceModel != null and deviceModel != ''">AND r.device_model = #{deviceModel}</if>
            <if test="attrKey != null and attrKey != ''">AND r.attr_key = #{attrKey}</if>
            <if test="checkType != null">AND r.check_type = #{checkType}</if>
            <if test="alarmLevel != null">AND r.alarm_level = #{alarmLevel}</if>
            <if test="alarmCode != null and alarmCode != ''">AND r.alarm_code = #{alarmCode}</if>
            <if test="enabled != null">AND r.enabled = #{enabled}</if>
            <if test="useForInspection != null">AND r.use_for_inspection = #{useForInspection}</if>
            <if test="useForRealtime != null">AND r.use_for_realtime = #{useForRealtime}</if>
            <if test="subsystemCode != null and subsystemCode != ''">AND r.subsystem_code = #{subsystemCode}</if>
        </where>
        ORDER BY r.rule_order, r.create_time DESC
    </select>

    <!-- 查询适用于实时监测的规则 -->
    <select id="selectForRealtime" parameterType="com.ruoyi.ems.domain.AlarmRule" resultMap="AlarmRuleResult">
        <include refid="selectVo"/>
        WHERE r.enabled = 1 AND r.use_for_realtime = 1
        <if test="deviceModel != null and deviceModel != ''">
            AND (r.device_model IS NULL OR r.device_model = '' OR r.device_model = #{deviceModel})
        </if>
        <if test="attrKey != null and attrKey != ''">
            AND r.attr_key = #{attrKey}
        </if>
        ORDER BY r.rule_order
    </select>

    <!-- 查询适用于巡检的规则 -->
    <select id="selectForInspection" parameterType="com.ruoyi.ems.domain.AlarmRule" resultMap="AlarmRuleResult">
        <include refid="selectVo"/>
        WHERE r.enabled = 1 AND r.use_for_inspection = 1
        <if test="deviceModel != null and deviceModel != ''">
            AND (r.device_model IS NULL OR r.device_model = '' OR r.device_model = #{deviceModel})
        </if>
        ORDER BY r.rule_order
    </select>

    <!-- 根据分组查询 -->
    <select id="selectByGroupCode" parameterType="String" resultMap="AlarmRuleResult">
        <include refid="selectVo"/>
        WHERE r.rule_code IN (
        SELECT rel.rule_code FROM adm_op_alarm_rule_group_rel rel WHERE rel.group_code = #{groupCode}
        )
        ORDER BY r.rule_order
    </select>

    <insert id="insert" parameterType="com.ruoyi.ems.domain.AlarmRule" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO adm_op_alarm_rule (
            rule_code, rule_name, area_code, target_type, target_scope, device_model,
            attr_key, attr_name, check_type, operator, threshold_value, threshold_min, threshold_max,
            alarm_level, alarm_code, alarm_msg_template, recovery_check, recovery_threshold,
            trigger_count, trigger_interval, cool_down, enabled, use_for_inspection, use_for_realtime,
            subsystem_code, rule_order, description, create_by, create_time
        ) VALUES (
                     #{ruleCode}, #{ruleName}, #{areaCode}, #{targetType}, #{targetScope}, #{deviceModel},
                     #{attrKey}, #{attrName}, #{checkType}, #{operator}, #{thresholdValue}, #{thresholdMin}, #{thresholdMax},
                     #{alarmLevel}, #{alarmCode}, #{alarmMsgTemplate}, #{recoveryCheck}, #{recoveryThreshold},
                     #{triggerCount}, #{triggerInterval}, #{coolDown}, #{enabled}, #{useForInspection}, #{useForRealtime},
                     #{subsystemCode}, #{ruleOrder}, #{description}, #{createBy}, NOW()
                 )
    </insert>

    <update id="update" parameterType="com.ruoyi.ems.domain.AlarmRule">
        UPDATE adm_op_alarm_rule
        <set>
            <if test="ruleName != null and ruleName != ''">rule_name = #{ruleName},</if>
            <if test="areaCode != null">area_code = #{areaCode},</if>
            <if test="targetType != null">target_type = #{targetType},</if>
            <if test="targetScope != null">target_scope = #{targetScope},</if>
            <if test="deviceModel != null">device_model = #{deviceModel},</if>
            <if test="attrKey != null">attr_key = #{attrKey},</if>
            <if test="attrName != null">attr_name = #{attrName},</if>
            <if test="checkType != null">check_type = #{checkType},</if>
            <if test="operator != null">operator = #{operator},</if>
            <if test="thresholdValue != null">threshold_value = #{thresholdValue},</if>
            <if test="thresholdMin != null">threshold_min = #{thresholdMin},</if>
            <if test="thresholdMax != null">threshold_max = #{thresholdMax},</if>
            <if test="alarmLevel != null">alarm_level = #{alarmLevel},</if>
            <if test="alarmCode != null">alarm_code = #{alarmCode},</if>
            <if test="alarmMsgTemplate != null">alarm_msg_template = #{alarmMsgTemplate},</if>
            <if test="recoveryCheck != null">recovery_check = #{recoveryCheck},</if>
            <if test="recoveryThreshold != null">recovery_threshold = #{recoveryThreshold},</if>
            <if test="triggerCount != null">trigger_count = #{triggerCount},</if>
            <if test="triggerInterval != null">trigger_interval = #{triggerInterval},</if>
            <if test="coolDown != null">cool_down = #{coolDown},</if>
            <if test="enabled != null">enabled = #{enabled},</if>
            <if test="useForInspection != null">use_for_inspection = #{useForInspection},</if>
            <if test="useForRealtime != null">use_for_realtime = #{useForRealtime},</if>
            <if test="subsystemCode != null">subsystem_code = #{subsystemCode},</if>
            <if test="ruleOrder != null">rule_order = #{ruleOrder},</if>
            <if test="description != null">description = #{description},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = NOW()
        </set>
        WHERE id = #{id}
    </update>

    <update id="updateEnabled">
        UPDATE adm_op_alarm_rule SET enabled = #{enabled}, update_time = NOW()
        WHERE rule_code = #{ruleCode}
    </update>

    <update id="batchUpdateEnabled">
        UPDATE adm_op_alarm_rule SET enabled = #{enabled}, update_time = NOW()
        WHERE rule_code IN
        <foreach item="ruleCode" collection="ruleCodes" open="(" separator="," close=")">
            #{ruleCode}
        </foreach>
    </update>

    <delete id="deleteByIds" parameterType="Long">
        DELETE FROM adm_op_alarm_rule WHERE id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>