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

    <resultMap type="EmsRecordAlarmData" id="EmsRecordAlarmDataResult">
        <result property="objId"    column="obj_id"    />
        <result property="monitorId"    column="monitor_id"    />
        <result property="monitorName"    column="monitor_name"    />
        <result property="monitorCode"    column="monitor_code"    />
        <result property="collectDeviceId"    column="collect_device_id"    />
        <result property="collectTime"    column="collect_time"    />
        <result property="alarmType"    column="alarm_type"    />
        <result property="alarmStatus"    column="alarm_status"    />
        <result property="alarmData"    column="alarm_data"    />
        <result property="operationName"    column="operation_name"    />
        <result property="operationTime"    column="operation_time"    />
        <result property="cause"    column="cause"    />
        <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="notifyUser"    column="notify_user"    />
        <result property="collectDeviceName"    column="collect_device_name"    />
        <result property="deviceName"    column="device_name"    />
    </resultMap>

    <sql id="selectEmsRecordAlarmDataVo">
        SELECT rad.obj_id,
               rad.monitor_id,
               rad.collect_device_id,
               rad.collect_time,
               rad.alarm_type,
               rad.alarm_status,
               rad.alarm_data,
               rad.operation_name,
               rad.operation_time,
               rad.cause,
               rad.create_by,
               rad.create_time,
               rad.update_by,
               rad.update_time,
               rad.notify_user,
               bmi.monitor_name,
               ebcdi.collect_device_name,
               IIF(rad.monitor_id is null, ebcdi.collect_device_name, bmi.monitor_name) device_name
        FROM ems_record_alarm_data rad
                 LEFT JOIN ems_base_monitor_info bmi ON rad.monitor_id = bmi.monitor_code
                 LEFT JOIN ems_base_collect_device_info ebcdi on rad.collect_device_id = ebcdi.collect_device_id
    </sql>

    <select id="selectEmsRecordAlarmDataList" parameterType="EmsRecordAlarmData" resultMap="EmsRecordAlarmDataResult">
        <include refid="selectEmsRecordAlarmDataVo"/>
        <where>
            <if test="monitorId != null  and monitorId != ''"> and monitor_id = #{monitorId}</if>
            <if test="collectDeviceId != null  and collectDeviceId != ''"> and collect_device_id = #{collectDeviceId}</if>
            <if test="collectTime != null "> and collect_time = #{collectTime}</if>
            <if test="alarmType != null "> and alarm_type = #{alarmType}</if>
            <if test="alarmStatus != null "> and alarm_status = #{alarmStatus}</if>
            <if test="alarmData != null  and alarmData != ''"> and alarm_data = #{alarmData}</if>
            <if test="operationName != null  and operationName != ''"> and operation_name like concat('%', #{operationName}, '%')</if>
            <if test="beginOperationTime != null "> and operation_time >= #{beginOperationTime}</if>
            <if test="endOperationTime != null "> and   #{endOperationTime} >= operation_time</if>
            <if test="cause != null  and cause != ''"> and cause = #{cause}</if>
            <if test="notifyUser != null  and notifyUser != ''"> and notify_user = #{notifyUser}</if>
        </where>
    </select>

    <select id="selectEmsRecordAlarmDataByObjId" parameterType="Long" resultMap="EmsRecordAlarmDataResult">
        <include refid="selectEmsRecordAlarmDataVo"/>
        where RAD.obj_id = #{objId}
    </select>

    <insert id="insertEmsRecordAlarmData" parameterType="EmsRecordAlarmData" useGeneratedKeys="true" keyProperty="objId">
        insert into ems_record_alarm_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="monitorId != null">monitor_id,</if>
            <if test="collectDeviceId != null">collect_device_id,</if>
            <if test="collectTime != null">collect_time,</if>
            <if test="alarmType != null">alarm_type,</if>
            <if test="alarmStatus != null">alarm_status,</if>
            <if test="alarmData != null">alarm_data,</if>
            <if test="operationName != null">operation_name,</if>
            <if test="operationTime != null">operation_time,</if>
            <if test="cause != null">cause,</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>
            <if test="notifyUser != null">notify_user,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="monitorId != null">#{monitorId},</if>
            <if test="collectDeviceId != null">#{collectDeviceId},</if>
            <if test="collectTime != null">#{collectTime},</if>
            <if test="alarmType != null">#{alarmType},</if>
            <if test="alarmStatus != null">#{alarmStatus},</if>
            <if test="alarmData != null">#{alarmData},</if>
            <if test="operationName != null">#{operationName},</if>
            <if test="operationTime != null">#{operationTime},</if>
            <if test="cause != null">#{cause},</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>
            <if test="notifyUser != null">#{notifyUser},</if>
         </trim>
    </insert>

    <update id="updateEmsRecordAlarmData" parameterType="EmsRecordAlarmData">
        update ems_record_alarm_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="monitorId != null">monitor_id = #{monitorId},</if>
            <if test="collectDeviceId != null">collect_device_id = #{collectDeviceId},</if>
            <if test="collectTime != null">collect_time = #{collectTime},</if>
            <if test="alarmType != null">alarm_type = #{alarmType},</if>
            <if test="alarmStatus != null">alarm_status = #{alarmStatus},</if>
            <if test="alarmData != null">alarm_data = #{alarmData},</if>
            <if test="operationName != null">operation_name = #{operationName},</if>
            <if test="operationTime != null">operation_time = #{operationTime},</if>
            <if test="cause != null">cause = #{cause},</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>
            <if test="notifyUser != null">notify_user = #{notifyUser},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteEmsRecordAlarmDataByObjId" parameterType="Long">
        delete from ems_record_alarm_data where obj_id = #{objId}
    </delete>

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