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

    <sql id="Base_Column_List">
            phase as phase,
            o2 as o2,
            n2 as n2,
            h2o as h2o,
            co2 as co2,
            h2 as h2,
            co as co,
            ch4 as ch4,
            c2h6 as c2h6,
            total_hydrocarbon as totalHydrocarbon,
            c2h2 as c2h2,
            c2h4 as c2h4,
            site_id as siteId,
            gas_press as gasPress
    </sql>

	<sql id="Base_Column_List2">
            t.id as id,
            t.cac_id as cacId,
            t.data_node_num as dataNodeNum,
            t.sensor_id as sensorId,
            ts.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.h2 as h2,
            t.ch4 as ch4,
            t.c2h6 as c2h6,
            t.c2h4 as c2h4,
            t.c2h2 as c2h2,
            t.co as co,
            t.co2 as co2,
            t.o2 as o2,
            t.n2 as n2,
            t.total_hydrocarbon as totalHydrocarbon,
            t.gas_press as gasPress,
            t.h2o as h2o,
            CAST(SUM(t.h2+t.ch4+t.c2h6+t.c2h4+t.c2h2+t.co) AS DECIMAL(15,4)) AS totalGas,
            t.main_equipment_id as mainEquipmentId,
            t.sub_equipment_id as subEquipmentId,
            t.main_tree_id as mainTreeId,
            t.sub_tree_id as subTreeId
    </sql>

    <select id="findBySensorId"  resultType="com.huatek.busi.vo.TransformerEpaDataVo">
        SELECT
        	<include refid="Base_Column_List"/>
        FROM
        transformer_epa_data t
        WHERE  t.sensor_id =#{sensorId}
        order by t.time_stamp desc limit 1
        
    </select>

    <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 = t.main_tree_id) as systemCode,
			(select tree.name from busi_site_tree tree where tree.id = t.main_tree_id) as equipmentName
		from 
			transformer_epa_data t LEFT JOIN busi_transformer_sensor ts ON t.sensor_id = ts.sensor_code
		<where>
			t.sensor_id = ts.sensor_code and ts.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>
		</where>
		GROUP by t.id
		order by t.time_stamp desc
	</select>
	<select id="exportCacMonitorData" resultType="com.huatek.busi.vo.TransformerEpaDataVo">
		select 
			<include refid="Base_Column_List2"/>,
			(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 transformer_epa_data t
		left join busi_transformer_sensor ts on t.sensor_id = ts.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="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>
		</where>
		GROUP by t.id
		order by t.time_stamp desc
	</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_epa_data 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_epa_data 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_epa_data 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="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.h2) AS decimal(20, 4)) AS h2,
		  CAST(AVG(t.ch4) AS decimal(20, 4)) AS ch4,
		  CAST(AVG(t.c2h6) AS decimal(20, 4)) AS c2h6,
		  CAST(AVG(t.c2h4) AS decimal(20, 4)) AS c2h4,
		  CAST(AVG(t.c2h2) AS decimal(20, 4)) AS c2h2,
		  CAST(AVG(t.co) AS decimal(20, 4)) AS co,
		  CAST(AVG(t.co2) AS decimal(20, 4)) AS co2,
		  CAST(AVG(t.o2) AS decimal(20, 4)) AS o2,
		  CAST(AVG(t.n2) AS decimal(20, 4)) AS n2,
		  CAST(AVG(t.total_hydrocarbon) AS decimal(20, 4)) AS totalHydrocarbon,
		  CAST(AVG(t.gas_press) AS decimal(20, 4)) AS gasPress,
		  CAST(AVG(t.h2o) AS decimal(20, 4)) AS h2o
		FROM transformer_epa_data 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>
</mapper>