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

    <sql id="Base_Column_List">
            t.id as id,
            t.cac_id as cacId,
            t.data_node_num as dataNodeNum,
            t.sensor_id as sensorId,
            s.sensor_name as sensorName,
            t.cac_monitor_type_code as cacMonitorTypeCode,
            t.equipment_id as equipmentId,
            t.time_stamp as timeStamp,
            t.equipment_code as equipmentCode,
            t.ext_equipment_code as extEquipmentCode,
            t.site_id as siteId,
            t.phase as phase,
            t.temperature as temperature,
            t.absolute_pressure as absolutePressure,
            t.density as density,
            t.moisture as moisture,
            t.pressure20C as pressure20c,
            t.main_equipment_id as mainEquipmentId,
            t.sub_equipment_id as subEquipmentId,
            t.main_tree_id as mainTreeId,
            t.sub_tree_id as subTreeId, 
            t.pressure as pressure,
            t.valided
    </sql>
    <select id="findCACPageData" resultType="java.util.Map">
		select 
			<include refid="Base_Column_List"/>,
			(select tree.system_code from busi_site_tree tree where tree.id = t.main_tree_id) as systemCode,
			(select tree.name from busi_site_tree tree where tree.id = t.main_tree_id) as equipmentName
		from
			gis_sf6_pressure t,
			busi_gis_sensor s
		<where>
			t.sensor_id = s.sensor_code and s.status = '1'
			<if test="cacMonitorTypeCode != null and cacMonitorTypeCode != ''">
	             and t.cac_monitor_type_code = #{cacMonitorTypeCode}
	        </if>
			<if test="mainTreeId != null and mainTreeId != ''">
	             and t.main_tree_id = #{mainTreeId}
	        </if>
	        <if test="subTreeId != null and subTreeId != ''">
	             and t.sub_tree_id = #{subTreeId}
	        </if>
			<if test="timeStamp != null and timeStamp != ''">
             	and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &gt;= #{timeStamp}
	        </if>
	        <if test="timeStampEnd != null and timeStampEnd != ''">
	             and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &lt;= #{timeStampEnd}
	        </if>
			<if test="sensorCode != null and sensorCode != ''">
				AND t.sensor_id = #{sensorCode}
			</if>
		</where>
		order by t.time_stamp desc
	</select>
	<select id="exportCacMonitorData" resultType="com.huatek.busi.vo.GisSf6PressureVo">
		select 
			<include refid="Base_Column_List"/>,
			(select tree.system_code from busi_site_tree tree where tree.id = t.main_tree_id) as systemCode,
			(select tree.name from busi_site_tree tree where tree.id = t.main_tree_id) as equipmentName
		from gis_sf6_pressure t
		left join busi_gis_sensor s  ON t.sensor_id = s.sensor_code
		<where>
			<if test="cacMonitorTypeCode != null and cacMonitorTypeCode != ''">
	             and t.cac_monitor_type_code = #{cacMonitorTypeCode}
	        </if>
			<if test="mainTreeId != null and mainTreeId != ''">
	             and t.main_tree_id = #{mainTreeId}
	        </if>
	        <if test="sensorCode != null and sensorCode != ''">
	             and t.sensor_id = #{sensorCode}
	        </if>
			<if test="timeStamp != null and timeStamp != ''">
             	and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &gt;= #{timeStamp}
	        </if>
	        <if test="timeStampEnd != null and timeStampEnd != ''">
	             and DATE_FORMAT(t.time_stamp,'%Y-%m-%d') &lt;= #{timeStampEnd}
	        </if>
			<if test="sensorId !=null and sensorId !=''">
				AND t.sensor_id = #{sensorId}
			</if>
		</where>
		order by t.time_stamp desc
	</select>
	<select id="exportReportData"  resultType="java.util.Map">
		select 
            t.sensor_id as sensorId,
            t.temperature as temperature,
            t.moisture as moisture,
            t.pressure as pressure,
            DATE_FORMAT(t.time_stamp, '%Y-%m-%d %H:%i:%S') as timeStamp,
            t.phase as phase,
            t.absolute_pressure as absolutePressure,
            t.density as density,
            t.pressure20C as pressure20c           
		from gis_sf6_pressure t
		<where>
	        <if test="sensorCode != null and sensorCode != ''">
	             and t.sensor_id = #{sensorCode}
	        </if>
			<if test="timeStamp != null">
             	and DATE_FORMAT(t.time_stamp,'%Y-%m-%d %H:%M:%S') &gt;= #{timeStamp}
	        </if>
	        <if test="timeStampEnd != null">
	             and DATE_FORMAT(t.time_stamp,'%Y-%m-%d %H:%M:%S') &lt;= #{timeStampEnd}
	        </if>
		</where>
		order by t.time_stamp desc
	</select>
	<select id="selectByStatus" resultType="com.huatek.busi.vo.stability.SF6StabilityVo" >
	   	SELECT
			t.id,
			t.temperature,
			t.moisture,
			t.absolute_pressure AS pressure
			t.sensor_id as sensorCode,
			t.time_stamp AS collectionTime
		FROM
			`gis_sf6_pressure` t
		WHERE
			t.sensor_id = #{sensorCode}
			AND t.run_state = #{status}
			and t.valided = '1'
			<if test="beginTime != null">
				AND t.time_stamp &gt;= #{beginTime}
			</if>
			<if test="endTime != null">
				AND t.time_stamp &lt;= #{endTime}
			</if>
			ORDER BY
				t.time_stamp DESC
    </select>

	<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,
		  CAST(AVG(t.temperature) AS decimal(20, 4)) AS temperature,
		  CAST(AVG(t.absolute_pressure) AS decimal(20, 4)) AS absolutePressure,
		  CAST(AVG(t.density) AS decimal(20, 4)) AS density,
		  CAST(AVG(t.moisture) AS decimal(20, 4)) AS moisture,
		  CAST(AVG(t.pressure20C) AS decimal(20, 4)) AS pressure20C,
		  CAST(AVG(t.equipment_sensitivity) AS decimal(20, 4)) AS equipmentSensitivity,
		  CAST(AVG(t.daily_leak_rate) AS decimal(20, 4)) AS dailyLeakRate
		FROM gis_sf6_pressure 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
	</select>

	<select id="getListEquInfo" resultType="com.huatek.busi.vo.GisSf6PressureStatisticsVo">
		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.temperature) AS decimal(20, 4)) AS avgTemperature,
			CAST(avg(t.absolute_pressure) AS decimal(20, 4)) AS avgAbsolutePressure,
			CAST(avg(t.density) AS decimal(20, 4)) AS avgDensity,
			CAST(avg(t.moisture) AS decimal(20, 4)) AS avgMoisture,
			CAST(avg(t.pressure20C) AS decimal(20, 4)) AS avgPressure20C,
			CAST(avg(t.daily_leak_rate) AS decimal(20, 4)) AS aveDailyLeakRate,
			COUNT(*) AS dataCount
		FROM 
			gis_sf6_pressure t
		WHERE 
			t.cac_monitor_type_code = #{cacMonitorTypeCode}
			AND t.valided = '1'
			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 gis_sf6_pressure 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 gis_sf6_pressure 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 gis_sf6_pressure 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>
	
	<select id="getRecordBySensorCode" resultType="java.util.Map">
		SELECT
		   t.sensor_id AS sensorCode,
		   t.pressure as pressure
		FROM gis_sf6_pressure t
		WHERE t.time_stamp &gt;= #{startTime}
			AND t.time_stamp &lt; #{endTime}
		    AND	t.sensor_id=#{sensorCode}
		ORDER BY t.time_stamp  ASC
	</select>
	
	<select id="queryAll" resultType="com.huatek.busi.entity.GisSf6Pressure">
		SELECT
		    t.id as id,
            t.cac_id as cacId,
            t.data_node_num as dataNodeNum,
            t.sensor_id as sensorId,
            t.cac_monitor_type_code as cacMonitorTypeCode,
            t.equipment_id as equipmentId,
            t.time_stamp as timeStamp,
            t.equipment_code as equipmentCode,
            t.ext_equipment_code as extEquipmentCode,
            t.site_id as siteId,
            t.phase as phase,
            t.temperature as temperature,
            t.absolute_pressure as absolutePressure,
            t.density as density,
            t.moisture as moisture,
            t.pressure20C as pressure20c,
            t.main_equipment_id as mainEquipmentId,
            t.sub_equipment_id as subEquipmentId,
            t.main_tree_id as mainTreeId,
            t.sub_tree_id as subTreeId, 
            t.pressure as pressure
		FROM gis_sf6_pressure t ORDER BY t.`time_stamp` DESC
	</select>
	
	
	
	<update id="updateData"  parameterType="com.huatek.busi.entity.GisSf6Pressure">
	 UPDATE 
	 	gis_sf6_pressure 
	 	set pressure = #{pressure}
	 WHERE id=#{id} and pressure is null
</update>

</mapper>