<?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.cable.EquipmentTemperatureCacheMapper">

    <select id="findByTreeId" resultType="com.huatek.busi.entity.EquipmentTemperatureCache">
        SELECT
       `id` ,
      `sensor_id` as sensorId,
      `cac_monitor_type_code` as cacMonitorTypeCode,
      `equipment_id` as equipmentId,
      `time_stamp` as  timeStamp,
      `equipment_code` as equipmentCode,
      `ext_equipment_code` as extEquipmentCode,
      `site_id` as siteId,
      `main_equipment_id` as mainEquipmentId,
      `sub_equipment_id`  as subEquipmentId,
      `main_tree_id` as mainTreeId,
      `sub_tree_id` as subTreeId,
      `run_state` as runState,
      `hbase_rowkey` as hbaseRowkey,
        `type`
            FROM
            equipment_temperature_cache
            WHERE
        sensor_id = #{sensorId}

    </select>

    <update id="updateSensor" parameterType="com.huatek.busi.entity.EquipmentTemperatureCache" >
        UPDATE equipment_temperature_cache
        <set >
        <if test="timeStamp != null" >
        time_stamp = #{timeStamp},
        </if>
        </set>
         WHERE
        sensor_id = #{sensorId}
    </update>
    <insert id="addSensor" parameterType="com.huatek.busi.entity.EquipmentTemperatureCache" >
        insert into
         equipment_temperature_cache
        (
          `id` ,
          `sensor_id`,
          `cac_monitor_type_code`,
          `equipment_id`,
          `time_stamp` ,
          `equipment_code` ,
          `ext_equipment_code`,
          `site_id`,
          `main_equipment_id`,
          `sub_equipment_id` ,
          `main_tree_id`,
          `sub_tree_id`,
          `run_state`,
          `hbase_rowkey`,
            `type`
        )
        VALUES
        (
        #{id},
        #{sensorId},
        #{cacMonitorTypeCode},
        #{equipmentId},
        #{timeStamp},
        #{equipmentCode},
        #{extEquipmentCode},
        #{siteId},
        #{mainEquipmentId},
        #{subEquipmentId},
        #{subTreeId},
        #{runState},
        #{hbaseRowkey},
        #{type}
        )
    </insert>
    <select id="findEquipmentList" resultType="com.huatek.busi.entity.EquipmentTemperatureCache">
        SELECT
        sensor_id as sensorId,
        time_stamp as timeStamp,
        site_id as siteId
        FROM
        equipment_temperature_cache
    </select>


    <insert id="addSensorVo" parameterType="com.huatek.busi.entity.EquipmentTemperatureCache" >
    insert into
         equipment_temperature_cache
        (
          `id`,
          `sensor_id`,
          `time_stamp`,
          `sub_equipment_id`,
          `main_equipment_id`,
          `ext_equipment_code`,
          `sensor_name`,
            type
        )
        VALUES
        (
        #{id},
        #{sensorId},
        #{timeStamp},
        #{subEquipmentId},
        #{mainEquipmentId},
        #{extEquipmentCode},
        #{sensorName},
        #{type}
        )
    </insert>

    <update id="updateSensorVo" parameterType="com.huatek.busi.entity.EquipmentTemperatureCache" >
        UPDATE equipment_temperature_cache
        <set >
            <if test="timeStamp != null" >
                time_stamp = #{timeStamp},
            </if>
        </set>
        WHERE
        sensor_id = #{sensorId}
    </update>


    <insert id="addEntity" parameterType="com.huatek.busi.entity.EquipmentTemperatureCache" >
        insert into
         equipment_temperature_cache
        (
          `id` ,
          `sensor_id`,
          `cac_monitor_type_code`,
          `equipment_id`,
          `time_stamp` ,
          `equipment_code` ,
          `ext_equipment_code`,
          `site_id`,
          `main_equipment_id`,
          `sub_equipment_id` ,
          `main_tree_id`,
          `sub_tree_id`,
          `run_state`,
          `hbase_rowkey`
        )
        VALUES
        (
        #{id},
        #{sensorId},
        #{cacMonitorTypeCode},
        #{equipmentId},
        #{timeStamp},
        #{equipmentCode},
        #{extEquipmentCode},
        #{siteId},
        #{mainEquipmentId},
        #{subEquipmentId},
        #{subTreeId},
        #{runState},
        #{hbaseRowkey}
        )
    </insert>
    <delete id="deleteEntity" parameterType="java.lang.String">
    DELETE
    FROM
        `equipment_temperature_cache`
    where sensor_id=#{sensorCacheId}
    </delete>
    <select id="findEquipmentVoList" resultType="com.huatek.busi.entity.EquipmentSensor">
    SELECT
	e.sensor_id as sensorId,
	e.time_stamp as timeStamp,
	e.sub_equipment_id as subEquipmentId,
	t.sensor_brand as sensorBrand,
	t.sensor_name as sensorName,
	t.equipment_type as equipmentType,
	t.status
    FROM
        `equipment_temperature_cache` e
    LEFT JOIN busi_transformer_sensor t ON e.sub_equipment_id = t.equipment_id
    WHERE
     t.`status` = '1'
    GROUP BY e.sensor_id
    </select>


    <select id="selectSumTransformerSensorInfo" resultType="com.huatek.busi.entity.EquipmentTemperatureCache">
        SELECT
        e.sensor_id as sensorId,
        e.time_stamp as timeStamp,
        e.sub_equipment_id as subEquipmentId,
        e.sensor_name as sensorName,
        e.type,
        e.run_state as runState,
        temp.position_des as positionDes
        FROM
        `equipment_temperature_cache` e

        left join (
        SELECT
            sensor_code,
            position_des
        FROM
            `busi_arrester_sensor`
        where
            status = '1'
        UNION ALL
        SELECT
            sensor_code,
            position_des
        FROM
            `busi_cable_sensor`
        where
            status = '1'
        UNION ALL
        SELECT
            sensor_code,
            position_des
        FROM
            `busi_generator_sensor`
        where
            status = '1'
        UNION ALL
        SELECT
            sensor_code,
            position_des
        FROM
            `busi_gis_sensor`
        where
            status = '1'
        UNION ALL
        SELECT
            sensor_code,
            position_des
        FROM
            `busi_transformer_sensor`
        where
            status = '1') temp on temp.sensor_code = e.sensor_id
        <if test="type != null and type != ''" >
            WHERE  e.type=#{type}
        </if>

    </select>

</mapper>
