<?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.amos.fas.business.dao.mapper.AlarmMapper">
	<!---->
	<select id="countPageData" resultType="long">
		<!-- SELECT
		count(a.id) AS total_num
		FROM `f_alarm` a 
		inner join 
		(
		select
			b.fire_equipment_id,c.`name` as protectObj,d.*
		from 
			f_equipment_fire_equipment b,f_equipment c, f_fire_equipment d
		where  
			c.id = b.equipment_id and d.id = b.fire_equipment_id
			<if test="productArea!=null"> and d.production_area = #{productArea} </if>
			<if test="protectObj!=null"> and c.id = #{protectObj} </if>
			<if test="name!=null"> and d.name like concat(concat("%",#{name}),"%")</if>
		) sa on sa.id = a.fire_equipment_id 
		<trim prefix="WHERE" prefixOverrides="AND ">
			<if test="beginDate!=null"> and a.alarm_time >= #{beginDate} </if>
			<if test="endDate!=null"> and a.alarm_time <![CDATA[<=]]> #{endDate} </if>
	    </trim> -->
	</select>

	<select id="countAlarmData" resultType="long">
		SELECT
		count(wlesa.id) AS total_num
		FROM wl_equipment_specific_alarm wlesa
		left join wl_equipment_specific wles on wlesa.equipment_specific_id = wles.id
		left join wl_equipment_detail wled on wles.equipment_detail_id = wled.id
		where wled.`name` is not null
		<if test="protectObj !=null and protectObj != '' "> and wled.`name` like concat(concat("%",#{protectObj}),"%") </if>
		<if test="beginDate!=null"> and wlesa.create_date >= #{beginDate} </if>
		<if test="endDate!=null"> and wlesa.create_date <![CDATA[<=]]> #{endDate} </if>
	</select>
	<!--分页查询 -->
	<select id="getAlarmMapperPage" resultType="java.util.HashMap">
		<!-- SELECT
			a.id, 
			sa.code,
			sa.equip_code as equipCode,
			sa.name,
			sa.prodArea,
			sa.protectObj,
			a.create_date AS createDate,
			a.type AS type,
			a.fire_equipment_point_name AS pointName,
			a.frequency AS frequency,
			a.status AS status,
			a.recovery_date AS recoveryDate

		FROM `f_alarm` a 
		inner join
		(
		select
			b.fire_equipment_id,c.`name` as protectObj,d.production_area as prodArea,d.*
		from 
			f_equipment_fire_equipment b,f_equipment c, f_fire_equipment d
		where  
			c.id = b.equipment_id and d.id = b.fire_equipment_id
			<if test="productArea!=null"> and d.production_area = #{productArea} </if>
			<if test="protectObj!=null"> and c.id = #{protectObj} </if>
			<if test="name!=null"> and d.name like concat(concat("%",#{name}),"%")</if>
		) sa on sa.id = a.fire_equipment_id 
		<trim prefix="WHERE" prefixOverrides="AND ">
			<if test="beginDate!=null"> and a.alarm_time >= #{beginDate} </if>
			<if test="endDate!=null"> and a.alarm_time <![CDATA[<=]]> #{endDate} </if>
	    </trim>
		order by a.id
		<choose>
	     <when test="pageSize==-1"></when>
	     <when test="pageSize!=-1">limit #{offset},#{pageSize}</when>  
	    </choose> -->
	</select>


	<select id="getAlarmSingleMapperPage" resultType="java.util.HashMap">
		SELECT
		d.*
		FROM
		(
		SELECT
		wlesa.id,
		wlesa.equipment_specific_id as fireEquipmentId,
		wles.code as fireEquipmentCode,
		wled.`name` as fireEquipmentName,
		wlesa.equipment_index_id as fireEquipmentPointId,
		wlesa.equipment_specific_index_key as fireEquipmentPointCode,
		wlesa.equipment_specific_index_name as fireEquipmentPointName,
		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,
		CASE
		wlesa.type
		WHEN 'FIREALARM' THEN '火灾报警'
		WHEN 'BREAKDOWN' THEN '故障告警' ELSE wlesa.type
		END AS type,
		wlesa.update_date as createDate,
		wlesa.recovery_date as recoveryDate,
		CONCAT(IFNULL(wws.full_name,''),' ',IFNULL(wlsd.description,'')) as warehouseStructureName,
		(
		SELECT
		group_concat(DISTINCT f.`name`)
		FROM
		f_equipment_fire_equipment efe
		JOIN f_equipment f ON efe.equipment_id = f.id
		JOIN wl_equipment_specific_index ind on ind.equipment_specific_id = efe.fire_equipment_id
		) 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.source_id
		left join wl_equipment_detail wled on wles.equipment_detail_id = wled.id
		) d
		WHERE 1=1
		AND d.fireEquipmentName IS NOT NULL
			<if test="protectObj !=null and protectObj != '' "> and d.fireEquipmentName like concat(concat("%",#{protectObj}),"%") </if>
			<if test="beginDate!=null"> and d.createDate >= #{beginDate} </if>
			<if test="endDate!=null"> and d.createDate <![CDATA[<=]]> #{endDate} </if>
		order by d.createDate desc
		<choose>
			<when test="pageSize==-1"></when>
			<when test="pageSize!=-1">limit #{offset},#{pageSize}</when>
		</choose>
	</select>
</mapper>