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

	<sql id="Base_Column_List">
		id as id,
		sensor_code as sensorCode,
		equipment_type as
		equipmentType,
		sub_equipment_type as subEquipmentType,
		day as day,
		run_state as runState,
		repair_count as repairCount,
		threshold_value as
		thresholdValue,
		warning_value as warningValue,
		monthly_relative_growth_rate as monthlyRelativeGrowthRate,
		monthly_absolutely_growth_rate as monthlyAbsolutelyGrowthRate,
		loss_rate as lossRate,
		false_alarm_rate as falseAlarmRate,
		stability_index as stabilityIndex,
		accuracy_index as accuracyIndex,
		created_time as createdTime,
		main_tree_id as mainTreeId,
		(SELECT  siteTree.name FROM `busi_site_tree` siteTree WHERE siteTree.id= t.main_tree_id ) AS mainTreeName,
		t.sub_tree_id AS subTreeId,
		CASE 
		  WHEN t.ext_type = '1' 
		  THEN (SELECT s.name FROM `busi_transformer_sub_tree` AS s WHERE s.id = t.sub_tree_id)
		  WHEN t.ext_type = '2' 
		  THEN (SELECT s.name FROM `busi_gis_sub_tree` AS s WHERE s.id = t.sub_tree_id)
		  WHEN t.ext_type = '5' 
		  THEN (SELECT s.name FROM `busi_other_sub_tree` AS s WHERE s.id = t.sub_tree_id)  
		  ELSE
		  (SELECT s.name FROM busi_arrester_sub_tree AS s WHERE s.id = t.sub_tree_id) 
		  END 
		AS subTreeName,
		main_equipment_id as mainEquipmentId,
		sub_equipment_id as subEquipmentId,
		site_id as siteId,
		phase as phase
	</sql>
	
	<sql id="Base_Column_List2">
		  t.id AS id,
		  t.sensor_code AS sensorCode,
		  t.equipment_type AS equipmentType,
		  t.sub_equipment_type AS subEquipmentType,
		  t.day AS DAY,
		  t.run_state AS runState,
		  t.repair_count AS repairCount,
		  t.threshold_value AS thresholdValue,
		  t.warning_value AS warningValue,
		  t.monthly_relative_growth_rate AS monthlyRelativeGrowthRate,
		  t.monthly_absolutely_growth_rate AS monthlyAbsolutelyGrowthRate,
		  t.loss_rate AS lossRate,
		  t.false_alarm_rate AS falseAlarmRate,
		  t.stability_index AS stabilityIndex,
		  t.accuracy_index as accuracyIndex,
		  t.created_time AS createdTime,
		  t.main_tree_id AS mainTreeId,
		  (SELECT  siteTree.name FROM `busi_site_tree` siteTree WHERE siteTree.id= t.main_tree_id ) AS mainTreeName,
		  t.sub_tree_id AS subTreeId,
	      CASE 
			  WHEN t.ext_type = '1' 
			  THEN (SELECT s.name FROM `busi_transformer_sub_tree` AS s WHERE s.id = t.sub_tree_id)
			  WHEN t.ext_type = '2' 
			  THEN (SELECT s.name FROM `busi_gis_sub_tree` AS s WHERE s.id = t.sub_tree_id)
			  WHEN t.ext_type = '5' 
			  THEN (SELECT s.name FROM `busi_other_sub_tree` AS s WHERE s.id = t.sub_tree_id)
		      WHEN t.ext_type = '6'
		      THEN (SELECT s.name FROM `busi_generator_sub_tree` AS s WHERE s.id = t.sub_tree_id)
			  WHEN t.ext_type = '7'
			  THEN (SELECT s.name FROM `busi_cable_sub_tree` AS s WHERE s.id = t.sub_tree_id)
			  ELSE
			  (SELECT s.name FROM busi_arrester_sub_tree AS s WHERE s.id = t.sub_tree_id) 
			  END 
			AS subTreeName,
		  t.main_equipment_id AS mainEquipmentId,
		  t.sub_equipment_id AS subEquipmentId,
		  t.site_id AS siteId,
		  PHASE AS PHASE,
		  (SELECT i.data FROM busi_online_monitor_data_info i WHERE i.ref_id = t.id) AS data,
			CONCAT_WS('', t_sensor.sensor_name, g_sensor.sensor_name, c_sensor.sensor_name,gen_sensor.sensor_name, a_sensor.sensor_name, o_sensor.sensor_name, p_sensor.sensor_name) AS sensorName,
			CONCAT_WS('', t_sensor.sensor_brand, g_sensor.sensor_brand, c_sensor.sensor_brand, gen_sensor.sensor_brand,a_sensor.sensor_brand, o_sensor.sensor_brand, p_sensor.sensor_brand) AS sensorBrand,
			CONCAT_WS('', t_sensor.manufactor, g_sensor.manufactor, c_sensor.manufactor, gen_sensor.manufactor,a_sensor.manufactor, o_sensor.manufactor, p_sensor.manufactor) AS sensorManufactor,
			CONCAT_WS('', t_sensor.position_des, g_sensor.position_des, c_sensor.position_des, gen_sensor.position_des, a_sensor.position_des, o_sensor.position_des, p_sensor.position_des) AS sensorInstallLocation,
			CONCAT_WS('', t_sensor.use_date, g_sensor.use_date, c_sensor.use_date,gen_sensor.use_date, a_sensor.use_date, o_sensor.use_date, p_sensor.use_date) AS sensorUseDate,
			CONCAT_WS('', t_sensor.position, g_sensor.position, c_sensor.position, gen_sensor.position, a_sensor.position, o_sensor.position, p_sensor.position) AS sensorposition,
			 CASE 
			  WHEN t.ext_type = '1' 
			  THEN (SELECT s.equipment_id FROM busi_transformer_sub_tree AS s WHERE s.id = ee.equipment_id)
			  WHEN t.ext_type = '2' 
			  THEN (SELECT s.equipment_id FROM busi_gis_sub_tree AS s WHERE s.id = ee.equipment_id)
			  WHEN t.ext_type = '5' 
			  THEN (SELECT s.equipment_id FROM busi_other_sub_tree AS s WHERE s.id = ee.equipment_id)
		      WHEN t.ext_type = '6'
		      THEN (SELECT s.equipment_id FROM busi_generator_sub_tree AS s WHERE s.id = ee.equipment_id)
			  WHEN t.ext_type = '7'
		      THEN (SELECT s.equipment_id FROM busi_cable_sub_tree AS s WHERE s.id = ee.equipment_id)
		      ELSE
			  (SELECT s.equipment_id FROM busi_arrester_sub_tree AS s WHERE s.id = ee.equipment_id) 
			  END 
			AS equipmentId,
		(SELECT tree.name FROM busi_site_tree AS tree WHERE tree.id=equipmentId) AS siteTreename
	</sql>
	
	<sql id="Base_Column_List3">
		t.id AS id,
		t.sensor_code AS sensorCode,
		t.equipment_type AS  equipmentType,
		t.sub_equipment_type AS subEquipmentType,
		t.day AS DAY,
		t.run_state AS runState,
		t.repair_count AS repairCount,
		t.threshold_value AS thresholdValue,
		t.warning_value AS warningValue,
		t.monthly_relative_growth_rate AS monthlyRelativeGrowthRate,
		t.monthly_absolutely_growth_rate AS monthlyAbsolutelyGrowthRate,
		t.loss_rate AS lossRate,
		t.false_alarm_rate AS falseAlarmRate,
		t.stability_index AS stabilityIndex,
		t.accuracy_index as accuracyIndex,
		t.created_time AS createdTime,
		t.main_tree_id AS mainTreeId,
		(SELECT  siteTree.name FROM `busi_site_tree` siteTree WHERE siteTree.id= t.main_tree_id ) AS mainTreeName,
		t.sub_tree_id AS subTreeId,
        CASE 
		  WHEN t.ext_type = '1' 
		  THEN (SELECT s.name FROM `busi_transformer_sub_tree` AS s WHERE s.id = t.sub_tree_id)
		  WHEN t.ext_type = '2' 
		  THEN (SELECT s.name FROM `busi_gis_sub_tree` AS s WHERE s.id = t.sub_tree_id)
		  WHEN t.ext_type = '5' 
		  THEN (SELECT s.name FROM `busi_other_sub_tree` AS s WHERE s.id = t.sub_tree_id)  
		  ELSE
		  (SELECT s.name FROM busi_arrester_sub_tree AS s WHERE s.id = t.sub_tree_id) 
		  END 
		AS subTreeName,
		t.main_equipment_id AS mainEquipmentId,
		t.sub_equipment_id AS subEquipmentId,
		t.site_id AS siteId,
		t.phase AS PHASE,
		(SELECT i.data FROM busi_online_monitor_data_info i WHERE i.ref_id = t.id) AS data,
		temper.`sensor_name` AS sensorName,
		temper.`sensor_brand` AS sensorBrand,
		temper.`manufactor` AS sensorManufactor,
		temper.`position_des` AS sensorInstallLocation,
		temper.`use_date` AS sensorUseDate,
		temper.`position` AS sensorposition,
        tree.`name` as siteTreename
	</sql>

	<select id="selectBusiOnlineMonitorDataPage"
		parameterType="com.huatek.busi.dto.monitor.BusiOnlineMonitorDataDto"
		resultType="com.huatek.busi.vo.monitor.BusiOnlineMonitorDataVo">
		SELECT
			<include refid="Base_Column_List2" />
		FROM
			busi_online_monitor_data t
		  LEFT JOIN busi_transformer_sensor t_sensor ON t.sensor_code = t_sensor.sensor_code
		  LEFT JOIN busi_gis_sensor g_sensor ON t.sensor_code = g_sensor.sensor_code
		  LEFT JOIN busi_arrester_sensor a_sensor ON t.sensor_code = a_sensor.sensor_code
		  LEFT JOIN busi_other_sensor o_sensor ON t.sensor_code = o_sensor.sensor_code
		  LEFT JOIN busi_temper_point p_sensor ON t.sensor_code = p_sensor.sensor_code
		  LEFT JOIN busi_cable_sensor c_sensor ON t.sensor_code = c_sensor.sensor_code
		  LEFT JOIN busi_generator_sensor gen_sensor ON t.sensor_code = gen_sensor.sensor_code
		LEFT JOIN busi_external_equipment ee ON ee.id=t.sub_equipment_id
		  LEFT JOIN busi_site_tree tree ON tree.id=t.main_tree_id
		<where>
			<if test="equipmentType != null and equipmentType != ''">
				and t.equipment_type = #{equipmentType}
			</if>
			<if test="treeId != null and treeId != ''">
				and tree.id = #{treeId}
			</if>
			<if test="subType != null and subType != ''">
	            <choose>
					<when test="equipmentType == '111'.toString and subType == '031'.toString">
				        and t.sub_equipment_type in ('031','081')
				    </when>
				    <otherwise>
				        and t.sub_equipment_type = #{subType}
				    </otherwise>
			    </choose>
			</if>
			<if test="externalId != null and externalId != ''">
				and t.sub_tree_id = #{ externalId}
			</if>
			<if test="sensorCode != null and sensorCode != ''">
				and t.sensor_code = #{sensorCode}
			</if>			
			<if test="day != null">
				and t.day &gt;= #{day}
			</if>
			<if test="dayEnd != null">
				and t.day &lt;= #{dayEnd}
			</if>
		</where>
		ORDER BY t.day desc, t.created_time desc
	</select>


	<select id="selectCWBusiOnlineMonitorDataPage"
		parameterType="com.huatek.busi.dto.monitor.BusiOnlineMonitorDataDto"
		resultType="com.huatek.busi.vo.monitor.BusiOnlineMonitorDataVo">
		SELECT
			<include refid="Base_Column_List3" />
		FROM busi_online_monitor_data AS t
		LEFT JOIN busi_site_tree AS tree  ON tree.id=t.main_tree_id
		LEFT JOIN `busi_temper_point` AS temper ON  t.sensor_code = temper.sensor_code
		<where>
			<if test="equipmentType != null and equipmentType != ''">
				and t.equipment_type = #{equipmentType}
			</if>
			<if test="treeId != null and treeId != ''">
				and tree.id = #{treeId}
			</if>
			<if test="sensorCode != null and sensorCode != ''">
				and t.sensor_code = #{sensorCode}
			</if>			
			<if test="day != null">
				and t.day &gt;= #{day}
			</if>
			<if test="dayEnd != null">
				and t.day &lt; #{dayEnd}
			</if>
		</where>
		ORDER BY t.day desc, t.created_time desc
	</select>
	
	<delete id="deleteBusiOnlineMonitorDataInfo">
		delete from busi_online_monitor_data_info where ref_id = #{refId}
	</delete>

	<select id="getBusiOnlineMonitorData" resultType="com.huatek.busi.entity.BusiOnlineMonitorData">
		SELECT
			<include refid="Base_Column_List" />
		FROM
			busi_online_monitor_data t
		WHERE t.day = #{day} AND t.sensor_code = #{sensorCode} and t.run_state = #{runState}
	</select>
	<!-- 导出 -->
	<select id="exportBusiOnlineMonitorDataPage"
		parameterType="com.huatek.busi.dto.monitor.BusiOnlineMonitorDataDto"
		resultType="com.huatek.busi.vo.monitor.BusiOnlineMonitorDataVo">
		SELECT
			<include refid="Base_Column_List2" />
		FROM
			busi_online_monitor_data t
		  LEFT JOIN busi_transformer_sensor t_sensor ON t.sensor_code = t_sensor.sensor_code
		  LEFT JOIN busi_gis_sensor g_sensor ON t.sensor_code = g_sensor.sensor_code
		  LEFT JOIN busi_arrester_sensor a_sensor ON t.sensor_code = a_sensor.sensor_code
		  LEFT JOIN busi_other_sensor o_sensor ON t.sensor_code = o_sensor.sensor_code
		  LEFT JOIN busi_temper_point p_sensor ON t.sensor_code = p_sensor.sensor_code
		  LEFT JOIN busi_cable_sensor c_sensor ON t.sensor_code = c_sensor.sensor_code
		  LEFT JOIN busi_generator_sensor gen_sensor ON t.sensor_code = gen_sensor.sensor_code
		  LEFT JOIN busi_external_equipment ee ON ee.id=t.sub_equipment_id
		  LEFT JOIN busi_site_tree tree ON tree.id=t.main_tree_id
		<where>
			<if test="equipmentType != null and equipmentType != ''">
				and t.equipment_type = #{equipmentType}
			</if>
			<if test="treeId != null and treeId != ''">
				and tree.id = #{treeId}
			</if>
			<if test="subType != null and subType != ''">
	            <choose>
					<when test="subType == '031'">
				        and t.sub_equipment_type in ('031','081')
				    </when>
				    <otherwise>
				        and t.sub_equipment_type = #{subType}
				    </otherwise>
			    </choose>
			</if>
			<if test="externalId != null and externalId != ''">
				and t.sub_tree_id = #{ externalId}
			</if>
			<if test="sensorCode != null and sensorCode != ''">
				and t.sensor_code = #{sensorCode}
			</if>			
			<if test="day != null">
				and t.day &gt;= #{day}
			</if>
			<if test="dayEnd != null">
				and t.day &lt;= #{dayEnd}
			</if>
		</where>
		ORDER BY t.day desc, t.created_time desc
	</select>


	<select id="exportCWBusiOnlineMonitorDataPage"
		parameterType="com.huatek.busi.dto.monitor.BusiOnlineMonitorDataDto"
		resultType="com.huatek.busi.vo.monitor.BusiOnlineMonitorDataVo">
		SELECT
			<include refid="Base_Column_List3" />
		FROM busi_online_monitor_data AS t
		LEFT JOIN busi_site_tree AS tree  ON tree.id=t.main_tree_id
		LEFT JOIN `busi_temper_point` AS temper ON  t.sensor_code = temper.sensor_code
		<where>
			<if test="equipmentType != null and equipmentType != ''">
				and t.equipment_type = #{equipmentType}
			</if>
			<if test="treeId != null and treeId != ''">
				and tree.id = #{treeId}
			</if>
			<if test="sensorCode != null and sensorCode != ''">
				and t.sensor_code = #{sensorCode}
			</if>			
			<if test="day != null">
				and t.day &gt;= #{day}
			</if>
			<if test="dayEnd != null">
				and t.day &lt; #{dayEnd}
			</if>
		</where>
		ORDER BY t.day desc, t.created_time desc
	</select>
	
</mapper>