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

    <sql id="Base_Column_List">
            phase as phase,
            precipitation as precipitation,
            radiation_intensity as radiationIntensity,
            air_temperature as airTemperature,
            site_id as siteId,
            humidity as humidity,
            air_pressure as airPressure,
            precipitation_intensity as precipitationIntensity
    </sql>
	<sql id="Base_Column_List2">
            m.phase as phase,
            m.precipitation as precipitation,
            m.radiation_intensity as radiationIntensity,
            m.air_temperature as airTemperature,
            m.site_id as siteId,
            m.humidity as humidity,
            m.time_stamp as timeStamp,
            m.air_pressure as airPressure,
            CONCAT_WS('', t_sensor.sensor_code, g_sensor.sensor_code, a_sensor.sensor_code) AS sensorCode,
            CONCAT_WS('', t_sensor.sensor_name, g_sensor.sensor_name, a_sensor.sensor_name) AS sensorName,
            m.precipitation_intensity as precipitationIntensity
    </sql>

	<select id="queryMonitorDataList" resultType="java.util.Map">
		SELECT
			t.sensor_id AS sensorCode,
			t.main_tree_id AS mainTreeId,
			t.main_equipment_id AS mainEquipmentId,
			t.sub_tree_id AS subTreeId,
			t.sub_equipment_id AS subEquipmentId,
			t.site_id AS siteId,
			t.phase,
		t.cac_monitor_type_code AS cacMonitorTypeCode,
			CAST(AVG(t.air_temperature) AS decimal(20, 4)) AS airTemperature,
			CAST(AVG(t.air_pressure) AS decimal(20, 4)) AS airPressure,
			CAST(AVG(t.humidity) AS decimal(20, 4)) AS humidity,
			CAST(AVG(t.precipitation) AS decimal(20, 4)) AS precipitation,
			CAST(AVG(t.precipitation_intensity) AS decimal(20, 4)) AS precipitationIntensity,
			CAST(AVG(t.radiation_intensity) AS decimal(20, 4)) AS radiationIntensity
		FROM transformer_meteorological t
		WHERE t.time_stamp &gt;= #{startTime}
			AND t.time_stamp &lt; #{endTime}
		GROUP BY t.sensor_id,
				t.main_tree_id,
				t.main_equipment_id,
				t.sub_tree_id,
				t.sub_equipment_id,
				t.site_id,
				t.phase,
				t.cac_monitor_type_code
	</select>
	<!-- 查询CAC气象原始数据 -->
	<select id="findCACPageData" resultType="java.util.Map">
		select 
			<include refid="Base_Column_List2"/>,
			(select tree.system_code from busi_site_tree tree where tree.id = m.main_tree_id) as systemCode,
			(select tree.name from busi_site_tree tree where tree.id = m.main_tree_id) as equipmentName
		from transformer_meteorological m
			left join busi_transformer_sensor t_sensor ON m.sensor_id = t_sensor.sensor_code
	        left join busi_gis_sensor g_sensor ON m.sensor_id = g_sensor.sensor_code
	        left join busi_arrester_sensor a_sensor ON m.sensor_id = a_sensor.sensor_code
		<where>
			CONCAT_WS('', t_sensor.status, g_sensor.status, a_sensor.status) = '1'
			<if test="cacMonitorTypeCode != null and cacMonitorTypeCode != ''">
	             and m.cac_monitor_type_code = #{cacMonitorTypeCode}
	        </if>
			<if test="mainTreeId != null and mainTreeId != ''">
	             and m.main_tree_id = #{mainTreeId}
	        </if>
	        <if test="subTreeId != null and subTreeId != ''">
	             and m.sub_tree_id = #{subTreeId}
	        </if>
			<if test="timeStamp != null and timeStamp != ''">
				and DATE_FORMAT(m.time_stamp,'%Y-%m-%d') &gt;=  #{timeStamp}
			</if>
			<if test="timeStampEnd != null and timeStampEnd != ''">
				and DATE_FORMAT(m.time_stamp,'%Y-%m-%d') &lt;= #{timeStampEnd}
			</if>
			<if test="sensorCode != null and sensorCode != ''">
				AND t.sensor_id = #{sensorCode}
			</if>
		</where>
		order by m.time_stamp desc
	</select>
	<!-- 数据输入变压器气象监测原始数据导出 -->
    <select id="exportCacMonitorData" resultType="com.huatek.busi.vo.TransformerMeteorologicalVo">
        SELECT
        <include refid="Base_Column_List2"/>,
        (select tree.system_code from busi_site_tree tree where tree.id = m.main_tree_id) as systemCode,
        (select tree.name from busi_site_tree tree where tree.id = m.main_tree_id) as equipmentName
        from transformer_meteorological m
		left join busi_transformer_sensor t_sensor ON m.sensor_id = t_sensor.sensor_code
        left join busi_gis_sensor g_sensor ON m.sensor_id = g_sensor.sensor_code
        left join busi_arrester_sensor a_sensor ON m.sensor_id = a_sensor.sensor_code
       <where>
	        <if test="cacMonitorTypeCode != null and cacMonitorTypeCode != ''">
	             m.cac_monitor_type_code = #{cacMonitorTypeCode}
	        </if>
			<if test="mainTreeId != null and mainTreeId != ''">
	             and m.main_tree_id = #{mainTreeId}
	        </if>
			<if test="timeStamp != null and timeStamp != ''">
				and DATE_FORMAT(m.time_stamp,'%Y-%m-%d') &gt;=  #{timeStamp}
			</if>
			<if test="timeStampEnd != null and timeStampEnd != ''">
				and DATE_FORMAT(m.time_stamp,'%Y-%m-%d') &lt;= #{timeStampEnd}
			</if>
		   <if test="sensorId !=null and sensorId !=''">
			   AND m.sensor_id = #{sensorId}
		   </if>
       </where>
        ORDER BY m.time_stamp DESC
    </select>

	<select id="getListEquInfo" resultType="com.huatek.busi.vo.MeteorologicalStatisticsVo">
		SELECT
			t.site_id AS siteId,
			t.main_tree_id AS mainTreeId,
			<!-- t.sub_tree_id AS subTreeId, -->
			t.main_equipment_id AS mainEquipmentId,
			<!-- t.sub_equipment_id AS subEquipmentId,
			t.sensor_id AS sensorCode, -->
			t.cac_monitor_type_code AS cacMonitorTypeCode,
			CAST(AVG(t.air_temperature) AS decimal(20, 4)) AS avgAirTemperature,
			CAST(AVG(t.air_pressure) AS decimal(20, 4)) AS avgAirPressure,
			CAST(AVG(t.humidity) AS decimal(20, 4)) AS avgHumidity,
			CAST(AVG(t.precipitation) AS decimal(20, 4)) AS avgPrecipitation,
			CAST(AVG(t.precipitation_intensity) AS decimal(20, 4)) AS avgPrecipitationIntensity,
			CAST(AVG(t.radiation_intensity) AS decimal(20, 4)) AS avgRadiationIntensity,
			CAST(AVG(t.wdgustspd) AS decimal(20, 4)) AS avgWdgustspd,
			COUNT(*) AS dataCount
		FROM
			transformer_meteorological t
		WHERE 
			t.cac_monitor_type_code = #{cacMonitorTypeCode}
			AND t.time_stamp &gt;= #{startTime}
			AND t.time_stamp &lt; #{endTime}
			<if test="sensorCode != null and sensorCode != ''">
				AND t.sensor_id = #{sensorCode}
			</if>
		GROUP BY t.site_id,
		         t.main_tree_id,
		         <!-- t.sub_tree_id,
		         t.sensor_id, -->
		         t.cac_monitor_type_code
	</select>

	<select id="findSensorCodeList" resultType="com.huatek.busi.dto.curve.BaseCurveQueryDto">
		SELECT DISTINCT
			t.sensor_id as sensorCode,
			t.main_tree_id as mainTreeId,
			count(*) as count
		FROM transformer_meteorological t
		WHERE t.time_stamp &gt;= #{startTime} and t.time_stamp &lt; #{endTime}
		GROUP BY t.sensor_id, t.main_tree_id
	</select>

	<select id="findDaylyPointList" resultType="com.huatek.busi.vo.curve.PointItemVo">
		SELECT
			t.sensor_id as sensorCode,
			t.time_stamp as timeStamp,
			CAST(t.${subType} AS decimal(20, 4)) as val
		FROM transformer_meteorological t
		WHERE t.main_tree_id = #{dto.mainTreeId}
		<if test="dto.sensorCode != null and dto.sensorCode != ''">
			AND t.sensor_id = #{dto.sensorCode}
		</if>
			AND t.time_stamp &gt;= #{startTime} 
			AND t.time_stamp &lt; #{endTime}
		ORDER BY t.time_stamp ASC
	</select>

	<select id="getDaylyPointStatistics" resultType="com.huatek.busi.dto.curve.PointItemDto">
		SELECT
			t.main_tree_id AS mainTreeId,
			t.sensor_id AS sensorCode,
			COUNT(*) AS count,
			CAST(SUM(t.${subType}) AS decimal(20, 4)) AS total,
			MAX(CAST(t.${subType} AS decimal(20, 4))) AS max,
			MIN(CAST(t.${subType} AS decimal(20, 4))) AS min
		FROM transformer_meteorological t
		WHERE t.main_tree_id = #{mainTreeId}
		<if test="sensorCode != null and sensorCode != ''">
			AND t.sensor_id = #{sensorCode}
		</if>
			AND t.time_stamp &gt;= #{startTime} 
			AND t.time_stamp &lt;= #{endTime}
	</select>
</mapper>