<?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.pm.device.mapper.IbmsDeviceAlarmsMapper">

    <resultMap type="pd" id="IbmsDeviceAlarmResult">
        <result property="id"    column="id"    />
        <result property="alarmCode"    column="alarm_code"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceType"    column="device_type"    />
        <result property="subsystemType"    column="subsystem_type"    />
        <result property="alarmLevel"    column="alarm_level"    />
        <result property="alarmType"    column="alarm_type"    />
        <result property="alarmDesc"    column="alarm_desc"    />
        <result property="buildingName"    column="building_name"    />
        <result property="floorName"    column="floor_name"    />
        <result property="roomName"    column="room_name"    />
        <result property="spaceLocation"    column="space_location"    />
        <result property="alarmTime"    column="alarm_time"    />
        <result property="isReset"    column="is_reset"    />
        <result property="resetTime"    column="reset_time"    />
        <result property="resetBy"    column="reset_by"    />
        <result property="serviceOrderNo"    column="service_order_no"    />
        <result property="handleStatus"    column="handle_status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectIbmsDeviceAlarmVo">
        select id, alarm_code, device_code, device_name, device_type, subsystem_type,
               alarm_level, alarm_type, alarm_desc, building_name, floor_name, room_name,
               space_location, alarm_time, is_reset, reset_time, reset_by,
               service_order_no, handle_status, create_time, update_time
        from ibms_device_alarms
    </sql>

    <select id="selectIbmsDeviceAlarmList" parameterType="pd" resultMap="IbmsDeviceAlarmResult">
        <include refid="selectIbmsDeviceAlarmVo"/>
        <where>
            <if test="alarmCode != null and alarmCode != ''">
                and alarm_code like concat('%', #{alarmCode}, '%')
            </if>
            <if test="deviceCode != null and deviceCode != ''">
                and device_code = #{deviceCode}
            </if>
            <if test="deviceName != null and deviceName != ''">
                and device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="deviceType != null and deviceType != ''">
                and device_type = #{deviceType}
            </if>
            <if test="alarmLevel != null">
                and alarm_level = #{alarmLevel}
            </if>
            <if test="buildingName != null and buildingName != ''">
                and building_name = #{buildingName}
            </if>
            <if test="floorName != null and floorName != ''">
                and floor_name = #{floorName}
            </if>
            <if test="isReset != null">
                and is_reset = #{isReset}
            </if>
            <if test="beginTime != null and beginTime != ''">
                and alarm_time &gt;= #{beginTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and alarm_time &lt;= #{endTime}
            </if>
        </where>
        order by alarm_time desc
    </select>

    <select id="selectIbmsDeviceAlarmById" parameterType="Long" resultMap="IbmsDeviceAlarmResult">
        <include refid="selectIbmsDeviceAlarmVo"/>
        where id = #{id}
    </select>

    <!-- 统计查询 -->
    <select id="selectAlarmStatistics" resultType="pd">
        select
        count(1) as total,
        sum(case when is_reset = 0 then 1 else 0 end) as unreset,
        sum(case when alarm_level >= 3 then 1 else 0 end) as highLevel,
        sum(case when date(alarm_time) = curdate() then 1 else 0 end) as today
        from ibms_device_alarms
        <where>
            <if test="beginTime != null and beginTime != ''">
                and alarm_time &gt;= #{beginTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and alarm_time &lt;= #{endTime}
            </if>
        </where>
    </select>

    <insert id="insertIbmsDeviceAlarm" parameterType="pd" useGeneratedKeys="true" keyProperty="id">
        insert into ibms_device_alarms
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="alarmCode != null and alarmCode != ''">alarm_code,</if>
            <if test="deviceCode != null and deviceCode != ''">device_code,</if>
            <if test="deviceName != null and deviceName != ''">device_name,</if>
            <if test="deviceType != null and deviceType != ''">device_type,</if>
            <if test="subsystemType != null and subsystemType != ''">subsystem_type,</if>
            <if test="alarmLevel != null">alarm_level,</if>
            <if test="alarmType != null and alarmType != ''">alarm_type,</if>
            <if test="alarmDesc != null and alarmDesc != ''">alarm_desc,</if>
            <if test="buildingName != null and buildingName != ''">building_name,</if>
            <if test="floorName != null and floorName != ''">floor_name,</if>
            <if test="roomName != null and roomName != ''">room_name,</if>
            <if test="spaceLocation != null and spaceLocation != ''">space_location,</if>
            <if test="alarmTime != null">alarm_time,</if>
            <if test="isReset != null">is_reset,</if>
            <if test="resetTime != null">reset_time,</if>
            <if test="resetBy != null and resetBy != ''">reset_by,</if>
            <if test="serviceOrderNo != null and serviceOrderNo != ''">service_order_no,</if>
            <if test="handleStatus != null">handle_status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="alarmCode != null and alarmCode != ''">#{alarmCode},</if>
            <if test="deviceCode != null and deviceCode != ''">#{deviceCode},</if>
            <if test="deviceName != null and deviceName != ''">#{deviceName},</if>
            <if test="deviceType != null and deviceType != ''">#{deviceType},</if>
            <if test="subsystemType != null and subsystemType != ''">#{subsystemType},</if>
            <if test="alarmLevel != null">#{alarmLevel},</if>
            <if test="alarmType != null and alarmType != ''">#{alarmType},</if>
            <if test="alarmDesc != null and alarmDesc != ''">#{alarmDesc},</if>
            <if test="buildingName != null and buildingName != ''">#{buildingName},</if>
            <if test="floorName != null and floorName != ''">#{floorName},</if>
            <if test="roomName != null and roomName != ''">#{roomName},</if>
            <if test="spaceLocation != null and spaceLocation != ''">#{spaceLocation},</if>
            <if test="alarmTime != null">#{alarmTime},</if>
            <if test="isReset != null">#{isReset},</if>
            <if test="resetTime != null">#{resetTime},</if>
            <if test="resetBy != null and resetBy != ''">#{resetBy},</if>
            <if test="serviceOrderNo != null and serviceOrderNo != ''">#{serviceOrderNo},</if>
            <if test="handleStatus != null">#{handleStatus},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateIbmsDeviceAlarm" parameterType="pd">
        update ibms_device_alarms
        <trim prefix="SET" suffixOverrides=",">
            <if test="alarmCode != null and alarmCode != ''">alarm_code = #{alarmCode},</if>
            <if test="deviceCode != null and deviceCode != ''">device_code = #{deviceCode},</if>
            <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if>
            <if test="deviceType != null and deviceType != ''">device_type = #{deviceType},</if>
            <if test="subsystemType != null and subsystemType != ''">subsystem_type = #{subsystemType},</if>
            <if test="alarmLevel != null">alarm_level = #{alarmLevel},</if>
            <if test="alarmType != null and alarmType != ''">alarm_type = #{alarmType},</if>
            <if test="alarmDesc != null and alarmDesc != ''">alarm_desc = #{alarmDesc},</if>
            <if test="buildingName != null and buildingName != ''">building_name = #{buildingName},</if>
            <if test="floorName != null and floorName != ''">floor_name = #{floorName},</if>
            <if test="roomName != null and roomName != ''">room_name = #{roomName},</if>
            <if test="spaceLocation != null and spaceLocation != ''">space_location = #{spaceLocation},</if>
            <if test="alarmTime != null">alarm_time = #{alarmTime},</if>
            <if test="isReset != null">is_reset = #{isReset},</if>
            <if test="resetTime != null">reset_time = #{resetTime},</if>
            <if test="resetBy != null and resetBy != ''">reset_by = #{resetBy},</if>
            <if test="serviceOrderNo != null and serviceOrderNo != ''">service_order_no = #{serviceOrderNo},</if>
            <if test="handleStatus != null">handle_status = #{handleStatus},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <!-- 复位告警 -->
    <update id="resetAlarm" parameterType="pd">
        update ibms_device_alarms
        set is_reset = 1,
            reset_time = #{resetTime},
            reset_by = #{resetBy},
            update_time = #{updateTime}
        where id = #{id}
    </update>

    <!-- 更新服务单号 -->
    <update id="updateServiceOrderNo" parameterType="pd">
        update ibms_device_alarms
        set service_order_no = #{serviceOrderNo},
            handle_status = 1,
            update_time = #{updateTime}
        where id = #{id}
    </update>

    <delete id="deleteIbmsDeviceAlarmById" parameterType="Long">
        delete from ibms_device_alarms where id = #{id}
    </delete>

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