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

    <resultMap type="DmsRecordAlarmInfo" id="DmsRecordAlarmInfoResult">
        <result property="alarmId" column="alarm_id"/>
        <result property="deviceId" column="device_id"/>
        <result property="alarmRuleId" column="alarm_rule_id"/>
        <result property="alarmBeginTime" column="alarm_begin_time"/>
        <result property="alarmEndTime" column="alarm_end_time"/>
        <result property="continueTime" column="continue_time"/>
        <result property="alarmReason" column="alarm_reason"/>
        <result property="alarmData" column="alarm_data"/>
        <result property="alarmStatus" column="alarm_status"/>
        <result property="handleSuggest" column="handle_suggest"/>
        <result property="noticeStatus" column="notice_status"/>
        <result property="handleUser" column="handle_user"/>
        <result property="handleTime" column="handle_time"/>
        <result property="causeAnalysis" column="cause_analysis"/>
        <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"/>
        <!-- 以下字段来自不存在的dms_base_alarm_rule表，已注释 -->
        <!-- <result property="alarmLevel" column="alarm_level"/> -->
        <!-- <result property="noticeType" column="notice_type"/> -->
        <!-- <result property="recoverType" column="recover_type"/> -->
        <result property="deviceCode" column="device_code"/>
        <result property="deviceName" column="device_name"/>
    </resultMap>

    <sql id="selectDmsRecordAlarmInfoVo">
        select drai.alarm_id,
               drai.device_id,
               dbdl.device_code,
               dbdl.device_name,
               drai.alarm_rule_id,
               drai.alarm_begin_time,
               drai.alarm_end_time,
               drai.continue_time,
               drai.alarm_reason,
               drai.alarm_data,
               drai.alarm_status,
               drai.handle_suggest,
               drai.handle_user,
               drai.handle_time,
               drai.cause_analysis,
               drai.remark
        from dms_record_alarm_info drai
                 left join base_deviceledger dbdl on drai.device_id = dbdl.OBJ_ID
    </sql>

    <select id="selectDmsRecordAlarmInfoList" parameterType="DmsRecordAlarmInfo" resultMap="DmsRecordAlarmInfoResult">
        <include refid="selectDmsRecordAlarmInfoVo"/>
        <where>
            <if test="alarmId != null ">and drai.alarm_id = #{alarmId}</if>
            <if test="deviceId != null ">and drai.device_id = #{deviceId}</if>
            <if test="alarmRuleId != null ">and drai.alarm_rule_id = #{alarmRuleId}</if>
            <if test="params != null and params.beginAlarmBeginTime != null and params.beginAlarmBeginTime != '' and params.endAlarmBeginTime != null and params.endAlarmBeginTime != ''">
                and drai.alarm_begin_time between #{params.beginAlarmBeginTime} and #{params.endAlarmBeginTime}
            </if>
            <if test="params != null and params.beginAlarmEndTime != null and params.beginAlarmEndTime != '' and params.endAlarmEndTime != null and params.endAlarmEndTime != ''">
                and drai.alarm_end_time between #{params.beginAlarmEndTime} and #{params.endAlarmEndTime}
            </if>
            <if test="continueTime != null ">and drai.continue_time = #{continueTime}</if>
            <if test="alarmReason != null  and alarmReason != ''">and drai.alarm_reason = #{alarmReason}</if>
            <if test="alarmData != null  and alarmData != ''">and drai.alarm_data = #{alarmData}</if>
            <if test="alarmStatus != null  and alarmStatus != ''">and drai.alarm_status = #{alarmStatus}</if>
            <if test="handleSuggest != null  and handleSuggest != ''">and drai.handle_suggest = #{handleSuggest}</if>
            <if test="noticeStatus != null  and noticeStatus != ''">and drai.notice_status = #{noticeStatus}</if>
            <if test="handleUser != null  and handleUser != ''">and drai.handle_user = #{handleUser}</if>
            <if test="params != null and params.beginHandleTime != null and params.beginHandleTime != '' and params.endHandleTime != null and params.endHandleTime != ''">
                and drai.handle_time between #{params.beginHandleTime} and #{params.endHandleTime}
            </if>
            <if test="causeAnalysis != null  and causeAnalysis != ''">and drai.cause_analysis = #{causeAnalysis}</if>
        </where>
        order by drai.alarm_begin_time desc
    </select>

    <select id="selectDmsRecordAlarmInfoByAlarmId" parameterType="Long" resultMap="DmsRecordAlarmInfoResult">
        <include refid="selectDmsRecordAlarmInfoVo"/>
        where alarm_id = #{alarmId}
    </select>

    <insert id="insertDmsRecordAlarmInfo" parameterType="DmsRecordAlarmInfo" useGeneratedKeys="true"
            keyProperty="alarmId">
        insert into dms_record_alarm_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="alarmRuleId != null">alarm_rule_id,</if>
            <if test="alarmBeginTime != null">alarm_begin_time,</if>
            <if test="alarmEndTime != null">alarm_end_time,</if>
            <if test="continueTime != null">continue_time,</if>
            <if test="alarmReason != null">alarm_reason,</if>
            <if test="alarmData != null">alarm_data,</if>
            <if test="alarmStatus != null and alarmStatus != ''">alarm_status,</if>
            <if test="handleSuggest != null">handle_suggest,</if>
            <if test="noticeStatus != null and noticeStatus != ''">notice_status,</if>
            <if test="handleUser != null">handle_user,</if>
            <if test="handleTime != null">handle_time,</if>
            <if test="causeAnalysis != null">cause_analysis,</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="alarmRuleId != null">#{alarmRuleId},</if>
            <if test="alarmBeginTime != null">#{alarmBeginTime},</if>
            <if test="alarmEndTime != null">#{alarmEndTime},</if>
            <if test="continueTime != null">#{continueTime},</if>
            <if test="alarmReason != null">#{alarmReason},</if>
            <if test="alarmData != null">#{alarmData},</if>
            <if test="alarmStatus != null and alarmStatus != ''">#{alarmStatus},</if>
            <if test="handleSuggest != null">#{handleSuggest},</if>
            <if test="noticeStatus != null and noticeStatus != ''">#{noticeStatus},</if>
            <if test="handleUser != null">#{handleUser},</if>
            <if test="handleTime != null">#{handleTime},</if>
            <if test="causeAnalysis != null">#{causeAnalysis},</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="updateDmsRecordAlarmInfo" parameterType="DmsRecordAlarmInfo">
        update dms_record_alarm_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="alarmRuleId != null">alarm_rule_id = #{alarmRuleId},</if>
            <if test="alarmBeginTime != null">alarm_begin_time = #{alarmBeginTime},</if>
            <if test="alarmEndTime != null">alarm_end_time = #{alarmEndTime},</if>
            <if test="continueTime != null">continue_time = #{continueTime},</if>
            <if test="alarmReason != null">alarm_reason = #{alarmReason},</if>
            <if test="alarmData != null">alarm_data = #{alarmData},</if>
            <if test="alarmStatus != null and alarmStatus != ''">alarm_status = #{alarmStatus},</if>
            <if test="handleSuggest != null">handle_suggest = #{handleSuggest},</if>
            <if test="noticeStatus != null and noticeStatus != ''">notice_status = #{noticeStatus},</if>
            <if test="handleUser != null">handle_user = #{handleUser},</if>
            <if test="handleTime != null">handle_time = #{handleTime},</if>
            <if test="causeAnalysis != null">cause_analysis = #{causeAnalysis},</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_id = #{alarmId}
    </update>

    <delete id="deleteDmsRecordAlarmInfoByAlarmId" parameterType="Long">
        delete
        from dms_record_alarm_info
        where alarm_id = #{alarmId}
    </delete>

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