<?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.IecOriginDataMapper">
  <resultMap id="BaseResultMap" type="com.huatek.busi.vo.IecOriginDataVo">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="site_id" jdbcType="VARCHAR" property="siteId" />
    <result column="site_tree_id" jdbcType="VARCHAR" property="siteTreeId" />
    <result column="d_time" jdbcType="TIMESTAMP" property="dTime" />
    <result column="state" jdbcType="VARCHAR" property="state" />
    <result column="stype" jdbcType="CHAR" property="stype" />
    <result column="sadr" jdbcType="INTEGER" property="sadr" />
    <result column="sadrval" jdbcType="VARCHAR" property="sadrval" />
    <result column="sensor_code" jdbcType="VARCHAR" property="sensorCode" />
  </resultMap>
  <sql id="Base_Column_List">
    origin.id as id, 
    origin.site_id as siteId, 
    origin.site_tree_id as siteTreeId,
    origin.d_time as dTime,
    origin.state as state, 
    origin.stype as stype,
    origin.sadr as sadr,
    origin.sadrval as sadrval,
    b.sensor_code as sensorCode 
  </sql>
  
  <!-- 
     根据避雷器的sensorCode查询主设备状态 1-发电状态 2：抽水状态 3：空闲状态
   -->
  <select id="selectByArresterSensorCode" parameterType="java.lang.String" resultMap="BaseResultMap">
	    select 
	    <include refid="Base_Column_List" />
	    from 
		     iec_origin_data origin,
		  (SELECT 
		    siteTree.*,
		    sensor_code 
		  FROM
		    busi_site_tree siteTree,
		    (SELECT 
		      sub.id AS sub_tree_id,
		      sensor.sensor_code,
		      sub.equipment_id 
		    FROM
		      busi_arrester_sensor sensor,
		      busi_arrester_sub_tree sub 
		    WHERE sensor.sensor_code = #{sensorCode} 
		      AND sensor.equipment_id = sub.id) a 
		  WHERE a.equipment_id = siteTree.id) b 
		WHERE origin.site_tree_id = b.id 
  </select>
  
  <!-- 
     根据GIS的sensorCode查询主设备状态 1-发电状态 2：抽水状态 3：空闲状态
   -->
   <select id="selectByGisSensorCode" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
	    from iec_origin_data origin,
		  (SELECT 
		    siteTree.*,
		    sensor_code 
		  FROM
		    busi_site_tree siteTree,
		    (SELECT 
		      sub.id AS sub_tree_id,
		      sensor.sensor_code,
		      sub.equipment_id 
		    FROM
		      busi_gis_sensor sensor,
		      busi_gis_sub_tree sub
		    WHERE sensor.sensor_code = #{sensorCode}  
		      AND sensor.equipment_id = sub.id) a 
		  WHERE a.equipment_id = siteTree.id) b 
		WHERE origin.site_tree_id = b.id 
  </select>
  
   <!-- 
     根据变压器的sensorCode查询主设备状态 1-发电状态 2：抽水状态 3：空闲状态
   -->
   <select id="selectBytransformerSensorCode" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from iec_origin_data origin,
	  (SELECT 
	    siteTree.*,
	    sensor_code 
	  FROM
	    busi_site_tree siteTree,
	    (SELECT 
	      sub.id AS sub_tree_id,
	      sensor.sensor_code,
	      sub.equipment_id 
	    FROM
	      busi_transformer_sensor sensor,
	      busi_transformer_sub_tree sub 
	    WHERE sensor.sensor_code = #{sensorCode}
	      AND sensor.equipment_id = sub.id) a 
	  WHERE a.equipment_id = siteTree.id) b 
	WHERE origin.site_tree_id = b.id 
  </select>
</mapper>
