<?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 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.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}
        </if>
        <if test="(dto.orderColumns == null or dto.orderColumns == '') and dto.sortOne != null and dto.sortOne != ''">
            order by ${dto.sortOne}
        </if>
        limit #{dto.current}, #{dto.size}
    </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.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>

    </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>
</mapper>
