<?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.GisNumberMapper">
    <select id="findGisNumber" parameterType="java.lang.String" resultType="com.huatek.busi.entity.EquipmentNumber">
        SELECT
        t.site_id as siteId,
        t.main_equipment_id as mainEquipmentId,
        tt.id as tId,
        tt.equipment_type as equipmentType,
        SUM(f.real_count) AS totalRealCount,
        SUM(f.must_count) AS totalMustCount,
        name ,
        tt.create_time as createTime,
        f.d_time as dTime,
        count(name) AS number,
        w.warning_type as waringType,
        tt.status
        FROM
        busi_offline_gis_brand t
        LEFT JOIN busi_gis_sub_tree tt on t.site_id=tt.site_id and t.main_equipment_id=tt.equipment_id
        LEFT JOIN failure_rate f on f.main_tree_id =tt.equipment_id and tt.id=f.sub_tree_id
        LEFT JOIN busi_eqm_warning w on tt.id = w.sub_equipment_id
        WHERE t.main_equipment_id=#{mainEquipmentId}
        and t.site_id =#{siteId}
        and tt.status='1'
        and tt.equipment_type LIKE '___'
        and f.d_time = #{dTime}
        group BY tt.equipment_type
    </select>
    <select id="findGisStatus" parameterType="java.lang.String" resultType="com.huatek.busi.entity.GisStatus">
        select
        t.site_id as siteId,
        t.main_equipment_id as mainEquipmentId,
        t.`status`,
        t.gis_name as gisName,
        w.warning_type as warningType
        FROM
        busi_offline_gis_brand t
        LEFT JOIN busi_site_tree tt on tt.id =t.main_equipment_id
        LEFT JOIN
        busi_eqm_warning w on t.main_equipment_id = w.sub_equipment_id
        where t.`status` = '1'
        and t.gis_type = '01'
        and tt.`status` = '1'
        GROUP BY t.main_equipment_id
    </select>

    <select id="findTotalGisList" resultType="com.huatek.busi.entity.GisStatus">
        select
        t.site_id as siteId,
        t.main_equipment_id as mainEquipmentId,
        t.`status`,
        t.gis_name as gisName,
        w.warning_type as warningType
        FROM
        busi_offline_gis_brand t
        LEFT JOIN busi_site_tree tt on tt.id =t.main_equipment_id
        LEFT JOIN
        busi_eqm_warning w on t.main_equipment_id = w.sub_equipment_id
        where t.`status` = '1'
        and tt.`status` = '1'
        GROUP BY t.main_equipment_id
    </select>

    <select id="findAllGisStatus" parameterType="java.lang.String" resultType="com.huatek.busi.entity.GisStatus">
        select
        t.site_id as siteId,
        t.main_equipment_id as mainEquipmentId,
        t.`status`,
        t.gis_name as gisName,
        w.warning_type as warningType
        FROM
        busi_offline_gis_brand t
        LEFT JOIN
        busi_eqm_warning w on t.main_equipment_id = w.sub_equipment_id
        where t.`status` = '1'
        GROUP BY t.main_equipment_id
    </select>

    <select id="findGisSumNumber" parameterType="java.lang.String"
            resultType="com.huatek.busi.entity.EquipmentSumNumber">
        SELECT
        COUNT(*) AS sumNumber
        FROM
        busi_offline_gis_brand t
        LEFT JOIN busi_gis_sub_tree tt ON t.site_id = tt.site_id
        AND t.main_equipment_id = tt.equipment_id
        LEFT JOIN failure_rate f ON f.main_tree_id = tt.equipment_id
        AND tt.id = f.sub_tree_id
        LEFT JOIN busi_eqm_warning w ON tt.id = w.sub_equipment_id
        WHERE
        tt. STATUS = '1'
        AND tt.equipment_type LIKE '___'
        AND f.d_time = #{dTime}
        UNION ALL
        SELECT
        COUNT(*) AS sumFault
        FROM
        busi_offline_gis_brand t
        LEFT JOIN busi_gis_sub_tree tt ON t.site_id = tt.site_id
        AND t.main_equipment_id = tt.equipment_id
        LEFT JOIN failure_rate f ON f.main_tree_id = tt.equipment_id
        AND tt.id = f.sub_tree_id
        LEFT JOIN busi_eqm_warning w ON tt.id = w.sub_equipment_id
        WHERE
        tt. STATUS = '1'
        AND w.warning_type = '1'
        AND tt.equipment_type LIKE '___'
        AND f.d_time = #{dTime}

    </select>
    <select id="findSumNumber" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT count(*) from
        (
        SELECT
        bs.`site_name` siteName,
        'Gis设备' equipmentType,
        '2' type,
        st.`name` equipmentName,
        gs.equipment_type pendantType,
        gst.name pendantName,
        gs.`sensor_name` sensorName,
        gs.`sensor_code` sensorCode,
        gs.sensor_brand sensorBrand,
        gs.manufactor,
        gs.position_des positionDes,
        gs.production_date productionDate,
        gs.use_date useDate
        FROM
        busi_gis_sensor gs
        LEFT JOIN busi_site bs ON gs.`site_id` = bs.`id`
        LEFT JOIN busi_gis_sub_tree gst ON gst.`id` = gs.`equipment_id`
        LEFT JOIN busi_site_tree st ON st.`id` = gst.`equipment_id`
        WHERE gs.`site_id` = #{siteId}
        AND gs.status = '1'
        )temp
    </select>

    <select id="findFaultNumber" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT
        COUNT(*) AS sumFault
        FROM
        busi_offline_gis_brand t
        LEFT JOIN busi_gis_sub_tree tt ON t.site_id = tt.site_id
        AND t.main_equipment_id = tt.equipment_id
        LEFT JOIN failure_rate f ON f.main_tree_id = tt.equipment_id
        AND tt.id = f.sub_tree_id
        LEFT JOIN busi_eqm_warning w ON tt.id = w.sub_equipment_id
        WHERE
        tt. STATUS = '1'
        AND w.warning_type = '1'
        AND tt.equipment_type LIKE '___'
        AND f.d_time = #{dTime}
    </select>

    <select id="faultNumber" parameterType="java.lang.String" resultType="java.lang.Integer">
        SELECT
        count(*) AS number
        FROM
        busi_offline_gis_brand t
        LEFT JOIN busi_gis_sub_tree tt on t.site_id=tt.site_id and t.main_equipment_id=tt.equipment_id
        LEFT JOIN busi_eqm_warning w on tt.id = w.sub_equipment_id
        WHERE tt.id = #{tId}
        and tt.status='1'
        and w.warning_type = '1'
        and tt.equipment_type LIKE '___'
    </select>


    <select id="findIU" parameterType="java.lang.String" resultType="com.huatek.busi.entity.GisIU">
        SELECT
        abort_current as abortCurrent,
        abort_voltage as abortVoltage
        FROM iec_breaker_param
        where eqm_code = #{gisId}
        order by ins_date desc
        limit 1
    </select>

    <select id="findTransformerStatus" parameterType="java.lang.String"
            resultType="com.huatek.busi.entity.TAndTransformerStatus">
        SELECT
        i.site_id AS siteId,
        i.eqm_code AS eqmCode,
        i.stype AS stype,
        i.ins_date AS ins_date
        FROM
        busi_offline_gis_brand t
        LEFT JOIN busi_equipment_relationship tt on t.main_equipment_id = tt.equipment_id
        LEFT JOIN iec_bydwork_param i ON tt.transformer_id = i.eqm_code
        WHERE
        t.main_equipment_id = #{gisId}
        ORDER BY
        ins_date DESC
        LIMIT 1
    </select>
    <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_gis_sensor t ON e.sub_equipment_id = t.equipment_id
        WHERE
        t.`status` = '1'
        GROUP BY e.sensor_id
    </select>
    <select id="findEquipment" resultType="com.huatek.busi.entity.EquipmentSensorNumber">
        SELECT
        e.id as id,
        e.sensor_id as sensorId,
        e.main_equipment_id as mainEquipmentId,
        e.sensor_name as sensorName,
        e.ext_equipment_code as extEquipmentCode,
        e.time_stamp as timeStamp
        FROM
        equipment_temperature_cache e
        LEFT JOIN busi_gis_sub_tree t on e.main_equipment_id = t.equipment_id
        where t.status = '1'
        GROUP BY e.main_equipment_id
    </select>
    <select id="findSensorCache" resultType="com.huatek.busi.entity.EquipmentSensorCache">
        SELECT
        id AS id,
        sensor_id as sensorId,
        time_stamp as timeStamp,
        sensor_name as sensorName,
        ext_equipment_code AS extEquipmentCode,
        main_equipment_id AS mainEquipmentId,
        number,
        normal_number AS normalNumber,
        fault_number AS faultNumber,
        online_rate AS onlineRate
        FROM
        `busi_gis_sensor_cache`;
    </select>
    <select id="findGisSensor" parameterType="java.lang.String"
            resultType="com.huatek.busi.entity.EquipmentSensorCache">
        SELECT
        t.main_equipment_id as mainEquipmentId,
        t.sensor_name as sensorName,
        t.ext_equipment_code as extEquipmentCode,
        t.time_stamp as timeStamp,
        t.number,
        sum(t.normal_number) as normalNumber,
        t.fault_number as faultNumber,
        t.online_rate as onlineRate
        FROM
        busi_gis_sensor_cache t
        where t.main_equipment_id = #{mainEquipmentId}
        GROUP BY t.ext_equipment_code
    </select>
    <select id="findOnlineRateByCode" parameterType="java.lang.String"
            resultType="com.huatek.busi.vo.EquipmentOnlineRateVo">
        SELECT
        sum(t.number) as number,
        sum(t.normal_number) as normalNumber,
        sum(t.fault_number) as faultNumber
        FROM
        busi_gis_sensor_cache t
        where t.ext_equipment_code = #{extEquipmentCode}
    </select>
    <select id="finSumNumber" resultType="java.lang.Integer">
        select COUNT(*)
        FROM
        (SELECT
        bs.`site_name` siteName,
        'Gis设备' equipmentType,
        '2' type,
        st.`name` equipmentName,
        gs.equipment_type pendantType,
        gst.name pendantName,
        gs.`sensor_name` sensorName,
        gs.`sensor_code` sensorCode,
        gs.sensor_brand sensorBrand,
        gs.manufactor,
        gs.position_des positionDes,
        gs.production_date productionDate,
        gs.use_date useDate,
        gs.`status`,
        gs.equipment_id equipmentId,
        st.id mainEquipmentId
        FROM
        busi_gis_sensor gs
        LEFT JOIN busi_site bs ON gs.`site_id` = bs.`id`
        LEFT JOIN busi_gis_sub_tree gst ON gst.`id` = gs.`equipment_id`
        LEFT JOIN busi_site_tree st ON st.`id` = gst.`equipment_id`
        WHERE gs.status = '1' AND st.`status` = '1' AND gs.sensor_code!='' AND gs.`sensor_code` is not null
        )temp
    </select>
    <select id="findSumSensorNumber" resultType="com.huatek.busi.entity.EquipmentSumSensorCache">
        SELECT
        id,
        site_id as siteId,
        number,
        normal_number as normalNumber,
        fault_number as faultNumber,
        type
        FROM
        `busi_equipment_sensor_cache`
        where
        type='02'
    </select>

    <update id="updateGisSensor" parameterType="com.huatek.busi.entity.EquipmentSensorNumber">
        UPDATE busi_gis_sensor_cache
        <set>
            <if test="number != null">
                number = #{number},
            </if>
            <if test="normalNumber != null">
                normal_number = #{normalNumber},
            </if>
            <if test="faultNumber != null">
                fault_number = #{faultNumber},
            </if>
            <if test="onlineRate != null">
                online_rate = #{onlineRate},
            </if>
        </set>
        WHERE
        sensor_id = #{sensorId}
    </update>
    <insert id="addGisSensor" parameterType="com.huatek.busi.entity.EquipmentSensorNumber">
        insert into
        busi_gis_sensor_cache
        (
        `id` ,
        `sensor_id`,
        `time_stamp` ,
        `sensor_name` ,
        `ext_equipment_code`,
        `main_equipment_id` ,
        `number`,
        `normal_number` ,
        `fault_number`,
        `online_rate`
        )
        VALUES
        (
        #{id},
        #{sensorId},
        #{timeStamp},
        #{sensorName},
        #{extEquipmentCode},
        #{mainEquipmentId},
        #{number},
        #{normalNumber},
        #{faultNumber},
        #{onlineRate}
        )
    </insert>


</mapper>