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

    <!-- 查询设备告警列表 -->
    <select id="selectVoPage" resultType="com.fuyuanshen.equipment.domain.vo.DeviceAlarmVo">
        select da.*, d.device_mac as deviceMac, d.device_imei as deviceImei,d.device_name as deviceName,
        d.device_type as deviceType,
        d.type_name as deviceTypeName, d.device_pic as devicePic
        from device_alarm da
        left join device d on da.device_id = d.id
        left join device_type dt on dt.id = d.device_type
        <where>
            <if test="bo.content != null">
                and d.device_name like concat('%', #{bo.content}, '%') or dt.type_name like concat('%', #{bo.content},
                '%')
            </if>
            <if test="bo.deviceName != null">
                and d.device_name like concat('%', #{bo.deviceName}, '%')
            </if>
            <if test="bo.deviceType != null">
                and dt.id = #{bo.deviceType}
            </if>
            <if test="bo.deviceAction != null">
                and da.device_action = #{bo.deviceAction}
            </if>
            <if test="bo.treatmentState != null">
                and da.treatment_state = #{bo.treatmentState}
            </if>
            <if test="bo.queryTime1 != null and  bo.queryTime2 != null ">
                and da.start_time BETWEEN #{bo.queryTime1} AND #{bo.queryTime2}
            </if>
        </where>
        order by da.create_time DESC
    </select>


    <!-- 根据设备IMEI查询最新的一条告警数据 -->
    <select id="selectLatestByDeviceImei" resultType="com.fuyuanshen.equipment.domain.vo.DeviceAlarmVo">
        select *, d.device_mac as deviceMac, d.device_imei as deviceImei,
        d.type_name as deviceTypeName, d.device_pic as devicePic
        from device_alarm da
        left join device d on da.device_id = d.id
        left join device_type dt on dt.id = da.device_type
        <where>
            da.device_imei = #{deviceImei}
        </where>
        order by da.create_time desc
        limit 1
    </select>


    <!-- 获取实时告警列表 -->
    <select id="getRealtimeAlarm" resultType="com.fuyuanshen.equipment.domain.vo.DeviceAlarmVo">
        select da.*,
               d.device_mac  as deviceMac,
               d.device_imei as deviceImei,
               d.device_name as deviceName,
               d.device_type as deviceType,
               d.type_name   as deviceTypeName,
               d.device_pic  as devicePic
        from device_alarm da
                 left join device d on da.device_id = d.id
                 left join device_type dt on dt.id = d.device_type
        WHERE da.treatment_state = 1
        order by da.create_time DESC
    </select>
    
    <!-- 获取报警统计数据 -->
    <select id="getAlarmStatistics" resultType="com.fuyuanshen.equipment.domain.vo.AlarmStatisticsVo">
        SELECT 
            (SELECT COUNT(1) FROM device_alarm WHERE treatment_state = 1) AS activeAlarms,
            (SELECT COUNT(1) FROM device_alarm) AS totalAlarms,
            (SELECT COUNT(1) FROM device_alarm WHERE treatment_state = 0) AS processedAlarms,
            (SELECT COUNT(1) FROM device_alarm WHERE device_action = 0) AS forcedAlarms,
            (SELECT COUNT(1) FROM device_alarm WHERE device_action = 1) AS intrusionImpactAlarms,
            (SELECT COUNT(1) FROM device_alarm WHERE device_action = 2) AS autoAlarms,
            (SELECT COUNT(1) FROM device_alarm WHERE device_action = 3) AS geoFenceAlarms
    </select>
    
    <!-- 获取最近一年每月告警统计数据 -->
    <select id="getMonthlyAlarmStatistics" resultType="map">
        SELECT 
        COUNT(CASE WHEN MONTH(create_time) = 1 AND create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH) THEN 1 END) AS m1,
        COUNT(CASE WHEN MONTH(create_time) = 2 AND create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH) THEN 1 END) AS m2,
        COUNT(CASE WHEN MONTH(create_time) = 3 AND create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH) THEN 1 END) AS m3,
        COUNT(CASE WHEN MONTH(create_time) = 4 AND create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH) THEN 1 END) AS m4,
        COUNT(CASE WHEN MONTH(create_time) = 5 AND create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH) THEN 1 END) AS m5,
        COUNT(CASE WHEN MONTH(create_time) = 6 AND create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH) THEN 1 END) AS m6,
        COUNT(CASE WHEN MONTH(create_time) = 7 AND create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH) THEN 1 END) AS m7,
        COUNT(CASE WHEN MONTH(create_time) = 8 AND create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH) THEN 1 END) AS m8,
        COUNT(CASE WHEN MONTH(create_time) = 9 AND create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH) THEN 1 END) AS m9,
        COUNT(CASE WHEN MONTH(create_time) = 10 AND create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH) THEN 1 END) AS m10,
        COUNT(CASE WHEN MONTH(create_time) = 11 AND create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH) THEN 1 END) AS m11,
        COUNT(CASE WHEN MONTH(create_time) = 12 AND create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH) THEN 1 END) AS m12
        FROM device_alarm
        WHERE create_time >= DATE_SUB(NOW(), INTERVAL 12 MONTH)
    </select>


</mapper>