<?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.yfd.platform.modules.patroltask.mapper.AlarmLogMapper">

    <!--静默监视查询-->
    <select id="getAlarmListByType" resultType="java.util.Map">
        SELECT
        area_id,sp.patroldevice_name,sp.area_name,sp.place,a.*
        FROM
        (
        SELECT
        patroldevice_code,
        monitor_type,
        COUNT(*) count,
        MAX(alarm_date) alarmDate
        FROM
        iis_alarm_log
        WHERE
        task_alarm_type = '2'
        AND station_id = #{stationId}
        <if test="areaId != null and areaId != ''">
            AND area_id = #{areaId}
        </if>
        <if test="patrolDeviceName != null and patrolDeviceName != ''">
            AND patroldevice_name LIKE concat('%',#{patrolDeviceName},'%')
        </if>
        <if test="monitorType != null and monitorType != ''">
            AND monitor_type = #{monitorType}
        </if>
        GROUP BY
        monitor_type,
        patroldevice_code) a
        LEFT JOIN iis_substation_patroldevice sp ON a.patroldevice_code= sp.patroldevice_code
    </select>

    <!--根据设备编号查询告警列表-->
    <select id="getAlarmListPage" resultType="java.util.Map">
        SELECT
            patroldevice_code,
            content,
            alarm_date,
            defect_file_path
        FROM
            iis_alarm_log
        WHERE
            task_alarm_type = '2'
        <if test="patrolDeviceCode != null and patrolDeviceCode != ''">
            AND patroldevice_code = #{patrolDeviceCode}
        </if>
        <if test="monitorType != null and monitorType != ''">
            AND monitor_type = #{monitorType}
        </if>
        <if test="startDate != null and startDate != ''">
            and str_to_date(alarm_date, '%Y-%m-%d %H:%i:%s') &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and str_to_date(alarm_date, '%Y-%m-%d %H:%i:%s') &lt;= #{endDate}
        </if>
        ORDER By alarm_date DESC
    </select>
    <select id="getConfirmationRate" resultType="java.util.Map">
        SELECT
        COUNT(*) AS total_alarms, -- 总告警数量
        SUM(check_result = 1) AS confirmed_count, -- 属实的数量（check_result=1）
        ROUND(
        SUM(check_result = 1) / COUNT(*) * 100, -- 计算比率（分子/分母*100）
        3
        ) AS confirmation_rate,-- 比率保留2位小数
        '%' AS unit
        FROM
        iis_alarm_log
        WHERE 1=1
        AND check_flag!='0'
        <if test="stationId != null and stationId != ''">
            AND station_id = #{stationId}
        </if>
    </select>
    <select id="getAlarmLogList" resultType="java.util.Map">
        SELECT
            id,
            station_id,
            alarm_source_type,
            content,
            alarm_level alarm_level,
            alarm_date
        FROM
            iis_alarm_log
        WHERE
            station_id = #{stationId}
        ORDER BY
        alarm_date DESC;
    </select>
    <select id="getAlarmLogById" resultType="java.util.Map">
        SELECT
            al.*,
            al.device_id deviceid,
            tr.patroldevice_code deviceId,
            tr.patroldevice_channelcode channelId,
            tr.patroldevice_pos patroldevicePos
        FROM
            iis_alarm_log al
        LEFT JOIN iis_task_result tr ON al.task_result_id = tr.result_id
        WHERE
            al.id = #{id}
    </select>
    <select id="getNotCheckAlarmCount" resultType="com.yfd.platform.modules.patroltask.domain.AlarmLog">
        SELECT
            al.*,
            al.device_id deviceid,
            tr.patroldevice_code deviceId,
            tr.patroldevice_channelcode channelId,
            tr.patroldevice_pos patroldevicePos
        FROM
            iis_alarm_log al
            LEFT JOIN iis_task_result tr ON al.task_result_id = tr.result_id
        WHERE
            check_flag = '0'
            AND task_alarm_type != '4'
            AND station_id = #{stationId}
    </select>
    <select id="getAlarmLogPage" resultType="java.util.Map">
        SELECT
            al.*,
            sd.device_class
        FROM
            iis_alarm_log al
        LEFT JOIN iis_substation_device sd ON al.device_id = sd.device_id
        WHERE 1=1
        <if test="stationCode != null and stationCode != ''">
            AND al.station_code = #{stationCode}
        </if>
        <if test="taskAlarmType != null and taskAlarmType != ''">
            AND al.task_alarm_type = #{taskAlarmType}
        </if>
        <if test="alarmLevel != null and alarmLevel != ''">
            AND al.alarm_level = #{alarmLevel}
        </if>
        <if test="checkFlag != null and checkFlag != ''">
            AND al.check_flag = #{checkFlag}
        </if>
        <if test="alarmSourceType != null and alarmSourceType != ''">
            AND al.alarm_source_type = #{alarmSourceType}
        </if>
        <if test="startDate != null and startDate != ''">
            and al.alarm_date &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            and al.alarm_date &lt; DATE_ADD(#{endDate}, INTERVAL 1 DAY)
        </if>



    </select>

    <!--    <select id="getDeviceAlarmRecordPage"-->
<!--            resultType="com.yfd.platform.modules.auxcontrol.domain.DeviceAlarmRecord">-->
<!--        SELECT-->
<!--        dar.*,-->
<!--        sdi1.dictname AS alarmTypeName,-->
<!--        sdi2.dictname AS alarmLevelName,-->
<!--        sdi3.dictname AS alarmStatusName,-->
<!--        COALESCE (-->
<!--        (-->
<!--        SELECT-->
<!--        GROUP_CONCAT( dictname ORDER BY itemcode SEPARATOR ',' )-->
<!--        FROM-->
<!--        sys_dictionary_items-->
<!--        WHERE-->
<!--        dictid = 'd3a5c69ef214012abcdb31495ef4e772'-->
<!--        AND FIND_IN_SET( itemcode, dar.notice_action ) > 0-->
<!--        ),-->
<!--        ''-->
<!--        ) AS noticeTypeName-->
<!--        FROM-->
<!--        fk_device_alarm_record dar-->
<!--        LEFT JOIN sys_dictionary_items sdi1 ON sdi1.dictid = 'aad92e9fd54d05e090b074f3e0666c8c'-->
<!--        AND sdi1.itemcode = dar.alarm_type-->
<!--        LEFT JOIN sys_dictionary_items sdi2 ON sdi2.dictid = '138d15a0ce89e5abd516389a1176db6e'-->
<!--        AND sdi2.itemcode = dar.alarm_level-->
<!--        LEFT JOIN sys_dictionary_items sdi3 ON sdi3.dictid = '1f0eb135658ed4c825021e2d5189efc0'-->
<!--        AND sdi3.itemcode = dar.`status`-->
<!--        where 1=1-->
<!--        <if test="systemcode != null and systemcode != ''">-->
<!--            and dar.systemcode= #{systemcode}-->
<!--        </if>-->
<!--        <if test="deviceName != null and deviceName != ''">-->
<!--            and dar.device_name like concat('%',#{deviceName},'%')-->
<!--        </if>-->
<!--        <if test="signalName != null and signalName != ''">-->
<!--            and dar.signal_name like concat('%',#{signalName},'%')-->
<!--        </if>-->
<!--        <if test="alarmLevel != null and alarmLevel != ''">-->
<!--            and dar.alarm_level = #{alarmLevel}-->
<!--        </if>-->
<!--        <if test="status != null and status != ''">-->
<!--            and dar.status = #{status}-->
<!--        </if>-->
<!--        <if test="startDate != null and startDate != ''">-->
<!--            and dar.alarm_time &gt;= #{startDate}-->
<!--        </if>-->

<!--        <if test="endDate != null and endDate != ''">-->
<!--            and dar.alarm_time &lt;= #{endDate}-->
<!--        </if>-->
<!--    </select>-->
</mapper>
