<?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.AdmOpAlarmPolicyMapper">

    <resultMap type="com.ruoyi.ems.domain.AdmOpAlarmPolicy" id="AdmOpAlarmPolicyResult">
        <result property="id"    column="id"    />
        <result property="areaCode"             column="area_code"    />
        <result property="areaName"             column="area_name"    />
        <result property="policyCode"           column="policy_code"    />
        <result property="policyName"           column="policy_name"    />
        <result property="alarmObjType"         column="alarm_obj_type"    />
        <result property="alarmObjIndex"        column="alarm_obj_index"    />
        <result property="alarmRuleType"        column="alarm_rule_type"    />
        <result property="alarmThresholdValue"  column="alarm_threshold_value"    />
        <result property="alarmCode"            column="alarm_code"    />
        <result property="alarmMsg"             column="alarm_msg"    />
        <result property="alarmType"            column="alarm_type"    />
    </resultMap>

    <sql id="selectAdmOpAlarmPolicyVo">
        select id, policy_code, policy_name, alarm_obj_type, alarm_obj_index, alarm_rule_type, alarm_threshold_value, alarm_code, alarm_msg, alarm_type from adm_op_alarm_policy
    </sql>

    <select id="selectAdmOpAlarmPolicyList" parameterType="com.ruoyi.ems.domain.AdmOpAlarmPolicy" resultMap="AdmOpAlarmPolicyResult">
        select
            p.id, p.area_code, a.area_name, p.policy_code, p.policy_name, p.alarm_obj_type, p.alarm_obj_index, p.alarm_rule_type, p.alarm_threshold_value, p.alarm_code, p.alarm_msg, p.alarm_type
        from adm_op_alarm_policy p
            left join adm_area a on p.area_code = a.area_code
        <where>
            <if test="areaCode != null and areaCode != '' and areaCode != '-1'">and p.area_code= #{areaCode}</if>
            <if test="alarmObjType != null "> and p.alarm_obj_type = #{alarmObjType}</if>
            <if test="alarmCode != null  and alarmCode != ''"> and p.alarm_code = #{alarmCode}</if>
            <if test="alarmType != null "> and p.alarm_type = #{alarmType}</if>
            <if test="policyName != null and policyName != ''"> and p.policy_name like concat('%', #{policyName}, '%')</if>
        </where>
    </select>

    <select id="selectAdmOpAlarmPolicyById" parameterType="Long" resultMap="AdmOpAlarmPolicyResult">
        <include refid="selectAdmOpAlarmPolicyVo"/>
        where id = #{id}
    </select>

    <insert id="insertAdmOpAlarmPolicy" parameterType="com.ruoyi.ems.domain.AdmOpAlarmPolicy" useGeneratedKeys="true" keyProperty="id">
        insert into adm_op_alarm_policy
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code,</if>
            <if test="policyCode != null and policyCode != ''">policy_code,</if>
            <if test="policyName != null and policyName != ''">policy_name,</if>
            <if test="alarmObjType != null">alarm_obj_type,</if>
            <if test="alarmObjIndex != null and alarmObjIndex != ''">alarm_obj_index,</if>
            <if test="alarmRuleType != null">alarm_rule_type,</if>
            <if test="alarmThresholdValue != null">alarm_threshold_value,</if>
            <if test="alarmCode != null">alarm_code,</if>
            <if test="alarmMsg != null">alarm_msg,</if>
            <if test="alarmType != null">alarm_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">#{areaCode},</if>
            <if test="policyCode != null and policyCode != ''">#{policyCode},</if>
            <if test="policyName != null and policyName != ''">#{policyName},</if>
            <if test="alarmObjType != null">#{alarmObjType},</if>
            <if test="alarmObjIndex != null and alarmObjIndex != ''">#{alarmObjIndex},</if>
            <if test="alarmRuleType != null">#{alarmRuleType},</if>
            <if test="alarmThresholdValue != null">#{alarmThresholdValue},</if>
            <if test="alarmCode != null">#{alarmCode},</if>
            <if test="alarmMsg != null">#{alarmMsg},</if>
            <if test="alarmType != null">#{alarmType},</if>
         </trim>
    </insert>

    <update id="updateAdmOpAlarmPolicy" parameterType="com.ruoyi.ems.domain.AdmOpAlarmPolicy">
        update adm_op_alarm_policy
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaCode != null and areaCode != ''">area_code = #{areaCode},</if>
            <if test="policyCode != null and policyCode != ''">policy_code = #{policyCode},</if>
            <if test="policyName != null and policyName != ''">policy_name = #{policyName},</if>
            <if test="alarmObjType != null">alarm_obj_type = #{alarmObjType},</if>
            <if test="alarmObjIndex != null and alarmObjIndex != ''">alarm_obj_index = #{alarmObjIndex},</if>
            <if test="alarmRuleType != null">alarm_rule_type = #{alarmRuleType},</if>
            <if test="alarmThresholdValue != null">alarm_threshold_value = #{alarmThresholdValue},</if>
            <if test="alarmCode != null">alarm_code = #{alarmCode},</if>
            <if test="alarmMsg != null">alarm_msg = #{alarmMsg},</if>
            <if test="alarmType != null">alarm_type = #{alarmType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAdmOpAlarmPolicyById" parameterType="Long">
        delete from adm_op_alarm_policy where id = #{id}
    </delete>

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