<?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.ConfirmAlarmMapper">
    <select id="getDetailsById" resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarmLog">
		select
			ala.id,
			(
			SELECT
			sou.scene_id
			FROM
			wl_source_scene sou
			WHERE
			find_in_set(ala.equipment_specific_id,sou.point_in_scene)>0
		    and EXISTS (SELECT distinct fi.instance_id from wl_form_instance fi where fi.instance_id = sou.source_id)
			limit 1
			) AS sceneId,
			(
			SELECT
			sou.source_id
			FROM
			wl_source_scene sou
			WHERE
			find_in_set(ala.equipment_specific_id,sou.point_in_scene)>0
			and EXISTS (SELECT distinct fi.instance_id from wl_form_instance fi where fi.instance_id = sou.source_id)
			limit 1
			) AS sourceId,
			ala.equipment_specific_id,
			spe.code AS equipmentSpecificCode,
			ala.equipment_specific_index_name,
		    (select  type_name from wl_signal_classify sc where sc.type_code = ala.type limit 1) as equipmentSpecificIndexName,
			ala.type as equipment_specific_index_key,
			concat(ala.equipment_specific_name,ala.equipment_specific_index_name) as alarmContent,
			ala.location AS alarmPlace,
			ala.create_date as create_date,
			ala.location as location,
 			-- if(spe.code is null,ala.equipment_specific_name,concat(ala.equipment_specific_name,'(',spe.code,')')) as equipmentName,
			ala.equipment_specific_name as equipmentName,
			(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 = ala.equipment_specific_id
			) as protectName,
			if(confirm_type is null,'去确认','已处理') isConfirm,
			confirm_type,
		    (select type_name from wl_signal_classify sc where sc.type_code = ala.confirm_type limit 1) as confirmTypeName,
			alarm_reason,
			resolve_result,
			confirm_user,
			confirm_user_name,
			confirm_date,
			(SELECT GROUP_CONCAT(fem.name) FROM `f_fire_fighting_system` fem WHERE find_in_set(fem.id,spe.system_id)) as systemName,
			if(ala.clean_time is null, '未消除', '已消除') AS cleanStatus,
			ala.clean_time,
			ala.defect_batch_id,
		    spe.biz_org_name AS  bizOrgName
        from
            wl_equipment_specific_alarm_log as ala
        left join wl_equipment_specific as spe on spe.id = ala.equipment_specific_id
        <where>
        <choose>
        	<when test="equipId != null and equipId != ''">
        		 ala.equipment_specific_id =#{equipId}
        		 order by ala.create_date desc limit 1
        	</when>
        	<otherwise>
        	 	ala.id =#{id} limit 1
        	</otherwise>
        </choose>
        </where>
    </select>
    <update id="confirmAlam" >
        update
        wl_equipment_specific_alarm_log
        set
        confirm_date = now(),
        confirm_type = #{confirmType},
		confirm_type_name = #{confirmTypeName},
        alarm_reason = #{alarmReason},
        resolve_result = #{resolveResult},
        confirm_user = #{confirmUser},
        confirm_user_name = #{confirmUserName}
        <where>
			confirm_date IS NULL
			<if test="isBatch == 0">
				AND id = #{id}
			</if>
			<if test="isBatch == 1">
				AND equipment_specific_id = #{equipmentSpecificId} AND equipment_specific_index_key = #{equipmentSpecificIndexKey}
			</if>
		</where>
    </update>
    <select id="getIdAndConfirmType" resultType="com.yeejoin.equipmanage.common.entity.EquipmentSpecificAlarmLog">
        select id,confirm_type,create_date,equipment_specific_alarm_id
            from wl_equipment_specific_alarm_log
            where equipment_specific_id =  #{id}
            order by confirm_type asc,equipment_specific_index_key desc,create_date desc
            limit 1
    </select>

	<select id="getFireAlarm" resultType="java.lang.Integer">
		SELECT
		count(1)
		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,
		wws.full_name 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>
			wlesal.confirm_type is null
			and wlesa.status = '1'
		</where>
		) d
		<where>
			d.fireEquipmentName IS NOT null
			AND d.type = 'FIREALARM'
		</where>
		ORDER BY
		d.createDate desc
    </select>

	<select id="getFaultAlarm" resultType="java.lang.Integer">
		SELECT
		count(1)
		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,
		wws.full_name 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>
			wlesal.confirm_type is null

		</where>
		) d
		<where>
			d.fireEquipmentName IS NOT null
			AND d.type = 'BREAKDOWN'
		</where>
		ORDER BY
		d.createDate desc
    </select>

</mapper>
