<?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.yeejoin.amos.boot.module.jxiop.biz.tdMapper2.PvHealthIndexMapper">

<!--<insert id="saveBatchHealthIndexList">-->
<!--    insert into-->
<!--    <foreach collection="list" separator=" " item="item" index="index">-->
<!--        ${tableName}-->
<!--        using pv_health_index_data TAGS (#{item.analysisType})-->
<!--        values-->
<!--        (-->
<!--        now,-->
<!--        #{item.recDate, jdbcType=VARCHAR},-->
<!--        #{item.analysisObjType, jdbcType=VARCHAR},-->
<!--        #{item.analysisObjSeq, jdbcType=VARCHAR},-->
<!--        #{item.weight, jdbcType=FLOAT},-->
<!--        #{item.healthIndex, jdbcType=FLOAT},-->
<!--        #{item.healthLevel, jdbcType=VARCHAR},-->
<!--        #{item.analysisStartTime, jdbcType=VARCHAR},-->
<!--        #{item.analysisEndTime, jdbcType=VARCHAR},-->
<!--        #{item.area, jdbcType=VARCHAR},-->
<!--        #{item.station, jdbcType=VARCHAR},-->
<!--        #{item.subarray, jdbcType=VARCHAR},-->
<!--        #{item.manufacturer, jdbcType=VARCHAR},-->
<!--        #{item.deviceType, jdbcType=VARCHAR},-->
<!--        #{item.equipmentName, jdbcType=VARCHAR},-->
<!--        #{item.gatewayId, jdbcType=VARCHAR},-->
<!--        #{item.indexAddress, jdbcType=VARCHAR},-->
<!--        #{item.anomaly, jdbcType=FLOAT},-->
<!--        #{item.pointName, jdbcType=VARCHAR},-->
<!--        #{item.analysisTime, jdbcType=VARCHAR},-->
<!--        #{item.kks, jdbcType=VARCHAR},-->
<!--        )-->
<!--    </foreach>-->
<!--    </insert>-->



    <insert id="saveBatchHealthIndexList">
        insert into ${tableName}
        using pv_health_index_data TAGS (#{analysisType})
        values
        <foreach collection="list" separator="," item="item" index="index">
            (
            now,
            #{item.recDate, jdbcType=VARCHAR},
            #{item.analysisObjType, jdbcType=VARCHAR},
            #{item.analysisObjSeq, jdbcType=VARCHAR},
            #{item.weight, jdbcType=FLOAT},
            #{item.healthIndex, jdbcType=FLOAT},
            #{item.healthLevel, jdbcType=VARCHAR},
            #{item.analysisStartTime, jdbcType=VARCHAR},
            #{item.analysisEndTime, jdbcType=VARCHAR},
            #{item.area, jdbcType=VARCHAR},
            #{item.station, jdbcType=VARCHAR},
            #{item.subarray, jdbcType=VARCHAR},
            #{item.manufacturer, jdbcType=VARCHAR},
            #{item.deviceType, jdbcType=VARCHAR},
            #{item.equipmentName, jdbcType=VARCHAR},
            #{item.gatewayId, jdbcType=VARCHAR},
            #{item.indexAddress, jdbcType=VARCHAR},
            #{item.anomaly, jdbcType=FLOAT},
            #{item.pointName, jdbcType=VARCHAR},
            #{item.analysisTime, jdbcType=VARCHAR},
            #{item.kks, jdbcType=VARCHAR},
            #{item.orgCode, jdbcType=VARCHAR}
            )
        </foreach>
    </insert>

    <select id="getInfoListByGroupByCdPv" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvHealthIndex">
        select
            index_address,
            gateway_id ,
            station,
            '测点' as analysis_obj_type,
            analysis_obj_seq,
            weight,
            area,
            subarray,
            manufacturer,
            equipment_name,
            avg(anomaly) as anomaly,
            point_name,
            device_type,
            kks,
            org_code,
            AVG(health_index) as health_index
        from
            #{tableName}
        where
            analysis_obj_type = #{analysisObjectType}
          and ts > #{startTime}
        group by
            gateway_id,
            index_address,
            station,
            analysis_obj_type,
            analysis_obj_seq,
            weight,
            area,
            subarray,
            manufacturer,
            equipment_name,
            point_name,
            device_type,
            kks,
            org_code
    </select>

    <select id="getInfoListByGroupBySbPv" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvHealthIndex">
        select
            gateway_id ,
            station,
            '设备' as analysis_obj_type,
            area,
            subarray,
            equipment_name,
            avg(anomaly) as anomaly,
            org_code,
            AVG(health_index) as health_index
        from
            #{tableName}
        where
            analysis_obj_type = #{analysisObjectType}
          and ts > #{startTime}
        group by
            gateway_id,
            station,
            analysis_obj_type,
            area,
            subarray,
            org_code,
            equipment_name
    </select>

    <select id="getInfoListByGroupByZzPv" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvHealthIndex">
        select
            gateway_id ,
            station,
            '子阵' as analysis_obj_type,
            area,
            subarray,
            avg(anomaly) as anomaly,
            org_code,
            AVG(health_index) as health_index
        from
            #{tableName}
        where
            analysis_obj_type = #{analysisObjectType}
          and ts > #{startTime}
        group by
            gateway_id,
            station,
            analysis_obj_type,
            area,
            subarray,
            org_code
    </select>

    <select id="getInfoListByGroupByCzPv" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvHealthIndex">
        select
            gateway_id ,
            station,
            '场站' as analysis_obj_type,
            area,
            avg(anomaly) as anomaly,
            org_code,
            AVG(health_index) as health_index
        from
            #{tableName}
        where
            analysis_obj_type = #{analysisObjectType}
          and ts > #{startTime}
        group by
            gateway_id,
            station,
            analysis_obj_type,
            area,
            org_code
    </select>

    <select id="getInfoListByGroupByQyPv" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvHealthIndex">
        select
            '片区' as analysis_obj_type,
            area,
            avg(anomaly) as anomaly,
            org_code,
            AVG(health_index) as health_index
        from
            #{tableName}
        where
            analysis_obj_type = #{analysisObjectType}
          and ts > #{startTime}
        group by
            analysis_obj_type,
            area,
            org_code
    </select>

    <select id="getInfoByPage" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvHealthIndex">
        SELECT * FROM ${dto.tableName}
        <where>
            <if test="dto.analysisObjType!= null and dto.analysisObjType!= ''">analysis_obj_type = #{dto.analysisObjType}</if>
            <if test="dto.analysisType!= null and dto.analysisType!= ''">and analysis_type = #{dto.analysisType}</if>
            <if test="dto.endDateTs!= null and dto.endDateTs!= ''">  and ts &lt;= #{dto.endDateTs} </if>
            <if test="dto.startDateTs!= null and dto.startDateTs!= ''">  and  ts &gt;= #{dto.startDateTs} </if>
           <!--<if test="dto.endDate!= null and dto.endDate!= ''">  and ts &lt;= #{dto.endDate} </if>
            <if test="dto.startDate!= null and dto.startDate!= ''">  and  ts &gt;= #{dto.startDate} </if>-->                    
            <if test="dto.area!= null and dto.area!= ''"> AND area = #{dto.area} </if>
            <if test="dto.subarray!= null and dto.subarray!= ''"> AND subarray = #{dto.subarray} </if>
            <if test="dto.pointName!= null and dto.pointName!= ''">AND point_name = #{dto.pointName} </if>
            <if test="dto.station!= null and dto.station!= ''">AND station = #{dto.station} </if>
            <if test="dto.healthLevel!= null and dto.healthLevel!= ''">AND health_level = #{dto.healthLevel} </if>
            <if test="dto.equipmentName!= null and dto.equipmentName!= ''">AND equipment_name = #{dto.equipmentName}
            </if>
            <if test="dto.orgCode != null and dto.orgCode != ''">
               and  org_code like #{dto.orgCode}
            </if>
        </where>

        <if test="dto.orderColumns != null and dto.orderColumns != ''">
            order by ${dto.orderColumns},ts desc
        </if>
        <if test="(dto.orderColumns == null or dto.orderColumns == '') and dto.sortOne != null and dto.sortOne != ''">
            order by ${dto.sortOne},ts desc
        </if>
        limit #{dto.current}, #{dto.size}
    </select>

    <select id="getTsByRecDateGT" resultType="long">
        SELECT ts FROM analysis_data.${tableName}
        where rec_date &gt;= #{recDate}  order by ts ${sort} limit 1   ;
    </select>
    
    <select id="getTsByRecDateLT" resultType="long">
        SELECT ts FROM analysis_data.${tableName}
        where rec_date &lt;= #{recDate}  order by ts ${sort} limit 1   ;
    </select>

    <select id="getInfoByPageTotal" resultType="java.lang.Integer">
        SELECT count(1) FROM pv_health_index_data
        <where>
            <if test="dto.analysisObjType!= null and dto.analysisObjType!= ''">analysis_obj_type = #{dto.analysisObjType}</if>
            <if test="dto.analysisType!= null and dto.analysisType!= ''">and analysis_type = #{dto.analysisType}</if>
            <if test="dto.endDateTs!= null and dto.endDateTs!= '' ">  and ts &lt;= #{dto.endDateTs} </if>
            <if test="dto.startDateTs!= null and dto.startDateTs!= ''">  and  ts &gt;= #{dto.startDateTs} </if>
            <if test="dto.area!= null and dto.area!= ''"> AND area = #{dto.area} </if>
            <if test="dto.subarray!= null and dto.subarray!= ''"> AND subarray = #{dto.subarray} </if>
            <if test="dto.pointName!= null and dto.pointName!= ''">AND point_name = #{dto.pointName} </if>
            <if test="dto.station!= null and dto.station!= ''">AND station = #{dto.station} </if>
            <if test="dto.healthLevel!= null and dto.healthLevel!= ''">AND health_level = #{dto.healthLevel} </if>
            <if test="dto.equipmentName!= null and dto.equipmentName!= ''">AND equipment_name = #{dto.equipmentName}
            </if>
            <if test="dto.orgCode != null and dto.orgCode != ''">
                and  org_code like #{dto.orgCode}
            </if>
        </where>

    </select>

    <select id="saveBatchHealthIndexLatestInfo" resultType="int">
        insert into ${tableName}
        values
        <foreach collection="list" separator="," item="item" index="index">
            (
            now,
            #{item.recDate, jdbcType=VARCHAR},
            #{item.analysisObjType, jdbcType=VARCHAR},
            #{item.analysisObjSeq, jdbcType=VARCHAR},
            #{item.weight, jdbcType=FLOAT},
            #{item.healthIndex, jdbcType=FLOAT},
            #{item.healthLevel, jdbcType=VARCHAR},
            #{item.analysisStartTime, jdbcType=VARCHAR},
            #{item.analysisEndTime, jdbcType=VARCHAR},
            #{item.area, jdbcType=VARCHAR},
            #{item.station, jdbcType=VARCHAR},
            #{item.subarray, jdbcType=VARCHAR},
            #{item.manufacturer, jdbcType=VARCHAR},
            #{item.deviceType, jdbcType=VARCHAR},
            #{item.equipmentName, jdbcType=VARCHAR},
            #{item.gatewayId, jdbcType=VARCHAR},
            #{item.indexAddress, jdbcType=VARCHAR},
            #{item.anomaly, jdbcType=FLOAT},
            #{item.pointName, jdbcType=VARCHAR},
            #{item.analysisTime, jdbcType=VARCHAR},
            #{item.kks, jdbcType=VARCHAR},
            #{item.orgCode, jdbcType=VARCHAR}
            )
        </foreach>
    </select>

    <select id="getInfoListByGroupByQgPv" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvHealthIndex">
        select
            '全域' as analysis_obj_type,
            avg(anomaly) as anomaly,
            org_code,
            AVG(health_index) as health_index
        from
            #{tableName}
        where
            analysis_obj_type = #{analysisObjectType}
          and ts > #{startTime}
        group by
            analysis_obj_type,
            org_code
    </select>

    <select id="getInfoList" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvHealthIndex">
        select
            index_address,
            gateway_id ,
            station,
            analysis_obj_type,
            analysis_obj_seq,
            weight,
            area,
            subarray,
            manufacturer,
            equipment_name,
            anomaly,
            org_code,
            health_index,
            device_type,
            kks,
            point_name
        from
            #{tableName}
        where
            analysis_obj_type = #{analysisObjectType}
            and ts > #{startTime}
    </select>

    <insert id="saveBatchHealthIndexListNew">
        insert into analysis_data.${tableName}
        using analysis_data.pv_health_index_data_new TAGS (#{analysisType},#{recDate})
        values
        <foreach collection="list" separator="," item="item" index="index">
            (
            now,
            #{item.analysisObjType, jdbcType=VARCHAR},
            #{item.analysisObjSeq, jdbcType=VARCHAR},
            #{item.weight, jdbcType=FLOAT},
            #{item.healthIndex, jdbcType=FLOAT},
            #{item.healthLevel, jdbcType=VARCHAR},
            #{item.analysisStartTime, jdbcType=VARCHAR},
            #{item.analysisEndTime, jdbcType=VARCHAR},
            #{item.area, jdbcType=VARCHAR},
            #{item.station, jdbcType=VARCHAR},
            #{item.subarray, jdbcType=VARCHAR},
            #{item.manufacturer, jdbcType=VARCHAR},
            #{item.deviceType, jdbcType=VARCHAR},
            #{item.equipmentName, jdbcType=VARCHAR},
            #{item.gatewayId, jdbcType=VARCHAR},
            #{item.indexAddress, jdbcType=VARCHAR},
            #{item.anomaly, jdbcType=FLOAT},
            #{item.pointName, jdbcType=VARCHAR},
            #{item.orgCode, jdbcType=VARCHAR},
            #{item.analysisTime, jdbcType=VARCHAR},
            #{item.kks, jdbcType=VARCHAR}
            )
        </foreach>
    </insert>
</mapper>
