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

    <sql id="Base_Column_List">
            id as id,
            site_id as siteId,
            site_eqm_code as siteEqmCode,
            d_time as dTime,
            air_temperature as airTemperature,
            air_pressure as airPressure,
            humidity as humidity
    </sql>
    
    
    <sql id="Base_Column_List2">
            transformerCashe.site_id AS siteId, 
			transformerCashe.site_eqm_code AS siteEqmCode,
			transformerCashe.d_time AS dTime,
			transformer.sensorCode AS sensorCode,
			transformer.sensorName AS sensorName,
			transformerCashe.air_temperature AS airTemperature,
			transformerCashe.air_pressure AS airPressure,
			transformerCashe.humidity AS humidity,
			transformerCashe.wdgustspd AS wdgustspd,
			transformer.treeId AS mainEquipmentId,
			transformer.mainEquipmentCode AS mainEquipmentCode,
			transformer.subEquipmentId AS subEquipmentId,
			transformer.subEquipmentCode AS subEquipmentCode,
			byq.load_rate AS loadRate
    </sql>

    <select id="selectTransformerMeteorologicalCachePage" parameterType="com.huatek.busi.dto.TransformerMeteorologicalCacheDto" resultType="com.huatek.busi.vo.TransformerMeteorologicalCacheVo">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        transformer_meteorological_cache t
        WHERE 1=1
        <if test="id != null and id != ''">
             and t.id = #{id}
        </if>
        <if test="siteId != null and siteId != ''">
             and t.site_id = #{siteId}
        </if>
        <if test="siteEqmCode != null and siteEqmCode != ''">
             and t.site_eqm_code = #{siteEqmCode}
        </if>
        <if test="dTime != null">
             and t.d_time = #{dTime}
        </if>
        <if test="airTemperature != null">
             and t.air_temperature = #{airTemperature}
        </if>
        <if test="airPressure != null">
             and t.air-pressure = #{airPressure}
        </if>
        <if test="humidity != null">
             and t.humidity = #{humidity}
        </if>
    </select>
	<select id="getTransformerMeteorologicalCache" parameterType="java.lang.String" resultType="com.huatek.busi.entity.TransformerMeteorologicalCache">
		SELECT
        <include refid="Base_Column_List"/>
        FROM
        transformer_meteorological_cache t
        WHERE 1=1 and t.site_id = #{siteId} and t.site_eqm_code = #{siteEqmCode}
	</select>
	
	<select id="queryMeteorologicalInfo" parameterType="java.lang.String" resultType="com.huatek.busi.vo.MeteorologicalVo">
    	SELECT  
		<include refid="Base_Column_List2"/>
		FROM 
		(
		SELECT tree.id AS treeId,
		tree.name AS treeName,
		tree.system_code AS mainEquipmentCode,
				sensor.sensor_code AS sensorCode,
				sensor.sensor_name AS sensorName,
				sub.id AS subEquipmentId,
				sub.system_code AS subEquipmentCode,
				tree.system_code 
				 FROM
					busi_transformer_sensor sensor,
					busi_transformer_sub_tree sub,
					busi_site_tree tree
				WHERE sensor.equipment_id = sub.id
					AND sub.equipment_id = tree.id
					AND sensor.sensor_code = #{sensorCode}
					AND sensor.status = '1'
					AND tree.status = '1'
					AND sub.status = '1'
					) transformer
		 LEFT JOIN transformer_meteorological_cache transformerCashe
		 ON transformerCashe.site_eqm_code = transformer.system_code
		 LEFT JOIN  (
					       SELECT 
							  t.* 
							FROM
						  busi_transformer_load t   
						   RIGHT JOIN 
						    (SELECT tt.main_tree_id,MAX(load_time) AS load_time FROM busi_transformer_load tt GROUP BY main_tree_id ) tmp
						    ON t.load_time=tmp.load_time AND t.main_tree_id=tmp.main_tree_id
				    ) byq 
    ON byq.main_tree_id = transformer.treeId 
    </select>
</mapper>