<?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.subsystem.mapper.DeviceAlarmMapper">

    <resultMap type="com.pm.common.config.PageData" id="DeviceAlarmResult">
    </resultMap>

    <sql id="selectDeviceAlarmVo">
        select id, alarm_id, device_code, device_name, subsystem_type,
               point_code, point_name, space_id, space_name,
               building_id, building_name, floor_id, floor_name,
               alarm_type, alarm_level, alarm_value, limit_value, unit,
               alarm_time, recovery_time, alarm_status, is_reset,
               reset_time, reset_user, reset_remark, priority,
               alarm_count, first_alarm_time, last_alarm_time,
               alarm_message, alarm_description, source_system,
               service_order_id, is_notified, notify_time, notify_users,
               create_time, update_time
        from device_alarm2
    </sql>

    <select id="selectDeviceAlarmById" parameterType="Long" resultMap="DeviceAlarmResult">
        <include refid="selectDeviceAlarmVo"/>
        where id = #{id}
    </select>

    <select id="selectDeviceAlarmList" parameterType="com.pm.common.config.PageData" resultMap="DeviceAlarmResult">
        <include refid="selectDeviceAlarmVo"/>
        <where>
            <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="alarmId != null and alarmId != ''"> and alarm_id = #{alarmId}</if>
            <if test="subsystemType != null and subsystemType != ''"> and subsystem_type = #{subsystemType}</if>
            <if test="pointCode != null and pointCode != ''"> and point_code = #{pointCode}</if>
            <if test="spaceId != null and spaceId != ''"> and space_id = #{spaceId}</if>
            <if test="buildingId != null and buildingId != ''"> and building_id = #{buildingId}</if>
            <if test="floorId != null and floorId != ''"> and floor_id = #{floorId}</if>
            <if test="alarmType != null and alarmType != ''"> and alarm_type = #{alarmType}</if>
            <if test="alarmLevel != null and alarmLevel != ''"> and alarm_level = #{alarmLevel}</if>
            <if test="alarmStatus != null"> and alarm_status = #{alarmStatus}</if>
            <if test="isReset != null"> and is_reset = #{isReset}</if>
            <if test="isNotified != null"> and is_notified = #{isNotified}</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="selectDeviceAlarmByAlarmId" parameterType="String" resultMap="DeviceAlarmResult">
        <include refid="selectDeviceAlarmVo"/>
        where alarm_id = #{alarmId}
    </select>

    <insert id="insertDeviceAlarm" parameterType="com.pm.common.config.PageData">
        insert into device_alarm2
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="alarmId != null">alarm_id,</if>
            <if test="deviceCode != null">device_code,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="subsystemType != null">subsystem_type,</if>
            <if test="pointCode != null">point_code,</if>
            <if test="pointName != null">point_name,</if>
            <if test="spaceId != null">space_id,</if>
            <if test="spaceName != null">space_name,</if>
            <if test="buildingId != null">building_id,</if>
            <if test="buildingName != null">building_name,</if>
            <if test="floorId != null">floor_id,</if>
            <if test="floorName != null">floor_name,</if>
            <if test="alarmType != null">alarm_type,</if>
            <if test="alarmLevel != null">alarm_level,</if>
            <if test="alarmValue != null">alarm_value,</if>
            <if test="limitValue != null">limit_value,</if>
            <if test="unit != null">unit,</if>
            <if test="alarmTime != null">alarm_time,</if>
            <if test="alarmStatus != null">alarm_status,</if>
            <if test="priority != null">priority,</if>
            <if test="alarmMessage != null">alarm_message,</if>
            <if test="alarmDescription != null">alarm_description,</if>
            <if test="sourceSystem != null">source_system,</if>
            <if test="firstAlarmTime != null">first_alarm_time,</if>
            <if test="lastAlarmTime != null">last_alarm_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="alarmId != null">#{alarmId},</if>
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="subsystemType != null">#{subsystemType},</if>
            <if test="pointCode != null">#{pointCode},</if>
            <if test="pointName != null">#{pointName},</if>
            <if test="spaceId != null">#{spaceId},</if>
            <if test="spaceName != null">#{spaceName},</if>
            <if test="buildingId != null">#{buildingId},</if>
            <if test="buildingName != null">#{buildingName},</if>
            <if test="floorId != null">#{floorId},</if>
            <if test="floorName != null">#{floorName},</if>
            <if test="alarmType != null">#{alarmType},</if>
            <if test="alarmLevel != null">#{alarmLevel},</if>
            <if test="alarmValue != null">#{alarmValue},</if>
            <if test="limitValue != null">#{limitValue},</if>
            <if test="unit != null">#{unit},</if>
            <if test="alarmTime != null">#{alarmTime},</if>
            <if test="alarmStatus != null">#{alarmStatus},</if>
            <if test="priority != null">#{priority},</if>
            <if test="alarmMessage != null">#{alarmMessage},</if>
            <if test="alarmDescription != null">#{alarmDescription},</if>
            <if test="sourceSystem != null">#{sourceSystem},</if>
            <if test="firstAlarmTime != null">#{firstAlarmTime},</if>
            <if test="lastAlarmTime != null">#{lastAlarmTime},</if>
        </trim>
    </insert>

    <update id="updateDeviceAlarm" parameterType="com.pm.common.config.PageData">
        update device_alarm2
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="alarmType != null">alarm_type = #{alarmType},</if>
            <if test="alarmLevel != null">alarm_level = #{alarmLevel},</if>
            <if test="alarmValue != null">alarm_value = #{alarmValue},</if>
            <if test="limitValue != null">limit_value = #{limitValue},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="recoveryTime != null">recovery_time = #{recoveryTime},</if>
            <if test="alarmStatus != null">alarm_status = #{alarmStatus},</if>
            <if test="isReset != null">is_reset = #{isReset},</if>
            <if test="resetTime != null">reset_time = #{resetTime},</if>
            <if test="resetUser != null">reset_user = #{resetUser},</if>
            <if test="resetRemark != null">reset_remark = #{resetRemark},</if>
            <if test="priority != null">priority = #{priority},</if>
            <if test="alarmCount != null">alarm_count = #{alarmCount},</if>
            <if test="lastAlarmTime != null">last_alarm_time = #{lastAlarmTime},</if>
            <if test="alarmMessage != null">alarm_message = #{alarmMessage},</if>
            <if test="alarmDescription != null">alarm_description = #{alarmDescription},</if>
            <if test="serviceOrderId != null">service_order_id = #{serviceOrderId},</if>
            <if test="isNotified != null">is_notified = #{isNotified},</if>
            <if test="notifyTime != null">notify_time = #{notifyTime},</if>
            <if test="notifyUsers != null">notify_users = #{notifyUsers},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDeviceAlarmById" parameterType="Long">
        delete from device_alarm2 where id = #{id}
    </delete>

    <delete id="deleteDeviceAlarmByIds" parameterType="Long">
        delete from device_alarm2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="resetDeviceAlarm" parameterType="com.pm.common.config.PageData">
        update device_alarm2
        set is_reset = 1,
            reset_time = #{resetTime},
            reset_user = #{resetUser},
            reset_remark = #{resetRemark}
        where alarm_id = #{alarmId}
          and is_reset = 0
    </update>

    <update id="batchResetDeviceAlarm" parameterType="List">
        update device_alarm2
        set is_reset = 1,
        reset_time = now()
        where alarm_id in
        <foreach item="alarmId" collection="list" open="(" separator="," close=")">
            #{alarmId}
        </foreach>
        and is_reset = 0
    </update>

    <select id="getAlarmStatistics" parameterType="com.pm.common.config.PageData" resultType="map">
        select
        count(1) as totalCount,
        sum(case when alarm_status = 1 then 1 else 0 end) as activeCount,
        sum(case when alarm_status = 0 then 1 else 0 end) as recoveredCount,
        sum(case when alarm_status = 2 then 1 else 0 end) as confirmedCount,
        sum(case when alarm_status = 3 then 1 else 0 end) as maskedCount,
        sum(case when is_reset = 1 then 1 else 0 end) as resetCount,
        sum(case when alarm_level = 'CRITICAL' then 1 else 0 end) as criticalCount,
        sum(case when alarm_level = 'MAJOR' then 1 else 0 end) as majorCount,
        sum(case when alarm_level = 'MINOR' then 1 else 0 end) as minorCount,
        sum(case when alarm_level = 'WARNING' then 1 else 0 end) as warningCount,
        sum(case when alarm_level = 'INFO' then 1 else 0 end) as infoCount
        from device_alarm2
        <where>
            <if test="deviceCode != null and deviceCode != ''"> and device_code = #{deviceCode}</if>
            <if test="subsystemType != null and subsystemType != ''"> and subsystem_type = #{subsystemType}</if>
            <if test="buildingId != null and buildingId != ''"> and building_id = #{buildingId}</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>
    </select>

    <select id="getAlarmTrend" parameterType="com.pm.common.config.PageData" resultType="map">
        select
        date_format(alarm_time, '%Y-%m-%d') as date,
        count(1) as count,
        alarm_level,
        alarm_type
        from device_alarm2
        <where>
            <if test="deviceCode != null and deviceCode != ''"> and device_code = #{deviceCode}</if>
            <if test="subsystemType != null and subsystemType != ''"> and subsystem_type = #{subsystemType}</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>
        group by date_format(alarm_time, '%Y-%m-%d'), alarm_level, alarm_type
        order by date
    </select>

    <insert id="insertAlarmResetRecord" parameterType="com.pm.common.config.PageData">
        insert into device_alarm_reset
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="resetId != null">reset_id,</if>
            <if test="alarmId != null">alarm_id,</if>
            <if test="deviceCode != null">device_code,</if>
            <if test="resetType != null">reset_type,</if>
            <if test="resetTime != null">reset_time,</if>
            <if test="resetUser != null">reset_user,</if>
            <if test="resetReason != null">reset_reason,</if>
            <if test="resetRemark != null">reset_remark,</if>
            <if test="beforeStatus != null">before_status,</if>
            <if test="afterStatus != null">after_status,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="resetId != null">#{resetId},</if>
            <if test="alarmId != null">#{alarmId},</if>
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="resetType != null">#{resetType},</if>
            <if test="resetTime != null">#{resetTime},</if>
            <if test="resetUser != null">#{resetUser},</if>
            <if test="resetReason != null">#{resetReason},</if>
            <if test="resetRemark != null">#{resetRemark},</if>
            <if test="beforeStatus != null">#{beforeStatus},</if>
            <if test="afterStatus != null">#{afterStatus},</if>
        </trim>
    </insert>

    <select id="selectAlarmResetRecords" parameterType="String" resultMap="DeviceAlarmResult">
        select id, reset_id, alarm_id, device_code, reset_type, reset_time,
               reset_user, reset_reason, reset_remark, before_status, after_status, create_time
        from device_alarm_reset
        where alarm_id = #{alarmId}
        order by reset_time desc
    </select>

    <update id="confirmAlarm" parameterType="com.pm.common.config.PageData">
        update device_alarm2
        set alarm_status = 2
        where alarm_id = #{alarmId}
          and alarm_status = 1
    </update>

    <update id="maskAlarm" parameterType="com.pm.common.config.PageData">
        update device_alarm2
        set alarm_status = 3
        where alarm_id = #{alarmId}
    </update>

</mapper>