<?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.yeejoin.equipmanage.mapper.EquipmentSpecificAlarmLogMapper">
    <update id="updateAlarmLogByIotCodeAndIndexKey">
        UPDATE wl_equipment_specific_alarm_log wesal
            SET wesal.confirm_type = #{dto.confirmType},
            wesal.resolve_result = #{dto.resolveResult},
            wesal.confirm_user_name = #{dto.confirmUserName},
            wesal.confirm_date = now()
        WHERE
            wesal.equipment_specific_alarm_id IN (
            SELECT
            id
            FROM
            wl_equipment_specific_alarm wesa
            WHERE
            wesa.iot_code = #{dto.iotCode}
            AND wesa.equipment_specific_index_key = #{dto.equipmentSpecificIndexKey}
        )
    </update>
    <select id="findByIdList" resultType="com.yeejoin.equipmanage.common.datasync.vo.AppAlarmExtVo">
        SELECT
            CONCAT(sal.create_date) AS time,
            IFNULL(a.`code`,' ') AS areaid,
            IFNULL(a.`name`,' ') AS areaname,
            f.`code` AS stationid,
            f.`name` AS stationname,
            s.`code` AS deviceid,
            s.`name` AS devicename,
            sal.equipment_specific_code AS partid,
            sal.equipment_specific_name AS partname,
            s.`system_type_code` AS type,
            IF
            ( sal.confirm_type = 'FIREALARM', 0, 1 ) AS eventlevel,
            -- CONCAT( sal.equipment_specific_name, ' ', sal.equipment_specific_index_name ) AS eventdesc,
            CONCAT( '位于', a.`name`, '的', sal.equipment_specific_name, '(', sal.equipment_specific_code, ')在',  sal.create_date, '发生', sal.equipment_specific_index_name, '，请相关人员进行核实确认现场情况！') AS eventdesc,
            sal.equipment_specific_index_name AS eventaction,
            IF
            ( sal.confirm_date IS NULL, FALSE, TRUE ) AS isreport ,
            CONCAT(sal.confirm_date) AS confirmtime,
            sal.alarm_reason AS eventreason
        FROM
            wl_equipment_specific_alarm_log sal
            LEFT JOIN wl_form_instance i ON sal.build_id = i.instance_id
            LEFT JOIN wl_area a ON a.id = i.field_value
            LEFT JOIN f_fire_fighting_system s ON s.id = IF(LOCATE(',' , sal.system_ids) > 0,SUBSTRING(sal.system_ids,1,LOCATE(',' , sal.system_ids)-1),sal.system_ids),
            f_station_info f
        WHERE
            i.field_name = "belongAreaId"
            <if test="list != null and list.size() >0">
                AND sal.id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        GROUP BY
            sal.id
    </select>
    <select id="findByIdListNoBuildId" resultType="com.yeejoin.equipmanage.common.datasync.vo.AppAlarmExtVo">
        SELECT
            CONCAT(sal.create_date) AS time,
            ' ' AS areaid,
            ' ' AS areaname,
            f.`code` AS stationid,
            f.`name` AS stationname,
            s.`code` AS deviceid,
            s.`name` AS devicename,
            sal.equipment_specific_code AS partid,
            sal.equipment_specific_name AS partname,
            s.`system_type_code` AS type,
            IF
            ( sal.confirm_type = 'FIREALARM', 0, 1 ) AS eventlevel,
            -- CONCAT( sal.equipment_specific_name, ' ', sal.equipment_specific_index_name ) AS eventdesc,
            CONCAT(sal.equipment_specific_name, '(', sal.equipment_specific_code, ')在',  sal.create_date, '发生', sal.equipment_specific_index_name, '，请相关人员进行核实确认现场情况！') AS eventdesc,
            sal.equipment_specific_index_name AS eventaction,
            IF
            ( sal.confirm_date IS NULL, FALSE, TRUE ) AS isreport ,
            CONCAT(sal.confirm_date) AS confirmtime,
            sal.alarm_reason AS eventreason
        FROM
            wl_equipment_specific_alarm_log sal
            LEFT JOIN wl_equipment_specific_alarm sa ON sa.id = sal.equipment_specific_alarm_id
            LEFT JOIN f_fire_fighting_system s ON s.id = IF(LOCATE(',' , sal.system_ids) > 0,SUBSTRING(sal.system_ids,1,LOCATE(',' , sal.system_ids)-1),sal.system_ids),
            f_station_info f
        <where>
            <if test="list != null and list.size() >0">
                AND sal.id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY
            sal.id
    </select>
    <select id="getIsConfirmByAlarmId" resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarmLog">
        SELECT
            *
        FROM
            wl_equipment_specific_alarm_log weal
        <where>
            <if test="equipmentSpecificId != null and equipmentSpecificId != ''">
                AND weal.equipment_specific_id = #{equipmentSpecificId}
            </if>
            <if test="equipmentSpecificIndexKey != null and equipmentSpecificIndexKey != ''">
                AND weal.equipment_specific_index_key = #{equipmentSpecificIndexKey}
            </if>
            <if test="isConfirm != null and isConfirm != '' and isConfirm == 0">
                AND weal.confirm_date IS NULL
            </if>
            <if test="isConfirm != null and isConfirm != '' and isConfirm == 1">
                AND weal.confirm_date IS NOT NULL
            </if>
        </where>
    </select>
    
    <select id="getFireEquipAlarmLogDetailsById"
            resultType="com.yeejoin.equipmanage.common.datasync.entity.FireEquipmentFireAlarm">
        SELECT
        sal.id,
        es.id AS fireEquipmentId,
        si.id AS equipmentMeasurementId,
        ei.name_key AS fieldName,
        ei.`name` AS fieldLabel,
        IFNULL(si.`value`,'') AS `value`,
        sa.frequency,
        sa.`status`,
        sa.type,
        IFNULL(es.code,'') AS fireEquipmentMRid,
        '' AS equipmentMeasurementMRid,
        ed.`name` AS fireEquipmentName,
        sa.recovery_date,
        sal.create_date,
        sal.update_date,
        sal.alarm_reason AS description,
        IFNULL(sal.equipment_specific_name,'') AS `name`,
        IFNULL(sal.equipment_specific_code,'') AS mrid
        FROM
        wl_equipment_specific_alarm_log sal
        LEFT JOIN wl_equipment_specific_alarm sa ON sal.equipment_specific_alarm_id = sa.id
        LEFT JOIN wl_equipment_specific es ON sa.equipment_specific_id = es.id
        LEFT JOIN wl_equipment_specific_index si ON es.id = si.equipment_specific_id
        LEFT JOIN wl_equipment_detail ed ON ed.id = es.equipment_detail_id
        LEFT JOIN wl_equipment_index ei ON si.equipment_index_id = ei.id
        <where>
            <if test="list != null and list.size > 0">
                sal.id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getPlanRecord" resultType="Map">
        SELECT
            start_time startTime,
            equipment_code equipmentCode,
            equipment_name equipmentName,
            equipment_id equipmentId,
            fire_equipment_id fireEquipmentId
        FROM
            c_plan_operation_record cpor
        WHERE
            cpor.status = 0
        ORDER BY cpor.create_date DESC
        LIMIT 1
    </select>

    <select id="getOtherEquipInfo" resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarmLog">
        SELECT
        *
        FROM
            wl_equipment_specific_alarm_log weal
        <where>
            weal.type != 'HearBeat'
            <if test="date != null">
                AND weal.create_date <![CDATA[>=]]> DATE_FORMAT(#{date}, '%Y-%m-%d %H:%i:%S')
            </if>
            <if test="equipmentId != null">
                AND weal.equipment_specific_id = #{equipmentId}
            </if>
        </where>
        ORDER BY weal.create_date DESC
    </select>
    <select id="alarmEquipLink" resultType="java.util.Map">
        SELECT
            <choose>
                <when test="pattern != null and pattern != ''">
                    <if test="pattern == '%Y-%m-%d'">
                        ( SELECT COUNT( DISTINCT wesal.equipment_specific_id ) AS sumNum FROM wl_equipment_specific_alarm_log wesal WHERE DATE_FORMAT( CURDATE(), '%Y-%m-%d' ) = DATE_FORMAT(wesal.create_date, '%Y-%m-%d' ) ) sumNum,
                        ( SELECT COUNT( DISTINCT wesal.equipment_specific_id ) AS sumNum FROM wl_equipment_specific_alarm_log wesal WHERE DATE_FORMAT( DATE_SUB(CURDATE() , INTERVAL 1 DAY), '%Y-%m-%d' ) = DATE_FORMAT(wesal.create_date, '%Y-%m-%d' ) ) hbSumNum
                    </if>
                    <if test="pattern == '%Y-%m'">
                        ( SELECT COUNT( DISTINCT wesal.equipment_specific_id ) AS sumNum FROM wl_equipment_specific_alarm_log wesal WHERE DATE_FORMAT( CURDATE(), '%Y-%m' ) = DATE_FORMAT(wesal.create_date, '%Y-%m' ) ) sumNum,
                        ( SELECT COUNT( DISTINCT wesal.equipment_specific_id ) AS sumNum FROM wl_equipment_specific_alarm_log wesal WHERE DATE_FORMAT( DATE_SUB(CURDATE() , INTERVAL 1 MONTH), '%Y-%m' ) = DATE_FORMAT(wesal.create_date, '%Y-%m' ) ) hbSumNum
                    </if>
                </when>
                <otherwise>
                    ( SELECT COUNT( DISTINCT wesal.equipment_specific_id ) AS sumNum FROM wl_equipment_specific_alarm_log wesal WHERE DATE_FORMAT( CURDATE(), '%Y-%m' ) = DATE_FORMAT(wesal.create_date, '%Y-%m-%d' ) ) sumNum,
                    ( SELECT COUNT( DISTINCT wesal.equipment_specific_id ) AS sumNum FROM wl_equipment_specific_alarm_log wesal WHERE DATE_FORMAT( DATE_SUB(CURDATE() , INTERVAL 1 DAY), '%Y-%m-%d' ) = DATE_FORMAT(wesal.create_date, '%Y-%m-%d' ) ) hbSumNum
                </otherwise>
            </choose>
    </select>
    <select id="unCleanAlarmEquipLink" resultType="java.util.Map">
        SELECT
            <choose>
                <when test="pattern != null and pattern != ''">
                    <if test="pattern == '%Y-%m-%d'">
                        ( SELECT COUNT( DISTINCT wesal.equipment_specific_id ) AS sumNum FROM wl_equipment_specific_alarm_log wesal WHERE wesal.clean_time IS NULL AND DATE_FORMAT( CURDATE(), '%Y-%m-%d' ) = DATE_FORMAT(wesal.create_date, '%Y-%m-%d' ) ) sumNum,
                        ( SELECT COUNT( DISTINCT wesal.equipment_specific_id ) AS sumNum FROM wl_equipment_specific_alarm_log wesal WHERE wesal.clean_time IS NULL AND DATE_FORMAT( DATE_SUB(CURDATE() , INTERVAL 1 DAY), '%Y-%m-%d' ) = DATE_FORMAT(wesal.create_date, '%Y-%m-%d' ) ) hbSumNum
                    </if>
                    <if test="pattern == '%Y-%m'">
                        ( SELECT COUNT( DISTINCT wesal.equipment_specific_id ) AS sumNum FROM wl_equipment_specific_alarm_log wesal WHERE wesal.clean_time IS NULL AND DATE_FORMAT( CURDATE(), '%Y-%m' ) = DATE_FORMAT(wesal.create_date, '%Y-%m' ) ) sumNum,
                        ( SELECT COUNT( DISTINCT wesal.equipment_specific_id ) AS sumNum FROM wl_equipment_specific_alarm_log wesal WHERE wesal.clean_time IS NULL AND DATE_FORMAT( DATE_SUB(CURDATE() , INTERVAL 1 MONTH), '%Y-%m' ) = DATE_FORMAT(wesal.create_date, '%Y-%m' ) ) hbSumNum
                    </if>
                </when>
                <otherwise>
                    ( SELECT COUNT( DISTINCT wesal.equipment_specific_id ) AS sumNum FROM wl_equipment_specific_alarm_log wesal WHERE wesal.clean_time IS NULL AND DATE_FORMAT( CURDATE(), '%Y-%m-%d' ) = DATE_FORMAT(wesal.create_date, '%Y-%m-%d' ) ) sumNum,
                    ( SELECT COUNT( DISTINCT wesal.equipment_specific_id ) AS sumNum FROM wl_equipment_specific_alarm_log wesal WHERE wesal.clean_time IS NULL AND DATE_FORMAT( DATE_SUB(CURDATE() , INTERVAL 1 DAY), '%Y-%m-%d' ) = DATE_FORMAT(wesal.create_date, '%Y-%m-%d' ) ) hbSumNum
                </otherwise>
            </choose>
    </select>
    <select id="getAlarmLogInfoList" resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarmLog">
        SELECT
        *
        FROM
        wl_equipment_specific_alarm_log wesal
        <where>
            <if test="equipmentCode != null and equipmentCode != ''">
                AND wesal.equipment_code LIKE CONCAT(  #{equipmentCode},'%')
            </if>
            <if test="nameKeys != null and nameKeys.split(',').length >0">
                AND wesal.equipment_specific_index_key IN
                <foreach collection="nameKeys.split(',')" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="value != null and value != ''">
                AND wesal.equipment_specific_index_value = #{value}
            </if>
            <if test="isCleanTime != null and isCleanTime == 'false'">
                AND wesal.clean_time IS NULL
            </if>
            <if test="isCleanTime != null and isCleanTime == 'true'">
                AND wesal.clean_time IS NOT NULL
            </if>
            <if test="bizOrgCode != null and bizOrgCode != ''">
                AND wesal.biz_org_code LIKE CONCAT(  #{bizOrgCode},'%')
            </if>
        </where>
        ORDER BY wesal.create_date DESC
    </select>

    <update id="updateStatusByAlarm">
    update  wl_equipment_specific_alarm_log
    set confirm_type = 'BREAKDOWN',
        clean_time = now() ,
        confirm_date = IFNULL(wl_equipment_specific_alarm_log.confirm_date,now()),
        resolve_result = #{resolveResult},
        confirm_user_name = #{confirmUserName},
        confirm_type_name = #{confirmTypeName},
        alarm_reason = concat((select name from wl_equipment_specific WHERE id = wl_equipment_specific_alarm_log.equipment_specific_id) ,'故障引起设备报警'),
        equipment_specific_index_value = false
        WHERE
        confirm_type is null or clean_time is null
    </update>

    <select id="getEquipAlarmList" resultType="map">
        SELECT
            id as id,
            equipment_specific_index_name AS alarmType,
            create_date AS alarmDate,
            clean_time AS cleanTime,
            concat( equipment_specific_name, equipment_specific_index_name ) AS alarmContent,
            ( CASE clean_time is NULL  WHEN TRUE THEN '未消除' ELSE '已消除' END ) AS clearStatus
        FROM
            wl_equipment_specific_alarm_log
        WHERE
              equipment_specific_id in (#{equipmentSpecificId})
    </select>

    <select id="getEquipAlarmIdList" resultType="string">
        select
            id as id
        from
            wl_equipment_specific_alarm_log
        where
            equipment_specific_id in (#{equipmentSpecificId})
    </select>
</mapper>