<?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="equipmentSpecificAlarmId != null">
                weal.equipment_specific_alarm_id = #{equipmentSpecificAlarmId}
            </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>
</mapper>