<?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.EmergencyMapper">

    <select id="getSystemState" resultType="Map">
        SELECT
        `fs`.`id` AS `key`,
        `fs`.`name` AS `name`,
        `fs`.system_type_code AS systemTypeCode,
        `fs`.`code` AS code,
        IF
        (((
        SELECT
        count( `wesa`.`equipment_specific_id` )
        FROM
        `wl_equipment_specific_alarm` `wesa`
        WHERE
        ( 0 <![CDATA[<>]]> find_in_set( `fs`.`id`, `wesa`.`system_ids` )
        AND `wesa`.`status` = 1)) > 0
        ),
        '异常',
        '正常'
        ) AS `status`
        FROM
        `f_fire_fighting_system` `fs`
        <where>
            -- 此接口要求过滤掉其他系统
            fs.system_type_code IS NOT NULL AND LENGTH(TRIM(fs.system_type_code)) > 0 AND fs.system_type_code != 'otherSys'
            <if test="bizOrgCode!=null and bizOrgCode!=''">
                AND fs.biz_org_code like concat(#{bizOrgCode}, '%')
            </if>
        </where>
        GROUP BY
        `fs`.`id`
        ORDER BY
        `fs`.`sort`
    </select>

    <select id="getSystemAlarmTheCount" resultType="java.util.Map">
        SELECT
        count(*) as total
        FROM
        (
        SELECT
        `fs`.`id` AS `key`,
        `fs`.`name` AS `name`,
        `fs`.system_type_code AS systemTypeCode,
        `fs`.`code` AS CODE,
        `fs`.`biz_org_code` AS bizOrgCode,
        `fs`.`biz_org_name` AS bizOrgName,
        (
        SELECT
        count( * )
        FROM
        ( SELECT id FROM wl_equipment_specific_alarm_log WHERE system_codes = fs.CODE AND clean_time IS NULL GROUP BY equipment_specific_id ) b
        ) AS alarmNum,
        IF
        (
        (
        (
        SELECT
        count( `wesa`.`equipment_specific_id` )
        FROM
        `wl_equipment_specific_alarm` `wesa`
        WHERE
        ( 0 <![CDATA[<>]]>  find_in_set( `fs`.`id`, `wesa`.`system_ids` ) AND `wesa`.`status` = 1 )
        ) > 0
        ),
        '异常',
        '正常'
        ) AS `status`
        FROM
        `f_fire_fighting_system` `fs`
        GROUP BY
        `fs`.`id`
        ORDER BY
        fs.biz_org_name
        ) sys
        <where>
            <if test="status != null and status != ''">
                sys.status = #{status}
            </if>
            <if test="bizOrgCode != null and bizOrgCode != ''">
             and   sys.bizOrgCode like concat('%',#{bizOrgCode},'%')
            </if>
        </where>
    </select>


    <select id="getSystemAlarmCount" resultType="Map">
        SELECT
        *
        FROM
        (
        SELECT
        `fs`.`id` AS `key`,
        `fs`.`name` AS `name`,
        `fs`.system_type_code AS systemTypeCode,
        `fs`.`code` AS CODE,
        `fs`.`biz_org_code` AS bizOrgCode,
        `fs`.`biz_org_name` AS bizOrgName,
        (
        SELECT
        count( * )
        FROM
        ( SELECT id FROM wl_equipment_specific_alarm_log WHERE system_codes = fs.CODE AND clean_time IS NULL GROUP BY equipment_specific_id ) b
        ) AS alarmNum,
        IF
        (
        (
        (
        SELECT
        count( `wesa`.`equipment_specific_id` )
        FROM
        `wl_equipment_specific_alarm` `wesa`
        WHERE
        ( 0 <![CDATA[<>]]>  find_in_set( `fs`.`id`, `wesa`.`system_ids` ) AND `wesa`.`status` = 1 )
        ) > 0
        ),
        '异常',
        '正常'
        ) AS `status`
        FROM
        `f_fire_fighting_system` `fs`
        GROUP BY
        `fs`.`id`
        ORDER BY
        fs.biz_org_name
        ) sys
        <where>
            <if test="status != null and status != ''">
                sys.status = #{status}
            </if>
            <if test="bizOrgCode != null and bizOrgCode != ''">
              and  sys.bizOrgCode like concat('%',#{bizOrgCode},'%')
            </if>
        </where>
        LIMIT #{current},#{size}
    </select>

    <select id="getCAFSWaterTankInfo" resultType="Map">
        SELECT
        wes.id AS specificId,
        wes.`name`,
        a.`value`,
        wes.`code`,
        a.sxrj,
        a.`unit`,
        a.minLevel,
        a.maxLevel,
        a.volume
        FROM
        wl_equipment_specific wes
        LEFT JOIN (
        SELECT
        wlesi.equipment_specific_id,
        IFNULL(wlesi.`value`, '--') AS `value`,
        wlesi.`unit`,
        (SELECT wfie.field_value FROM wl_form_instance_equip wfie WHERE wfie.instance_id = wlesi.equipment_specific_id AND field_name = 'maxLevel') AS maxLevel,
        (SELECT IFNULL( wfie.field_value, 0 ) FROM wl_form_instance_equip wfie WHERE wfie.instance_id = wlesi.equipment_specific_id AND field_name = 'minLevel') AS minLevel,
        CASE
        WHEN ( SELECT IFNULL( wfie.field_value, 0 ) FROM wl_form_instance_equip wfie WHERE wfie.instance_id = wlesi.equipment_specific_id AND field_name = 'volume' ) <![CDATA[<>]]> 0 THEN
        CONCAT(( SELECT field_value FROM wl_form_instance_equip wfie WHERE wfie.instance_id = wlesi.equipment_specific_id AND field_name = 'volume' ),'m³') ELSE '--m³'
        END AS sxrj,
        (SELECT IFNULL( wfie.field_value, 0 ) FROM wl_form_instance_equip wfie WHERE wfie.instance_id = wlesi.equipment_specific_id AND field_name = 'volume') AS volume
        FROM
        wl_equipment_specific_index wlesi
        WHERE
        wlesi.equipment_index_key = 'CAFS_WaterTank_WaterTankLevel'
        ) a ON a.equipment_specific_id = wes.id
        WHERE
        -- 因为此处属于确定查询条件，所以此处写入常量
        wes.equipment_code LIKE CONCAT( '92032000', '%' )
        ORDER BY a.`value` DESC
<!--        <if test="bizOrgCode!=null and bizOrgCode!=''">-->
<!--            AND wes.biz_org_code like concat(#{bizOrgCode}, '%')-->
<!--        </if>-->
    </select>

    <select id="selectEmergencyMaterials" resultType="java.util.Map">
        SELECT
            ( SELECT COUNT( 1 ) FROM `wl_equipment_specific` wel WHERE wel.equipment_code LIKE CONCAT( '3104', '%' ) ) AS fireExtinguisher,
            ( SELECT COUNT( 1 ) FROM `wl_equipment_specific` wel WHERE wel.equipment_code LIKE CONCAT( '3105', '%' ) ) AS fireHydrant,
            ( SELECT COUNT( 1 ) FROM `wl_equipment_specific` wel WHERE wel.equipment_code LIKE CONCAT( '3910', '%' ) ) AS fireShovel,
            ( SELECT COUNT( 1 ) FROM `wl_equipment_specific` wel WHERE wel.equipment_code LIKE CONCAT( '3904', '%' ) ) AS fireAxe,
            ( SELECT COUNT( 1 ) FROM `wl_equipment_specific` wel WHERE wel.equipment_code LIKE CONCAT( '3911', '%' ) ) AS fireBucket,
            ( SELECT COUNT( 1 ) FROM `wl_equipment_specific` wel WHERE wel.equipment_code LIKE CONCAT( '1106', '%' ) ) AS respirator
    </select>
    <select id="selectEmergencyMaterialsDetails" resultType="java.util.Map">
        SELECT
            IFNULL(ec.name,'--') AS name ,
            count( 1 ) AS num,
            IFNULL(ws.`name`,'--') AS buildingname,
            IFNULL(un.name,'--') as unit
        FROM
            `wl_equipment_specific` wel
                LEFT JOIN wl_warehouse_structure ws ON wel.warehouse_structure_id = ws.id
                LEFT JOIN wl_equipment_detail wed ON wel.equipment_detail_id = wed.id
                LEFT JOIN wl_equipment we ON wed.equipment_Id = we.id
                LEFT JOIN wl_equipment_category ec ON we.category_id = ec.id
                LEFT JOIN wl_unit un ON we.unit_id = un.id
        WHERE
            (
                    wel.equipment_code LIKE CONCAT( '3104', '%' )
                    OR wel.equipment_code LIKE CONCAT( '3105', '%' )
                    OR wel.equipment_code LIKE CONCAT( '3910', '%' )
                    OR wel.equipment_code LIKE CONCAT( '3904', '%' )
                    OR wel.equipment_code LIKE CONCAT( '3911', '%' )
                    OR wel.equipment_code LIKE CONCAT( '1106', '%' )
                )
          AND wel.biz_org_code LIKE CONCAT( #{bizOrgCode}, '%' )
        GROUP BY
            wel.warehouse_structure_id  ,wel.equipment_code
    </select>
    <select id="selectFireMonitor" resultType="java.util.Map">
        SELECT temp.name, temp.alarm ,temp.id, temp.code,
               IFNULL((SELECT wesi.value FROM  wl_equipment_specific_index wesi  WHERE wesi.equipment_specific_id = temp.id  AND wesi.equipment_index_key = 'CAFS_CAFSFireGun_FireGunPressure'
                       ORDER BY wesi.update_date desc LIMIT 1),0)	 AS value,
	        IFNULL((SELECT CASE WHEN wesi.value_label IS NULL OR trim( wesi.value_label ) = '' THEN (CASE
				wesi.`value`
				WHEN 'true' THEN
				concat( wesi.equipment_index_name, '(是)' )
				WHEN 'false' THEN
				concat( wesi.equipment_index_name, '(否)' )
				ELSE
				wesi.equipment_index_name
		END) ELSE wesi.value_label END FROM  wl_equipment_specific_index wesi
		LEFT JOIN wl_equipment_index  wei on wesi.equipment_index_id = wei.id
		 WHERE wesi.equipment_specific_id = temp.id  AND wei.is_trend = 0
		 AND wesi.value is NOT NULL AND wesi.value <![CDATA[<>]]> ''
	        ORDER BY wesi.update_date desc LIMIT 1),'--') AS status,
	        '0' AS flow
        FROM(
            SELECT wes.id,wes.name,wes.code,
            CASE
            WHEN ( SELECT is_alarm FROM wl_equipment_specific_index wesi WHERE wesi.equipment_specific_id = wes.id AND wesi.equipment_index_key = wes.realtime_iot_index_key ) = 1
            AND wes.realtime_iot_index_value = 'true' THEN
            1 ELSE 0
            END AS alarm
            FROM  wl_equipment_specific wes
            JOIN f_equipment_fire_equipment fire ON wes.id = fire.fire_equipment_id
            WHERE
            wes.equipment_code LIKE concat( '920322', '%' )
            <if test="code != null and  code!='' ">
                AND fire.equipment_id = (SELECT id  FROM f_equipment WHERE `code` = #{code})
            </if>
            AND wes.biz_org_code LIKE CONCAT( #{bizOrgCode}, '%' )
            ) temp
    </select>
    <select id="selectFoamTank" resultType="java.util.Map">
        SELECT
            temp.id,
            temp.name,
            temp.code,
            temp.nowLevel,
            temp.volume,
            temp.unit,
            temp.minLevel,
            temp.maxLevel
--             CASE
--                 WHEN ( temp.nowLevel = '--' ) THEN
--                     '--'
--                 WHEN ( temp.minLevel - temp.nowLevel > 0 ) THEN
--                     '液位低'
--                 WHEN ( temp.nowLevel - temp.maxLevel > 0) THEN
--                     '液位高' ELSE '正常'
--                 END AS status,
--             CASE
--                 WHEN ( temp.nowLevel = '--' ) THEN
--                     '--' ELSE format(
--                     IF
--                         (
--                                 (
--                                         abs( ( ( temp.nowLevel ) / IF ( temp.maxLevel = 0, 1, temp.maxLevel ) ) ) * 100
--                                     ) > 100,
--                                 100,
--                                 (
--                                         abs( ( ( temp.nowLevel ) / IF ( temp.maxLevel = 0, 1, temp.maxLevel ) ) ) * 100
--                                     )
--                         ),
--                     2
--                 )
--                 END AS abs
        FROM
            (
                SELECT
                    wes.id,
                    wes.name,
                    wes.code,
                    IFNULL( ( SELECT value FROM wl_equipment_specific_index wesi WHERE wesi.equipment_specific_id = wes.id AND equipment_index_key = 'CAFS_FoamTank_FoamTankLevel' ), '--' ) AS nowLevel,
                    IFNULL( ( SELECT unit FROM wl_equipment_specific_index wesi WHERE wesi.equipment_specific_id = wes.id AND equipment_index_key = 'CAFS_FoamTank_FoamTankLevel' ), '--' ) AS unit,
                    IFNULL(( SELECT field_value FROM wl_form_instance_equip WHERE instance_id = wes.id AND field_name = 'maxLevel' ) , 0 )AS maxLevel,
                    IFNULL( ( SELECT field_value FROM wl_form_instance_equip WHERE instance_id = wes.id AND field_name = 'minLevel' ), 0 ) AS minLevel,
                    IFNULL( ( SELECT field_value FROM wl_form_instance_equip WHERE instance_id = wes.id AND field_name = 'volume' ), 0 ) AS volume
                FROM
                    wl_equipment_specific wes
                WHERE
                    wes.equipment_code LIKE concat( '920319', '%' )
            ) temp
        ORDER BY temp.nowLevel DESC
    </select>

    <select id="selectOilDrainage" resultType="java.util.Map">
        SELECT
            wes.id,
            '1' as type,
            (SELECT wei.emergency_level FROM  wl_equipment_index wei LEFT JOIN wl_equipment_specific_index wesi  on wei.id = wesi.equipment_index_id
                WHERE wesi.equipment_specific_id = wes.id AND wesi.equipment_index_key = wes.realtime_iot_index_key) as level,
            wes.code as code ,
            wes.name,
              IFNULL(wes.realtime_iot_index_name,
						'暂无信号'
					) AS status,
            CASE

                WHEN ( SELECT is_alarm FROM wl_equipment_specific_index wesi WHERE wesi.equipment_specific_id = wes.id AND wesi.equipment_index_key = wes.realtime_iot_index_key ) = 1
                    AND wes.realtime_iot_index_value = 'true' THEN
                    1 ELSE 0
                END AS alarm
        FROM
            wl_equipment_specific wes
                JOIN f_equipment_fire_equipment fire ON wes.id = fire.fire_equipment_id
        WHERE
            wes.equipment_code LIKE concat( '921003', '%')
            <if test="code != null and  code!='' ">
                AND fire.equipment_id = (SELECT id  FROM f_equipment WHERE `code` = #{code})
            </if>
          AND wes.biz_org_code LIKE CONCAT( #{bizOrgCode}, '%' ) UNION
        (
            SELECT
                wes.id,
                '2' as type,
                (SELECT wei.emergency_level FROM  wl_equipment_index wei LEFT JOIN wl_equipment_specific_index wesi  on wei.id = wesi.equipment_index_id
                 WHERE wesi.equipment_specific_id = wes.id AND wesi.equipment_index_key = wes.realtime_iot_index_key) as level,
                wes.code as code ,
                wes.name,
                 IFNULL(wes.realtime_iot_index_name,
						'暂无信号'
					) AS status,
                CASE

                    WHEN ( SELECT is_alarm FROM wl_equipment_specific_index wesi WHERE wesi.equipment_specific_id = wes.id AND wesi.equipment_index_key = wes.realtime_iot_index_key ) = 1
                        AND wes.realtime_iot_index_value = 'true' THEN
                        1 ELSE 0
                    END AS alarm
            FROM
                wl_equipment_specific wes
                    JOIN f_equipment_fire_equipment fire ON wes.id = fire.fire_equipment_id
            WHERE
                wes.equipment_code LIKE concat( '921002', '%' )
                <if test="code != null and  code!='' ">
                    AND fire.equipment_id = (SELECT id  FROM f_equipment WHERE `code` = #{code})
                </if>
              AND wes.biz_org_code LIKE CONCAT( #{bizOrgCode}, '%' )
        ) UNION
        (
            SELECT
                wes.id,
                '3' as type,
                (SELECT wei.emergency_level FROM  wl_equipment_index wei LEFT JOIN wl_equipment_specific_index wesi  on wei.id = wesi.equipment_index_id
                 WHERE wesi.equipment_specific_id = wes.id AND wesi.equipment_index_key = wes.realtime_iot_index_key) as level,
                wes.code as code ,
                wes.name,
                   IFNULL(wes.realtime_iot_index_name,
						'暂无信号'
					) AS status,
                CASE

                    WHEN ( SELECT is_alarm FROM wl_equipment_specific_index wesi WHERE wesi.equipment_specific_id = wes.id AND wesi.equipment_index_key = wes.realtime_iot_index_key ) = 1
                        AND wes.realtime_iot_index_value = 'true' THEN
                        1 ELSE 0
                    END AS alarm
            FROM
                wl_equipment_specific wes
                    JOIN f_equipment_fire_equipment fire ON wes.id = fire.fire_equipment_id
            WHERE
                wes.equipment_code LIKE concat( '921004', '%' )
                <if test="code != null and  code!='' ">
                    AND fire.equipment_id = (SELECT id  FROM f_equipment WHERE `code` = #{code})
                </if>
              AND wes.biz_org_code LIKE CONCAT( #{bizOrgCode}, '%' )
        ) UNION
        (
            SELECT
                wes.id,
                '4' as type,
                (SELECT wei.emergency_level FROM  wl_equipment_index wei LEFT JOIN wl_equipment_specific_index wesi  on wei.id = wesi.equipment_index_id
                 WHERE wesi.equipment_specific_id = wes.id AND wesi.equipment_index_key = wes.realtime_iot_index_key) as level,
                wes.code as code ,
                wes.name,
                   IFNULL(wes.realtime_iot_index_name,
						'暂无信号'
					) AS status,
                CASE

                    WHEN ( SELECT is_alarm FROM wl_equipment_specific_index wesi WHERE wesi.equipment_specific_id = wes.id AND wesi.equipment_index_key = wes.realtime_iot_index_key ) = 1
                        AND wes.realtime_iot_index_value = 'true' THEN
                        1 ELSE 0
                    END AS alarm
            FROM
                wl_equipment_specific wes
                    JOIN f_equipment_fire_equipment fire ON wes.id = fire.fire_equipment_id
            WHERE
                wes.equipment_code LIKE concat( '921005', '%' )
                <if test="code != null and  code!='' ">
                    AND fire.equipment_id = (SELECT id  FROM f_equipment WHERE `code` = #{code})
                </if>
              AND wes.biz_org_code LIKE CONCAT( #{bizOrgCode}, '%' )
        )
    </select>

    <select id="selectPressureFlow" resultType="java.util.Map">
        SELECT
        temp.id,
        temp.name,
        IF(temp.nowPressure = '--', '--', TRUNCATE(ROUND(temp.nowPressure, 2),2)) AS nowPressure,
        temp.area,
        temp.minPressure AS `minValue`,
        temp.maxPressure AS `maxValue`,
        CASE
        WHEN temp.nowPressure = '--' THEN
        '--'
        WHEN temp.maxPressure = '' THEN
            '--'
        WHEN temp.minPressure = '' THEN
            '--'
        WHEN (temp.nowPressure - temp.maxPressure) > 0 THEN
        '压力高'
        WHEN (temp.minPressure - temp.nowPressure) > 0 THEN
        '压力低' ELSE '正常'
        END AS status,
        1 AS type
        FROM
        (
        SELECT
        wes.id,
        wes.name,
        concat_ws( '-', ws.full_name, ed.area ) area,
        IFNULL( ( SELECT VALUE FROM wl_equipment_specific_index wesi WHERE wesi.equipment_specific_id = wes.id AND equipment_index_key = 'FHS_PipePressureDetector_PipePressure' ), '--' ) AS nowPressure,
        IFNULL( ( SELECT field_value FROM wl_form_instance_equip WHERE instance_id = wes.id AND field_name = 'maxPressure' AND field_value <![CDATA[<>]]> '' ), 1.5 ) AS maxPressure,
        IFNULL( ( SELECT field_value FROM wl_form_instance_equip WHERE instance_id = wes.id AND field_name = 'minPressure' AND field_value <![CDATA[<>]]> '' ), 0.5 ) AS minPressure
        FROM
        wl_equipment_specific wes
        LEFT JOIN wl_equipment_detail ed ON wes.equipment_detail_id = ed.id
        LEFT JOIN wl_warehouse_structure ws ON ws.id = wes.warehouse_structure_id
        LEFT JOIN wl_equipment_specific_index ei ON wes.id = ei.equipment_specific_id
        LEFT JOIN wl_form_instance_equip fi ON fi.instance_id = wes.id
        WHERE
        wes.equipment_code LIKE concat( '92011000', '%' ) AND wes.biz_org_code LIKE CONCAT( #{bizOrgCode}, '%' )
        ) temp
        UNION
        SELECT
        temp.id,
        temp.name,
        IF(temp.nowFlow = '--', '--', TRUNCATE(ROUND(temp.nowFlow, 2),2)) AS nowFlow,
        temp.area,
        temp.minFlow AS `minValue`,
        temp.maxFlow AS `maxValue`,
        CASE
            WHEN temp.nowFlow = '--' THEN
                '--'
            WHEN temp.maxFlow = '' THEN
                '--'
            WHEN temp.minFlow = '' THEN
                '--'
            WHEN (temp.nowFlow - IFNULL(temp.maxFlow, 0)) > 0 THEN
                '流量高'
            WHEN (IFNULL(temp.minFlow, 0) - temp.nowFlow) > 0  THEN
                '流量低' ELSE '正常'
            END AS status,
        2 AS type
        FROM
        (
        SELECT
        wes.id,
        wes.NAME,
        concat_ws( '-', ws.full_name, ed.area ) area,
        IFNULL( ( SELECT VALUE FROM wl_equipment_specific_index wesi WHERE wesi.equipment_specific_id = wes.id AND equipment_index_key = 'FHS_FirePoolDevice_InfluentFlow' ), '--' ) AS nowFlow,
        IFNULL( ( SELECT field_value FROM wl_form_instance_equip WHERE instance_id = wes.id AND field_name = 'maxFlow' AND field_value <![CDATA[<>]]> '' ), 2.4 ) AS maxFlow,
        IFNULL( ( SELECT field_value FROM wl_form_instance_equip WHERE instance_id = wes.id AND field_name = 'minFlow' AND field_value <![CDATA[<>]]> '' ), 0.5 ) AS minFlow
        FROM
        wl_equipment_specific wes
        LEFT JOIN wl_equipment_detail ed ON wes.equipment_detail_id = ed.id
        LEFT JOIN wl_warehouse_structure ws ON ws.id = wes.warehouse_structure_id
        LEFT JOIN wl_equipment_specific_index ei ON wes.id = ei.equipment_specific_id
        LEFT JOIN wl_form_instance_equip fi ON fi.instance_id = wes.id
        WHERE
        wes.equipment_code LIKE concat( '92010700', '%' ) AND wes.biz_org_code LIKE CONCAT( #{bizOrgCode}, '%' )
        ) temp
    </select>
    <select id="selectAlarmAnalysis" resultType="java.util.Map">
        SELECT
            temp.date,
            (
                SELECT
                    COUNT(

                        wespa.equipment_specific_id

                        )
                FROM
                    wl_equipment_specific_alarm_log wespa
                WHERE
                    wespa.equipment_specific_index_key LIKE concat('%', 'Fault')
                  AND wespa.create_date LIKE concat(temp.date, '%')
                  AND wespa.equipment_specific_index_value = 'true'
                    <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
                        wespa.system_codes  like concat('%',#{code},'%')
                    </foreach>
            ) AS faultNum,
            (
                SELECT
                    COUNT(

                        wespa.equipment_specific_id

                        )
                FROM
                    wl_equipment_specific_alarm_log wespa
                WHERE
                    wespa.equipment_specific_index_key LIKE concat('%', 'FireAlarm')
                  AND wespa.create_date LIKE concat(temp.date, '%')
                  AND wespa.equipment_specific_index_value = 'true'
                    <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
                        wespa.system_codes  like concat('%',#{code},'%')
                    </foreach>
            ) AS alarmNum,
            (
                SELECT
                    COUNT(

                        wespa.equipment_specific_id

                        )
                FROM
                    wl_equipment_specific_alarm_log wespa
                WHERE
                    wespa.equipment_specific_index_key LIKE concat('%', 'Shield')
                  AND wespa.create_date LIKE concat(temp.date, '%')
                  AND wespa.equipment_specific_index_value = 'true'
                    <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
                        wespa.system_codes  like concat('%',#{code},'%')
                    </foreach>
            ) AS shieldNum
        FROM
        <if test="dataType == 1">
            (

            SELECT
            date_format(
            (#{date} - INTERVAL 0 MONTH),
            '%Y-%m'
            ) AS `date`
            UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -1 MONTH),
            '%Y-%m'
            ) AS `date`)
            UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -2 MONTH),
            '%Y-%m'
            ) AS `date`)
            UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -3 MONTH),
            '%Y-%m'
            ) AS `date`)
            UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -4 MONTH),
            '%Y-%m'
            ) AS `date`)
            UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -5 MONTH),
            '%Y-%m'
            ) AS `date`)UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -6 MONTH),
            '%Y-%m'
            ) AS `date`)UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -7 MONTH),
            '%Y-%m'
            ) AS `date`)UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -8 MONTH),
            '%Y-%m'
            ) AS `date`)UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -9 MONTH),
            '%Y-%m'
            ) AS `date`)UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -10 MONTH),
            '%Y-%m'
            ) AS `date`)UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -11 MONTH),
            '%Y-%m'
            ) AS `date`))temp
        </if>
        <if test="dataType != 1">
            (
            SELECT temp.selected_date as date FROM
            (SELECT ADDDATE('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date FROM
            (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
            (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
            (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
            (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
            (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4)
            temp
            WHERE selected_date BETWEEN #{startDate} AND #{endDate}
            ) temp
            ORDER BY
            temp.date
        </if>
    </select>
    <select id="selectAlarmAnalysisCount" resultType="java.util.Map">
        SELECT
            (
                SELECT
                    COUNT(

                        wespa.equipment_specific_id

                        )
                FROM
                    wl_equipment_specific_alarm_log wespa
                WHERE
                    wespa.equipment_specific_index_key LIKE concat('%', 'Fault')
                  AND wespa.equipment_specific_index_value = 'true'
                  AND wespa.create_date BETWEEN #{startDate}
                    AND #{endDate}
                <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
                    wespa.system_codes  like concat('%',#{code},'%')
                </foreach>
            ) AS faultNum,
            (
                SELECT
                    COUNT(

                        wespa.equipment_specific_id

                        )
                FROM
                    wl_equipment_specific_alarm_log wespa
                WHERE
                    wespa.equipment_specific_index_key LIKE concat('%', 'FireAlarm')
                  AND wespa.equipment_specific_index_value = 'true'
                  AND wespa.create_date BETWEEN #{startDate}
                    AND #{endDate}
                    <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
                        wespa.system_codes  like concat('%',#{code},'%')
                    </foreach>
            ) AS fireAlarmNum,
            (
                SELECT
                    COUNT(

                        wespa.equipment_specific_id

                        )
                FROM
                    wl_equipment_specific_alarm_log wespa
                WHERE
                    wespa.equipment_specific_index_key LIKE concat('%', 'Shield')
                  AND wespa.equipment_specific_index_value = 'true'
                  AND wespa.create_date BETWEEN #{startDate}
                    AND #{endDate}
                <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
                    wespa.system_codes  like concat('%',#{code},'%')
                </foreach>
            ) AS shieldNum

    </select>
    <select id="selectSystemCodes" resultType="java.lang.String">
        SELECT
            fs. code
        FROM
            f_fire_fighting_system fs
        WHERE
            fs.system_type_code = #{systemType}
    </select>
    <select id="selectFireCannonAlarmAnalysisPie" resultType="java.util.Map">
        SELECT
        (
        SELECT
        COUNT(

        wespa.equipment_specific_id

        )
        FROM
        wl_equipment_specific_alarm_log wespa
        WHERE
        wespa.equipment_specific_index_key LIKE concat('%', 'Fault')
        AND wespa.equipment_specific_index_value = 'true'
        AND wespa.create_date BETWEEN #{startDate}
        AND #{endDate}
        <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
            wespa.system_codes  like concat('%',#{code},'%')
        </foreach>
        ) AS faultNum,
        (
        SELECT
        COUNT(

        wespa.equipment_specific_id

        )
        FROM
        wl_equipment_specific_alarm_log wespa
        WHERE
        wespa.equipment_specific_index_key LIKE concat('%', 'Alarm', '%')
        AND wespa.equipment_specific_index_value = 'true'
        AND wespa.create_date BETWEEN #{startDate}
        AND #{endDate}
        <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
            wespa.system_codes  like concat('%',#{code},'%')
        </foreach>
        ) AS fireAlarmNum,
        (
        SELECT
        COUNT(

        wespa.equipment_specific_id

        )
        FROM
        wl_equipment_specific_alarm_log wespa
        WHERE
        wespa.equipment_specific_index_key LIKE concat('%', 'Shield')
        AND wespa.equipment_specific_index_value = 'true'
        AND wespa.create_date BETWEEN #{startDate}
        AND #{endDate}
        <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
            wespa.system_codes  like concat('%',#{code},'%')
        </foreach>
        ) AS shieldNum,
        (
        SELECT
        COUNT(

        wespa.equipment_specific_id

        )
        FROM
        wl_equipment_specific_alarm_log wespa
        WHERE
        wespa.equipment_specific_index_key LIKE concat('%', 'PowerLoss')
        AND wespa.equipment_specific_index_value = 'true'
        AND wespa.create_date BETWEEN #{startDate}
        AND #{endDate}
        <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
            wespa.system_codes  like concat('%',#{code},'%')
        </foreach>
        ) AS lossNum



    </select>
    <select id="waterSystemAlarmAnalysis" resultType="java.util.Map">
        SELECT
            temp.date,
            (
                SELECT
                    COUNT(1)
                FROM
                    wl_equipment_specific_alarm_log wespa
                WHERE
                   wespa.create_date LIKE concat(temp.date, '%')
        and wespa.equipment_code LIKE CONCAT('92010600','%')
                <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
                    wespa.system_codes  like concat('%',#{code},'%')
                </foreach>
                ) AS firePumpsAlarmNum,
            (
                SELECT
                    COUNT(1)
                FROM
                    wl_equipment_specific_alarm_log wespa
                WHERE
                   wespa.create_date LIKE concat(temp.date, '%')
        and wespa.equipment_code LIKE CONCAT('92010800','%')
                    <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
                        wespa.system_codes  like concat('%',#{code},'%')
                    </foreach>
            ) AS stabilizedPumpAlarmNum,
            (
                SELECT
                    COUNT(1)
                FROM
                    wl_equipment_specific_alarm_log wespa
                WHERE
                   wespa.create_date LIKE concat(temp.date, '%')
        and wespa.equipment_code LIKE CONCAT('92010500','%')
                <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
                    wespa.system_codes  like concat('%',#{code},'%')
                </foreach>
            ) AS controlCabinetAlarmNum
        FROM
             <if test="dataType == 1">
                 (

                 SELECT
                 date_format(
                 (#{date} - INTERVAL 0 MONTH),
                 '%Y-%m'
                 ) AS `date`
                 UNION
                 (SELECT
                 date_format(
                 (#{date} - INTERVAL -1 MONTH),
                 '%Y-%m'
                 ) AS `date`)
                 UNION
                 (SELECT
                 date_format(
                 (#{date} - INTERVAL -2 MONTH),
                 '%Y-%m'
                 ) AS `date`)
                 UNION
                 (SELECT
                 date_format(
                 (#{date} - INTERVAL -3 MONTH),
                 '%Y-%m'
                 ) AS `date`)
                 UNION
                 (SELECT
                 date_format(
                 (#{date} - INTERVAL -4 MONTH),
                 '%Y-%m'
                 ) AS `date`)
                 UNION
                 (SELECT
                 date_format(
                 (#{date} - INTERVAL -5 MONTH),
                 '%Y-%m'
                 ) AS `date`)UNION
                 (SELECT
                 date_format(
                 (#{date} - INTERVAL -6 MONTH),
                 '%Y-%m'
                 ) AS `date`)UNION
                 (SELECT
                 date_format(
                 (#{date} - INTERVAL -7 MONTH),
                 '%Y-%m'
                 ) AS `date`)UNION
                 (SELECT
                 date_format(
                 (#{date} - INTERVAL -8 MONTH),
                 '%Y-%m'
                 ) AS `date`)UNION
                 (SELECT
                 date_format(
                 (#{date} - INTERVAL -9 MONTH),
                 '%Y-%m'
                 ) AS `date`)UNION
                 (SELECT
                 date_format(
                 (#{date} - INTERVAL -10 MONTH),
                 '%Y-%m'
                 ) AS `date`)UNION
                 (SELECT
                 date_format(
                 (#{date} - INTERVAL -11 MONTH),
                 '%Y-%m'
                 ) AS `date`))temp
             </if>
                     <if test="dataType != 1">
                         (
                         SELECT temp.selected_date as date FROM
                         (SELECT ADDDATE('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date FROM
                         (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
                         (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
                         (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
                         (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
                         (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4)
                         temp
                         WHERE selected_date BETWEEN #{startDate} AND #{endDate}
                         ) temp
                         ORDER BY
                         temp.date
                     </if>


    </select>
    <select id="fireCannonSystemAlarmAnalysis" resultType="java.util.Map">
        SELECT
        temp.date,
        (
        SELECT
        COUNT(1)
        FROM
        wl_equipment_specific_alarm_log wespa
        WHERE
        wespa.equipment_specific_index_key LIKE concat('%', 'Alarm', '%')
        AND wespa.create_date LIKE concat(temp.date, '%')
        AND wespa.equipment_specific_index_value = 'true'
        <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
            wespa.system_codes  like concat('%',#{code},'%')
        </foreach>
        ) AS fireAlarmNum,
        (
        SELECT
        COUNT(1)
        FROM
        wl_equipment_specific_alarm_log wespa
        WHERE
        wespa.equipment_specific_index_key LIKE concat('%', 'Shield')
        AND wespa.create_date LIKE concat(temp.date, '%')
        AND wespa.equipment_specific_index_value = 'true'
        <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
            wespa.system_codes  like concat('%',#{code},'%')
        </foreach>
        ) AS shieldNum,
        (
        SELECT
        COUNT(1)
        FROM
        wl_equipment_specific_alarm_log wespa
        WHERE
        wespa.equipment_specific_index_key LIKE concat('%', 'Fault')
        AND wespa.create_date LIKE concat(temp.date, '%')
        AND wespa.equipment_specific_index_value = 'true'
        <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
            wespa.system_codes  like concat('%',#{code},'%')
        </foreach>
        ) AS faultNum,
        (
        SELECT
        COUNT(1)
        FROM
        wl_equipment_specific_alarm_log wespa
        WHERE
        wespa.equipment_specific_index_key LIKE concat('%', 'PowerLoss')
        AND wespa.create_date LIKE concat(temp.date, '%')
        AND wespa.equipment_specific_index_value = 'true'
        <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
            wespa.system_codes  like concat('%',#{code},'%')
        </foreach>
        ) AS powerLossNum
        FROM
        <if test="dataType == 1">
            (

            SELECT
            date_format(
            (#{date} - INTERVAL 0 MONTH),
            '%Y-%m'
            ) AS `date`
            UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -1 MONTH),
            '%Y-%m'
            ) AS `date`)
            UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -2 MONTH),
            '%Y-%m'
            ) AS `date`)
            UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -3 MONTH),
            '%Y-%m'
            ) AS `date`)
            UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -4 MONTH),
            '%Y-%m'
            ) AS `date`)
            UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -5 MONTH),
            '%Y-%m'
            ) AS `date`)UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -6 MONTH),
            '%Y-%m'
            ) AS `date`)UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -7 MONTH),
            '%Y-%m'
            ) AS `date`)UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -8 MONTH),
            '%Y-%m'
            ) AS `date`)UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -9 MONTH),
            '%Y-%m'
            ) AS `date`)UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -10 MONTH),
            '%Y-%m'
            ) AS `date`)UNION
            (SELECT
            date_format(
            (#{date} - INTERVAL -11 MONTH),
            '%Y-%m'
            ) AS `date`))temp
        </if>
        <if test="dataType != 1">
            (
            SELECT temp.selected_date as date FROM
            (SELECT ADDDATE('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date FROM
            (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
            (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
            (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
            (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
            (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4)
            temp
            WHERE selected_date BETWEEN #{startDate} AND #{endDate}
            ) temp
            ORDER BY
            temp.date
        </if>
    </select>
    <select id="waterSystemAlarmAnalysisPie" resultType="java.util.Map">
        SELECT
        (
        SELECT
        COUNT(
        	wespa.equipment_specific_id
        )
        FROM
        wl_equipment_specific_alarm_log wespa
        WHERE
        wespa.create_date BETWEEN #{startDate}
        AND #{endDate}
        and wespa.equipment_code LIKE CONCAT('92010600','%')
        <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
            wespa.system_codes  like concat('%',#{code},'%')
        </foreach>
        ) AS firePumpsAlarmNum,
        (
        SELECT
        COUNT(
        	wespa.equipment_specific_id
        )
        FROM
        wl_equipment_specific_alarm_log wespa
        WHERE wespa.create_date BETWEEN #{startDate}
        AND #{endDate}
        and wespa.equipment_code LIKE CONCAT('92010800','%')
        <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
            wespa.system_codes  like concat('%',#{code},'%')
        </foreach>
        ) AS stabilizedPumpAlarmNum,
        (
        SELECT
        COUNT(
        	wespa.equipment_specific_id
        )
        FROM
        wl_equipment_specific_alarm_log wespa
        WHERE
        wespa.create_date BETWEEN #{startDate}
        AND #{endDate}
        and wespa.equipment_code LIKE CONCAT('92010500','%')
        <foreach collection="list" open="and (" close=")" item="code" index="index" separator="or">
            wespa.system_codes  like concat('%',#{code},'%')
        </foreach>
        ) AS controlCabinetAlarmNum

    </select>


    <select id="getSystemAndCarCount" resultType="java.util.Map">
        SELECT
        (SELECT count(*) FROM f_fire_fighting_system fs  where  fs.system_type_code <![CDATA[<>]]> 'otherSys')  fireSystem,
                (SELECT count(*) FROM wl_car) fireCar
    </select>
    <select id="getEquipExpiryStatistics" resultType="java.util.Map">
        SELECT
            a.`name`,
            a.value,
            CONCAT( CAST( ROUND(( a.VALUE / b.num )* 100,1) AS CHAR),'%') percent
        FROM
            (
                SELECT
                    CONCAT( temp.NAME, '类' ) AS `name`,
                    FORMAT(IFNULL(SUM(temp.amount),0),0) AS `value`,
                    CODE
                FROM
                    (
                        SELECT
                            es.id,
                            e.`name`,
                            DATE_FORMAT( ed.production_date, '%Y-%m-%d' ) AS productDate,
                            ed.CODE,
                            ifnull(sd.amount,0) as amount
                        FROM
                            wl_equipment_specific es
                                LEFT JOIN wl_equipment_detail ed ON ed.id = es.equipment_detail_id
                                LEFT JOIN wl_equipment e ON ed.equipment_id = e.id
                                LEFT JOIN wl_stock_detail sd ON sd.equipment_detail_id = ed.id
                        WHERE
                            ed.production_date IS NOT NULL
                          AND (
                                ( sd.`status` != 7 )
                                OR ( sd.`status` IS NULL )
                                OR ( sd.`status` = '' ))
                          AND e.expiry_date IS NOT NULL
                          AND #{expiryDayNum} >= DATEDIFF( DATE_ADD( DATE_FORMAT( ed.production_date, '%Y-%m-%d' ), INTERVAL e.expiry_date YEAR ), CURRENT_DATE )
                    ) temp
                GROUP BY
                    temp.CODE
            ) a
                LEFT JOIN ( SELECT equipment_code CODE, COUNT( 1 ) num FROM wl_equipment_specific es GROUP BY equipment_code ) b ON a.`code` = b.`code`
    </select>

    <select id="getEquipExpiryList" resultType="java.util.Map">
        SELECT
            @rownum := @rownum + 1 AS id,
	        t.*
        FROM
            ( SELECT @rownum := 0 ) r,
            (
            SELECT
            ed.`name` AS specificName,
            IFNULL( ed.standard, '' ) AS modelNumber,
            DATEDIFF( DATE_ADD( DATE_FORMAT( ed.production_date, '%Y-%m-%d' ), INTERVAL e.expiry_date YEAR ), CURRENT_DATE ) AS dayNum,
            format(nullif(SUM(sd.amount), 0), 0)  sum,
            IFNULL( ws.full_name, '' ) AS equipAddress
            FROM
            wl_equipment_specific es
            LEFT JOIN wl_equipment_detail ed ON ed.id = es.equipment_detail_id
            LEFT JOIN wl_warehouse_structure ws ON ws.id = es.warehouse_structure_id
            LEFT JOIN wl_equipment e ON ed.equipment_id = e.id
            LEFT JOIN wl_stock_detail sd ON sd.equipment_detail_id = ed.id
            WHERE
            ed.production_date IS NOT NULL
            AND (
            ( sd.`status` != 7 )
            OR ( sd.`status` IS NULL )
            OR ( sd.`status` = '' ))
            AND e.expiry_date IS NOT NULL
            AND #{expiryDayNum} >= DATEDIFF( DATE_ADD( DATE_FORMAT( ed.production_date, '%Y-%m-%d' ), INTERVAL e.expiry_date YEAR ), CURRENT_DATE )
            GROUP BY
            ed.standard,
            ed.CODE,
            equipAddress
            ORDER BY
            DATEDIFF( DATE_ADD( DATE_FORMAT( ed.production_date, '%Y-%m-%d' ), INTERVAL e.expiry_date YEAR ), CURRENT_DATE ) ASC
            ) AS t
    </select>

    <select id="getStockEquipStatistics" resultType="java.util.Map">
        SELECT
            (
                SELECT
                    count( 1 )
                FROM
                    wl_stock_detail sd
                        LEFT JOIN wl_equipment_specific es ON es.id = sd.equipment_specific_id
                WHERE
                    sd.`status` = 7
                  AND update_date LIKE CONCAT( DATE_FORMAT( CURRENT_DATE, '%Y' ), '%' )) yearScrap,
            (
                SELECT
                    count( 1 )
                FROM
                    wl_stock_detail sd
                        LEFT JOIN wl_equipment_specific es ON es.id = sd.equipment_specific_id
                WHERE
                    sd.`status` = 7
                  AND update_date LIKE CONCAT( DATE_FORMAT( CURRENT_DATE, '%Y-%m' ), '%' )) monthScrap,
            (
                SELECT
                    FORMAT(IFNULL(SUM(sd.amount),0),0)
                FROM
                    wl_equipment_specific es
                        LEFT JOIN wl_equipment_detail ed ON ed.id = es.equipment_detail_id
                        LEFT JOIN wl_equipment e ON ed.equipment_id = e.id
                        LEFT JOIN wl_stock_detail sd ON sd.equipment_detail_id = ed.id
                WHERE
                    ed.production_date IS NOT NULL
                  AND (
                        (sd.`status` != 7)
                        OR (sd.`status` IS NULL)
                        OR (sd.`status` = '')
                    )
                  AND e.expiry_date IS NOT NULL
                  AND 0 > DATEDIFF(
                        DATE_ADD(
                                DATE_FORMAT(
                                        ed.production_date,
                                        '%Y-%m-%d'
                                    ),
                                INTERVAL e.expiry_date YEAR
			),
                        CURRENT_DATE
                    )
            ) scrap,
            (
                SELECT
                    FORMAT(IFNULL(SUM(sd.amount),0),0)
                FROM
                    wl_equipment_specific es
                        LEFT JOIN wl_equipment_detail ed ON ed.id = es.equipment_detail_id
                        LEFT JOIN wl_equipment e ON ed.equipment_id = e.id
                        LEFT JOIN wl_stock_detail sd ON sd.equipment_detail_id = ed.id
                WHERE
                    ed.production_date IS NOT NULL
                  AND (
                        ( sd.`status` != 7 )
                        OR ( sd.`status` IS NULL )
                        OR ( sd.`status` = '' ))
                  AND e.expiry_date IS NOT NULL
                  AND 30 >= DATEDIFF( DATE_ADD( DATE_FORMAT( ed.production_date, '%Y-%m-%d' ), INTERVAL e.expiry_date YEAR ), CURRENT_DATE )) oneImminent,
            (
                SELECT
                    FORMAT(IFNULL(SUM(sd.amount),0),0)
                FROM
                    wl_equipment_specific es
                        LEFT JOIN wl_equipment_detail ed ON ed.id = es.equipment_detail_id
                        LEFT JOIN wl_equipment e ON ed.equipment_id = e.id
                        LEFT JOIN wl_stock_detail sd ON sd.equipment_detail_id = ed.id
                WHERE
                    ed.production_date IS NOT NULL
                  AND (
                        ( sd.`status` != 7 )
                        OR ( sd.`status` IS NULL )
                        OR ( sd.`status` = '' ))
                  AND e.expiry_date IS NOT NULL
                  AND 60 >= DATEDIFF( DATE_ADD( DATE_FORMAT( ed.production_date, '%Y-%m-%d' ), INTERVAL e.expiry_date YEAR ), CURRENT_DATE )) twoImminent,
            (
                SELECT
                    FORMAT(IFNULL(SUM(sd.amount),0),0)
                FROM
                    wl_equipment_specific es
                        LEFT JOIN wl_equipment_detail ed ON ed.id = es.equipment_detail_id
                        LEFT JOIN wl_equipment e ON ed.equipment_id = e.id
                        LEFT JOIN wl_stock_detail sd ON sd.equipment_detail_id = ed.id
                WHERE
                    ed.production_date IS NOT NULL
                  AND (
                        ( sd.`status` != 7 )
                        OR ( sd.`status` IS NULL )
                        OR ( sd.`status` = '' ))
                  AND e.expiry_date IS NOT NULL
                  AND 180 >= DATEDIFF( DATE_ADD( DATE_FORMAT( ed.production_date, '%Y-%m-%d' ), INTERVAL e.expiry_date YEAR ), CURRENT_DATE )) sixImminent
    </select>
    <select id="alarmList" resultType="java.util.Map">
        SELECT
        wlesal.id,
        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,
        wlesal.equipment_index_id AS fireEquipmentIndexId,
        wlesal.equipment_specific_code AS code,
        wlesal.equipment_specific_index_key AS fireEquipmentSpecificIndexKey,
        wlesal.equipment_specific_index_name AS fireEquipmentSpecificIndexName,
        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,
        wlesal.equipment_specific_name AS equipmentSpecificName,
        wlesal.location,
        date_format(
        wlesal.create_date,
        '%Y-%m-%d %H:%i:%s'
        ) createDate,
        wlesal.emergency_level_describe AS emergencyLevelDescribe
        FROM
        wl_equipment_specific_alarm_log wlesal
        <where>

            <if test="bizOrgCode != null and bizOrgCode != ''">
                and wlesal.biz_org_code like concat(#{bizOrgCode},'%')
            </if>

            <if test="name != null and name != ''">
                and wlesal.equipment_specific_name like concat('%', #{name},'%')
            </if>

            <if test="cleanStatus != null and cleanStatus != '' and cleanStatus == 2">
                and wlesal.clean_time IS NULL
            </if>

            <if test="cleanStatus != null and cleanStatus != '' and cleanStatus == 1">
                and wlesal.clean_time IS NOT NULL
            </if>

            <if test="handleStatus != null and handleStatus != '' and handleStatus == 2">
                and wlesal.confirm_type IS NULL
            </if>

            <if test="handleStatus != null and handleStatus != '' and handleStatus == 1">
                and wlesal.confirm_type IS NOT NULL
            </if>

            <if test="types != null">
                <foreach collection="types" index="index" item="item" open="and (" separator=" OR " close=")">
                    wlesal.type = #{item}
                </foreach>
            </if>

            <if test="emergencyLevels != null">
                <foreach collection="emergencyLevels" index="index" item="item" open="and (" separator=" OR " close=")">
                    wlesal.emergency_level = #{item}
                </foreach>
            </if>
        </where>
        ORDER BY
        wlesal.create_date DESC
    </select>

</mapper>
