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


	<resultMap id="BaseResultMap" type="com.huatek.busi.entity.BusiExternalEquipment">
		<id column="id" property="id" />
		<result column="equipment_name" property="equipmentName" />
		<result column="equipment_brand" property="equipmentBrand" />
		<result column="manufactor" property="manufactor" />
		<result column="production_date" property="productionDate" />
		<result column="use_date" property="useDate" />
		<result column="install_location" property="installLocation" />
		<result column="mark" property="mark" />
		<result column="equipment_id" property="equipmentId" />
		<result column="site_id" property="siteId" />
		<result column="external_type" property="externalType" />
	</resultMap>

	<select id="getByEquipmentId" resultType="com.huatek.busi.entity.BusiExternalEquipment">
		SELECT
		t.id,
		t.equipment_name as equipmentName,
		t.equipment_brand as equipmentBrand,
		t.manufactor as manufactor,
		t.production_date as productionDate,
		t.use_date as useDate,
		t.install_location as installLocation,
		t.mark as mark,
		t.equipment_id as equipmentId,
		t.site_id as siteId,
		t.external_type as externalType
		FROM
		busi_external_equipment t
		WHERE t.equipment_id = #{equipmentId}
	</select>

	<select id="queryBusiExternalEquipment" resultType="com.huatek.busi.entity.BusiExternalEquipment">
		SELECT 
            id,
            equipmentName,
            equipmentBrand,
            manufactor,
            productionDate,
            useDate,
            installLocation, 
            mark,
            equipmentId, 
            siteId,
            externalType
		 FROM (SELECT
		    t.id,
			t.equipment_name AS equipmentName,
			t.equipment_brand AS equipmentBrand,
			t.manufactor AS manufactor,
			t.production_date AS productionDate,
			t.use_date AS useDate,
			t.install_location AS installLocation,
			t.mark AS mark,
			t.equipment_id AS equipmentId,
			t.site_id AS siteId,
			t.external_type AS externalType
		FROM
		busi_external_equipment t, `busi_arrester_sub_tree`  a  WHERE t.`equipment_id`=a.`id` AND a.`equipment_type` IN ('021','051')  AND a.status='1'
		UNION ALL	
		SELECT
		    t.id,
			t.equipment_name AS equipmentName,
			t.equipment_brand AS equipmentBrand,
			t.manufactor AS manufactor,
			t.production_date AS productionDate,
			t.use_date AS useDate,
			t.install_location AS installLocation,
			t.mark AS mark,
			t.equipment_id AS equipmentId,
			t.site_id AS siteId,
			t.external_type AS externalType
		FROM
		busi_external_equipment t, `busi_gis_sub_tree`  b  WHERE t.`equipment_id`=b.`id` AND b.`equipment_type` IN ('051','061')  AND b.status='1'
		UNION ALL
		SELECT
			t.id,
			t.equipment_name AS equipmentName,
			t.equipment_brand AS equipmentBrand,
			t.manufactor AS manufactor,
			t.production_date AS productionDate,
			t.use_date AS useDate,
			t.install_location AS installLocation,
			t.mark AS mark,
			t.equipment_id AS equipmentId,
			t.site_id AS siteId,
			t.external_type AS externalType
		FROM
		busi_external_equipment t, `busi_transformer_sub_tree` c  WHERE t.`equipment_id`=c.`id` AND c.`equipment_type` IN ('041','051')
 		AND c.status='1'		
		) tmp_t
		ORDER BY tmp_t.equipmentName DESC
	</select>
	
	<select id="queryBusiExternalEquipmentByCondition" resultType="com.huatek.busi.entity.BusiExternalEquipment">
		SELECT
			t.id,
			t.equipment_name as equipmentName,
			t.equipment_brand as equipmentBrand,
			t.manufactor as manufactor,
			t.production_date as productionDate,
			t.use_date as useDate,
			t.install_location as installLocation,
			t.mark as mark,
			t.equipment_id as equipmentId,
			t.site_id as siteId,
			t.external_type as externalType
		FROM
		busi_external_equipment t,
		`busi_site_tree` tree 
		WHERE t.`site_id`=tree.`site_id`
		and tree.id = #{mainEqmId}
		and t.external_type=#{externalType}
	</select>
	

	<select id="selectTransformerBySensorCode" resultMap="BaseResultMap">
		SELECT
			e.*
		FROM
			`busi_external_equipment` e
		LEFT JOIN busi_transformer_sensor s ON e.equipment_id = s.equipment_id
		WHERE s.sensor_code= #{sensorCode}
   		and status = 1
	</select>

	<select id="selectGisBySensorCode" resultMap="BaseResultMap">
		SELECT
			e.*
		FROM
			`busi_external_equipment` e
		LEFT JOIN busi_gis_sensor s ON e.equipment_id = s.equipment_id
		WHERE
			s.sensor_code = #{sensorCode}
		and status = 1
	</select>

	<select id="selectArresterBySensorCode" resultMap="BaseResultMap">
		SELECT
			e.*
		FROM
			`busi_external_equipment` e
		LEFT JOIN busi_arrester_sensor s ON e.equipment_id = s.equipment_id
		WHERE s.sensor_code = #{sensorCode}
		and status = 1
	</select>

	<select id="selectGeneratorBySensorCode" resultMap="BaseResultMap">
		SELECT
			e.*
		FROM
			`busi_external_equipment` e
		LEFT JOIN busi_generator_sensor s ON e.equipment_id = s.equipment_id
		WHERE s.sensor_code = #{sensorCode}
		and status = 1
	</select>

	<select id="selectOtherBySensorCode" resultMap="BaseResultMap">	
	    SELECT
			e.*
		FROM
			`busi_external_equipment` e
		LEFT JOIN busi_other_sensor s ON e.equipment_id = s.equipment_id
		WHERE s.sensor_code = #{sensorCode}
	 	and status = 1
	</select>

	<select id="selectCableBySensorCode" resultMap="BaseResultMap">
	    SELECT
			e.*
		FROM
			`busi_external_equipment` e
		LEFT JOIN busi_cable_sensor s ON e.equipment_id = s.equipment_id
		WHERE s.sensor_code = #{sensorCode}
	 	and status = 1
	</select>

	<select id="selectTransformerByMainEqmId" resultMap="BaseResultMap">
		SELECT
			e.*
		FROM
			`busi_offline_transformer_brand` e
		WHERE e.status = 'A' and e.main_equipment_id= #{mainEqmId} ;
	</select>
	
		<select id="selectGisByMainEqmId" resultMap="BaseResultMap">
		SELECT
			e.*
		FROM
			`busi_offline_gis_brand` e 
		WHERE e.status = 'A' and e.main_equipment_id= #{mainEqmId} ;
	</select>
	
	<select id="selectArresterByMainEqmId" resultMap="BaseResultMap">
		SELECT
			e.*
		FROM
			`busi_offline_arrester_brand` e
		WHERE e.status = 'A' and e.main_equipment_id= #{mainEqmId} ;
	</select>
	
</mapper>