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

	<resultMap id="BaseResultMap" type="com.huatek.busi.dto.EquipmentInfoDto">
	
		<result column="sensorCode" property="sensorCode" />
		<result column="sensorName" property="sensorName" />
		<result column="sensorBrand" property="sensorBrand" />
		<result column="sensorPosition" property="sensorPosition" />
		<result column="sensorPositionDes" property="sensorPositionDes" />
		<result column="sensorUseDate" property="sensorUseDate" />
		<result column="sensorManufactor" property="sensorManufactor" />
		<result column="oilType" property="oilType" />
		<result column="oilDetailType" property="oilDetailType" />
		<result column="subEquipmentId" property="subEquipmentId" />
		<result column="subEquipmentName" property="subEquipmentName" />
		<result column="subEquipmentBrand" property="subEquipmentBrand" />
		<result column="subEquipmentPosition" property="subEquipmentPosition" />
		<result column="subEquipmentManufactor" property="subEquipmentManufactor" />
		<result column="subEquipmentUseDate" property="subEquipmentUseDate" />
		
		<result column="mainEquipmentId" property="mainEquipmentId" />
		<result column="mainEquipmentName" property="mainEquipmentName" />
		<result column="mainEquipmenBrand" property="mainEquipmenBrand" />
		<result column="mainEquipmentManufactor" property="mainEquipmentManufactor" />
		<result column="mainEquipmentUseDate" property="mainEquipmentUseDate" />

		<result column="subTreeId" property="subTreeId" />
		<result column="subSysCode" property="subSysCode" />
		<result column="mainTreeId" property="mainTreeId" />
		<result column="mainSysCode" property="mainSysCode" />
		
		<result column="siteId" property="siteId" />
		<result column="siteName" property="siteName" />
	</resultMap>


	<select id="getTransformerBySensor" resultMap="BaseResultMap">
		SELECT
			se.sensor_code AS sensorCode,
			se.sensor_name AS sensorName,
			se.sensor_brand AS sensorBrand,
			se.position AS sensorPosition,
			se.position_des AS sensorPositionDes,
			se.use_date AS sensorUseDate,
			se.manufactor AS sensorManufactor,
			
			ext.id AS subEquipmentId,
			ext.equipment_name AS subEquipmentName,
			ext.equipment_brand AS subEquipmentBrand,
			ext.install_location AS subEquipmentPosition,
			ext.manufactor AS subEquipmentManufactor,
			ext.use_date AS subEquipmentUseDate,
			
			brand.id AS mainEquipmentId,
			brand.transformer_name AS mainEquipmentName,
			brand.equipment_model AS mainEquipmenBrand,
			brand.manufacturer AS mainEquipmentManufactor,
			brand.equipment_delivery_time AS mainEquipmentUseDate,
			
			sub.id AS subTreeId,
			main.id AS mainTreeId,
			
			sub.system_code as subSysCode,
			main.system_code as mainSysCode,
	
			site.id AS siteId,
			site.site_name AS siteName,
			
			sub.equipment_type as equipmentType
		FROM
			busi_transformer_sensor se
		LEFT JOIN busi_external_equipment ext ON ext.equipment_id = se.equipment_id
		LEFT JOIN busi_transformer_sub_tree sub ON sub.id = se.equipment_id
		LEFT JOIN busi_site site ON site.id = se.site_id
		LEFT JOIN busi_site_tree main ON main.id = sub.equipment_id
		LEFT JOIN busi_offline_transformer_brand brand ON brand.main_equipment_id = main.id
		WHERE
			se.sensor_code = #{sensorCode}
			  AND se.status = '1' 
			  AND sub.status = '1' 
			  AND main.status = '1'
			  AND brand.status = '1'
	</select>


	<select id="getGisBySensor" resultMap="BaseResultMap">
		SELECT
			se.sensor_code AS sensorCode,
			se.sensor_name AS sensorName,
			se.sensor_brand AS sensorBrand,
			se.position AS sensorPosition,
			se.position_des AS sensorPositionDes,
			se.use_date AS sensorUseDate,
			se.manufactor AS sensorManufactor,
		
			ext.id AS subEquipmentId,
			ext.equipment_name AS subEquipmentName,
			ext.equipment_brand AS subEquipmentBrand,
			ext.install_location AS subEquipmentPosition,
			ext.manufactor AS subEquipmentManufactor,
			ext.use_date AS subEquipmentUseDate,
		
			brand.id AS mainEquipmentId,
			brand.gis_name AS mainEquipmentName,
			brand.equipment_model AS mainEquipmenBrand,
			brand.manufactor AS mainEquipmentManufactor,
			brand.transport_time AS mainEquipmentUseDate,
		
			sub.id AS subTreeId,
			main.id AS mainTreeId,
			sub.system_code as subSysCode,
			main.system_code as mainSysCode,
		
			site.id AS siteId,
			site.site_name AS siteName
		FROM
			busi_gis_sensor se
		LEFT JOIN busi_external_equipment ext ON ext.equipment_id = se.equipment_id
		LEFT JOIN busi_gis_sub_tree sub ON sub.id = se.equipment_id
		LEFT JOIN busi_site site ON site.id = se.site_id
		LEFT JOIN busi_site_tree main ON main.id = sub.equipment_id
		LEFT JOIN busi_offline_gis_brand brand ON brand.main_equipment_id = main.id
		WHERE
			se.sensor_code = #{sensorCode}
				AND se.status = '1' 
				AND sub.status = '1' 
				AND main.status = '1'
				AND brand.status = '1'
	</select>

	<select id="getGeneratorBySensor" resultMap="BaseResultMap">
		SELECT
			se.sensor_code AS sensorCode,
			se.sensor_name AS sensorName,
			se.sensor_brand AS sensorBrand,
			se.position AS sensorPosition,
			se.position_des AS sensorPositionDes,
			se.use_date AS sensorUseDate,
			se.manufactor AS sensorManufactor,

			ext.id AS subEquipmentId,
			ext.equipment_name AS subEquipmentName,
			ext.equipment_brand AS subEquipmentBrand,
			ext.install_location AS subEquipmentPosition,
			ext.manufactor AS subEquipmentManufactor,
			ext.use_date AS subEquipmentUseDate,

			brand.id AS mainEquipmentId,
			brand.generator_name AS mainEquipmentName,
			brand.equipment_model AS mainEquipmenBrand,

			sub.id AS subTreeId,
			main.id AS mainTreeId,
			sub.system_code as subSysCode,
			main.system_code as mainSysCode,

			site.id AS siteId,
			site.site_name AS siteName
		FROM
			busi_generator_sensor se
		LEFT JOIN busi_external_equipment ext ON ext.equipment_id = se.equipment_id
		LEFT JOIN busi_generator_sub_tree sub ON sub.id = se.equipment_id
		LEFT JOIN busi_site site ON site.id = se.site_id
		LEFT JOIN busi_site_tree main ON main.id = sub.equipment_id
		LEFT JOIN busi_offline_generator_brand brand ON brand.main_equipment_id = main.id
		WHERE
			se.sensor_code = #{sensorCode}
				AND se.status = '1'
				AND sub.status = '1'
				AND main.status = '1'
				AND brand.status = '1'
	</select>

	<select id="getCableBySensor" resultMap="BaseResultMap">
		SELECT
			se.sensor_code AS sensorCode,
			se.sensor_name AS sensorName,
			se.sensor_brand AS sensorBrand,
			se.position AS sensorPosition,
			se.position_des AS sensorPositionDes,
			se.use_date AS sensorUseDate,
			se.manufactor AS sensorManufactor,

			ext.id AS subEquipmentId,
			ext.equipment_name AS subEquipmentName,
			ext.equipment_brand AS subEquipmentBrand,
			ext.install_location AS subEquipmentPosition,
			ext.manufactor AS subEquipmentManufactor,
			ext.use_date AS subEquipmentUseDate,

			brand.id AS mainEquipmentId,
			brand.cable_name AS mainEquipmentName,

			sub.id AS subTreeId,
			main.id AS mainTreeId,
			sub.system_code as subSysCode,
			main.system_code as mainSysCode,

			site.id AS siteId,
			site.site_name AS siteName
		FROM
			busi_cable_sensor se
		LEFT JOIN busi_external_equipment ext ON ext.equipment_id = se.equipment_id
		LEFT JOIN busi_cable_sub_tree sub ON sub.id = se.equipment_id
		LEFT JOIN busi_site site ON site.id = se.site_id
		LEFT JOIN busi_site_tree main ON main.id = sub.equipment_id
		LEFT JOIN busi_offline_cable_brand brand ON brand.main_equipment_id = main.id
		WHERE
			se.sensor_code = #{sensorCode}
				AND se.status = '1'
				AND sub.status = '1'
				AND main.status = '1'
				AND brand.status = '1'
	</select>

	<select id="getArresterBySensor" resultMap="BaseResultMap">
		SELECT
			se.sensor_code AS sensorCode,
			se.sensor_name AS sensorName,
			se.sensor_brand AS sensorBrand,
			se.position AS sensorPosition,
			se.position_des AS sensorPositionDes,
			se.use_date AS sensorUseDate,
			se.manufactor AS sensorManufactor,
		
			ext.id AS subEquipmentId,
			ext.equipment_name AS subEquipmentName,
			ext.equipment_brand AS subEquipmentBrand,
			ext.install_location AS subEquipmentPosition,
			ext.manufactor AS subEquipmentManufactor,
			ext.use_date AS subEquipmentUseDate,
		
			brand.id AS mainEquipmentId,
			brand.arrester_name AS mainEquipmentName,
			brand.arrester_model AS mainEquipmenBrand,
		
			sub.id AS subTreeId,
			main.id AS mainTreeId,
			sub.system_code as subSysCode,
			main.system_code as mainSysCode,
		
			site.id AS siteId,
			site.site_name AS siteName
		FROM
			busi_arrester_sensor se
		LEFT JOIN busi_external_equipment ext ON ext.equipment_id = se.equipment_id
		LEFT JOIN busi_arrester_sub_tree sub ON sub.id = se.equipment_id
		LEFT JOIN busi_site site ON site.id = se.site_id
		LEFT JOIN busi_site_tree main ON main.id = sub.equipment_id
		LEFT JOIN busi_offline_arrester_brand brand ON brand.main_equipment_id = main.id

		WHERE
			se.sensor_code = #{sensorCode}
				AND se.status = '1' 
				AND sub.status = '1' 
				AND main.status = '1'
				AND brand.status = '1'
	</select>


	<select id="getOtherBySensor" resultMap="BaseResultMap">
		SELECT
			se.sensor_code AS sensorCode,
			se.sensor_name AS sensorName,
			se.sensor_brand AS sensorBrand,
			se.position AS sensorPosition,
			se.position_des AS sensorPositionDes,
			se.use_date AS sensorUseDate,
			se.manufactor AS sensorManufactor,
		
			ext.id AS subEquipmentId,
			ext.equipment_name AS subEquipmentName,
			ext.equipment_brand AS subEquipmentBrand,
			ext.install_location AS subEquipmentPosition,
			ext.manufactor AS subEquipmentManufactor,
			ext.use_date AS subEquipmentUseDate,
			sub.id AS subTreeId,
			main.id AS mainTreeId,
			sub.system_code as subSysCode,
			main.system_code as mainSysCode,
		
			site.id AS siteId,
			site.site_name AS siteName
		FROM
			busi_other_sensor se
		LEFT JOIN busi_external_equipment ext ON ext.equipment_id = se.equipment_id
		LEFT JOIN busi_other_sub_tree sub ON sub.id = se.equipment_id
		LEFT JOIN busi_site site ON site.id = se.site_id
		LEFT JOIN busi_site_tree main ON main.id = sub.equipment_id

		WHERE
			se.sensor_code = #{sensorCode}
				AND se.status = '1' 
				AND sub.status = '1' 
				AND main.status = '1'
	</select>
	

	<select id="getCwBySensor" resultMap="BaseResultMap">
		<!-- SELECT
			sensor.sensor_code AS sensorCode,
			sensor.sensor_device_code AS sensorDeviceCode,
			sensor.position AS position,
			sensor.position_des AS positionDes,
			tr.system_code AS mainSysCode,
			tr.`name` AS mainEquipmentName,
			tr.id AS mainEquipmentId,
			site.id AS siteId,
			site.site_name AS siteName
		FROM
			`busi_temper_point` sensor
		LEFT JOIN busi_site_tree tr ON tr.id = sensor.equipment_id
		LEFT JOIN busi_site site ON site.id = sensor.site_id
		WHERE
			sensor.sensor_code = #{sensorCode}; -->
		SELECT
			se.sensor_code AS sensorCode,
			se.sensor_name AS sensorName,
			se.sensor_brand AS sensorBrand,
			se.position AS sensorPosition,
			se.position_des AS sensorPositionDes,
			se.use_date AS sensorUseDate,
			se.manufactor AS sensorManufactor,
		
			main.id AS mainTreeId,
			main.system_code as mainSysCode,
		
			site.id AS siteId,
			site.site_name AS siteName
		FROM
			busi_temper_point se
		LEFT JOIN busi_site site ON site.id = se.site_id
		LEFT JOIN busi_site_tree main ON main.id = se.equipment_id
		WHERE se.sensor_code = #{sensorCode}
			AND se.status = '1'
			AND main.status = '1'
	</select>

	<select id="getTransformerSubTree" resultType="com.huatek.busi.vo.BusiTreeNodeVo">
		SELECT
			t.id,
			t.`name` AS name,
			t.parent_id AS pid,
			t.equipment_type AS type,
			t.site_id as siteId, 
			t.equipment_id as equipmentId
		FROM
			busi_transformer_sub_tree t
		where t.`status` = '1'
		<if test="equipmentId != null and equipmentId != ''">
			and t.equipment_id = #{equipmentId}
		</if>
		<if test="equipmentType != null and equipmentType != ''">
			and t.equipment_type = #{equipmentType}
		</if>
		ORDER BY
			t.sort ASC;
	</select>


	<select id="getGisSubTree" resultType="com.huatek.busi.vo.BusiTreeNodeVo">
		SELECT
			t.id,
			t.`name` AS name,
			t.parent_id AS pid,
			t.equipment_type AS type,
			t.site_id as siteId,
			t.equipment_id AS equipmentId
		FROM
			busi_gis_sub_tree t
		<where>
			<if test="equipmentId != null and equipmentId != ''">
				and t.equipment_id = #{equipmentId}
			</if>
			<if test="equipmentType != null and equipmentType != ''">
				and t.equipment_type = #{equipmentType}
			</if>
		</where>
		AND t.`status` = '1'
		ORDER BY
			t.equipment_type ASC;
	</select>

	<select id="getGenSubTree" resultType="com.huatek.busi.vo.BusiTreeNodeVo">
		SELECT
		t.id,
		t.`name` AS name,
		t.parent_id AS pid,
		t.equipment_type AS type,
		t.site_id as siteId,
		t.equipment_id AS equipmentId
		FROM
		busi_gis_sub_tree t
		<where>
			<if test="equipmentId != null and equipmentId != ''">
				and t.equipment_id = #{equipmentId}
			</if>
			<if test="equipmentType != null and equipmentType != ''">
				and t.equipment_type = #{equipmentType}
			</if>
		</where>
		AND t.`status` = '1'
		ORDER BY
		t.equipment_type ASC;
	</select>


	<select id="getArresterSubTree" resultType="com.huatek.busi.vo.BusiTreeNodeVo">
		SELECT
			t.id,
			t.`name` AS name,
			t.parent_id AS pid,
			t.equipment_type AS type,
			t.site_id as siteId,
			t.equipment_id AS equipmentId
		FROM
			busi_arrester_sub_tree  t
		<where>
			<if test="equipmentId != null and equipmentId != ''">
				and t.equipment_id = #{equipmentId}
			</if>
			<if test="equipmentType != null and equipmentType != ''">
				and t.equipment_type = #{equipmentType}
			</if>
		</where>
		AND t.`status` = '1'
		ORDER BY
			t.equipment_type ASC;
	</select>
	
	<select id="getOtherSubTree" resultType="com.huatek.busi.vo.BusiTreeNodeVo">
		SELECT
			t.id,
			t.`name` AS name,
			t.parent_id AS pid,
			t.equipment_type AS type,
			t.site_id as siteId,
			t.equipment_id AS equipmentId
		FROM
			busi_other_sub_tree  t
		<where>
			<if test="equipmentId != null and equipmentId != ''">
				and t.equipment_id = #{equipmentId}
			</if>
			<if test="equipmentType != null and equipmentType != ''">
				and t.equipment_type = #{equipmentType}
			</if>
		</where>
		AND t.`status` = '1'
		ORDER BY
			t.equipment_type ASC;
	</select>

	<select id="getGeneratorSubTree" resultType="com.huatek.busi.vo.BusiTreeNodeVo">
		SELECT
		t.id,
		t.`name` AS name,
		t.parent_id AS pid,
		t.equipment_type AS type,
		t.site_id as siteId,
		t.equipment_id AS equipmentId
		FROM
		busi_generator_sub_tree  t
		<where>
			<if test="equipmentId != null and equipmentId != ''">
				and t.equipment_id = #{equipmentId}
			</if>
			<if test="equipmentType != null and equipmentType != ''">
				and t.equipment_type = #{equipmentType}
			</if>
		</where>
		AND t.`status` = '1'
		ORDER BY
		t.equipment_type ASC;
	</select>

	<select id="getCableSubTree" resultType="com.huatek.busi.vo.BusiTreeNodeVo">
		SELECT
		t.id,
		t.`name` AS name,
		t.parent_id AS pid,
		t.equipment_type AS type,
		t.site_id as siteId,
		t.equipment_id AS equipmentId
		FROM
		busi_cable_sub_tree  t
		<where>
			<if test="equipmentId != null and equipmentId != ''">
				and t.equipment_id = #{equipmentId}
			</if>
			<if test="equipmentType != null and equipmentType != ''">
				and t.equipment_type = #{equipmentType}
			</if>
		</where>
		AND t.`status` = '1'
		ORDER BY
		t.equipment_type ASC;
	</select>
</mapper>