<?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.huatek.busi.mapper.ReadTableVisualizationMapper">

    <sql id="Arrester_Column_List">
            t.id AS id,
	        bs.site_name AS siteName,
	        tree.name AS eqmName,
	        s.sensor_name AS sensorName,
	        s.position_des AS position,
	        t.dlval AS dataval,
	        t.time_stamp AS TIMESTAMP,
	        t.work_state AS workState,
	        t.picpath AS picpath 
    </sql>
    
    <sql id="Gis_Column_List">
            t.id AS id,
	        bs.site_name AS siteName,
	        tree.name AS eqmName,
	        s.sensor_name AS sensorName,
	        s.position_des AS position,
	        t.dataval AS dataval,
	        t.time_stamp AS TIMESTAMP,
	        t.work_state AS workState,
	        t.picpath AS picpath	
    </sql>
    
    <sql id="Transformer_Column_List">
            t.id AS id,
	        bs.site_name AS siteName,
	        tree.name AS eqmName,
	        s.sensor_name AS sensorName,
	        s.position_des AS position,
	        t.dataval AS dataval,
	        t.d_time AS TIMESTAMP,
	        t.work_state AS workState,
	        t.picpath AS picpath	
    </sql>

    <!-- 图片数据查询 -->
    <select id="findReadTableVisualizationPageData"  parameterType="com.huatek.busi.dto.ReadTableVisualization"
		resultType="com.huatek.busi.vo.ReadTableVisualizationVo">
        SELECT
        <include refid="Arrester_Column_List"></include>
        FROM arrester_read_table t 
        	 LEFT JOIN busi_arrester_sensor s ON t.sensor_code = s.sensor_code	
         	 LEFT JOIN busi_site bs ON t.site_id = bs.id
         	 LEFT JOIN busi_site_tree tree ON t.main_tree_id = tree.id
	       <where>
		        <if test="id != null and id != ''">
		             t.id = #{id}
		        </if>
		        <if test="eqmName != null and eqmName != '' ">
		         	tree.name like concat("%",#{eqmName},"%") 
		        </if>
		        <if test="position != null and position != '' ">
		         	and s.position_des like concat("%",#{position},"%")
		        </if>
		        <if test="sensorName != null  and sensorName != '' ">
		         	and s.sensor_name like concat("%",#{sensorName},"%")
		        </if>        
		        <if test="startDate != null and startDate != ''">
	             	and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &gt;= #{startDate}
		        </if>
		        <if test="endDate != null and endDate != ''">
		             and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &lt; #{endDate}
		        </if>		        
		        <if test="dataval != null and dataval != ''">
		             t.dlval = #{dataval}
		        </if>		        
		        <if test="workState != null and workState != ''">
		             t.work_state = #{workState}
		        </if>
	       </where>
       UNION
        SELECT
        <include refid="Gis_Column_List"></include>
        FROM gis_read_table t 
        	 LEFT JOIN busi_gis_sensor s ON t.sensor_code = s.sensor_code	
         	 LEFT JOIN busi_site bs ON t.site_id = bs.id
         	 LEFT JOIN busi_site_tree tree ON t.main_tree_id = tree.id
	      <where>
	        	 <if test="id != null and id != ''">
		             t.id = #{id}
		        </if>
		        <if test="eqmName != null and eqmName != '' ">
		         	tree.name like concat("%",#{eqmName},"%") 
		        </if>
		        <if test="position != null and position != '' ">
		         	and s.position_des like concat("%",#{position},"%")
		        </if>
		        <if test="sensorName != null  and sensorName != '' ">
		         	and s.sensor_name like concat("%",#{sensorName},"%")
		        </if>        
		        <if test="startDate != null and startDate != ''">
	             	and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &gt;= #{startDate}
		        </if>
		        <if test="endDate != null and endDate != ''">
		             and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &lt; #{endDate}
		        </if>		        
		        <if test="dataval != null and dataval != ''">
		             t.dlval = #{dataval}
		        </if>		        
		        <if test="workState != null and workState != ''">
		             t.work_state = #{workState}
		        </if>
	      </where>
        UNION
       
        SELECT
        <include refid="Transformer_Column_List"></include>
        FROM transformer_read_table t 
        	 LEFT JOIN busi_transformer_sensor s ON t.sensor_code = s.sensor_code	 
         	 LEFT JOIN busi_site bs ON t.site_id = bs.id 
         	 LEFT JOIN busi_site_tree tree ON t.main_tree_id = tree.id 
	       <where>
		        <if test="id != null and id != ''">
		             t.id = #{id}
		        </if>
		        <if test="eqmName != null and eqmName != '' ">
		         	tree.name like concat("%",#{eqmName},"%") 
		        </if>
		        <if test="position != null and position != '' ">
		         	and s.position_des like concat("%",#{position},"%")
		        </if>
		        <if test="sensorName != null  and sensorName != '' ">
		         	and s.sensor_name like concat("%",#{sensorName},"%")
		        </if>        
		        <if test="startDate != null and startDate != ''">
	             	and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &gt;= #{startDate}
		        </if>
		        <if test="endDate != null and endDate != ''">
		             and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &lt; #{endDate}
		        </if>		        
		        <if test="dataval != null and dataval != ''">
		             t.dlval = #{dataval}
		        </if>		        
		        <if test="workState != null and workState != ''">
		             t.work_state = #{workState}
		        </if>
	       </where>
	      ORDER BY TIMESTAMP DESC 
    </select>

	<!-- 图片实时数据查询 -->
	<sql id="Base_Column_List">
		sensor.id AS sensor_id,
		sensor.site_id,
		tree.id AS main_tree_id,
		sub.id AS sub_tree_id,
		sensor.sensor_code,
		sensor.sensor_name,
		sensor.sensor_brand,
		ext.equipment_name,
		sensor.sensor_name,
		sensor.manufactor,
		sensor.use_date,
		sensor.position_des,
		ext.equipment_brand
	</sql>
	<resultMap id="Read_Table_Current_ResultMap" type="com.huatek.busi.vo.ReadTableCurrentVo">
		<id column="sensor_id" property="sensorId" />
		<result column="site_id" property="siteId"/>
		<result column="main_tree_id" property="mainTreeId"/>
		<result column="sub_tree_id" property="subTreeId"/>
		<result column="sensor_code" property="sensorCode"/>
		<result column="sensor_name" property="sensorBrand"/>
		<result column="sensor_brand" property="equipmentName"/>
		<result column="equipment_name" property="manufactor"/>
		<result column="manufactor" property="siteId"/>
		<result column="use_date" property="useDate"/>
		<result column="position_des" property="installLocation"/>
		<result column="equipment_brand" property="equipmentBrand"/>
		<result column="eq_type" property="eqType"/>
	</resultMap>

	<resultMap id="Trans_Read_Table_Current_DataMap" type="com.huatek.busi.vo.readtable.TransReadTableCurrentVo">
		<id column="id" property="id" />
		<result column="dataval" property="dlval"/>
		<result column="d_time" property="dTime"/>
		<result column="picpath" property="picPath"/>
		<result column="work_state" property="runState"/>
		<result column="threshold" property="thresholdVal"/>
	</resultMap>

	<select id="getTransReadTableData" resultMap="Trans_Read_Table_Current_DataMap">
		SELECT
			t.id ,
			t.dataval,
			t.d_time,
			t.picpath,
			t.work_state,
			threshold.threshold
		FROM 
			transformer_read_table t
			LEFT JOIN photo_threshold threshold ON t.main_tree_id = threshold.main_tree_id
				AND t.sensor_code = threshold.sensor_code
				AND t.work_state = threshold.work_status
		WHERE t.sensor_code = #{sensorCode}
		ORDER BY t.d_time DESC
		LIMIT 1
	</select>

	<select id="getArresterReadTableData" resultMap="Trans_Read_Table_Current_DataMap">
		SELECT
			t.id,
			t.dlval AS dataval,
			t.time_stamp AS d_time,
			t.picpath,
			t.work_state,
			threshold.threshold
		FROM
			arrester_read_table t
			LEFT JOIN photo_threshold threshold ON t.main_tree_id = threshold.main_tree_id
				AND t.sensor_code = threshold.sensor_code
				AND t.work_state = threshold.work_status
		WHERE
			t.sensor_code = #{sensorCode}
		ORDER BY t.time_stamp DESC
		LIMIT 1
	</select>

	<select id="getGisReadTableData" resultMap="Trans_Read_Table_Current_DataMap">
		SELECT
			t.id,
			t.dataval,
			t.time_stamp AS d_time,
			t.picpath,
			t.work_state,
			threshold.threshold
		FROM gis_read_table t
		  LEFT JOIN photo_threshold threshold
		    ON t.main_tree_id = threshold.main_tree_id
		    AND t.sensor_code = threshold.sensor_code
		    AND t.work_state = threshold.work_status
		WHERE
			t.sensor_code = #{sensorCode}
		ORDER BY t.time_stamp DESC
		LIMIT 1
	</select>

	<!-- 避雷器图片实时数据查询 -->
	<resultMap id="Arrester_Read_Table_Current_ResultMap" type="com.huatek.busi.vo.ReadTableCurrentVo" extends="Read_Table_Current_ResultMap">
		<association property="data" column="{sensorCode=sensor_code}" select="getArresterReadTableData"/>
	</resultMap>
	<select id="findArresterReadTableCurrentPageData" resultMap="Arrester_Read_Table_Current_ResultMap">
		SELECT
			<include refid="Base_Column_List"/>,
			'3' AS eq_type
		FROM busi_arrester_sensor sensor,
		     busi_arrester_sub_tree sub,
		     busi_external_equipment ext,
		     busi_site_tree tree
		WHERE sensor.equipment_id = sub.id
			AND sub.equipment_id = tree.id
			AND ext.equipment_id = sub.id
			AND sensor.equipment_type = '051'
			AND sensor.status = '1'
			<if test="equipmentName != null and equipmentName != ''">
				and ext.equipment_name like CONCAT("%",#{equipmentName},"%")
			</if>
			<if test="equipmentBrand != null and equipmentBrand != ''">
				and ext.equipment_brand like CONCAT("%",#{equipmentBrand},"%")
			</if>
			<if test="dTime != null and dTimeEnd != null">
				AND exists (select 1 from arrester_read_table rt 
					where 
						rt.sensor_code = sensor.sensor_code
					having max(rt.time_stamp) &gt;= #{dTime}
						and max(rt.time_stamp) &lt; #{dTimeEnd}
					order by rt.time_stamp desc 
					limit 1)
			</if>
			<if test="dTime == null and dTimeEnd == null">
				AND exists (select 1 from arrester_read_table rt 
				where rt.sensor_code = sensor.sensor_code order by rt.time_stamp desc limit 1)
			</if>
			<if test="dTime != null and dTimeEnd == null">
				AND exists (select 1 from arrester_read_table rt 
					where 
						rt.sensor_code = sensor.sensor_code
					having max(rt.time_stamp) &gt;= #{dTime}
					order by rt.d_time desc 
					limit 1)
			</if>
			<if test="dTime == null and dTimeEnd != null">
				AND exists (select 1 from arrester_read_table rt 
					where 
						rt.sensor_code = sensor.sensor_code
					having max(rt.time_stamp) &lt; #{dTimeEnd}
					order by rt.time_stamp desc 
					limit 1)
			</if>
	</select>

	<!-- GIS图片实时数据 -->
	<resultMap id="Gis_Read_Table_Current_ResultMap" type="com.huatek.busi.vo.ReadTableCurrentVo" extends="Read_Table_Current_ResultMap">
		<association property="data" column="{sensorCode=sensor_code}" select="getGisReadTableData"/>
	</resultMap>
	<select id="findGisReadTableCurrentPageData" resultMap="Gis_Read_Table_Current_ResultMap">
		SELECT
			<include refid="Base_Column_List"/>,
			'2' AS eq_type
		FROM busi_gis_sensor sensor,
		     busi_gis_sub_tree sub,
		     busi_external_equipment ext,
		     busi_site_tree tree
		WHERE sensor.equipment_id = sub.id
			AND sub.equipment_id = tree.id
			AND ext.equipment_id = sub.id
			AND sensor.equipment_type = '061'
			AND sensor.status = '1'
			<if test="equipmentName != null and equipmentName != ''">
				and ext.equipment_name like CONCAT("%",#{equipmentName},"%")
			</if>
			<if test="equipmentBrand != null and equipmentBrand != ''">
				and ext.equipment_brand like CONCAT("%",#{equipmentBrand},"%")
			</if>
			<if test="dTime != null and dTimeEnd != null">
				AND exists (select 1 from gis_read_table rt 
					where 
						rt.sensor_code = sensor.sensor_code
					having max(rt.time_stamp) &gt;= #{dTime}
						and max(rt.time_stamp) &lt; #{dTimeEnd}
					order by rt.time_stamp desc 
					limit 1)
			</if>
			<if test="dTime == null and dTimeEnd == null">
				AND exists (select 1 from gis_read_table rt 
				where rt.sensor_code = sensor.sensor_code order by rt.time_stamp desc limit 1)
			</if>
			<if test="dTime != null and dTimeEnd == null">
				AND exists (select 1 from gis_read_table rt 
					where 
						rt.sensor_code = sensor.sensor_code
					having max(rt.time_stamp) &gt;= #{dTime}
					order by rt.time_stamp desc 
					limit 1)
			</if>
			<if test="dTime == null and dTimeEnd != null">
				AND exists (select 1 from gis_read_table rt 
					where 
						rt.sensor_code = sensor.sensor_code
					having max(rt.time_stamp) &lt; #{dTimeEnd}
					order by rt.time_stamp desc 
					limit 1)
			</if>
	</select>

	<!-- 变压器——图片实时数据查询 -->
	<resultMap id="Trans_Read_Table_Current_ResultMap" type="com.huatek.busi.vo.ReadTableCurrentVo" extends="Read_Table_Current_ResultMap">
		<association property="data" column="{sensorCode=sensor_code}" select="getTransReadTableData"/>
	</resultMap>

	<select id="findTransformerReadTableCurrentPageData" resultMap="Trans_Read_Table_Current_ResultMap">
		SELECT
			<include refid="Base_Column_List"/>,
			'1' AS eq_type
		FROM busi_transformer_sensor sensor,
			busi_transformer_sub_tree sub,
			busi_external_equipment ext,
			busi_site_tree tree
		WHERE sensor.equipment_id = sub.id
			AND sub.equipment_id = tree.id
			AND ext.equipment_id = sub.id
			AND sensor.equipment_type = '051'
			AND sensor.status = '1'
			<if test="equipmentName != null and equipmentName != ''">
				and ext.equipment_name like CONCAT("%",#{equipmentName},"%")
			</if>
			<if test="equipmentBrand != null and equipmentBrand != ''">
				and ext.equipment_brand like CONCAT("%",#{equipmentBrand},"%")
			</if>
			<if test="dTime != null and dTimeEnd != null">
				AND exists (select 1 from transformer_read_table rt 
					where 
						rt.sensor_code = sensor.sensor_code
					having max(rt.d_time) &gt;= #{dTime}
						and max(rt.d_time) &lt; #{dTimeEnd}
					order by rt.d_time desc 
					limit 1)
			</if>
			<if test="dTime == null and dTimeEnd == null">
				AND exists (select 0 from transformer_read_table rt 
				where rt.sensor_code = sensor.sensor_code order by rt.d_time desc limit 1)
			</if>
			<if test="dTime != null and dTimeEnd == null">
				AND exists (select 1 from transformer_read_table rt 
					where 
						rt.sensor_code = sensor.sensor_code
					having max(rt.d_time) &gt;= #{dTime}
					order by rt.d_time desc 
					limit 1)
			</if>
			<if test="dTime == null and dTimeEnd != null">
				AND exists (select 1 from transformer_read_table rt 
					where 
						rt.sensor_code = sensor.sensor_code
					having max(rt.d_time) &lt; #{dTimeEnd}
					order by rt.d_time desc 
					limit 1)
			</if>
	</select>
</mapper>