<?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.EquipmentSpecificAlarmMapper">
    <resultMap id="resultMapDto" type="com.yeejoin.equipmanage.common.entity.dto.EquipSpecificAlarmDTO">
        <result column="id" property="id"/>
        <result column="equipmentSpecificId" property="equipmentSpecificId"/>
        <result column="fireEquipmentCode" property="fireEquipmentCode"/>
        <result column="fireEquipmentName" property="fireEquipmentName"/>
        <result column="warehouseStructureName" property="warehouseStructureName"/>
        <result column="fireEquipmentSpecificIndexName" property="fireEquipmentSpecificIndexName"/>
        <result column="createDate" property="createDate"/>
        <result column="status" property="status"/>
        <result column="confirmType" property="confirmType"/>
    </resultMap>

    <update id="updateStatusByAlarm">
        update wl_equipment_specific_alarm set status = 0,recovery_date = now(),equipment_specific_index_value = false where status = 1
    </update>

    <select id="getEquipmentSpecAlarmByIotCode"
            resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarm">
        SELECT
        wes.org_code AS orgCode,
        wed.name AS equipmentSpecificName,
        wes.id AS equipmentSpecificId,
        wes.iot_code AS iotCode,
        wei.name AS equipmentSpecificIndexName,
        wei.name_key AS equipmentSpecificIndexKey,
        wesi.value AS equipmentSpecificIndexValue,
        wesi.id as equipmentSpecificIndexId,
        wei.type_name AS type,
        wei.id AS equipmentIndexId,
        wei.type_code AS alarmCode,
        wes.qr_code as qrCode
        FROM
        wl_equipment_specific_index wesi
        LEFT JOIN wl_equipment_specific wes ON wes.id = wesi.equipment_specific_id
        LEFT JOIN wl_equipment_detail wed ON wed.id = wes.equipment_detail_id
        LEFT JOIN wl_equipment_index wei ON wei.id = wesi.equipment_index_id
        WHERE
        wes.iot_code = #{iotCode}
        AND
        wei.is_iot = 1
    </select>

    <select id="getCount" resultType="int">
        SELECT
        COUNT(DISTINCT(wesa.id))
        FROM
        wl_equipment_specific_alarm wesa
        LEFT JOIN wl_equipment_specific wes ON wes.id = wesa.equipment_specific_id
        LEFT JOIN wl_equipment_detail wed ON wed.id = wes.equipment_detail_id
        LEFT JOIN wl_equipment_specific_index wesi ON wesi.equipment_specific_id = wes.id
        LEFT JOIN wl_equipment_index wei ON wei.id = wesi.equipment_index_id
        WHERE
        wes.iot_code = #{iotCode}
        AND
        wei.is_iot = 1
    </select>

    <select id="getEquipmentSpecAlarmVoListByEquipIndexId"
            resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarm">
        SELECT
        *
        FROM
        wl_equipment_specific_alarm wesa
        LEFT JOIN wl_equipment_index wei ON wei.id = wesa.equipment_index_id
        WHERE
        wesa.equipment_index_id = #{equipmentIndex.id}
    </select>

    <select id="checkIfExist" resultType="int">
        SELECT
        COUNT(1)
        FROM
        wl_equipment_specific_alarm wesa
        LEFT JOIN wl_equipment_index wei ON wei.id = wesa.equipment_index_id
        WHERE
        wesa.equipment_index_id = #{equipmentIndex.id}
    </select>

    <select id="getAlarmDataByIndexId" resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarm">
        SELECT
        *
        FROM
        wl_equipment_specific_alarm wesa
        WHERE
        wesa.equipment_index_id = #{indexId}
        ORDER BY wesa.id
    </select>

    <select id="getEquipmentSpecAlarmByIot" resultType="com.yeejoin.equipmanage.common.entity.vo.EquipmentIotAlarmVO">
        SELECT
        wei.id AS equipmentIndexId,
        wei.name_key AS equipmentSpecificIndexKey
        FROM
        wl_equipment_specific_index wesi
        LEFT JOIN wl_equipment_specific wes ON wes.id = wesi.equipment_specific_id
        LEFT JOIN wl_equipment_detail wed ON wed.id = wes.equipment_detail_id
        LEFT JOIN wl_equipment_index wei ON wei.id = wesi.equipment_index_id
        WHERE
        wes.iot_code = #{iotCode}
        AND
        wei.is_iot = 1
    </select>


    <select id="getByIndexId" resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarm">
        SELECT
        *
        FROM
        wl_equipment_specific_alarm wesa
        WHERE
        wesa.equipment_index_id = #{indexId}
    </select>

    <select id="getEquipListBySpecific" resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarm">
        <![CDATA[

                SELECT
                    *
                FROM
                    wl_equipment_specific_alarm
                WHERE
                    status = #{status}
                    AND equipment_specific_id = #{equipmentSpecificId}
                ORDER BY
                    create_date DESC

        ]]>
    </select>

    <select id="findEquipmentSpecificAlarmByEquipmentSpecificIdAndEquipmentIndexIdAndStatusIstrue"
            resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarm">
    	SELECT
			*
		FROM
			wl_equipment_specific_alarm
		WHERE
			STATUS = 1
		AND equipment_index_id = #{equipmentIndexId}
		AND equipment_specific_id = #{equipmentSpecificId}
    </select>

    <select id="checkIfExistByIotCode" resultType="int">
        SELECT
        count(1)
        FROM
        wl_equipment_specific_alarm
        WHERE
        status = 1 and iot_code = #{iotCode}
    </select>
    <select id="getAlarmByEquipmentSpecificIdAndEquipmentIndexId"
            resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarm">
        SELECT
        *
        FROM
        wl_equipment_specific_alarm
        WHERE
        STATUS = 1
		AND equipment_index_id = #{equipmentIndexId}
		AND equipment_specific_id = #{equipmentSpecificIndexId}
    </select>
    <select id="getAlarmByIotCode" resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarm">
        SELECT
        *
        FROM
        wl_equipment_specific_alarm
        WHERE
        iot_code = #{iotCode}
    </select>
    <select id="page" resultType="java.util.HashMap">
        SELECT
	        wlesal.id,
	        wlesal.org_code,
	        wlesal.equipment_specific_id AS fireEquipmentId,
	        wles.equipment_detail_id AS equipDetailId,
	        wles.code AS fireEquipmentCode,
	        wles.system_id AS systemId,
            wlesal.equipment_specific_name as fireEquipmentName,
            wlesal.equipment_specific_index_name as alamContent,
	        if(wlesal.confirm_type is null,'未处理','已处理') handleStatus,
	        IF (
	        wlesal.clean_time IS NOT NULL,
	        '已消除',
	        '未消除'
	        ) cleanStatus,
	        confirm_type as handleType,
	        wlesal.equipment_index_id AS fireEquipmentIndexId,
	        wlesal.equipment_specific_index_key AS fireEquipmentSpecificIndexKey,
	        wlesal.equipment_specific_index_name AS fireEquipmentSpecificIndexName,
	        wles.warehouse_structure_id AS buildId,
	        we.img AS imgUrl,
	        CASE
	        wlesal.equipment_specific_index_value
	        WHEN 'true' THEN '是'
	        WHEN 'false' THEN '否'
	        ELSE wlesal.equipment_specific_index_value END
	        AS fireEquipmentPointValue,
	        wlesal.type AS typeCode,
	        (select type_name from wl_signal_classify sc where sc.type_code = wlesal.type limit 1) as type,
            date_format(
            wlesal.create_date,
            '%Y-%m-%d %H:%i:%s'
            ) createDate,
	        CONCAT_WS(' ', wles.position,wled.area) AS warehouseStructureName, /*告警列表拼接详细地址*/
	        (select
	        group_concat(fet.`name`)
	        from f_equipment_fire_equipment as fefe
	        left join f_equipment as fet on fet.id = fefe.equipment_id
	        where fefe.fire_equipment_id = wlesal.equipment_specific_id
	        ) as equipmentName,
	        wlesal.equipment_specific_name as equipmentSpecificName,
	        wles.position,
            wles.biz_org_name as bizOrgName
        FROM wl_equipment_specific_alarm_log wlesal
        LEFT JOIN wl_equipment_specific wles ON wlesal.equipment_specific_id = wles.id
        LEFT JOIN wl_equipment_detail wled ON wles.equipment_detail_id = wled.id
        LEFT JOIN wl_equipment we ON wled.equipment_id = we.id
        <where>
            <if test="param.warehouseStructureName != null and param.warehouseStructureName != ''">
                CONCAT_WS(' ', wles.position,wled.area) like
                concat(concat('%',#{param.warehouseStructureName}),'%')
            </if>
            <if test="param.equipCode != null and param.equipCode != ''">AND wles.code like
                concat(concat('%',#{param.equipCode}),'%')
            </if>
            <if test="param.beginDate!=null">AND wlesal.create_date <![CDATA[>=]]> #{param.beginDate}</if>
            <if test="param.endDate!=null">AND wlesal.create_date <![CDATA[<=]]> #{param.endDate}</if>
            <if test="param.alarmType == 'BREAKDOWN'">AND wlesal.type = #{param.alarmType}</if>
            <if test="param.alarmType == 'FIREALARM'">AND wlesal.type = #{param.alarmType}</if>
            <if test="param.isFireAlarm == 'false'">AND wlesal.type != 'FIREALARM'</if>
            <if test="param.orgCode != null and param.orgCode != ''">AND wlesal.org_code = #{param.orgCode}</if>
            <if test="param.type != null and param.type != ''">AND wlesal.type = #{param.type}</if>
            <if test="param.handleStatus != null and param.handleStatus != '' and param.handleStatus == 1">AND
                wlesal.confirm_type IS NOT NULL
            </if>
            <if test="param.handleStatus != null and param.handleStatus != '' and param.handleStatus == 2">AND
               wlesal.confirm_type IS NULL
            </if>
            <if test="param.system != null and param.system != ''">
                AND find_in_set(#{param.system},wles.system_id)
            </if>
            <if test="param.buildIds!=null">
--                 AND wlesal.build_id IN
                AND wles.warehouse_structure_id IN
                <foreach item="item" collection="param.buildIds" separator=","
                         open="(" close=")" index="">#{item}
                </foreach>
            </if>
            <if test="param.id!=null and param.id!=''">AND wlesal.equipment_specific_id = #{param.id}</if>
            <if test="param.cleanStatus != null and param.cleanStatus != '' and param.cleanStatus == 1">AND
                wlesal.clean_time IS NOT NULL
            </if>
            <if test="param.systemCode != null and param.systemCode != ''">AND
                wlesal.system_codes = #{systemCode}
            </if>
            <if test="param.cleanStatus != null and param.cleanStatus != '' and param.cleanStatus == 2">AND
                 wlesal.clean_time IS NULL
            </if>
            <if test="param.isRemoveShield != null and param.isRemoveShield != ''">AND
                wlesal.type != 'SHIELD'
            </if>
            <if test="param.bizOrgCode != null and param.bizOrgCode != ''">AND
                wles.biz_org_code like concat (#{param.bizOrgCode},'%')
            </if>
        </where>
        ORDER BY wlesal.create_date DESC

    </select>
    <select id="zlzxPage" resultType="java.util.HashMap">
        SELECT
	        wlesal.id,
	        wlesal.equipment_specific_id AS equipmentSpecificId,
	        concat(wlesal.equipment_specific_name,wlesal.equipment_specific_index_name) as alamContent,
	        if(wlesal.confirm_type is null,'未处理','已处理') handleStatus,
	        IF (
	        wlesal.clean_time IS NOT NULL,
	        '已消除',
	        '未消除'
	        ) cleanStatus,
	        confirm_type as handleType,
	        wlesal.type AS typeCode,
	        (select type_name from wl_signal_classify sc where sc.type_code = wlesal.type limit 1) as type,
            date_format(
            wlesal.create_date,
            '%Y-%m-%d %H:%i:%s'
            ) createDate,
	        wlesal.equipment_specific_name as equipmentSpecificName,
            wlesal.location,
            wlesal.station_code AS stationCode,
            wlesal.biz_org_name AS stationName
        FROM wl_equipment_specific_alarm_log wlesal
        <where>
            wlesal.clean_time IS NULL
            <if test="param.alarmType == 'BREAKDOWN'">AND wlesal.type = #{param.alarmType}</if>
            <if test="param.alarmType == 'FIREALARM'">AND wlesal.type = #{param.alarmType}</if>
            <if test="param.isFireAlarm == 'false'">AND wlesal.type != 'FIREALARM'</if>
        </where>
        ORDER BY wlesal.create_date DESC
    </select>
    <select id="pageQuery" resultType="java.util.HashMap">
        SELECT
            `wlesal`.`equipment_specific_alarm_id` AS `id`,
            `wlesal`.id as alarmLogId,
            `wles`.`code` AS `code`,
            `wlesal`.`equipment_specific_name` AS `specificName`,
            `wlesal`.`equipment_specific_index_key` AS `indexKey`,
            `wlesal`.`equipment_specific_index_name` AS `indexName`,
            `wlesal`.`equipment_specific_id` AS `specificId`,
            `wlesal`.emergency_level_color as levelColor,
            IF
                ( `wlesal`.`clean_time` IS NOT NULL, '已消除', '未消除' ) AS `cleanStatus`,
            `wlesal`.`iot_code` AS `iotCode`,
            CONVERT ( `wlesal`.`create_date`, CHAR ) AS `createDate`
        FROM
            `wl_equipment_specific_alarm_log` `wlesal`
                LEFT JOIN `wl_equipment_specific` `wles` ON `wlesal`.`equipment_specific_id` = `wles`.`id`
        <where>
            <if test="param.system != null and param.system != ''">
                find_in_set( #{param.system}, `wlesal`.`system_codes` )
            </if>
            <if test="param.bizOrgCode != null and param.bizOrgCode != ''">
                AND wles.biz_org_code like concat (#{param.bizOrgCode},'%')
            </if>
        </where>
        ORDER BY
            `wlesal`.`create_date` DESC
    </select>
    <select id="pageAlarmsInfo" resultType="Map">
        SELECT
            wlesal.id AS alarmId,
            wlesal.org_code AS orgCode,
            wlesal.equipment_specific_id AS fireEquipmentId,
            wlesal.equipment_detail_id AS equipDetailId,
            wlesal.equipment_specific_code AS fireEquipmentCode,
            wlesal.equipment_specific_name AS fireEquipmentName,
            concat(
            wlesal.equipment_specific_name,
            wlesal.equipment_specific_index_name
            ) AS alamContent,
            IF (
            wlesal.confirm_type <![CDATA[<>]]> '',
            '已处理',
            '未处理'
            ) handleStatus,
            IF (
            wlesal.clean_time IS NOT NULL,
            '已消除',
            '未消除'
            ) cleanStatus,
            IF (
            wlesal.clean_time IS NOT NULL,
            '1',
            '2'
            ) cleanStatusVal,
            wlesal.confirm_type AS handleType,
            wlesal.system_codes AS systemCodes,
            wlesal.equipment_index_id AS fireEquipmentIndexId,
            wlesal.equipment_specific_index_key AS fireEquipmentSpecificIndexKey,
            wlesal.equipment_specific_index_name AS fireEquipmentSpecificIndexName,
            we.img AS imgUrl,
            CASE wlesal.equipment_specific_index_value
            WHEN 'true' THEN
            '是'
            WHEN 'false' THEN
            '否'
            ELSE
            wlesal.equipment_specific_index_value
            END AS fireEquipmentPointValue,
            wlesa.frequency AS frequency,
            wlesal.status AS status,
            wlesal.type AS `type`,
            (select  type_name from wl_signal_classify sc where sc.type_code = wlesal.type limit 1) as alarmType,
            wlesal.create_date AS createDate,
            wlesal.build_id AS buildId,
            wlesa.recovery_date AS recoveryDate,
            wlesal.location AS warehouseStructureName,
             <!-- (
            SELECT
            group_concat(fet.`name`)
            FROM
            f_equipment_fire_equipment AS fefe
            LEFT JOIN f_equipment AS fet ON fet.id = fefe.equipment_id
            WHERE
            fefe.fire_equipment_id = wlesal.equipment_specific_id
            )  页面未使用该字段返回值，不进行查询，该字段查询影响查询效率-->
            '' AS equipmentName,
            wlesal.equipment_code AS equipmentCode
        FROM
            wl_equipment_specific_alarm_log wlesal
            LEFT JOIN wl_equipment we ON wlesal.equipment_code = we.code
            LEFT JOIN wl_equipment_specific_alarm wlesa ON wlesa.id = wlesal.equipment_specific_alarm_id
            LEFT JOIN wl_equipment_specific wls ON wls.id = wlesal.equipment_specific_id
        <where>
        	<if test="param.bizOrgCode != null and param.bizOrgCode != ''">AND
                wls.biz_org_code like  concat (#{param.bizOrgCode},'%')
            </if>
            <if test="param.alarmLogId != null">
                and wlesal.id = #{param.alarmLogId}
            </if>
            <if test="param.warehouseStructureName != null and param.warehouseStructureName != ''">
                and wlesal.location like concat(concat('%',#{param.warehouseStructureName}),'%')
            </if>
            <if test="param.equipCode != null and param.equipCode != ''">
                AND wlesal.equipment_specific_code like concat(concat('%',#{param.equipCode}),'%')
            </if>
            <if test="param.confirmType != null and param.confirmType != '' and param.confirmType == 1">
                AND wlesal.confirm_type  <![CDATA[<>]]> ''
            </if>
            <if test="param.confirmType != null and param.confirmType != '' and param.confirmType == 0">
                AND wlesal.confirm_type IS NULL
            </if>
            <if test="param.confirmType != null and param.confirmType != '' and param.confirmType == 2">
                AND wlesal.clean_time IS NULL
            </if>
            <if test="param.confirmType != null and param.confirmType != '' and param.confirmType == 3">
                AND wlesal.clean_time IS NOT NULL
            </if>
            <if test="param.beginDate!=null">AND wlesal.create_date <![CDATA[>=]]> #{param.beginDate}</if>
            <if test="param.endDate!=null">AND wlesal.create_date <![CDATA[<=]]> #{param.endDate}</if>
            <if test="param.alarmType != null and param.alarmType != ''">AND wlesal.type = #{param.alarmType}</if>
            <if test="param.systemCode != null and param.systemCode != ''">
                AND find_in_set(#{param.systemCode}, wlesal.system_codes)
            </if>
            <if test="param.buildId != null and param.buildId != ''">
                and (wlesal.build_id=#{param.buildId}
                or find_in_set(wlesal.equipment_specific_id, (SELECT s.point_in_scene FROM `wl_source_scene` s where s.source_id =
                #{param.buildId})))
            </if>
            <if test="param.id!=null and param.id!=''">AND wlesal.equipment_specific_id = #{param.id}</if>
            <if test="param.status!=null and param.status!=3">AND wlesal.status = #{param.status}</if>
            <if test="param.cleanStatus != null and param.cleanStatus != '' and param.cleanStatus == 1">AND
                wlesal.clean_time IS NOT NULL
            </if>
            <if test="param.cleanStatus != null and param.cleanStatus != '' and param.cleanStatus == 2">AND
                wlesal.clean_time IS NULL
            </if>
            <if test="param.isRemoveShield != null and param.isRemoveShield != ''">AND
                wlesal.type != 'SHIELD'
            </if>
            <if test="param.isRemovedFire != null and param.isRemovedFire == 1">AND
                wlesal.type != 'FIREALARM'
            </if>
            <if test="param.equipmentCode != null and param.equipmentCode != ''">
                AND wlesal.equipment_code like concat(concat('%',#{param.equipmentCode}),'%')
            </if>
            <if test="param.equipDetailCode != null and param.equipDetailCode != ''">
                AND wlesal.equipment_code like concat(concat('%',#{param.equipDetailCode}),'%')
            </if>
            <if test="param.indexTypeCode != null and param.indexTypeCode != ''">
                AND wlesal.type = #{param.indexTypeCode}
            </if>
            <if test="param.fireEquipmentSpecificIndexKey != null and param.fireEquipmentSpecificIndexKey != ''">
                AND wlesal.equipment_specific_index_key like concat(concat('%',#{param.fireEquipmentSpecificIndexKey}),'%')
            </if>
        </where>
            ORDER BY wlesal.create_date DESC
    </select>
    <select id="getAlarmList" resultType="java.util.HashMap">
        SELECT
        d.*
        FROM
        (
        SELECT
        wlesal.id AS id,
        wlesal.equipment_specific_id AS fireEquipmentId,
        wles.equipment_detail_id AS equipDetailId,
        wles.`code` AS fireEquipmentCode,

        IF (
        wles.`code` IS NULL,
        wlesal.equipment_specific_name,
        concat(
        wlesal.equipment_specific_name,
        '(',
        wles.`code`,
        ')'
        )
        ) AS fireEquipmentName,
        concat(
        wlesal.equipment_specific_name,
        wlesal.equipment_specific_index_name
        ) AS alarmContent,
        IF (
        wlesal.clean_time IS NOT NULL,
        '已消除',
        '未消除'
        ) cleanStatus,
        IF (
        wlesal.confirm_type IS NULL,
        '未处理',
        '已处理'
        ) handleStatus,
        wlesal.confirm_type AS handleType,
        wlesal.equipment_index_id AS fireEquipmentIndexId,
        wlesal.equipment_specific_index_key AS fireEquipmentSpecificIndexKey,
        wlesal.equipment_specific_index_name AS fireEquipmentSpecificIndexName,
        wlesal.resolve_result as resolveResult,
        wlesal.confirm_date as confirmDate,
        wlesal.alarm_reason as alarmReason,
        CASE wlesal.equipment_specific_index_value
        WHEN 'true' THEN
        '是'
        WHEN 'false' THEN
        '否'
        ELSE
        wlesal.equipment_specific_index_value
        END AS fireEquipmentPointValue,
        wlesal.type AS type,
        wlesal.create_date AS createDate,
        wlesal.location  AS warehouseStructureName,
        (
        SELECT
        group_concat(fet.`name`)
        FROM
        f_equipment_fire_equipment AS fefe
        LEFT JOIN f_equipment AS fet ON fet.id = fefe.equipment_id
        WHERE
        fefe.fire_equipment_id = wlesal.equipment_specific_id
        ) AS equipmentName
        FROM
        wl_equipment_specific_alarm_log wlesal
        LEFT JOIN wl_equipment_specific wles ON wlesal.equipment_specific_id = wles.id
        ) d
        <where>
            <if test="warehouseStructureName != null and warehouseStructureName != ''">
                 d.warehouseStructureName like
                concat(concat('%',#{warehouseStructureName}),'%')
            </if>
            <if test="equipCode != null and equipCode != ''">AND d.fireEquipmentCode like
                concat(concat('%',#{equipCode}),'%')
            </if>
            <if test="beginDate!=null">AND d.createDate >= #{beginDate}</if>
            <if test="endDate!=null">AND d.createDate <![CDATA[<=]]> #{endDate}</if>
            <if test="alarmType != null and alarmType != ''">AND d.type = #{alarmType}</if>
            <if test="id != null and id != ''">AND d.id = #{id}</if>
            <if test="isFireAlarm == 'false'">AND d.type != 'FIREALARM'</if>
        </where>
        ORDER BY d.createDate DESC
    </select>
    <select id="getAlarmDetail" resultType="java.util.HashMap">
       SELECT
        d.*
        FROM
        (
        SELECT
        wlesa.id,
        wlesa.equipment_specific_id AS fireEquipmentId,
        wles.equipment_detail_id AS equipDetailId,
        wles.code AS fireEquipmentCode,
        if(wles.code is null,wlesa.equipment_specific_name,concat(wlesa.equipment_specific_name,'(',wles.code,')')) as fireEquipmentName,
        concat(wlesa.equipment_specific_name,wlesa.equipment_specific_index_name) as alamContent,
        if(confirm_type is null,'去确认','已处理') handleStatus,
        confirm_type as handleType,
        wlesa.equipment_index_id AS fireEquipmentIndexId,
        wlesa.equipment_specific_index_key AS fireEquipmentSpecificIndexKey,
        wlesa.equipment_specific_index_name AS fireEquipmentSpecificIndexName,
        we.img AS imgUrl,
        CASE
        wlesa.equipment_specific_index_value
        WHEN 'true' THEN '是'
        WHEN 'false' THEN '否'
        ELSE wlesa.equipment_specific_index_value END
        AS fireEquipmentPointValue,
        wlesa.frequency AS frequency,
        wlesa.status AS status,
        wlesa.type AS type,
        wlesa.create_date AS createDate,
        wlesa.recovery_date AS recoveryDate,
        CONCAT_WS(' ',ware.full_name,wlsd.description) AS warehouseStructureName,
        (select
        group_concat(fet.`name`)
        from f_equipment_fire_equipment as fefe
        left join f_equipment as fet on fet.id = fefe.equipment_id
        where fefe.fire_equipment_id = wlesa.equipment_specific_id
        ) as equipmentName
        FROM wl_equipment_specific_alarm wlesa
        LEFT JOIN wl_equipment_specific wles ON wlesa.equipment_specific_id = wles.id
 		left join wl_stock_detail wlsd ON wlsd.equipment_specific_id=wles.id
        left join wl_warehouse_structure as ware on wlsd.warehouse_structure_id = ware.id
        LEFT JOIN wl_equipment_detail wled ON wles.equipment_detail_id = wled.id
        LEFT JOIN wl_equipment we ON wled.equipment_id = we.id
        ) d
        WHERE
         d.id = #{id}
        AND d.type = #{alarmType}
        ORDER BY d.createDate DESC
    </select>
    <select id="getEquipmentDetails" resultType="java.util.HashMap">
        SELECT
	    d.*
        FROM
        (
        SELECT
            wlesa.id,
            wlesa.equipment_specific_id AS equipmentSpecificId,
            wles.equipment_detail_id AS equipDetailId,
            wled.`name` AS fireEquipmentName,
            we.img AS imgUrl,
            wlesa.type AS type,
            wled.brand AS brand,
            wled.standard AS batchNum,
            wles.qr_code AS qrCode,
            (
                SELECT
                    GROUP_CONCAT(ffs.name)
                FROM
                    f_fire_fighting_system ffs
                WHERE
                    find_in_set(ffs.id, wles.system_id)
            ) AS systemName,
            CONCAT_WS(' ',ware.full_name,wlsd.description) AS warehouseStructureName
        FROM
            wl_equipment_specific_alarm wlesa
            LEFT JOIN wl_equipment_specific wles ON wlesa.equipment_specific_id = wles.id
	   		left join wl_stock_detail wlsd ON wlsd.equipment_specific_id=wles.id
	        left join wl_warehouse_structure as ware on wlsd.warehouse_structure_id = ware.id
            LEFT JOIN wl_equipment_detail wled ON wles.equipment_detail_id = wled.id
            LEFT JOIN wl_equipment we ON wled.equipment_id = we.id
        ) d
        WHERE
        d.equipmentSpecificId = #{id}
        AND d.type = #{alarmType}
    </select>
    <select id="getEquipmentAlarmCount" resultType="int">
        SELECT
	    count(1)
        FROM
        (
        SELECT
            wlesa.id,
            wlesa.type,
            wled.name  as fireEquipmentName,
            wles.id as equipmentSpecificId
        FROM
            wl_equipment_specific_alarm wlesa
            LEFT JOIN wl_equipment_specific wles ON wlesa.equipment_specific_id = wles.id
            LEFT JOIN wl_stock_detail wlsd ON wlsd.equipment_specific_id = wles.id
            LEFT JOIN wl_warehouse_structure wws ON wlsd.warehouse_structure_id = wws.id
            LEFT JOIN wl_equipment_detail wled ON wles.equipment_detail_id = wled.id
            LEFT JOIN wl_equipment we ON wled.equipment_id = we.id
        ) d
        WHERE d.equipmentSpecificId = #{id}
        AND d.type = #{alarmType}
    </select>
    <select id="findByTypeAndDate" resultMap="resultMapDto">
        SELECT
        d.id,
        d.equipmentSpecificId,
        d.fireEquipmentCode,
        d.fireEquipmentName,
        d.warehouseStructureName,
        d.fireEquipmentSpecificIndexName,
        d.createDate,
        d.`status`,
        d.type,
        d.confirmType
        FROM
        (
        SELECT
        wlesa.id,
        wles.id as equipmentSpecificId,
        wlesa.equipment_specific_id AS fireEquipmentId,
        wles.CODE AS fireEquipmentCode,
        wled.`name` AS fireEquipmentName,
        wlesa.equipment_index_id AS fireEquipmentIndexId,
        wlesa.equipment_specific_index_key AS fireEquipmentSpecificIndexKey,
        wlesa.equipment_specific_index_name AS fireEquipmentSpecificIndexName,
        CASE
        WHEN wlesa.confirm_type IS NULL THEN
        0
        ELSE 1
        END AS confirmType,
        CASE
        wlesa.equipment_specific_index_value
        WHEN 'true' THEN
        '是'
        WHEN 'false' THEN
        '否' ELSE wlesa.equipment_specific_index_value
        END AS fireEquipmentPointValue,
        wlesa.frequency AS frequency,
        wlesa.`status`,
        wlesa.type AS type,
        wlesa.create_date AS createDate,
        CONCAT_WS(' ',wws.full_name,wlsd.description) AS warehouseStructureName,
        (select
        group_concat(fet.`name`)
        from f_equipment_fire_equipment as fefe
        left join f_equipment as fet on fet.id = fefe.equipment_id
        where fefe.fire_equipment_id = wlesa.equipment_specific_id
        ) as equipmentName
        FROM
        wl_equipment_specific_alarm wlesa
        LEFT JOIN wl_equipment_specific wles ON wlesa.equipment_specific_id = wles.id
        LEFT JOIN wl_stock_detail wlsd ON wlsd.equipment_specific_id = wles.id
        LEFT JOIN wl_warehouse_structure wws ON wlsd.warehouse_structure_id = wws.id
        LEFT JOIN wl_equipment_detail wled ON wles.equipment_detail_id = wled.id
        <where>
            <if test="dto.status != null and dto.status != ''">
                wlesa.`status`= #{dto.status}
            </if>
            <if test='dto.createDate != null and dto.createDate == "1"'>
                AND wlesa.create_date LIKE CONCAT( DATE_FORMAT( NOW( ), '%Y-%m-%d' ), '%' )
            </if>
           <if test='dto.createDate != null and dto.createDate == "7"'>
                AND wlesal.create_date > CONCAT( DATE_ADD( DATE_FORMAT( NOW( ), '%Y-%m-%d ' ), INTERVAL - 7 DAY ), ' 23:59:59' )
            </if>
            <if test='dto.createDate != null and dto.createDate == "30"'>
                 AND wlesal.create_date > CONCAT( DATE_ADD( DATE_FORMAT( NOW( ), '%Y-%m-%d ' ), INTERVAL - 30 DAY ),' 23:59:59' )
            </if>
            <if test="dto.createDate != null and dto.createDate.length() > 7">
                AND wlesa.create_date LIKE CONCAT( #{dto.createDate}, '%' )
            </if>
            <if test="dto.isFirm == 'yes' ">
                and wlesa.confirm_type is not null
            </if>
            <if test="dto.isFirm == 'no' ">
                and wlesa.confirm_type is null
            </if>
        </where>
        ) d
        <where>
            <choose>
                <when test="dto.type != null and dto.type != ''">
                    AND d.type = #{dto.type}
                </when>
                <otherwise>
                    AND ( d.type = 'FIREALARM' OR d.type = 'BREAKDOWN' OR d.type = 'SHIELD' )
                </otherwise>
            </choose>
        </where>
        ORDER BY
        d.createDate DESC
    </select>
    <select id="getFireEquipAlarmDetailsByAlarmId"
            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,
        NULL 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.equipment_specific_alarm_id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="indexIds != null and indexIds.size > 0">
                AND si.equipment_index_id IN
                <foreach collection="indexIds" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="findInfoByTypeAndDate" resultMap="resultMapDto">
        SELECT
        d.id,
        d.equipmentSpecificId,
        d.fireEquipmentCode,
        d.fireEquipmentName,
        d.warehouseStructureName,
        d.fireEquipmentSpecificIndexName,
        d.createDate,
        d.`status`,
        d.type,
        d.confirmType
        FROM
        (
        SELECT
        wlesal.id,
        wles.id as equipmentSpecificId,
        wlesal.equipment_specific_id AS fireEquipmentId,
        wles.CODE AS fireEquipmentCode,
        wled.`name` AS fireEquipmentName,
        wlesal.equipment_index_id AS fireEquipmentIndexId,
        wlesal.equipment_specific_index_key AS fireEquipmentSpecificIndexKey,
        wlesal.equipment_specific_index_name AS fireEquipmentSpecificIndexName,
        CASE
        WHEN wlesal.confirm_type IS NULL THEN
        0
        ELSE 1
        END AS confirmType,
        CASE
        wlesal.equipment_specific_index_value
        WHEN 'true' THEN
        '是'
        WHEN 'false' THEN
        '否' ELSE wlesal.equipment_specific_index_value
        END AS fireEquipmentPointValue,
        wlesa.frequency AS frequency,
        wlesa.`status`,
        wlesal.type AS type,
        wlesal.create_date AS createDate,
        CONCAT_WS(' ', wlesal.location,wled.area) AS warehouseStructureName,/*告警列表拼接详细地址*/
        (select
        group_concat(fet.`name`)
        from f_equipment_fire_equipment as fefe
        left join f_equipment as fet on fet.id = fefe.equipment_id
        where fefe.fire_equipment_id = wlesal.equipment_specific_id
        ) as equipmentName
        FROM
        wl_equipment_specific_alarm_log wlesal
        LEFT JOIN wl_equipment_specific wles ON wlesal.equipment_specific_id = wles.id
        LEFT JOIN wl_stock_detail wlsd ON wlsd.equipment_specific_id = wles.id
        LEFT JOIN wl_equipment_detail wled ON wles.equipment_detail_id = wled.id
        LEFT JOIN wl_equipment_specific_alarm wlesa ON wlesa.id = wlesal.equipment_specific_alarm_id
        <where>
            <if test="dto.status != null and dto.status != ''">
                wlesa.`status`= #{dto.status}
            </if>
            <if test='dto.createDate != null and dto.createDate == "1"'>
                AND wlesal.create_date LIKE CONCAT( DATE_FORMAT( NOW( ), '%Y-%m-%d' ), '%' )
            </if>
            <if test='dto.createDate != null and dto.createDate == "7"'>
                AND wlesal.create_date BETWEEN CONCAT( DATE_ADD( DATE_FORMAT( NOW( ), '%Y-%m-%d ' ), INTERVAL - 7 DAY ),
                '00:00:00' )
                AND CONCAT( DATE_FORMAT( NOW( ), '%Y-%m-%d ' ), '%', '23:59:59' )
            </if>
            <if test='dto.createDate != null and dto.createDate == "30"'>
                AND wlesal.create_date BETWEEN CONCAT( DATE_ADD( DATE_FORMAT( NOW( ), '%Y-%m-%d ' ), INTERVAL - 30 DAY
                ), '00:00:00' )
                AND CONCAT( DATE_FORMAT( NOW( ), '%Y-%m-%d ' ), '%', '23:59:59' )
            </if>
            <if test="dto.createDate != null and dto.createDate.length() > 7">
                AND wlesal.create_date LIKE CONCAT( #{dto.createDate}, '%' )
            </if>
            <if test="dto.isFirm == 'yes' ">
                and wlesal.confirm_type is not null
            </if>
            <if test="dto.isFirm == 'no' ">
                and wlesal.confirm_type is null
            </if>
        </where>
        ) d
        <where>
            <choose>
                <when test="dto.type != null and dto.type != ''">
                    AND d.type = #{dto.type}
                </when>
                <otherwise>
                    AND ( d.type = 'FIREALARM' OR d.type = 'BREAKDOWN' OR d.type = 'SHIELD' )
                </otherwise>
            </choose>
        </where>
        ORDER BY
        d.createDate DESC
        LIMIT 10
    </select>

    <select id="getProObject" resultType="java.util.HashMap">
        select
        fet.*
        from f_equipment_fire_equipment as fefe
        left join f_equipment as fet on fet.id = fefe.equipment_id
        where fefe.fire_equipment_id = #{id}
    </select>


    <select id="getCountAlarm" resultType="java.util.HashMap">
        select
        count(wl_equipment_specific_alarm.id) num,
        wl_equipment_specific_alarm.type
        from  wl_equipment_specific_alarm where  wl_equipment_specific_alarm.`status`=1
        GROUP BY wl_equipment_specific_alarm.type

    </select>

    <select id="getCountAlarmEquipment" resultType="int">
       select
       count(wl_equipment_specific_alarm.id) num
       from  wl_equipment_specific_alarm where  wl_equipment_specific_alarm.`status`=1

    </select>

    <select id="getCountEquipment" resultType="int">
    select sum(wl_stock.amount) num  from  wl_equipment_detail
    LEFT JOIN (select DISTINCT  wl_equipment_index.equipment_id from wl_equipment_index where wl_equipment_index.is_iot=1) b on b.equipment_id=wl_equipment_detail.equipment_id
    LEFT JOIN wl_stock on wl_equipment_detail.id =wl_stock.equipment_detail_id
    </select>

    <select id="getAlarmPage" resultType="com.yeejoin.equipmanage.common.entity.dto.EquipSpecificAlarmDTO">
        SELECT
        d.id,
        d.equipment_specific_alarm_id,
        d.equipmentSpecificId,
        d.fireEquipmentCode,
        d.fireEquipmentName,
        d.warehouseStructureName,
        d.fireEquipmentSpecificIndexName,
        d.createDate,
        d.`status`,

--         (CASE WHEN d.type='FIREALARM' THEN '火警'
--         WHEN d.type ='BREAKDOWN' THEN '故障'
--         WHEN d.type='SHIELD' THEN '屏蔽'
--         WHEN d.type='NOTICE' THEN '预警'
--         ELSE '' END) AS type,

        (select type_name from wl_signal_classify  where type_code =d.type)AS type,

        d.confirmUserName,
        d.systemIds,
        d.confirmType,
        d.isFirm,
        d.confirm_date,
        d.alarm_reason,
        d.resolve_result,
        d.code
        FROM
        (
        SELECT
        wlesal.id,
        wlesal.equipment_specific_alarm_id,
        wles.id AS equipmentSpecificId,
        wlesal.equipment_specific_id AS fireEquipmentId,
        wles.CODE AS fireEquipmentCode,
        wled.`name` AS fireEquipmentName,
        wlesal.equipment_index_id AS fireEquipmentIndexId,
        wlesal.equipment_specific_index_key AS fireEquipmentSpecificIndexKey,
        wlesal.equipment_specific_index_name AS fireEquipmentSpecificIndexName,
        CASE
        WHEN wlesal.confirm_type <![CDATA[<>]]> ''
        THEN 'yes'
        ELSE 'no'
        END AS isFirm,
        wlesal.confirm_type confirmType,
        CASE
        wlesal.equipment_specific_index_value
        WHEN 'true' THEN
        '是'
        WHEN 'false' THEN
        '否' ELSE wlesal.equipment_specific_index_value
        END AS fireEquipmentPointValue,
        wlesa.frequency AS frequency,
        wlesa.`status`,
        wlesal.type AS type,
        wlesal.create_date AS createDate,
        wlesal.location AS warehouseStructureName,
        wlesal.confirm_user_name confirmUserName,
        wlesal.system_ids systemIds,
        (select
        group_concat(fet.`name`)
        from f_equipment_fire_equipment as fefe
        left join f_equipment as fet on fet.id = fefe.equipment_id
        where fefe.fire_equipment_id = wlesal.equipment_specific_id
        ) as equipmentName,
        wlesal.confirm_date,
        wlesal.alarm_reason,
        wlesal.resolve_result,
        wles.code
        FROM
--         wl_equipment_specific_alarm_log wlesal

        (SELECT *
        FROM (
        SELECT DISTINCT(s.id) tid, s.*  FROM `wl_equipment_specific_alarm_log` s
        ORDER BY s.create_date DESC
        ) t
        GROUP BY t.`equipment_specific_id`)
        wlesal


        LEFT JOIN wl_equipment_specific wles ON wlesal.equipment_specific_id = wles.id
        LEFT JOIN wl_stock_detail wlsd ON wlsd.equipment_specific_id = wles.id
        LEFT JOIN wl_equipment_detail wled ON wles.equipment_detail_id = wled.id
        LEFT JOIN wl_equipment_specific_alarm wlesa ON wlesa.id = wlesal.equipment_specific_alarm_id
        <where>
            <if test="dto.status != null and dto.status != ''">
                wlesa.`status`= #{dto.status}  <!--状态-->
            </if>
            <if test='dto.createDate != null and dto.createDate == "1"'>
                AND to_days(now()) = to_days(wlesal.create_date) <!--今天-->
            </if>
            <if test='dto.createDate != null and dto.createDate == "2"'>
                AND to_days(now()) - to_days(wlesal.create_date) = 1  <!--昨天-->
            </if>

            <if test='dto.createDate != null and dto.createDate == "3"'>
                AND wlesal.create_date &gt;= subdate(curdate(),date_format(curdate(),'%w')-1) and wlesal.create_date
                &lt;= subdate(curdate(),date_format(curdate(),'%w')-7) <!--本周-->
            </if>

            <if test='dto.createDate != null and dto.createDate == "4"'>
                AND wlesal.create_date &gt;= subdate(curdate(),date_format(curdate(),'%w')+6) and wlesal.create_date
                &lt;= subdate(curdate(),date_format(curdate(),'%w')-0)<!--上周-->
            </if>
            <if test='dto.createDate != null and dto.createDate == "5"'>
                AND date_format(wlesa.create_date,'%y-%m')=date_format(curdate(),'%y-%m')<!--本月-->
            </if>

            <if test='dto.createDate != null and dto.createDate == "6"'>
                AND date_format(wlesa.create_date,'%y-%m')=date_format(date_sub(curdate(), interval 1
                month),'%y-%m') <!--上月-->
            </if>
            <if test="dto.isFirm == 'yes'">
                and wlesal.confirm_type <![CDATA[<>]]> ''
            </if>
            <if test="dto.isFirm == 'no'">
                and wlesal.confirm_type is null
            </if>
        </where>
        ) d
        <where>
            <choose>
                <when test="dto.type != null and dto.type != ''">
                    AND d.type = #{dto.type}
                </when>
                <otherwise>


                    and  (d.type in (select type_code from wl_signal_classify where  is_alarm=1))
                </otherwise>
            </choose>
            <if test="dto.fireEquipmentName != null and dto.fireEquipmentName != '' ">
                AND d.fireEquipmentName LIKE CONCAT('%', #{dto.fireEquipmentName}, '%' )
            </if>
            <if test='dto.systemIds != null  '>
                AND d.systemIds in (#{dto.systemIds})
            </if>
            <if test="dto.confirmUserName != null  and dto.confirmUserName != '' ">
                AND d.confirmUserName LIKE CONCAT('%', #{dto.confirmUserName}, '%' )
            </if>
            <if test="dto.code != null">
                and d.code like CONCAT('%', #{dto.code}, '%' )
            </if>
            <if test="dto.fireEquipmentCode != null">
                and d.fireEquipmentCode like CONCAT('%', #{dto.fireEquipmentCode}, '%' )
            </if>
            <if test='dto.startDate != null and dto.startDate != "" and dto.endDate  != null and dto.endDate != ""'>
                and d.createDate BETWEEN #{dto.startDate} and #{dto.endDate}
            </if>
        </where>
        ORDER BY
        d.createDate DESC
    </select>

    <select id="getEqPage" resultType="com.yeejoin.equipmanage.common.entity.dto.EquipSpecificAlarmDTO">
        select * from (
        select
        b.address warehouseStructureName,
        b.eqname fireEquipmentName,
        b.code fireEquipmentCode,
        b.eqimg eqimg,
        b.alarmTime alarmTime,
        b.qrCode code,
        IFNULL(( select DISTINCT wl_equipment_specific_alarm.`status` from wl_equipment_specific_alarm where
        wl_equipment_specific_alarm.`status`=1 and wl_equipment_specific_alarm.equipment_detail_id=b.eqid ), 0) `status`
        from
        (select
        wl_warehouse_structure.full_name address,
        wl_equipment_detail.name eqname,
        wl_equipment_specific.code code,
        wl_stock_detail.qr_code qrCode,
        wl_equipment.img eqimg,
        wl_equipment_detail.id eqid,
        wl_equipment_specific.system_id,
--         wl_equipment_specific_alarm.create_date alarmTime
        alarm.create_date alarmTime
        from wl_equipment_detail
        LEFT JOIN wl_equipment_specific ON wl_equipment_specific.equipment_detail_id = wl_equipment_detail.id
        LEFT JOIN (select DISTINCT wl_equipment_index.equipment_id from wl_equipment_index where
        wl_equipment_index.is_iot=1) b on b.equipment_id=wl_equipment_detail.equipment_id
        LEFT JOIN wl_stock_detail on wl_equipment_detail.id =wl_stock_detail.equipment_detail_id
        LEFT JOIN wl_warehouse_structure on wl_stock_detail.warehouse_structure_id = wl_warehouse_structure.id
        LEFT JOIN wl_equipment on wl_equipment.id = wl_equipment_detail.equipment_id
      --  LEFT JOIN wl_equipment_specific_alarm on wl_equipment_specific_alarm.equipment_detail_id=wl_equipment_detail.id


        LEFT JOIN
        (select * from wl_equipment_specific_alarm as t
        where t.id  in
        (
        select SUBSTRING_INDEX(GROUP_CONCAT(id ORDER BY `create_date` DESC ),',',1) from wl_equipment_specific_alarm
        group by equipment_specific_id
        )
        ) alarm on alarm.equipment_detail_id=wl_equipment_detail.id


        where wl_equipment.is_iot = 1
        )b
        )m
        <where>
            <if test="dto.status != null and dto.status != ''">
                `status`= #{dto.status}
            </if>
            <if test="dto.warehouseStructureName != null and dto.warehouseStructureName != ''">
                AND warehouseStructureName LIKE CONCAT('%', #{dto.warehouseStructureName}, '%' )
            </if>
            <if test="dto.fireEquipmentCode != null and dto.fireEquipmentCode!= ''">
                AND fireEquipmentCode LIKE CONCAT('%', #{dto.fireEquipmentCode}, '%' )
            </if>
            <if test="dto.fireEquipmentName != null and dto.fireEquipmentName!= ''">
                AND fireEquipmentName LIKE CONCAT('%', #{dto.fireEquipmentName}, '%' )
            </if>
            <if test="dto.systemIds != null and dto.systemIds!= ''">
                AND systemIds in (#{dto.systemIds})
            </if>
        </where>
        ORDER BY alarmTime DESC,status DESC
    </select>

    <select id="getConfirmUser" resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarmLog">

        select *, count(distinct confirm_user) from  wl_equipment_specific_alarm_log  WHERE
            confirm_user IS NOT NULL group by confirm_user
    </select>

    <select id="getbyqrCode" resultType="com.yeejoin.equipmanage.common.entity.dto.EquipSpecificAlarmDTO">

       SELECT
         wl_signal_classify.type_name AS type,
        ( CASE WHEN d.update_date = NULL THEN d.create_date ELSE d.update_date END ) AS createDate
         FROM
        wl_equipment_specific_alarm d
        LEFT JOIN wl_equipment_specific on wl_equipment_specific.id=d.equipment_specific_id
        LEFT JOIN wl_signal_classify on wl_signal_classify.type_code=d.type
        where wl_equipment_specific.qr_code = #{code}
      ORDER BY d.create_date ,d.update_date  LIMIT 1

    </select>


    <select id="getcountAlarmtype" resultType="int">
        select
        count(wl_equipment_specific_alarm.id) num
        from wl_equipment_specific_alarm where wl_equipment_specific_alarm.`status`=1
        <if test="tyep != null and tyep!= ''">
            AND wl_equipment_specific_alarm.type = #{tyep}
        </if>


    </select>

    <select id="getcountAlarmHandle" resultType="int">
        select
        count(wl_equipment_specific_alarm.id) num
        from wl_equipment_specific_alarm where wl_equipment_specific_alarm.`status`=1
        <if test="tyep == 'yes'">
            and wl_equipment_specific_alarm.confirm_type <![CDATA[<>]]> '' AND wl_equipment_specific_alarm.confirm_type
            IS NOT NULL
        </if>
        <if test="tyep == 'no'">
            and wl_equipment_specific_alarm.confirm_type is null
        </if>
    </select>

    <select id="selectCountByIotCode" resultType="int">
        SELECT
            count(*)
        FROM
            wl_equipment_specific_alarm
        WHERE
            iot_code = #{iotCode,jdbcType=VARCHAR}
          AND equipment_specific_index_value = #{indexValue,jdbcType=VARCHAR}
          AND equipment_specific_index_key = #{indexKey,jdbcType=VARCHAR}
          and `status` = 1
    </select>
    <select id="equipStatusCount" parameterType="java.util.HashMap" resultType="java.util.Map">
        SELECT
            '总数' AS `name`,
            COUNT( DISTINCT wles.id ) AS `value`
        FROM
            `wl_equipment_specific` `wles`
            LEFT JOIN f_fire_fighting_system fs ON FIND_IN_SET( fs.id, wles.system_id )
            LEFT JOIN wl_equipment_detail wed ON wed.id = wles.equipment_detail_id
            LEFT JOIN wl_equipment we ON we.id = wed.equipment_id
        <where>
            we.is_iot = 1
            <if test="systemCode != null and systemCode != ''">
                AND fs.code = #{systemCode}
            </if>
            <if test="bizOrgCode != null and bizOrgCode != ''">
                AND wles.biz_org_code like concat (#{bizOrgCode},'%')
            </if>
        </where>
        union all
          SELECT
            '异常' AS `name`,
            COUNT( DISTINCT wlesa.equipment_specific_id ) AS `value`
        FROM
            wl_equipment_specific_alarm wlesa
		LEFT JOIN wl_equipment_specific wes on wes.id = wlesa.equipment_specific_id
        LEFT JOIN wl_equipment_detail wed ON wed.id = wes.equipment_detail_id
        LEFT JOIN wl_equipment we ON we.id = wed.equipment_id
        <where>
           	wlesa.`status` = 1
            and we.is_iot = 1
            <if test="systemCode != null and systemCode != ''">
            	AND FIND_IN_SET( #{systemCode}, wlesa.system_codes )>0
            </if>
            <if test="bizOrgCode != null and bizOrgCode != ''">
                AND wes.biz_org_code like concat (#{bizOrgCode},'%')
            </if>
        </where>
    </select>
    <select id="equipAlarmLine" parameterType="java.util.HashMap" resultType="com.yeejoin.equipmanage.common.vo.EquipAlarmLineVo">
        SELECT
        s.`index`,
        s.date,
        IFNULL(s1.equipment_specific_index_name,'') AS indexName,
        IFNULL(s1.equipment_specific_index_key,'') AS `indexKey`,
        IFNULL(s1.`total`,0) AS total
        FROM
        (
        SELECT
        ( @s := @s + 1 ) + 1 AS `index`,
        DATE( DATE_SUB( CURRENT_DATE, INTERVAL @s DAY ) ) AS date
        FROM
        mysql.help_topic,
        ( SELECT @s := - 1 ) temp
        WHERE
        @s &lt; 6
        ) s
        LEFT JOIN (
        SELECT
        t.equipment_specific_index_name,
        t.equipment_specific_index_key,
        t.`total`,
        t.date
        FROM
        (
        SELECT
        wlesa.equipment_specific_id,
        wlesa.equipment_specific_index_name,
        wlesa.equipment_specific_index_key,
        COUNT( DISTINCT wlesa.equipment_specific_id ) AS `total`,
        LEFT ( wlesa.create_date, 10 ) AS date
        FROM
        `wl_equipment_specific_alarm_log` `wlesa`
        LEFT JOIN `wl_equipment_specific` `wles` ON `wlesa`.`equipment_specific_id` = `wles`.`id`
        LEFT JOIN f_fire_fighting_system fs ON FIND_IN_SET( fs.id, wles.system_id )
        <where>
            DATE_SUB( CURDATE( ), INTERVAL 7 DAY ) &lt;= date( wlesa.create_date )
--             AND wlesa.equipment_specific_index_value = 'true'
            <if test="systemCode != null and systemCode != ''">
                AND fs.code = #{systemCode}
            </if>
            <if test="bizOrgCode != null and bizOrgCode != ''">
                AND wles.biz_org_code like concat (#{bizOrgCode},'%')
            </if>
            <if test="indexKeys != null and indexKeys.length >0">
                AND wlesa.equipment_specific_index_key IN
                <foreach collection="indexKeys" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY
        wlesa.equipment_specific_id,
        wlesa.equipment_specific_index_key,
        LEFT ( wlesa.create_date, 10 )
        ) t
        GROUP BY
        t.equipment_specific_index_key,
        t.date
        ) s1 ON s.date = s1.date
        GROUP BY
        s.date,
        s1.equipment_specific_index_key
        ORDER BY
        s.date DESC
    </select>
    <select id="fireFacilitiesList" resultType="java.util.Map">

             SELECT temp.id,temp.name,IFNULL(temp1.notReturned,0) AS notReturned ,(IFNULL(temp2.total,0) - IFNULL(temp1.notReturned,0)) AS returned,
        CASE
            WHEN (temp1.notReturned  > 0 ) THEN
            "异常"
             ELSE "正常"
        END
             AS `status`
        FROM
        (SELECT ffs.id,ffs.name FROM f_fire_fighting_system ffs
            WHERE ffs.biz_org_code like concat(#{bizOrgCode} ,'%')
        ) temp
        LEFT JOIN
        (SELECT esa.system_ids,COUNT(DISTINCT(esa.iot_code)) AS notReturned
        FROM wl_equipment_specific_alarm esa
        WHERE esa.type = "BREAKDOWN"
        and esa.equipment_specific_index_value = "true" and esa.`status`=true  and esa.create_date BETWEEN #{startTime}  and  #{endTime}

        GROUP BY esa.system_ids)temp1
        on
         FIND_IN_SET(temp.id,temp1.system_ids)
        LEFT JOIN
        (SELECT esa.system_ids,COUNT(DISTINCT(esa.iot_code)) AS total
        FROM wl_equipment_specific_alarm esa
        WHERE esa.type = "BREAKDOWN" and esa.create_date BETWEEN #{startTime}  and  #{endTime}
        GROUP BY esa.system_ids)temp2
        on
         FIND_IN_SET(temp.id,temp2.system_ids)
    </select>
    <select id="dailyMaintenance" resultType="java.util.Map">

         SELECT temp.total,temp.notReturned, temp.alarmEquipNum,(temp.alarmEquipNum - temp.notReturned) AS returned
         FROM(
            select
         (SELECT
			count(1 )  AS total
		FROM
			wl_equipment_specific es
			LEFT JOIN wl_equipment_detail ed ON es.equipment_detail_id = ed.id
			LEFT JOIN wl_equipment e ON e.id = ed.equipment_id

		WHERE
			e.is_iot = 1  and  es.biz_org_code like concat(#{bizOrgCode},'%')
         )AS total,
        (
        SELECT COUNT(DISTINCT(esa.iot_code))  FROM wl_equipment_specific_alarm esa
        WHERE    esa.org_code =  #{orgCode}
             and update_date between #{startTime} and #{endTime}
        )AS alarmEquipNum,
        (
        SELECT COUNT(DISTINCT(esa.iot_code)) FROM wl_equipment_specific_alarm esa
        WHERE    esa.org_code =  #{orgCode}   AND esa.equipment_specific_index_value = "true"
        and update_date between #{startTime} and #{endTime}
        )as notReturned)temp
    </select>
    <select id="inspectionMessage" resultType="java.util.Map">
        SELECT
    (SELECT COUNT(DISTINCT(equipment_id))  FROM p_point_classify
    )  AS  total,
    (
    SELECT count(DISTINCT(point_id))  FROM p_check WHERE org_code like CONCAT(#{orgCode},'%')
    and check_time
    between #{startTime} and #{endTime}
    )as pointNum,
    (
    SELECT count(DISTINCT(ppc.equipment_id))  FROM

    (SELECT point_classify_id  FROM  p_check_input  WHERE  point_classify_id in(SELECT id FROM p_point_classify)
    and is_ok = 2 and org_code like CONCAT(#{orgCode},'%') and create_date
    between #{startTime} and #{endTime})
    pci
    LEFT JOIN p_point_classify ppc on pci.point_classify_id = ppc.id
    )AS unqualified,
    (
    SELECT count(DISTINCT(ppc.equipment_id))  FROM

    ( SELECT point_classify_id  FROM  p_check_input  WHERE  point_classify_id in(SELECT id FROM p_point_classify)
        and is_ok = 3 and org_code like CONCAT(#{orgCode},'%') and create_date
        between #{startTime} and #{endTime})
        pci
        LEFT JOIN p_point_classify ppc on pci.point_classify_id = ppc.id
    )AS missedCheck
    </select>

    <select id="fireAlarmLogPage" resultType="java.util.HashMap">
        SELECT
        `wlesal`.id as alarmLogId,
        ( SELECT type_name FROM wl_signal_classify sc WHERE sc.type_code = wlesal.type LIMIT 1 ) AS equipment_specific_index_name,
        `wlesal`.equipment_specific_name,
        DATE_FORMAT(`wlesal`.`create_date`,'%m-%d %H:%i:%s') alarm_time,
        wlesal.location,
        wlesal.emergency_level_color,
        IF
        ( `wlesal`.`clean_time` IS NOT NULL, '已消除', '未消除' ) AS `cleanStatus`,
        wlesal.station_name AS sationName
        FROM
        `wl_equipment_specific_alarm_log` `wlesal`
        LEFT JOIN `wl_equipment_specific_alarm` wesa ON wesa.id = wlesal.equipment_specific_alarm_id
        LEFT JOIN `wl_equipment_specific` `wles` ON `wlesal`.`equipment_specific_id` = `wles`.`id`
        <where>
            wesa.`status` = 1 AND wlesal.clean_time IS NULL
            <if test="param.system != null and param.system != ''">
                AND find_in_set( #{param.system}, `wlesal`.`system_codes` )
            </if>
            <if test="param.fireEquipmentSpecificIndexKey != null and param.fireEquipmentSpecificIndexKey != ''">
                AND wlesal.type = #{param.fireEquipmentSpecificIndexKey}
            </if>
            <if test="param.bizOrgCode != null and param.bizOrgCode != ''">
                AND wlesal.biz_org_code like concat (#{param.bizOrgCode},'%')
            </if>
        </where>
        ORDER BY
        `wlesal`.`create_date` DESC
    </select>

    <select id="dcFireAlarmLogPage" resultType="java.util.HashMap">
        SELECT
        `wlesal`.equipment_specific_index_name type,
        `wlesal`.equipment_specific_name alamContent,
        DATE_FORMAT(`wlesal`.`create_date`,'%m-%d %H:%i:%s') createDate,
        wlesal.location
        FROM
        `wl_equipment_specific_alarm_log` `wlesal`
        LEFT JOIN `wl_equipment_specific_alarm` wesa ON wesa.id = wlesal.equipment_specific_alarm_id
        LEFT JOIN `wl_equipment_specific` `wles` ON `wlesal`.`equipment_specific_id` = `wles`.`id`
        <where>
            wesa.`status` = 1
            <if test="param.system != null and param.system != ''">
                AND find_in_set( #{param.system}, `wlesal`.`system_codes` )
            </if>
            <if test="param.fireEquipmentSpecificIndexKey != null and param.fireEquipmentSpecificIndexKey != ''">
                AND wlesal.type = #{param.fireEquipmentSpecificIndexKey}
            </if>
            <if test="param.bizOrgCode != null and param.bizOrgCode != ''">
                AND wlesal.biz_org_code like concat (#{param.bizOrgCode},'%')
            </if>
        </where>
        ORDER BY
        `wlesal`.`create_date` DESC
    </select>
    <select id="alarmTrend" resultType="java.util.Map">
        SELECT
            `result`.`click_date` AS `click_date`,
            IFNULL( `a`.`fireAlarmNum`, 0 ) AS `fireAlarmNum`,
            IFNULL( `b`.`breakdownNum`, 0 ) AS `breakdownNum`,
            IFNULL( `c`.`shieldNum`, 0 ) AS `shieldNum`,
            IFNULL( `d`.`otherNum`, 0 ) AS `otherNum`
        FROM
            ((((
                `v_monitor_event_utils_week` `result`
                    LEFT JOIN (
                        SELECT
                            SUBSTRING_INDEX( log.create_date, " ", 1 ) date,
                            count( SUBSTRING_INDEX( log.create_date, " ", 1 )) AS `fireAlarmNum`
                        FROM
                            `wl_equipment_specific_alarm_log` `log`
                            LEFT JOIN wl_equipment_specific wes ON `log`.equipment_specific_id = wes.id
                            AND log.`type` = 'FIREALARM'
                            <where>
                                <if test="bizOrgCode != null and bizOrgCode != ''">
                                    wes.biz_org_code like concat (#{bizOrgCode},'%')
                                </if>
                            </where>
                        GROUP BY
                        SUBSTRING_INDEX(log.create_date," ",1)
            ) `a` ON ((
            `a`.`date` = `result`.`click_date`
            )))
            LEFT JOIN (
                SELECT
                    SUBSTRING_INDEX( log.create_date, " ", 1 ) date,
                    count( SUBSTRING_INDEX( log.create_date, " ", 1 )) AS `breakdownNum`
                FROM
                    `wl_equipment_specific_alarm_log` `log`
                    LEFT JOIN wl_equipment_specific wes ON `log`.equipment_specific_id = wes.id
                    AND log.`type` = 'BREAKDOWN'
                    <where>
                        <if test="bizOrgCode != null and bizOrgCode != ''">
                            wes.biz_org_code like concat (#{bizOrgCode},'%')
                        </if>
                    </where>
                GROUP BY
                        SUBSTRING_INDEX(log.create_date," ",1)
                ) `b` ON ((
                `b`.`date` = `result`.`click_date`
                )))
            LEFT JOIN (
                SELECT
                    SUBSTRING_INDEX( log.create_date, " ", 1 ) date,
                    count( SUBSTRING_INDEX( log.create_date, " ", 1 )) AS `shieldNum`
                FROM
                    `wl_equipment_specific_alarm_log` `log`
                    LEFT JOIN wl_equipment_specific wes ON `log`.equipment_specific_id = wes.id
                    AND log.`type` = 'SHIELD'
                    <where>
                        <if test="bizOrgCode != null and bizOrgCode != ''">
                            wes.biz_org_code like concat (#{bizOrgCode},'%')
                        </if>
                    </where>
                GROUP BY
                    SUBSTRING_INDEX(log.create_date," ",1)
            ) `c` ON ((
            `c`.`date` = `result`.`click_date`
            )))
            LEFT JOIN (
                SELECT
                    SUBSTRING_INDEX( log.create_date, " ", 1 ) date,
                    count( SUBSTRING_INDEX( log.create_date, " ", 1 )) AS `otherNum`
                FROM
                    `wl_equipment_specific_alarm_log` `log`
                    LEFT JOIN wl_equipment_specific wes ON `log`.equipment_specific_id = wes.id
                    AND log.`type` NOT IN ( 'SHIELD', 'BREAKDOWN', 'FIREALARM' )
                    <where>
                        <if test="bizOrgCode != null and bizOrgCode != ''">
                            wes.biz_org_code like concat (#{bizOrgCode},'%')
                        </if>
                    </where>
                GROUP BY
                    SUBSTRING_INDEX(log.create_date," ",1)
            ) `d` ON ((
            `d`.`date` = `result`.`click_date`
            )))
        ORDER BY
            `result`.`click_date`
    </select>
    
    <select id="pressureMessage" resultType="java.util.Map">
        select
            (
                select
                    count(1)
                from
                    wl_equipment_specific_index wesi
                    LEFT JOIN  wl_equipment_specific  wes on wesi.equipment_specific_id = wes.id
                where
                        wesi.equipment_index_key in ('FHS_PressurePump_Start', 'FHS_PressurePump_Stop')
                  and update_date >=
                      #{startTime} and  #{endTime} >=  update_date
                <if test="orgCode != null and orgCode != ''">
                    and wes.biz_org_code like concat(#{orgCode}, '%')
                </if>
            ) as allNum,
            (
                select
                    (
                        select
                            count(1)
                        from
                            wl_equipment_detail wed
                            LEFT JOIN  wl_equipment_specific  wes on wes.equipment_detail_id = wed.id
                        where
                            wed.code like '92010800%'
                        <if test="orgCode != null and orgCode != ''">
                            and wes.biz_org_code like concat(#{orgCode}, '%')
                        </if>
                        ) as allEqu) as allEqu,
            (
                select
                    '稳压泵平均打压频次' as name) as name,
            (
                select
                    (
                        select
                            count(1)
                        from
                            wl_equipment_specific_index wesi
                                LEFT JOIN  wl_equipment_specific  wes on wesi.equipment_specific_id = wes.id
                        where
                            wesi.equipment_index_key in ('FHS_PressurePump_Start')
                          and wesi.update_date >=
                              #{startTime} and  #{endTime} >=  update_date
                                <if test="orgCode != null and orgCode != ''">
                                    and wes.biz_org_code like concat(#{orgCode}, '%')
                                </if>
                    ) as startNum) as startNum
    </select>     
         
    <select id="stationInfo" resultType="java.util.Map">
        SELECT
            a.`name`,
            a.`code`,
            CASE

                WHEN a.countAlarmTotal = 0 THEN
                    SUM( a.NAME ) ELSE count( a.`name` )
                END AS alarmEquipTotal,
            IFNULL( SUM( a.countAlarmTotal ), 0 ) countAlarmTotal
        FROM
            (
                SELECT
                    fs.`name`,
                    fs.`code`,
                    IFNULL( sum( wesi.`status` ), 0 ) AS countAlarmTotal
                FROM
                    idx_biz_station_info fs
                        LEFT JOIN wl_equipment_specific_alarm wesi ON wesi.station_code = fs.`code`
                        AND wesi.`status` = 1
                GROUP BY
                    fs.id,
                    wesi.equipment_specific_id
            ) a
        GROUP BY
            a.`name`
    </select>

    <delete id="clearAlarmData">
        DELETE FROM wl_equipment_specific_alarm WHERE equipment_specific_id = #{equipmentSpecificId};
        DELETE FROM wl_equipment_specific_alarm_log WHERE equipment_specific_id = #{equipmentSpecificId};
        DELETE FROM wl_equipment_alarm_report_day WHERE equipment_specific_id = #{equipmentSpecificId};
    </delete>
</mapper>