<?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.aucma.dms.mapper.DmsBaseAlarmRuleMapper">

    <resultMap type="DmsBaseAlarmRule" id="DmsBaseAlarmRuleResult">
        <result property="alarmRuleId" column="alarm_rule_id"/>
        <result property="deviceId" column="device_id"/>
        <result property="alarmType" column="alarm_type"/>
        <result property="alarmLevel" column="alarm_level"/>
        <result property="noticeType" column="notice_type"/>
        <result property="recoverType" column="recover_type"/>
        <result property="continueTime" column="continue_time"/>
        <result property="alarmStatusWord" column="alarm_status_word"/>
        <result property="alarmReason" column="alarm_reason"/>
        <result property="handleSuggest" column="handle_suggest"/>
        <result property="remark" column="remark"/>
        <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="deviceCode" column="device_code"/>
        <result property="deviceName" column="device_name"/>
    </resultMap>

    <sql id="selectDmsBaseAlarmRuleVo">
        select dbar.alarm_rule_id,
               dbar.device_id,
               dbdl.device_code,
               dbdl.device_name,
               dbar.alarm_type,
               dbar.alarm_level,
               dbar.notice_type,
               dbar.recover_type,
               dbar.continue_time,
               dbar.alarm_status_word,
               dbar.alarm_reason,
               dbar.handle_suggest,
               dbar.remark,
               dbar.create_by,
               dbar.create_time,
               dbar.update_by,
               dbar.update_time
        from dms_base_alarm_rule dbar
                 left join base_deviceledger dbdl on dbar.device_id = dbdl.OBJ_ID
    </sql>

    <select id="selectDmsBaseAlarmRuleList" parameterType="DmsBaseAlarmRule" resultMap="DmsBaseAlarmRuleResult">
        <include refid="selectDmsBaseAlarmRuleVo"/>
        <where>
            <if test="deviceId != null ">and dbar.device_id = #{deviceId}</if>
            <if test="alarmType != null  and alarmType != ''">and dbar.alarm_type = #{alarmType}</if>
            <if test="alarmLevel != null  and alarmLevel != ''">and dbar.alarm_level = #{alarmLevel}</if>
            <if test="noticeType != null  and noticeType != ''">and dbar.notice_type like concat('%', #{noticeType}, '%')
            </if>
            <if test="recoverType != null  and recoverType != ''">and dbar.recover_type = #{recoverType}</if>
            <if test="continueTime != null ">and dbar.continue_time = #{continueTime}</if>
            <if test="alarmStatusWord != null  and alarmStatusWord != ''">and dbar.alarm_status_word = #{alarmStatusWord}
            </if>
            <if test="alarmReason != null  and alarmReason != ''">and dbar.alarm_reason like concat('%', #{alarmReason},
                '%')
            </if>
            <if test="handleSuggest != null  and handleSuggest != ''">and dbar.handle_suggest like concat('%',
                #{handleSuggest}, '%')
            </if>
        </where>
    </select>

    <select id="selectDmsBaseAlarmRuleByAlarmRuleId" parameterType="Long" resultMap="DmsBaseAlarmRuleResult">
        <include refid="selectDmsBaseAlarmRuleVo"/>
        where dbar.alarm_rule_id = #{alarmRuleId}
    </select>

    <insert id="insertDmsBaseAlarmRule" parameterType="DmsBaseAlarmRule" useGeneratedKeys="true"
            keyProperty="alarmRuleId">
        insert into dms_base_alarm_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="alarmType != null and alarmType != ''">alarm_type,</if>
            <if test="alarmLevel != null and alarmLevel != ''">alarm_level,</if>
            <if test="noticeType != null">notice_type,</if>
            <if test="recoverType != null">recover_type,</if>
            <if test="continueTime != null">continue_time,</if>
            <if test="alarmStatusWord != null">alarm_status_word,</if>
            <if test="alarmReason != null">alarm_reason,</if>
            <if test="handleSuggest != null">handle_suggest,</if>
            <if test="remark != null">remark,</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="deviceId != null">#{deviceId},</if>
            <if test="alarmType != null and alarmType != ''">#{alarmType},</if>
            <if test="alarmLevel != null and alarmLevel != ''">#{alarmLevel},</if>
            <if test="noticeType != null">#{noticeType},</if>
            <if test="recoverType != null">#{recoverType},</if>
            <if test="continueTime != null">#{continueTime},</if>
            <if test="alarmStatusWord != null">#{alarmStatusWord},</if>
            <if test="alarmReason != null">#{alarmReason},</if>
            <if test="handleSuggest != null">#{handleSuggest},</if>
            <if test="remark != null">#{remark},</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="updateDmsBaseAlarmRule" parameterType="DmsBaseAlarmRule">
        update dms_base_alarm_rule
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="alarmType != null and alarmType != ''">alarm_type = #{alarmType},</if>
            <if test="alarmLevel != null and alarmLevel != ''">alarm_level = #{alarmLevel},</if>
            <if test="noticeType != null">notice_type = #{noticeType},</if>
            <if test="recoverType != null">recover_type = #{recoverType},</if>
            <if test="continueTime != null">continue_time = #{continueTime},</if>
            <if test="alarmStatusWord != null">alarm_status_word = #{alarmStatusWord},</if>
            <if test="alarmReason != null">alarm_reason = #{alarmReason},</if>
            <if test="handleSuggest != null">handle_suggest = #{handleSuggest},</if>
            <if test="remark != null">remark = #{remark},</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 alarm_rule_id = #{alarmRuleId}
    </update>

    <delete id="deleteDmsBaseAlarmRuleByAlarmRuleId" parameterType="Long">
        delete
        from dms_base_alarm_rule
        where alarm_rule_id = #{alarmRuleId}
    </delete>

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