<?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.pwt.web.mapper.PwtAlarmRecordMapper">
    
    <resultMap type="PwtAlarmRecord" id="PwtAlarmRecordResult">
        <result property="recordId"    column="record_id"    />
        <result property="alarmTime"    column="alarm_time"    />
        <result property="sourceDevice"    column="source_device"    />
        <result property="faultName"    column="fault_name"    />
        <result property="faultLevel"    column="fault_level"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <resultMap type="PwtAlarmRecordVo" id="PwtAlarmRecordVo">
        <result property="recordId"    column="record_id"    />
        <result property="alarmTime"    column="alarm_time"    />
        <result property="sourceDevice"    column="source_device"    />
        <result property="description"    column="fault"    />
        <result property="faultLevel"    column="fault_level"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>


    <sql id="selectPwtAlarmRecordVo">
        select record_id, alarm_time, concat(dt.device_type_name, ar.source_device_id) as `source_device`,
               fault, fault_level, create_time from pwt_alarm_record ar
        JOIN pwt_device_type dt
             ON ar.source_device_type = dt.id
        JOIN pwt_robot_fault fault
             ON ar.code = fault.code
                 AND ar.source_device_type = fault.source_device_type
    </sql>

    <select id="selectPwtAlarmRecordList" parameterType="PwtAlarmRecordQueryParam" resultMap="PwtAlarmRecordVo">
        <include refid="selectPwtAlarmRecordVo"/>
        <where>

            <if test="beginTime != null "> and alarm_time &gt;= #{beginTime}</if>
            <if test="endTime != null "> and alarm_time &lt;= #{endTime}</if>
            <if test="sourceDeviceType != null"> and ar.source_device_type = #{sourceDeviceType}</if>
            <if test="faultLevel != null"> and fault_level = #{faultLevel}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectPwtAlarmRecordById" parameterType="Long" resultMap="PwtAlarmRecordResult">
        <include refid="selectPwtAlarmRecordVo"/>
        where record_id = #{recordId}
    </select>

    <select id="getSourceDeviceDict"  resultType="com.pwt.web.domain.DeviceType">
        select id, device_type_name as deviceTypeName from pwt_device_type
    </select>

    <insert id="insertPwtAlarmRecord" parameterType="com.pwt.netty.message2.Alarm" useGeneratedKeys="true">
        insert into pwt_alarm_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dateTime != null">alarm_time,</if>
            <if test="sourceDeviceType != null">source_device_type,</if>
            <if test="sourceDeviceId != null">source_device_id,</if>
            <if test="code != null">code,</if>
            create_time,
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dateTime != null">#{dateTime},</if>
            <if test="sourceDeviceType != null">#{sourceDeviceType},</if>
            <if test="sourceDeviceId != null">#{sourceDeviceId},</if>
            <if test="code != null">#{code},</if>
            now(),
        </trim>

    </insert>
    <insert id="insertPwtAlarmRecords" useGeneratedKeys="true">
        insert into pwt_alarm_record (alarm_time, source_device_type, source_device_id, code, create_time) values
            <foreach collection="collection" item="item" index="index" separator=",">
               ( #{item.dateTime}, #{item.sourceDeviceType}, #{item.sourceDeviceId}, #{item.code}, now())
            </foreach>
    </insert>


    <update id="updatePwtAlarmRecord" parameterType="PwtAlarmRecord">
        update pwt_alarm_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="alarmTime != null">alarm_time = #{alarmTime},</if>
            <if test="sourceDevice != null">source_device = #{sourceDevice},</if>
            <if test="faultName != null">fault_name = #{faultName},</if>
            <if test="faultLevel != null">fault_level = #{faultLevel},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where record_id = #{recordId}
    </update>

    <delete id="deletePwtAlarmRecordById" parameterType="Long">
        delete from pwt_alarm_record where record_id = #{recordId}
    </delete>

    <delete id="deletePwtAlarmRecordByIds" parameterType="String">
        delete from pwt_alarm_record where record_id in 
        <foreach item="recordId" collection="array" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </delete>
    
</mapper>