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

    <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">
            arresterCashe.site_id AS siteId, 
			arresterCashe.site_eqm_code AS siteEqmCode,
			arresterCashe.d_time AS dTime,
			t.sensorCode AS sensorCode,
			t.sensorName AS sensorName,
			arresterCashe.air_temperature AS airTemperature,
			arresterCashe.air_pressure AS airPressure,
			arresterCashe.humidity AS humidity,
			transfornLoad.load_rate AS loadRate,
			t.treeId AS mainEquipmentId,
			t.mainEquipmentCode AS mainEquipmentCode,
			t.subEquipmentId AS subEquipmentId,
			t.subEquipmentCode AS subEquipmentCode
    </sql>
    
     <sql id="Base_Column_List3">
            arresterCashe.siteId,
		    arresterCashe.siteEqmCode,
		    arresterCashe.dTime,
		    arresterCashe.airTemperature,
		    arresterCashe.airPressure,
		    arresterCashe.humidity,   
		    transfornLoad.load_rate AS loadRate,
		    arresterCashe.siteId AS mainEquipmentId,
 			'' AS sensorCode,
		    '' AS sensorName,
  			'' AS mainEquipmentCode,
		    '' AS subEquipmentId,
		    '' AS subEquipmentCode	
    </sql>

    <select id="selectArresterMeteorologicalCachePage" parameterType="com.huatek.busi.dto.ArresterMeteorologicalCacheDto" resultType="com.huatek.busi.vo.ArresterMeteorologicalCacheVo">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        arrester_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="getArresterMeteorologicalCache" parameterType="java.lang.String" resultType="com.huatek.busi.entity.ArresterMeteorologicalCache">
    	SELECT
        <include refid="Base_Column_List"/>
        FROM
        arrester_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_arrester_sensor sensor,
	      busi_arrester_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') AS t
	     LEFT JOIN   arrester_meteorological_cache arresterCashe 
	    ON arresterCashe.site_eqm_code = t.system_code 
	  LEFT JOIN busi_equipment_relationship equipment 
	    ON equipment.equipment_id = t.treeId 
	  LEFT JOIN busi_transformer_load transfornLoad 
	    ON transfornLoad.equipment_id = equipment.equipment_id 
      GROUP BY arresterCashe.site_eqm_code
    </select>
    
    
     <select id="defaultMeteorologicalInfo" resultType="com.huatek.busi.vo.MeteorologicalVo">
    	SELECT  
		<include refid="Base_Column_List3"/>
		FROM 
		(
		SELECT 
		   t.`site_id` AS siteId,
		   t.site_eqm_code AS siteEqmCode,
		   t.d_time AS dTime,
		   t.air_temperature AS airTemperature,
		   t.air_pressure AS airPressure,
		   t.humidity AS humidity   
		 FROM arrester_meteorological_cache t ORDER BY `d_time` LIMIT 1) AS arresterCashe
		 LEFT JOIN  busi_site_tree siteTree ON arresterCashe.siteEqmCode=siteTree.system_code 
		 LEFT JOIN busi_equipment_relationship  equipment ON equipment.equipment_id=siteTree.`id`
		 LEFT JOIN busi_transformer_load transfornLoad ON transfornLoad.equipment_id=equipment.equipment_id
	 
    </select>
    
    
    
    
</mapper>