<?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.dcs.hnyz.mapper.AlarmConfigMapper">
    
    <resultMap type="AlarmConfig" id="AlarmConfigResult">
        <result property="configId"    column="config_id"    />
        <result property="equipmentId"    column="equipment_id"    />
        <result property="alarmCode"    column="alarm_code"    />
        <result property="alarmName"    column="alarm_name"    />
        <result property="alarmLevel"    column="alarm_level"    />
        <result property="alarmType"    column="alarm_type"    />
        <result property="source"    column="source"    />
        <result property="thresholdType"    column="threshold_type"    />
        <result property="thresholdValue"    column="threshold_value"    />
        <result property="unit"    column="unit"    />
        <result property="delaySeconds"    column="delay_seconds"    />
        <result property="description"    column="description"    />
        <result property="notificationMethod"    column="notification_method"    />
        <result property="status"    column="status"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectAlarmConfigVo">
        select config_id, equipment_id, alarm_code, alarm_name, alarm_level, alarm_type, source, threshold_type, threshold_value, unit, delay_seconds, description, notification_method, status, create_time, create_by, update_time, update_by, remark from alarm_config
    </sql>

    <select id="selectAlarmConfigList" parameterType="AlarmConfig" resultMap="AlarmConfigResult">
        <include refid="selectAlarmConfigVo"/>
        <where>  
            <if test="equipmentId != null "> and equipment_id = #{equipmentId}</if>
            <if test="alarmCode != null  and alarmCode != ''"> and alarm_code = #{alarmCode}</if>
            <if test="alarmName != null  and alarmName != ''"> and alarm_name like concat('%', #{alarmName}, '%')</if>
            <if test="alarmLevel != null  and alarmLevel != ''"> and alarm_level = #{alarmLevel}</if>
            <if test="alarmType != null  and alarmType != ''"> and alarm_type = #{alarmType}</if>
            <if test="source != null  and source != ''"> and source = #{source}</if>
            <if test="thresholdType != null  and thresholdType != ''"> and threshold_type = #{thresholdType}</if>
            <if test="thresholdValue != null  and thresholdValue != ''"> and threshold_value = #{thresholdValue}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="delaySeconds != null "> and delay_seconds = #{delaySeconds}</if>
            <if test="description != null  and description != ''"> and description = #{description}</if>
            <if test="notificationMethod != null  and notificationMethod != ''"> and notification_method = #{notificationMethod}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectAlarmConfigByConfigId" parameterType="Long" resultMap="AlarmConfigResult">
        <include refid="selectAlarmConfigVo"/>
        where config_id = #{configId}
    </select>

    <insert id="insertAlarmConfig" parameterType="AlarmConfig" useGeneratedKeys="true" keyProperty="configId">
        insert into alarm_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="equipmentId != null">equipment_id,</if>
            <if test="alarmCode != null and alarmCode != ''">alarm_code,</if>
            <if test="alarmName != null and alarmName != ''">alarm_name,</if>
            <if test="alarmLevel != null and alarmLevel != ''">alarm_level,</if>
            <if test="alarmType != null and alarmType != ''">alarm_type,</if>
            <if test="source != null and source != ''">source,</if>
            <if test="thresholdType != null and thresholdType != ''">threshold_type,</if>
            <if test="thresholdValue != null">threshold_value,</if>
            <if test="unit != null">unit,</if>
            <if test="delaySeconds != null">delay_seconds,</if>
            <if test="description != null and description != ''">description,</if>
            <if test="notificationMethod != null and notificationMethod != ''">notification_method,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="equipmentId != null">#{equipmentId},</if>
            <if test="alarmCode != null and alarmCode != ''">#{alarmCode},</if>
            <if test="alarmName != null and alarmName != ''">#{alarmName},</if>
            <if test="alarmLevel != null and alarmLevel != ''">#{alarmLevel},</if>
            <if test="alarmType != null and alarmType != ''">#{alarmType},</if>
            <if test="source != null and source != ''">#{source},</if>
            <if test="thresholdType != null and thresholdType != ''">#{thresholdType},</if>
            <if test="thresholdValue != null">#{thresholdValue},</if>
            <if test="unit != null">#{unit},</if>
            <if test="delaySeconds != null">#{delaySeconds},</if>
            <if test="description != null and description != ''">#{description},</if>
            <if test="notificationMethod != null and notificationMethod != ''">#{notificationMethod},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateAlarmConfig" parameterType="AlarmConfig">
        update alarm_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="equipmentId != null">equipment_id = #{equipmentId},</if>
            <if test="alarmCode != null and alarmCode != ''">alarm_code = #{alarmCode},</if>
            <if test="alarmName != null and alarmName != ''">alarm_name = #{alarmName},</if>
            <if test="alarmLevel != null and alarmLevel != ''">alarm_level = #{alarmLevel},</if>
            <if test="alarmType != null and alarmType != ''">alarm_type = #{alarmType},</if>
            <if test="source != null and source != ''">source = #{source},</if>
            <if test="thresholdType != null and thresholdType != ''">threshold_type = #{thresholdType},</if>
            <if test="thresholdValue != null">threshold_value = #{thresholdValue},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="delaySeconds != null">delay_seconds = #{delaySeconds},</if>
            <if test="description != null and description != ''">description = #{description},</if>
            <if test="notificationMethod != null and notificationMethod != ''">notification_method = #{notificationMethod},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where config_id = #{configId}
    </update>

    <delete id="deleteAlarmConfigByConfigId" parameterType="Long">
        delete from alarm_config where config_id = #{configId}
    </delete>

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