<?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.zehong.system.mapper.TDeviceAlarmMapper">
    
    <resultMap type="TDeviceAlarm" id="TDeviceAlarmResult">
        <result property="alarmId"    column="alarm_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="deviceType"    column="device_type"    />
        <result property="orderId"    column="order_id"    />
        <result property="alarmType"    column="alarm_type"    />
        <result property="alarmValue"    column="alarm_value"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="dealStatus"    column="deal_status"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectTDeviceAlarmVo">
        select alarm_id, device_id, device_type, order_id, alarm_type, alarm_value, start_time, end_time, deal_status, update_time, create_time from t_device_alarm
    </sql>

    <select id="selectTDeviceAlarmList" parameterType="DeviceAlarmForm" resultMap="TDeviceAlarmResult">
        <include refid="selectTDeviceAlarmVo"/>
        <where>
            <if test="startTime1 != null "> and start_time &gt;= #{startTime1}</if>
            <if test="endTime1 != null "> and start_time &lt;= #{endTime1}</if>
            <if test="alarmType != null  and alarmType != ''"> and alarm_type = #{alarmType}</if>
            <if test="dealStatus != null  and dealStatus != ''"> and deal_status = #{dealStatus}</if>
        </where>
        ORDER BY create_time DESC 
    </select>

    <select id="selectTDeviceAlarmByPipeCode" parameterType="DeviceAlarmForm" resultMap="TDeviceAlarmResult">
        select t.alarm_id, t.device_id, t.device_type, t.order_id, t.alarm_type, t.alarm_value, t.start_time, t.end_time, t.deal_status, t.update_time, t.create_time
        from t_device_alarm t
        left join t_pipe p on t.device_id = p.pipe_id
        <where>
            <if test="deviceCode != null "> and p.pipe_code like concat('%',#{deviceCode},'%')</if>
            <if test="startTime1 != null "> and t.start_time &gt;= #{startTime1}</if>
            <if test="endTime1 != null "> and t.start_time &lt;= #{endTime1}</if>
            <if test="alarmType != null  and alarmType != ''"> and t.alarm_type = #{alarmType}</if>
            <if test="dealStatus != null  and dealStatus != ''"> and t.deal_status = #{dealStatus}</if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="selectTDeviceAlarmByDeviceCode" parameterType="DeviceAlarmForm" resultMap="TDeviceAlarmResult">
        select t.alarm_id, t.device_id, t.device_type, t.order_id, t.alarm_type, t.alarm_value, t.start_time, t.end_time, t.deal_status, t.update_time, t.create_time
        from t_device_alarm t
        left join t_device_info d on t.device_id = d.device_id
        <where>
            <if test="deviceCode != null "> and d.device_code like concat('%',#{deviceCode},'%')</if>
            <if test="startTime1 != null "> and t.start_time &gt;= #{startTime1}</if>
            <if test="endTime1 != null "> and t.start_time &lt;= #{endTime1}</if>
            <if test="alarmType != null  and alarmType != ''"> and t.alarm_type = #{alarmType}</if>
            <if test="dealStatus != null  and dealStatus != ''"> and t.deal_status = #{dealStatus}</if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="selectTDeviceAlarmById" parameterType="int" resultMap="TDeviceAlarmResult">
        <include refid="selectTDeviceAlarmVo"/>
        where alarm_id = #{alarmId}
    </select>
        
    <insert id="insertTDeviceAlarm" parameterType="TDeviceAlarm" useGeneratedKeys="true" keyProperty="alarmId">
        insert into t_device_alarm
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">device_id,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="orderId != null">order_id,</if>
            <if test="alarmType != null">alarm_type,</if>
            <if test="alarmValue != null">alarm_value,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="dealStatus != null">deal_status,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceId != null">#{deviceId},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="alarmType != null">#{alarmType},</if>
            <if test="alarmValue != null">#{alarmValue},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="dealStatus != null">#{dealStatus},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateTDeviceAlarm" parameterType="TDeviceAlarm">
        update t_device_alarm
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="alarmType != null">alarm_type = #{alarmType},</if>
            <if test="alarmValue != null">alarm_value = #{alarmValue},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="dealStatus != null">deal_status = #{dealStatus},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where alarm_id = #{alarmId}
    </update>

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

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

    <select id="selectAlarmDevice" resultMap="TDeviceAlarmResult">
        SELECT
            alarm_id,
            device_id,
            device_type,
            order_id,
            alarm_type,
            alarm_value,
            start_time,
            end_time,
            deal_status,
            update_time,
            create_time
        FROM
            t_device_alarm
        WHERE
          end_time IS NULL

    </select>
</mapper>