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

	<sql id="Base_Column_List">
		id as id,
		eqm_id as eqmId,
		phase as phase,
		d_time as dTime,
		h_temperature as hTemperature,
		l_temperature as lTemperature,
		avg_tmp as avgTmp,
		sensor_id as sensorId,
		site_id as siteId,
		site_eqm_code as siteEqmCode,
		hw_id as hwId,
		created_time as createdTime,
		modified_time as modifiedTime,
		main_tree_id as mainTreeId,
		sub_tree_id as subTreeId,
		type as type,
		run_state as runState
	</sql>

	<select id="selectTransformerInfraredMonitoringCachePage" resultType="com.huatek.busi.vo.TransformerInfraredMonitoringCacheVo">
		select a.* from (SELECT
			t.id,
			t.eqm_id AS eqmId,
			t.phase,
			t.d_time AS dTime,
			t.h_temperature AS hTemperature,
			t.l_temperature AS lTemperature,
			t.avg_tmp AS avgTmp,
			t.sensor_id AS sensorId,
			t.site_id AS siteId,
			t.site_eqm_code AS siteEqmCode,
			t.created_time AS createdTime,
			t.modified_time AS modifiedTime,
			t.main_tree_id AS mainTreeId,
			t.sub_tree_id AS subTreeId,
			t.type,
			t.run_state AS runState,
			ext.id as equipmentId,
			ext.equipment_name AS equipmentName,
			ext.equipment_brand AS equipmentBrand,
			threshold.region_threshold AS thresholdValue,
			CONCAT_WS('', t_sensor.sensor_name, g_sensor.sensor_name, a_sensor.sensor_name) AS sensorName,
			CONCAT_WS('', t_sensor.sensor_brand, g_sensor.sensor_brand, a_sensor.sensor_brand) AS sensorBrand,
			CONCAT_WS('', t_sensor.manufactor, g_sensor.manufactor, a_sensor.manufactor) AS manufactor,
			CONCAT_WS('', t_sensor.use_date, g_sensor.use_date, a_sensor.use_date) AS useDate,
			CONCAT_WS('', t_sensor.position_des, g_sensor.position_des, a_sensor.position_des) AS installLocation
		FROM 
			transformer_infrared_monitoring_cache t
				LEFT JOIN busi_external_equipment ext ON t.sub_tree_id = ext.equipment_id
				LEFT JOIN infrared_threshold threshold ON t.sensor_id = threshold.sensor_code AND t.run_state = threshold.work_status
				LEFT JOIN busi_transformer_sensor t_sensor ON t.sensor_id = t_sensor.sensor_code AND t_sensor.status = '1'
				LEFT JOIN busi_gis_sensor g_sensor ON t.sensor_id = g_sensor.sensor_code AND g_sensor.status = '1'
				LEFT JOIN busi_arrester_sensor a_sensor ON t.sensor_id = a_sensor.sensor_code AND a_sensor.status = '1'
		<where>
			<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 DATE_FORMAT(t.d_time,'%Y-%m-%d') &gt;= DATE_FORMAT(#{dTime},'%Y-%m-%d')
			</if>
			<if test="dTimeEnd != null">
				and DATE_FORMAT(t.d_time,'%Y-%m-%d') &lt;= DATE_FORMAT(#{dTimeEnd},'%Y-%m-%d')
			</if>
		</where>
		) a ORDER BY a.sensorName ASC
	</select>

	<select id="getTransformerInfraredMonitoringCache" resultType="com.huatek.busi.entity.TransformerInfraredMonitoringCache">
		SELECT
			<include refid="Base_Column_List"/>
		FROM
			transformer_infrared_monitoring_cache t
		<where>
			<if test="sensorId != null and sensorId != ''">
				and t.sensor_id = #{sensorId}
			</if>
			<if test="siteId != null and siteId != ''">
				and t.site_id = #{siteId}
			</if>
			<if test="cacMonitorTypeCode != null and cacMonitorTypeCode != ''">
				and t.type = #{cacMonitorTypeCode}
			</if>
		</where>
		limit 1
	</select>
</mapper>