<?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.hw.ems.record.mapper.RecordAlarmDataMapper">

    <resultMap type="RecordAlarmData" id="RecordAlarmDataResult">
        <result property="objId"    column="objId"    />
        <result property="monitorId"    column="monitor_id"    />
        <result property="collectDeviceId"    column="collect_device_id"    />
        <result property="collectTime"    column="collect_time"    />
        <result property="alarmType"    column="alarm_type"    />
        <result property="alarmStatus"    column="alarmStatus"    />
        <result property="alarmData"    column="alarmData"    />
        <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="deptId"    column="dept_id"    />
        <result property="userId"    column="user_id"    />
        <result property="monitorName"    column="monitor_name"    />
    </resultMap>
    <sql id="selectRecordAlarmDataVo">
        select record_alarm_data.objId,
               record_alarm_data.monitor_id,
               case when m.monitor_name is null then cdi.collect_device_name else m.monitor_name end monitor_name,
               record_alarm_data.collect_device_id,collect_time,alarm_type,alarmStatus,
               alarmData,operation_name,operation_time,cause,record_alarm_data.create_by,record_alarm_data.create_time,
               record_alarm_data.update_by,record_alarm_data.update_time,notify_user,
               record_alarm_data.dept_id,record_alarm_data.user_id
        from record_alarm_data
                 left join base_monitor_info m on m.monitor_id = record_alarm_data.monitor_id
                 left join base_collect_device_info cdi on cdi.collect_device_id = record_alarm_data.collect_device_id
    </sql>

    <select id="selectRecordAlarmDataList" parameterType="RecordAlarmData" resultMap="RecordAlarmDataResult">
        <include refid="selectRecordAlarmDataVo"/>
        <where>
            <if test="monitorId != null  and monitorId != ''"> and record_alarm_data.monitor_id = #{monitorId}</if>
            <if test="collectDeviceId != null  and collectDeviceId != ''"> and record_alarm_data.collect_device_id = #{collectDeviceId}</if>
            <if test="collectTime != null "> and record_alarm_data.collect_time = #{collectTime}</if>
            <if test="alarmType != null "> and alarm_type = #{alarmType}</if>
            <if test="alarmStatus != null "> and alarmStatus = #{alarmStatus}</if>
            <if test="alarmData != null  and alarmData != ''"> and alarmData = #{alarmData}</if>
            <if test="operationName != null  and operationName != ''"> and operation_name like concat('%', #{operationName}, '%')</if>
            <if test="operationTime != null "> and operation_time = #{operationTime}</if>
            <if test="cause != null  and cause != ''"> and cause = #{cause}</if>
            <if test="notifyUser != null  and notifyUser != ''"> and notify_user like concat('%', #{notifyUser}, '%')</if>
            <!--            <if test="deptId != null "> and dept_id = #{deptId}</if>-->
            <!--            <if test="userId != null "> and user_id = #{userId}</if>-->
        </where>
    </select>

    <select id="selectRecordAlarmDataByObjId" parameterType="Long" resultMap="RecordAlarmDataResult">
        <include refid="selectRecordAlarmDataVo"/>
        where record_alarm_data.objId = #{objId}
    </select>

    <insert id="insertRecordAlarmData" parameterType="RecordAlarmData" useGeneratedKeys="true" keyProperty="objId">
        insert into 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">alarmStatus,</if>
            <if test="alarmData != null">alarmData,</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>
            <if test="deptId != null">dept_id,</if>
            <if test="userId != null">user_id,</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>
            <if test="deptId != null">#{deptId},</if>
            <if test="userId != null">#{userId},</if>
        </trim>
    </insert>

    <update id="updateRecordAlarmData" parameterType="RecordAlarmData">
        update 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">alarmStatus = #{alarmStatus},</if>
            <if test="alarmData != null">alarmData = #{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>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userId != null">user_id = #{userId},</if>
        </trim>
        where objId = #{objId}
    </update>

    <delete id="deleteRecordAlarmDataByObjId" parameterType="Long">
        delete from record_alarm_data where objId = #{objId}
    </delete>

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