<?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.TransformerEpaDataOriginMapper">
    <select id="getOneData" resultType="com.huatek.busi.vo.epa.EpaOneDataVo">
    	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 sensorId,
		  t.cac_id AS cacId,
		  t.equipment_id AS equipmentId,
		  COUNT(*) AS totalCount,
		  CAST(AVG(t.h2) AS decimal(20, 8)) AS avgH2,
		  CAST(AVG(t.ch4) AS decimal(20, 8)) AS avgCH4,
		  CAST(AVG(t.c2h6) AS decimal(20, 8)) AS avgC2H6,
		  CAST(AVG(t.c2h4) AS decimal(20, 8)) AS avgC2H4,
		  CAST(AVG(t.c2h2) AS decimal(20, 8)) AS avgC2H2,
		  CAST(AVG(t.co) AS decimal(20, 8)) AS avgCO,
		  CAST(AVG(t.co2) AS decimal(20, 8)) AS avgCO2,
		  CAST(AVG(t.o2) AS decimal(20, 8)) AS avgO2,
		  CAST(AVG(t.n2) AS decimal(20, 8)) AS avgN2,
		  CAST(AVG(t.tmp) AS decimal(20, 8)) AS avgTmp,
		  CAST(AVG(t.dew) AS decimal(20, 8)) AS avgDew,
		  CAST(AVG(t.mst) AS decimal(20, 8)) AS avgMst,
		  CAST(AVG(t.flatgas) AS decimal(20, 8)) AS avgFlatGas,
		  CAST(AVG(t.cmbugas) AS decimal(20, 8)) AS avgCmbuGas,
		  CAST(AVG(t.total_hydrocarbon) AS decimal(20, 8)) AS avgTotalHydrocarbon,
		  CAST(AVG(t.h2o) AS decimal(20, 8)) AS avgH2O
		FROM transformer_epa_data_origin t
		WHERE
			t.site_id IS NOT NULL
			AND t.gas_press = '1'
			AND t.d_time &gt;= #{startTime}
			AND t.d_time &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.main_equipment_id,
		         t.sub_equipment_id,
		         t.sensor_id,
		         t.cac_id,
		         t.equipment_id
    </select>

    <select id="getEvaluatData" resultType="com.huatek.busi.vo.epa.EpaEvaluateDataVo">
		SELECT
			CONVERT(AVG(CAST(t.h2 AS DECIMAL (10, 4))), DECIMAL(10,2)) AS h2,
			CONVERT(AVG(CAST(t.ch4 AS DECIMAL (10, 4))), DECIMAL(10,2)) AS ch4,
			CONVERT(AVG(CAST(t.h2 AS DECIMAL (10, 4))), DECIMAL(10,2)) AS h2,
		    CONVERT(AVG(CAST(t.c2h2 AS DECIMAL (10, 4))), DECIMAL(10,2)) AS c2h2,
			CONVERT(AVG(CAST(t.c2h4 AS DECIMAL (10, 4))), DECIMAL(10,2)) AS c2h4,
			CONVERT(AVG(CAST(t.c2h6 AS DECIMAL (10, 4))), DECIMAL(10,2)) AS c2h6,
			CONVERT(AVG(CAST(t.co AS DECIMAL (10, 4))), DECIMAL(10,2)) AS co,
			CONVERT(AVG(CAST(t.co2 AS DECIMAL (10, 4))), DECIMAL(10,2)) AS co2,
			CONVERT(AVG(CAST(t.tmp AS DECIMAL (10, 4))), DECIMAL(10,2)) AS tmp,
			CONVERT(AVG(CAST(t.dew AS DECIMAL (10, 4))), DECIMAL(10,2)) AS dew,
			CONVERT(AVG(CAST(t.mst AS DECIMAL (10, 4))), DECIMAL(10,2)) AS mst,
			CONVERT(AVG(CAST(t.flatgas AS DECIMAL (10, 4))), DECIMAL(10,2)) AS flatGas,
			CONVERT(AVG(CAST(t.cmbugas AS DECIMAL (10, 4))), DECIMAL(10,2)) AS cmbuGas,
			CONVERT(AVG(CAST(t.total_hydrocarbon AS DECIMAL (10, 4))), DECIMAL(10,2)) AS zt
		FROM
			`transformer_epa_data_statistics` t
		WHERE
			t.main_equipment_id = #{mainEquipmentId}
		<!-- AND t.gas_press = '1' -->
		AND t.d_time &gt; #{startDate}
		AND t.d_time &lt; #{endDate}
		<if test="sensorCode != null and sensorCode != ''">
			AND t.sensor_id = #{sensorCode}
		</if>
    </select>
    <select id="getEpaAbsIncreaseCacheInfo" resultType="com.huatek.busi.entity.TransformerEpaAbsIncreaseCache">
    	SELECT
    		IFNULL(h2,'') as h2,
    		IFNULL(ch4,'') as ch4,
    		IFNULL(c2h6,'') as c2h6,
    		IFNULL(c2h4,'') as c2h4,
    		IFNULL(c2h2,'') as c2h2,
    		IFNULL(co,'') as co,
    		IFNULL(co2,'') as co2,
    		IFNULL(o2,'') as o2,
    		IFNULL(n2,'') as n2,
    		IFNULL(h2o,'') as h2o,
    		IFNULL(tmp,'') as tmp,
    		IFNULL(dew,'') as dew,
    		IFNULL(mst,'') as mst,
    		IFNULL(flatgas,'') as flatGas,
    		IFNULL(cmbugas,'') as cmbuGas,
    		IFNULL(total_hydrocarbon,'') as totalHydrocarbon
    	FROM
    		transformer_epa_abs_increase_cache
    	WHERE
    		sensor_code = #{sensorCode}
    </select>
    <!-- 相对增长率 -->
    <select id="getEpaRelativeIncreaseCacheInfo" resultType="com.huatek.busi.entity.TransformerEpaIncreaseCache">
    	SELECT
    		IFNULL(h2,'') as h2,
    		IFNULL(ch4,'') as ch4,
    		IFNULL(c2h6,'') as c2h6,
    		IFNULL(c2h4,'') as c2h4,
    		IFNULL(c2h2,'') as c2h2,
    		IFNULL(co,'') as co,
    		IFNULL(co2,'') as co2,
    		IFNULL(o2,'') as o2,
    		IFNULL(n2,'') as n2,
    		IFNULL(h2o,'') as h2o,
    		IFNULL(tmp,'') as tmp,
    		IFNULL(dew,'') as dew,
    		IFNULL(mst,'') as mst,
    		IFNULL(flatgas,'') as flatGas,
    		IFNULL(cmbugas,'') as cmbuGas,
    		IFNULL(total_hydrocarbon,'') as totalHydrocarbon
    	FROM
    		transformer_epa_increase_cache
    	WHERE
    		sensor_code = #{sensorCode}
    </select>
     <select id="getByStatus" resultType="com.huatek.busi.vo.stability.OilStabilityVo">
    	SELECT
    		t.id,
    		t.sensor_id as sensorCode,
    		IFNULL(h2,'') as h2,
    		IFNULL(ch4,'') as ch4,
    		IFNULL(c2h6,'') as c2h6,
    		IFNULL(c2h4,'') as c2h4,
    		IFNULL(c2h2,'') as c2h2,
    		IFNULL(co,'') as co,
    		IFNULL(co2,'') as co2,
    		IFNULL(o2,'') as o2,
    		IFNULL(n2,'') as n2,
    		IFNULL(h2o,'') as h2o,
			 IFNULL(tmp,'') as tmp,
			 IFNULL(dew,'') as dew,
			 IFNULL(mst,'') as mst,
			 IFNULL(flatgas,'') as flatGas,
			 IFNULL(cmbugas,'') as cmbuGas,
    		IFNULL(total_hydrocarbon,'') as zj,
    		t.d_time as collectionTime
    	FROM
    		transformer_epa_data_origin t
    	WHERE
    		t.sensor_id = #{sensorCode}
    		AND t.gas_press > '0'
			<if test="startTime != null">
				AND t.d_time &gt;= #{startTime}
			</if>
			<if test="endTime != null">
				AND t.d_time &lt;= #{endTime}
			</if>
			ORDER BY t.d_time
    </select>

    <select id="getSensorCodeById" resultType="string">
		SELECT
			sensor_code
		FROM
			busi_transformer_sensor
		WHERE
			`status` = #{deleted}
			AND equipment_id IN (
			SELECT
				a.id
			FROM
				busi_transformer_sub_tree a
				LEFT JOIN busi_offline_transformer_brand b ON a.equipment_id = b.main_equipment_id
			WHERE
				b.id = #{id}
				AND a.equipment_type = #{type}
				AND a.`status` = #{deleted}
				AND b.`status` = #{deleted}
			)
			AND type = #{oilType}
	</select>
    
	<select id="getSubId" resultType="com.huatek.busi.entity.BusiSiteTree">
		SELECT
			id
		FROM
			busi_transformer_sub_tree
		WHERE
			equipment_id = #{mainEquipmentId}
			AND equipment_type = #{type}
	</select>
	<select id="getSensorCode" resultType="com.huatek.busi.entity.BusiTransformerSensor">
		SELECT
			sensor_code AS sensorCode
		FROM
			busi_transformer_sensor
		WHERE
			equipment_id = #{id}
	</select>
	<select id="getTemperature" resultType="string">
		SELECT
		CONVERT(AVG(CAST(avg_max_tmp AS DECIMAL (10, 4))), DECIMAL(10,2)) AS maxTmp
		FROM
		`transformer_winding_temperature_statistics`
		WHERE
		sensor_id = #{sensorCode}
		AND d_time &gt; #{startDate}
		AND d_time &lt; #{endDate}
	</select>

</mapper>
