<?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>
    <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
        d.*
        FROM
        (
        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,
        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 alamContent,
        if(confirm_type is null,'未处理','已处理') handleStatus,
        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.build_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 type,
        wlesal.create_date AS createDate,
        CONCAT_WS(' ',ware.full_name,wsd.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 = 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 as wsd on wsd.equipment_specific_id = wlesal.equipment_specific_id
        left join wl_warehouse_structure as ware on wsd.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>
            <if test="param.warehouseStructureName != null and param.warehouseStructureName != ''">and
                d.warehouseStructureName like
                concat(concat("%",#{param.warehouseStructureName}),"%")
            </if>
            <if test="param.equipCode != null and param.equipCode != ''">AND d.fireEquipmentCode like
                concat(concat("%",#{param.equipCode}),"%")
            </if>
            <if test="param.beginDate!=null">AND d.createDate <![CDATA[>=]]> #{param.beginDate}</if>
            <if test="param.endDate!=null">AND d.createDate <![CDATA[<=]]> #{param.endDate}</if>
            <if test="param.alarmType == 'BREAKDOWN'">AND d.type = #{param.alarmType}</if>
            <if test="param.alarmType == 'FIREALARM'">AND d.type = #{param.alarmType}</if>
            <if test="param.orgCode != null and param.orgCode != ''">AND d.org_code = #{param.orgCode}</if>
            <if test="param.type != null and param.type != ''">AND d.type = #{param.type}</if>
            <if test="param.handleStatus != null and param.handleStatus != '' and param.handleStatus == 1">AND
                d.handleStatus = '已处理'
            </if>
            <if test="param.handleStatus != null and param.handleStatus != '' and param.handleStatus == 2">AND
                d.handleStatus = '未处理'
            </if>
            <if test="param.system != null and param.system != ''">
                AND find_in_set(#{param.system},d.systemId)
            </if>
            <if test="param.buildIds!=null">
                AND d.buildId IN
                <foreach item="item" collection="param.buildIds" separator=","
                         open="(" close=")" index="">#{item}
                </foreach>
            </if>
        </where>
        ORDER BY d.createDate DESC
    </select>
    <select id="count" resultType="java.lang.Integer">
        SELECT
        count(1) AS total_num
        FROM
        (
        SELECT
        wlesa.id,
        wlesa.equipment_specific_id AS fireEquipmentId,
        wles.code AS fireEquipmentCode,
        wlesa.equipment_index_id AS fireEquipmentIndexId,
        wlesa.equipment_specific_index_key AS fireEquipmentSpecificIndexKey,
        wlesa.equipment_specific_index_name AS fireEquipmentSpecificIndexName,
        wlesa.type AS type,
        wlesa.create_date AS createDate,
        CONCAT_WS(' ',wws.full_name,wlsd.description) AS warehouseStructureName
        FROM wl_equipment_specific_alarm_log 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
        ) d
        <where>
            <if test="warehouseStructureName != null and warehouseStructureName != ''">and 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 == 'BREAKDOWN'">AND d.type = #{alarmType}</if>
            <if test="alarmType == 'FIREALARM'">AND d.type = #{alarmType}</if>
        </where>
    </select>
    <select id="pageAlarmsInfo" resultType="Map">
        SELECT
        d.*
        FROM
        (
        SELECT
        wlesal.id AS alarmId,
        wlesal.org_code,
        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,
        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,
        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
        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) d
        WHERE 1=1
        AND d.fireEquipmentName IS NOT NULL
        <if test="param.warehouseStructureName != null and param.warehouseStructureName != ''">and
            d.warehouseStructureName like
            concat(concat("%",#{param.warehouseStructureName}),"%")
        </if>
        <if test="param.equipCode != null and param.equipCode != ''">AND d.fireEquipmentCode like
            concat(concat("%",#{param.equipCode}),"%")
        </if>
        <choose>
            <when test="param.confirmType != null and param.confirmType != '' and param.confirmType == 1">
                AND d.handleType  <![CDATA[<>]]> ''
            </when>
            <when test="param.confirmType != null and param.confirmType != '' and param.confirmType == 0">
                AND d.handleType IS NULL
            </when>
        </choose>
        <if test="param.beginDate!=null">AND d.createDate <![CDATA[>=]]> #{param.beginDate}</if>
        <if test="param.endDate!=null">AND d.createDate <![CDATA[<=]]> #{param.endDate}</if>
        <if test="param.alarmType == 'BREAKDOWN' or param.alarmType == 'FIREALARM'">AND d.type = #{param.alarmType}</if>
        <if test="param.systemCode != null and param.systemCode != ''">
            AND find_in_set(#{param.systemCode},d.systemCodes)
        </if>
        <if test="param.buildId != null and param.buildId != ''">
            and (d.buildId=#{param.buildId}
            or find_in_set(d.fireEquipmentId,(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 d.fireEquipmentId = #{param.id}</if>
        <if test="param.status!=null and param.status!=3">AND d.status = #{param.status}</if>
        ORDER BY d.createDate 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.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 != ''">
                and 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>
        </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 1=1
        AND d.fireEquipmentName IS NOT NULL
        AND 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
        1 = 1
        AND d.fireEquipmentName IS NOT NULL
        AND 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.fireEquipmentName <![CDATA[<>]]> ''
        AND 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 wlesa.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 wlesa.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 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>
            d.fireEquipmentName IS NOT NULL
            <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(' ',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 = 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_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_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>
            d.fireEquipmentName IS NOT NULL
            <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,
        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,
        CONCAT_WS(' ',wws.full_name,wlsd.description) 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
        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_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_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.startDate != null and dto.endDate  != null '>
                AND DATE_FORMAT(wlesa.create_date, '%Y-%m-%d ' ) between DATE_FORMAT(#{dto.startDate}, '%Y-%m-%d ' ) and
                DATE_FORMAT(#{dto.endDate}, '%Y-%m-%d ' )
            </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>
            d.fireEquipmentName IS NOT NULL
            <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' OR d.type = 'NOTICE' )
                </otherwise>
            </choose>
            <if test='dto.fireEquipmentName != null  '>
                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 d.confirmUserName LIKE CONCAT('%', #{dto.confirmUserName}, '%' )
            </if>
            <if test="dto.code != null">
                and d.code like CONCAT('%', #{dto.code}, '%' )
            </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.qrcode fireEquipmentCode,
        b.eqimg eqimg,
        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_stock_detail.qr_code qrcode,
        wl_equipment_detail.img eqimg,
        wl_equipment_detail.id eqid,
        wl_equipment_specific.system_id
        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
        )b
        )m
        <where>
            <if test="dto.status != null and dto.status != ''">
                AND `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 status DESC
    </select>

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

       SELECT
	(
	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,
		( 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
	  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>

</mapper>