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

	<sql id="Base_Column_List">
		t.id as id,
		t.d_time as dTime,
		t.site_id as siteId,
		t.sensor_id as sensorId,
		s.sensor_name as sensorName,
		t.sub_tree_id as subTreeId,
		t.main_tree_id as mainTreeId,
		t.type_id as typeId,
		t.discharge_capacity as dischargeCapacity,
		t.discharge_position as dischargePosition,
		t.pulse_count as pulseCount,
		t.discharge_waveform as dischargeWaveform,
		CASE t.pa_dsch_alm WHEN '' THEN '0' ELSE t.pa_dsch_alm END AS paDschAlm,
		t.dsch_type AS dschType,
		t.run_state as runState
	</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 busi_generator_pd t
			left join busi_generator_sensor s ON t.sensor_id = s.sensor_code
		<where>
			s.status = '1'
			<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.d_time,'%Y-%m-%d') &gt;= #{timeStamp}
	        </if>
	        <if test="timeStampEnd != null and timeStampEnd != ''">
	             and DATE_FORMAT(t.d_time,'%Y-%m-%d') &lt;= #{timeStampEnd}
	        </if>
			<if test="sensorCode != null and sensorCode != ''">
				AND t.sensor_id = #{sensorCode}
			</if>
		</where>
		order by t.d_time desc
	</select>
	
	<select id="exportCacMonitorData" resultType="com.huatek.busi.vo.BusiGeneratorPdVo">
		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 busi_generator_pd t
		left join busi_generator_sensor s ON t.sensor_id = s.sensor_code
		where
			s.status = '1'
			<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.d_time,'%Y-%m-%d') &gt;= #{timeStamp}
	        </if>
	        <if test="timeStampEnd != null and timeStampEnd != ''">
	             and DATE_FORMAT(t.d_time,'%Y-%m-%d') &lt;= #{timeStampEnd}
	        </if>
			<if test="sensorId !=null and sensorId !=''">
				AND t.sensor_id = #{sensorId}
			</if>
		order by t.d_time desc
	</select>

	<select id="queryMonitorDataList" resultType="java.util.Map">
		SELECT
		  t.sensor_id AS sensorCode,
		  t.main_tree_id AS mainTreeId,
		  t.sub_tree_id AS subTreeId,
		  t.site_id AS siteId,
		  CAST(AVG(t.discharge_capacity) AS decimal(20, 4)) AS dischargeCapacity,
		  CAST(AVG(t.pulse_count) AS decimal(20, 4)) AS pulseCount
		FROM busi_generator_pd t
		WHERE t.d_time &gt;= #{startTime}
			AND t.d_time &lt;= #{endTime}
		GROUP BY t.sensor_id,
		         t.main_tree_id,
		         t.sub_tree_id,
		         t.site_id
	</select>

	<select id="findReportDataList" resultType="com.huatek.busi.vo.BusiGeneratorPdVo">
		SELECT
		t.id as id,
		t.d_time as dTime,
		t.site_id as siteId,
		t.sensor_id as sensorId,
		t.sub_tree_id as subTreeId,
		t.main_tree_id as mainTreeId,
		t.type_id as typeId,
		t.discharge_capacity as dischargeCapacity,
		t.discharge_position as dischargePosition,
		t.pulse_count as pulseCount,
		t.discharge_waveform as dischargeWaveform,
		CASE t.pa_dsch_alm WHEN '' THEN '0' ELSE t.pa_dsch_alm END AS paDschAlm,
		t.dsch_type AS dschType,
		t.run_state as runState
		FROM busi_generator_pd t
		WHERE t.sensor_id = #{sensorCode}
		<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 DESC
	</select>
	
	<select id="findReportDataListByFirstData" resultType="java.util.Map">
		SELECT
		  t.discharge_capacity AS val2,
		  t.dsch_type as val3,
		  t.pulse_count AS val4,
		  t.discharge_position as val5,
		  DATE_FORMAT(t.d_time, '%Y-%m-%d %H:%i:%S') AS val6
		FROM busi_generator_pd t
		WHERE t.sensor_id = #{sensorCode}
		ORDER BY t.d_time DESC
		Limit 1
	</select>
	<select id="getListEquInfo" resultType="com.huatek.busi.vo.GeneratorPdCountVo">
		SELECT
			t.site_id AS siteId,
			t.main_tree_id AS mainTreeId,
			t.sub_tree_id AS subTreeId,
			COUNT(*) AS dataCount
		FROM
		busi_generator_pd t
		WHERE 
			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
	</select>
	
	
	<select id="findPdPage" resultType="java.util.Map">
		SELECT
		t.id AS id,
		t.sensor_id AS sensorId,
		s.sensor_name AS sensorName,
		transformerTree.name AS subTreeName,
		t.d_time AS day,
		t.site_id AS siteId,
		s.position AS PHASE,
		t.discharge_capacity AS dischargeCapacity,
		t.discharge_position AS dischargePosition,
		t.pulse_count AS pulseCount,
		t.discharge_waveform AS dischargeWaveform,
		t.dsch_type AS dschType,
		t.pa_dsch_alm AS paDschAlm,
		t.main_tree_id AS mainTreeId,
		bst.name AS mainTreeName,
		t.sub_tree_id AS subTreeId,
		'611' AS equipmentType,
		'021' AS subEquipmentType,
		(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 siteTreename,
		s.`sensor_name` AS sensorName,
		s.`sensor_brand` AS sensorBrand,
		s.`manufactor` AS sensorManufactor,
		s.`position_des` AS sensorInstallLocation,
		s.`use_date` AS sensorUseDate,
		s.`position` AS sensorposition,
		pdFile.pic_file as matlabImage
		FROM
		busi_generator_pd t
		LEFT JOIN busi_generator_sensor s ON t.sensor_id = s.sensor_code
		LEFT JOIN `busi_generator_sub_tree` transformerTree ON transformerTree.id=t.`sub_tree_id`
		left join iec_pd_file_header pdFile on pdFile.pd_id=t.id
		left join busi_site_tree bst ON t.main_tree_id = bst.id
		<where>
			s.status = '1'
			<if test="sensorId != null and sensorId != ''">
	             and t.sensor_id = #{sensorId}
	        </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">
             	 <![CDATA[   and DATE_FORMAT(t.d_time, '%Y-%m-%d') >=  DATE_FORMAT(#{timeStamp}, '%Y-%m-%d')   ]]>
	        </if>
	        <if test="timeStampEnd != null">
	              <![CDATA[   and DATE_FORMAT(t.d_time, '%Y-%m-%d') <=  DATE_FORMAT(#{timeStampEnd}, '%Y-%m-%d')   ]]>
	        </if>
		</where>
		order by t.d_time desc
	</select>
	<select id="exportGeneratorPdPage" resultType="com.huatek.busi.vo.monitor.GeneratorPdVo">
		SELECT
			t.id AS id,
			t.sensor_id AS sensorId,
			s.sensor_name AS sensorName,
			subTree.name AS subTreeName,
			t.d_time AS day,
			t.site_id AS siteId,
			t.discharge_capacity AS dischargeCapacity,
			t.discharge_position AS dischargePosition,
			t.pulse_count AS pulseCount,
		(CASE t.dsch_type
		WHEN '0' THEN '正常'
		WHEN '1' THEN '尖端放电'
		WHEN '2' THEN '悬浮放电'
		WHEN '3' THEN '沿面放电'
		WHEN '4' THEN '内部放电'
		WHEN '5' THEN '颗粒放电'
		WHEN '6' THEN '外部干扰'
		WHEN '7' THEN '其它'
		ELSE t.dsch_type END) AS dschType,
			t.discharge_waveform AS dischargeWaveform,
	        t.main_tree_id AS mainTreeId,
	        t.sub_tree_id AS subTreeId,
	        '发电机' AS equipmentType,
			"局放信号" AS subEquipmentType,
			(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 siteTreename,
			s.`sensor_name` AS sensorName,
			s.`sensor_brand` AS sensorBrand,
			s.`manufactor` AS sensorManufactor,
			s.`position_des` AS sensorInstallLocation,
		(CASE s.position
		WHEN 'O' THEN '全相'
		WHEN '1' THEN 'A相'
		WHEN '2' THEN 'B相'
		WHEN '3' THEN 'C相'
		ELSE s.position END) AS PHASE,
			s.`use_date` AS sensorUseDate,
			s.`position` AS sensorposition,
			pdFile.pic_file1 as matlabImage
		FROM
			busi_generator_pd t
			LEFT JOIN busi_generator_sensor s ON t.sensor_id = s.sensor_code
			LEFT JOIN `busi_generator_sub_tree` subTree ON subTree.id=t.`sub_tree_id`
			left join iec_pd_file_header pdFile on pdFile.pd_id=t.id 
		<where>
			s.status = '1'
			<if test="sensorId != null and sensorId != ''">
	             and t.sensor_id = #{sensorId}
	        </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 DATE_FORMAT(t.d_time,'%Y-%m-%d') &gt;= #{timeStamp}
	        </if>
	        <if test="timeStampEnd != null">
	             and DATE_FORMAT(t.d_time,'%Y-%m-%d') &lt;= #{timeStampEnd}
	        </if>
		</where>
		order by t.d_time desc
	</select>

</mapper>