<?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.frame.base.mapper.ModelMapper">

    <sql id="Base_Ref_Column_List">
        name,
        original_name as originalName,
        status,
        site_id as siteId,
        time_stamp as time,
        model_type as modelType,
        model_id as modelId
    </sql>

    <insert id="updateModelList">
        INSERT ignore INTO sys_model
        (original_name,model_id)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.originalName},#{item.modelId})
        </foreach>
    </insert>

    <select id="selectModelById" resultType="com.huatek.frame.base.entity.SysModel">
        select
        <include refid="Base_Ref_Column_List"/>
        from sys_model where model_id = #{model_id} and  deleted='0'
    </select>

    <select id="selectModelList" resultType="com.huatek.frame.base.vo.ModelVO">
        SELECT
        m.name,
        m.original_name as originalName,
        m.status,
        m.site_id as siteId,
        s.site_name as siteName,
        m.time_stamp as time,
        m.model_type as modelType,
        m.file_path as filePath,
        m.model_id as modelId,
        m.model_params as modelParams
        FROM
        sys_model m
        left join busi_site s
        on m.site_id = s.id
        where   m.deleted='0'

            <if test="name !=null and name !=''">
                AND m.name like concat("%",#{name},"%")
            </if>
            <if test="siteId != null and siteId != ''">
                AND m.site_id = #{siteId}
            </if>
        <if test="modelType != null and modelType != ''">
            AND m.model_type = #{modelType}
        </if>
            <if test="status != null and status != ''">
                AND m.status = #{status}
            </if>

        ORDER BY m.time_stamp desc, m.original_name
    </select>

    <select id="selectModelListRemoveSelf" resultType="com.huatek.frame.base.vo.ModelVO">
        SELECT
        m.name,
        m.original_name as originalName,
        m.status,
        m.site_id as siteId,
        s.site_name as siteName,
        m.time_stamp as time,
        m.model_type as modelType,
        m.model_id as modelId
        FROM
        sys_model m
        left join busi_site s
        on m.site_id = s.id
        where 1 = 1
        <if test="siteId != null and siteId != ''">
            AND m.site_id = #{siteId}
        </if>
        <if test="status != null and status != ''">
            AND m.status = #{status}
        </if>
        <if test="modelId != null and modelId != ''">
            AND m.model_id != #{modelId}
        </if>
        <if test="modelType != null and modelType != ''">
            AND m.model_type = #{modelType}
        </if>
        ORDER BY m.time_stamp desc, m.original_name
    </select>

    <update id="updateModel" >
        update sys_model
        <set>
            <if test="status != null and status !=''">
                status = #{status,jdbcType=VARCHAR},
            </if>
            <if test="name != null and name !=''">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="siteId != null and siteId !=''">
                site_id = #{siteId,jdbcType=VARCHAR},
            </if>
            <if test="modelType != null and modelType !=''">
                model_type = #{modelType,jdbcType=VARCHAR},
            </if>
        </set>
        where model_id = #{modelId,jdbcType=VARCHAR}
    </update>

    <update id="updateModelParams">
        update sys_model
         set   model_params = #{modelParams,jdbcType=VARCHAR}

        where model_id = #{modelId,jdbcType=VARCHAR}
    </update>

    <update id="enableModelComponentData">
        update model_component_data
        set status = #{enableType}
        where id in
        <foreach collection="ids" item="id" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="selectDeviceListList" parameterType="java.lang.String"
            resultType="com.huatek.frame.base.entity.SysModelDevice">
        SELECT
        model_type as modelType, device_code as deviceCode, device_name as deviceName
        FROM
        sys_model_device
        WHERE model_type = #{modelType}
    </select>

    <select id="selectDataItemsByDeviceCode" parameterType="java.lang.String"
            resultType="com.huatek.frame.base.entity.DataItem">
        SELECT
        item_code as itemCode, item_name as itemName
        FROM
        sys_device_data_item
        WHERE device_code = #{deviceCode}
    </select>

    <select id="selectStationList" resultType="com.huatek.frame.base.vo.SiteVo">
        SELECT
        id as siteId,
        site_name as siteName
        FROM
        busi_site
        where status = '1'
        and id != 1
    </select>

    <select id="selectGisStatus" parameterType="java.lang.String" resultType="com.huatek.frame.base.entity.BusiSiteTree">
        select
        t.id as id, t.parent_id as parentId, t.name as name, t.status as status, t.site_id as siteId,
        t.equipment_type as equipmentType, t.system_code as systemCode, t.create_time as createTime
        from
        busi_site_tree t
        left join
        busi_offline_gis_brand b
        t.id = b.main_equipment_id
        where t.site_id = #{siteId}
        and t.status = '1'
        and b.status = '1'
        and b.gis_type = '1'
        group by t.create_time
    </select>

    <!--···········-->
    <!--站点树下的子树-->
    <!--···········-->
    <!--变压器-->
    <select id="findListByEquipmentIdAndType" resultType="com.huatek.frame.base.entity.BusiTransfromerSubTree">
        SELECT
        t.id AS id ,
        t.parent_id AS parentId ,
        t.name AS name ,
        t.status AS status ,
        t.equipment_id AS equipmentId ,
        t.equipment_type AS equipmentType ,
        t.system_code AS systemCode ,
        t.site_id AS siteId ,
        t.create_time AS createTime
        FROM
        `busi_transformer_sub_tree` t
        WHERE
        t.status = '1'
        AND t.equipment_id = #{equipmentId}
        AND t.equipment_type = #{equipmentType}
    </select>

    <!--GIS-->
    <select id="findListByEquipmentIdAndTypeGis" resultType="com.huatek.frame.base.entity.BusiGisSubTree">
        SELECT
        t.id AS id ,
        t.parent_id AS parentId ,
        t.name AS name ,
        t.status AS status ,
        t.equipment_id AS equipmentId ,
        t.equipment_type AS equipmentType ,
        t.system_code AS systemCode ,
        t.site_id AS siteId ,
        t.create_time AS createTime
        FROM
        `busi_gis_sub_tree` t
        WHERE
        t.status = '1'
        AND t.equipment_id = #{equipmentId}
        AND t.equipment_type = #{equipmentType}
    </select>

    <!--避雷器-->
    <select id="findListByEquipmentIdAndTypeBlq" resultType="com.huatek.frame.base.entity.BusiArresterSubTree">
        SELECT
        t.id AS id ,
        t.parent_id AS parentId ,
        t.name AS name ,
        t.status AS status ,
        t.equipment_id AS equipmentId ,
        t.equipment_type AS equipmentType ,
        t.system_code AS systemCode ,
        t.site_id AS siteId ,
        t.create_time AS createTime
        FROM
        `busi_arrester_sub_tree` t
        WHERE
        t.status = '1'
        AND t.equipment_id = #{equipmentId}
        AND t.equipment_type = #{equipmentType}
    </select>

    <!--电缆-->
    <select id="findListByEquipmentIdAndTypeDl" resultType="com.huatek.frame.base.entity.BusiCableSubTree">
        SELECT
        t.id AS id ,
        t.parent_id AS parentId ,
        t.name AS name ,
        t.status AS status ,
        t.equipment_id AS equipmentId ,
        t.equipment_type AS equipmentType ,
        t.system_code AS systemCode ,
        t.site_id AS siteId ,
        t.create_time AS createTime
        FROM
        `busi_cable_sub_tree` t
        WHERE
        t.status = '1'
        AND t.equipment_id = #{equipmentId}
        AND t.equipment_type = #{equipmentType};
    </select>

    <!--发电机-->
    <select id="findListByEquipmentIdAndTypeGen" resultType="com.huatek.frame.base.entity.BusiGeneratorSubTree">
        SELECT
        t.id AS id ,
        t.parent_id AS parentId ,
        t.name AS name ,
        t.status AS status ,
        t.equipment_id AS equipmentId ,
        t.equipment_type AS equipmentType ,
        t.system_code AS systemCode ,
        t.site_id AS siteId ,
        t.create_time AS createTime
        FROM
        `busi_generator_sub_tree` t
        WHERE
        t.status = '1'
        AND t.equipment_id = #{equipmentId}
        AND t.equipment_type = #{equipmentType};
    </select>


    <!--···········-->
    <!--传感器-->
    <!--···········-->
    <!--变压器-->
    <select id="querySensorByEquipmentId" parameterType="java.lang.String"
            resultType="com.huatek.frame.base.vo.BusiSensorVo">
        SELECT
        id AS id,
        sensor_code AS sensorCode,
        sensor_name AS sensorName,
        sensor_name AS sensorNameDetail,
        sensor_brand AS sensorBrand,
        position AS position,
        position_des AS positionDes,
        manufactor AS manufactor,
        production_date AS productionDate,
        use_date AS useDate,
        equipment_id as equipmentId,
        '1' as type,
        equipment_type as equipmentType
        FROM
        busi_transformer_sensor t
        WHERE
        t.equipment_id = #{equipmentId}
        AND t.status = '1'
        AND t.sensor_code is not null
        ORDER BY t.sensor_code
    </select>

    <select id="querySensorByEquipmentIdGis" parameterType="java.lang.String"
            resultType="com.huatek.frame.base.vo.BusiSensorVo">
        SELECT
        id AS id,
        sensor_code AS sensorCode,
        sensor_name AS sensorName,
        sensor_name AS sensorNameDetail,
        sensor_brand AS sensorBrand,
        position AS position,
        position_des AS positionDes,
        manufactor AS manufactor,
        production_date AS productionDate,
        use_date AS useDate,
        equipment_id as equipmentId,
        '2' as type,
        equipment_type as equipmentType
        FROM
        busi_gis_sensor t
        WHERE
        t.equipment_id = #{equipmentId}
        AND t.status = '1'
        AND t.sensor_code is not null
        ORDER BY t.sensor_code
    </select>

    <select id="querySensorByEquipmentIdBlq" parameterType="java.lang.String"
            resultType="com.huatek.frame.base.vo.BusiSensorVo">
        SELECT
        id AS id,
        sensor_code AS sensorCode,
        sensor_name AS sensorName,
        sensor_name AS sensorNameDetail,
        sensor_brand AS sensorBrand,
        position AS position,
        position_des AS positionDes,
        manufactor AS manufactor,
        production_date AS productionDate,
        use_date AS useDate,
        equipment_id as equipmentId,
        '4' as type,
        equipment_type as equipmentType
        FROM
        busi_arrester_sensor t
        WHERE
        t.equipment_id = #{equipmentId}
        AND t.status = '1'
        AND t.sensor_code is not null
        ORDER BY t.sensor_code
    </select>

    <select id="querySensorByEquipmentIdCw" parameterType="java.lang.String"
            resultType="com.huatek.frame.base.vo.BusiSensorVo">
        SELECT id, sensor_code as sensorCode, sensor_name as sensorName, sensor_name as sensorNameDetail,
        equipment_id as equipmentId
        FROM busi_temper_point
        WHERE
        equipment_id = #{equipmentId}
        and status = '1'
        AND sensor_code is not null
        order by sensor_code
    </select>


    <select id="querySensorByEquipmentIdDl" resultType="com.huatek.frame.base.vo.BusiSensorVo">
        SELECT
        id,
        sensor_code AS sensorCode,
        sensor_name AS sensorName,
        sensor_name AS sensorNameDetail
        FROM
        busi_cable_sensor
        WHERE
        equipment_id = #{equipmentId}
        AND status = '1'
        AND sensor_code is not null
    </select>

    <select id="querySensorByEquipmentIdGen" resultType="com.huatek.frame.base.vo.BusiSensorVo">
        SELECT
        id,
        sensor_code AS sensorCode,
        sensor_name AS sensorName,
        sensor_name AS sensorNameDetail,
        ampl_type AS amplType
        FROM
        busi_generator_sensor
        WHERE
        equipment_id = #{equipmentId}
        AND status = '1'
        AND sensor_code is not null
    </select>
    <!--···········-->
    <!--变压器实时数据-->
    <!--···········-->
    <!--油色谱-->
    <select id="selectYspRecentlyData" resultType="map">
        select d_time,h2,ch4,c2h6,c2h4,c2h2,co,co2,o2,n2,h2o,total_hydrocarbon,tmp,dew,mst
        from transformer_epa_data_cache
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <!--局放（变压器/GIS/电缆）-->
    <select id="selectJfRecentlyData" resultType="map">
        select d_time,discharge_capacity,
        (CASE dsch_type
        WHEN '0' THEN '正常'
        WHEN '1' THEN '尖端放电'
        WHEN '2' THEN '悬浮放电'
        WHEN '3' THEN '沿面放电'
        WHEN '4' THEN '内部放电'
        WHEN '5' THEN '颗粒放电'
        WHEN '6' THEN '外部干扰'
        WHEN '7' THEN '其它'
        ELSE dsch_type END) AS dsch_type,
        dichange_count, discharge_position
        from transformer_pd_cache
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <!--铁芯/夹件-->
    <select id="selectTxRecentlyData" resultType="map">
        select d_time,total_core_current
        from transformer_icge_cache
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <!--红外（变压器/GIS/避雷器/电缆）-->
    <!--改为transformer_infrared_monitoring_cache缓存表-->
    <select id="selectHwRecentlyData" resultType="map">
        select d_time, h_temperature as max_tmp, l_temperature as min_tmp, avg_tmp
        from transformer_infrared_monitoring_cache
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <!--图片-->
    <select id="selectTpRecentlyDataList" resultType="map">
        select d_time, dataval, ifnull(picpath, "") as picpath
        from transformer_read_table
        where sensor_code = #{sensorId} order by d_time desc limit 3
    </select>

    <select id="selectRxRecentlyData" resultType="map">
        select time_stamp as d_time, total_current, capacitance, loss_factor, system_voltage
        from capacitance_equipment_im_cache
        where sensor_id = #{sensorId} and type = #{type} order by d_time limit 1
    </select>

    <!--红外/图片装置 视频地址-->
    <select id="selectVideoUrl" parameterType="string" resultType="string">
        select ifnull(http_url, "")
        from busi_video
        where task_code = #{sensor_code} and del_flag = '0'
        order by create_date desc limit 1
    </select>

    <!--容性-->
    <select id="selectRxRecentlyDataByq" resultType="map">
        select time_stamp as d_time, total_current, capacitance, loss_factor, system_voltage
        from capacitance_equipment_im
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <!--气象（变压器/GIS ）-->
    <select id="selectQxRecentlyData" resultType="map">
        select time_stamp as d_time, air_temperature, air_pressure, humidity, precipitation, radiation_intensity
        from transformer_meteorological
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <!--测温装置（变压器 ）-->
    <select id="selectCwzzRecentlyData" resultType="map">
        select time_stamp as d_time, tmp
        from cwzz_temperature
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <!--绕组测温（变压器 ）-->
    <select id="selectRzcwRecentlyData" resultType="map">
        select d_time as d_time, h_temperature as max_tmp
        from transformer_winding_temperature_cache
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <!--···········-->
    <!--GIS实时数据-->
    <!--···········-->
    <!--sf6-->
    <select id="selectSf6RecentlyData" resultType="map">
        select modified_time as d_time, temper as temperature, moisture, pressure as pressure20C
        from rpt_gis_monitor_sf6
        where sensor_id = #{sensorId}
    </select>

    <!--图片-->
    <select id="selectTpRecentlyDataGis" resultType="map">
        select time_stamp as d_time, dataval, ifnull(picpath, "") as picpath
        from gis_read_table
        where sensor_code = #{sensorId} order by d_time desc limit 1
    </select>


    <select id="selectSf6EnvRecentlyData" resultType="map">
        select d_time as d_time, tmp, dew, conc,o_content as ocontent
        from gis_sf6_env_monitoring_statistics
        where sensor_id = #{sensorId}
    </select>

    <!--···········-->
    <!--避雷器实时数据-->
    <!--···········-->
    <!--绝缘监测-->
    <select id="selectJyRecentlyData" resultType="map">
        select time_stamp as d_time, system_voltage, total_current, resistive_current, action_count
        from lightningrod_im
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <!--图片-->
    <select id="selectTpRecentlyDataBlq" resultType="map">
        select time_stamp as d_time, dlval as dataval, ifnull(picpath, "") as picpath
        from arrester_read_table
        where sensor_code = #{sensorId} order by d_time desc limit 1
    </select>

    <!--···········-->
    <!--开关柜实时数据-->
    <!--···········-->
    <select id="selectCwRecentlyData" resultType="java.util.Map">
        select  d_time, temper as tmp
        from rpt_termper_record
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <!--···········-->
    <!--电缆实时数据-->
    <!--···········-->
    <!--电缆环流-->
    <select id="selectDlhlRecentlyData" resultType="java.util.Map">
        select d_time, total_core_current
        from busi_cable_circulation_cache
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <!--电缆沟环境-->
    <select id="selectDlgRecentlyData" resultType="java.util.Map">
        select time_stamp as d_time, temperature, humidity, a_water,
        smoke_alarm1, smoke_alarm2, smoke_alarm3
        from busi_cable_dlg
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <!--···········-->
    <!--发电机实时数据-->
    <!--···········-->
    <!--振动-->
    <select id="selectZdRecentlyData" resultType="java.util.Map">
        SELECT d_time, ampl_x, ampl_y, ampl_z, freq_x, freq_y, freq_z, spd_x, spd_y, spd_z, aircap_am, aircap_bm, aircap_cm, aircap_dm
        from busi_generator_ampl_cache
        where sensor_code = #{sensorId} order by d_time desc limit 1
    </select>

    <!--压力-->
    <select id="selectYlRecentlyData" resultType="java.util.Map">
        SELECT d_time, pv_a, pv_b
        from busi_generator_pressure_cache
        where sensor_code = #{sensorId} order by d_time desc limit 1
    </select>

    <!--局放 发电机-->
    <select id="selectGenJfRecentlyData" resultType="map">
        select d_time,discharge_capacity,
        (CASE dsch_type
        WHEN '0' THEN '正常'
        WHEN '1' THEN '尖端放电'
        WHEN '2' THEN '悬浮放电'
        WHEN '3' THEN '沿面放电'
        WHEN '4' THEN '内部放电'
        WHEN '5' THEN '颗粒放电'
        WHEN '6' THEN '外部干扰'
        WHEN '7' THEN '其它'
        ELSE dsch_type END) AS dsch_type,
        pulse_count, discharge_position
        from busi_generator_pd_cache
        where sensor_id = #{sensorId} order by d_time desc limit 1
    </select>

    <select id="selectMasterStation" resultType="com.huatek.frame.base.vo.MasterStationPartVO">
        select
            device_code as deviceCode,
            main_tree_id as mainTreeId,
            position_x as positionx,
            position_y as positiony,
            position_z as positionz
        from
            model_component_data
        where
            model_id = #{modelId}
            and del_flag = '0'
            and status = '1'
    </select>
    <select id="querySensorByEquipmentIdCwNew" resultType="com.huatek.frame.base.vo.BusiSensorNewVo">
        select
            p.id,
            p.sensor_code as sensorCode,
            p.sensor_name as sensorName,
            p.sensor_name as sensorNameDetail,
            p.equipment_id as equipmentId,
            r.d_time as dTime,
            r.temper as tmp
        from
            rpt_termper_record r
            left join busi_temper_point p on
            p.sensor_code = r.sensor_id
        where
            p.equipment_id = #{mainTreeId}
            and p.status = '1'
            and p.sensor_code is not null
    </select>
</mapper>