<?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.mapper2.IdxBizPvHealthIndexMapper">

    <select id="queryForLeftTableListByEquip"
            resultType="map">
        SELECT
        b.SEQUENCE_NBR,
        b.ARAE,
        b.STATION,
        b.EQUIPMENT_NAME,
        CAST(HEALTH_INDEX AS UNSIGNED) as HEALTH_INDEX,
        CAST(HEALTH_INDEX AS UNSIGNED) as value,
        b.HEALTH_LEVEL,
        CONCAT( '子阵 ', b.SUBARRAY ) AS 子阵,
        ( CASE b.HEALTH_LEVEL WHEN '危险' THEN 3 WHEN '警告' THEN 2 WHEN '注意' THEN 1 ELSE 0 END ) AS status,
        b.REC_DATE,
        b.SUBARRAY
        FROM
        `idx_biz_pv_health_index` b
        WHERE
        DATE_FORMAT(b.REC_DATE,"%Y-%m-%d") = DATE_FORMAT(NOW(),"%Y-%m-%d") AND b.ANALYSIS_OBJ_TYPE = '设备' AND
        b.ANALYSIS_TYPE = '按天'
        <if test="SUBARRAY != '' and SUBARRAY != null">
            AND b.SUBARRAY = #{SUBARRAY}
        </if>
        <if test="STATION != '' and STATION != null">
            AND b.STATION = #{STATION}
        </if>
        <if test="EQUIPMENTNAME != '' and EQUIPMENTNAME != null">
            AND b.EQUIPMENT_NAME = #{EQUIPMENTNAME}
        </if>
        <if test="HEALTHLEVEL != '' and HEALTHLEVEL != null">
            AND b.HEALTH_LEVEL = #{HEALTHLEVEL}
        </if>
        order by b.HEALTH_INDEX ASC
        limit ${current},${size}
    </select>


    <select id="queryForLeftTableListByEquipNum"
            resultType="map">
        SELECT
        CAST(  b.HEALTH_INDEX AS UNSIGNED) as value
        FROM
        `idx_biz_pv_health_index` b
        WHERE
        DATE_FORMAT(b.REC_DATE,"%Y-%m-%d") = DATE_FORMAT(NOW(),"%Y-%m-%d") AND b.ANALYSIS_OBJ_TYPE = '设备' AND
        b.ANALYSIS_TYPE = '按天'
        <if test="SUBARRAY != '' and SUBARRAY != null">
            AND b.SUBARRAY = #{SUBARRAY}
        </if>
        <if test="STATION != '' and STATION != null">
            AND b.STATION = #{STATION}
        </if>
        <if test="EQUIPMENTNAME != '' and EQUIPMENTNAME != null">
            AND b.EQUIPMENT_NAME = #{EQUIPMENTNAME}
        </if>
        <if test="HEALTHLEVEL != '' and HEALTHLEVEL != null">
            AND b.HEALTH_LEVEL = #{HEALTHLEVEL}
        </if>
    </select>
    <select id="queryForLeftTableListByEquipCount"
            resultType="int">
        SELECT
        count(1) as value
        FROM
        `idx_biz_pv_health_index` b
        WHERE
        DATE_FORMAT(b.REC_DATE,"%Y-%m-%d") = DATE_FORMAT(NOW(),"%Y-%m-%d") AND b.ANALYSIS_OBJ_TYPE = '设备' AND
        b.ANALYSIS_TYPE = '按天'
        <if test="SUBARRAY != '' and SUBARRAY != null">
            AND b.SUBARRAY = #{SUBARRAY}

        </if>
        <if test="STATION != '' and STATION != null">
            AND b.STATION = #{STATION}

        </if>
        <if test="HEALTHLEVEL != '' and HEALTHLEVEL != null">
            AND b.HEALTH_LEVEL = #{HEALTHLEVEL}
        </if>
        <if test="EQUIPMENTNAME != '' and EQUIPMENTNAME != null">
            AND b.EQUIPMENT_NAME = #{EQUIPMENTNAME}
        </if>

    </select>


    <select id="queryForLeftTableListByPoint"
            resultType="map">
        SELECT
        b.*
        FROM
        (
        SELECT
        POINT_NAME ,
        STATION,
        CAST(HEALTH_INDEX AS UNSIGNED) as HEALTH_INDEX,
        CAST(HEALTH_INDEX AS UNSIGNED) as value,
        HEALTH_LEVEL,
        ( CASE HEALTH_LEVEL WHEN '危险' THEN 3 WHEN '警告' THEN 2 WHEN '注意' THEN 1 ELSE 0 END ) AS status,
        SUBARRAY,
        REC_DATE,
        EQUIPMENT_NAME,
        INDEX_ADDRESS,
        ANALYSIS_OBJ_SEQ,
        ANALYSIS_TYPE
        FROM
        `idx_biz_pv_health_index`
        WHERE
        ANALYSIS_OBJ_TYPE = '测点'
        AND ( ANALYSIS_TYPE = '按小时' AND REC_DATE >= DATE_SUB( NOW( ), INTERVAL 1 HOUR ) )
        OR ( REC_DATE >= CURRENT_DATE ( ) AND ANALYSIS_TYPE = '按天' )
        OR ( ANALYSIS_TYPE = '按10分钟' AND REC_DATE >= DATE_SUB( NOW( ), INTERVAL 10 MINUTE ) )
        ) b
        INNER JOIN idx_biz_pv_warning_rule_set rule ON rule.ANALYSIS_POINT_ID = b.ANALYSIS_OBJ_SEQ
        AND rule.WARNING_NAME = '注意'
        AND rule.ANALYSIS_TYPE = b.ANALYSIS_TYPE
        <where>
            <if test="STATION != null and  STATION != '' ">
                b.STATION = #{STATION}
            </if>
            <if test="EQUIPMENTNAME != null and  EQUIPMENTNAME != '' ">
                AND b.EQUIPMENT_NAME = #{EQUIPMENTNAME}
            </if>
            <if test="SUBARRAY != null and  SUBARRAY != '' ">
                AND b.SUBARRAY = #{SUBARRAY}
            </if>
            <if test="HEALTHLEVEL != null and  HEALTHLEVEL != '' ">
                AND b.HEALTH_LEVEL = #{HEALTHLEVEL}
            </if>
            <if test="POINTNAME != null and  POINTNAME != '' ">
                AND b.POINT_NAME = #{POINTNAME}
            </if>
        </where>
        group by INDEX_ADDRESS
        order by b.HEALTH_INDEX ASC

        limit ${current},${size}
    </select>

    <select id="queryForLeftTableListByPointCount"
            resultType="int">
        SELECT
        COUNT(1) as value
        FROM
        (
        SELECT
        POINT_NAME ,
        STATION,
        HEALTH_INDEX,
        HEALTH_INDEX as value,
        HEALTH_LEVEL,
        SUBARRAY,
        REC_DATE,
        EQUIPMENT_NAME,
        INDEX_ADDRESS,
        ANALYSIS_OBJ_SEQ,
        ANALYSIS_TYPE
        FROM
        `idx_biz_pv_health_index`
        WHERE
        ANALYSIS_OBJ_TYPE = '测点'
        AND ( ANALYSIS_TYPE = '按小时' AND REC_DATE >= DATE_SUB( NOW( ), INTERVAL 1 HOUR ) )
        OR ( REC_DATE >= CURRENT_DATE ( ) AND ANALYSIS_TYPE = '按天' )
        OR ( ANALYSIS_TYPE = '按10分钟' AND REC_DATE >= DATE_SUB( NOW( ), INTERVAL 10 MINUTE ) )
        ) b
        INNER JOIN idx_biz_pv_warning_rule_set rule ON rule.ANALYSIS_POINT_ID = b.ANALYSIS_OBJ_SEQ
        AND rule.WARNING_NAME = '注意'
        AND rule.ANALYSIS_TYPE = b.ANALYSIS_TYPE
        <where>
            <if test="STATION != null and  STATION != '' ">
                b.STATION = #{STATION}
            </if>
            <if test="EQUIPMENTNAME != null and  EQUIPMENTNAME != '' ">
                AND b.EQUIPMENT_NAME = #{EQUIPMENTNAME}
            </if>
            <if test="SUBARRAY != null and  SUBARRAY != '' ">
                AND b.SUBARRAY = #{SUBARRAY}
            </if>
            <if test="HEALTHLEVEL != null and  HEALTHLEVEL != '' ">
                AND b.HEALTH_LEVEL = #{HEALTHLEVEL}
            </if>
            <if test="POINTNAME != null and  POINTNAME != '' ">
                AND b.POINT_NAME = #{POINTNAME}
            </if>
        </where>
<!--        group by INDEX_ADDRESS-->

    </select>

    <select id="queryForLeftTableListByPointNum"
            resultType="map">
        SELECT
        CAST(HEALTH_INDEX AS UNSIGNED) as value
        FROM
        (
        SELECT
        POINT_NAME ,
        STATION,
        CAST(HEALTH_INDEX AS UNSIGNED) as HEALTH_INDEX,
        CAST(HEALTH_INDEX AS UNSIGNED) as value,
        HEALTH_LEVEL,
        SUBARRAY,
        REC_DATE,
        EQUIPMENT_NAME,
        INDEX_ADDRESS,
        ANALYSIS_OBJ_SEQ,
        ANALYSIS_TYPE
        FROM
        `idx_biz_pv_health_index`
        WHERE
        ANALYSIS_OBJ_TYPE = '测点'
        AND ( ANALYSIS_TYPE = '按小时' AND REC_DATE >= DATE_SUB( NOW( ), INTERVAL 1 HOUR ) )
        OR ( REC_DATE >= CURRENT_DATE ( ) AND ANALYSIS_TYPE = '按天' )
        OR ( ANALYSIS_TYPE = '按10分钟' AND REC_DATE >= DATE_SUB( NOW( ), INTERVAL 10 MINUTE ) )
        ) b
        INNER JOIN idx_biz_pv_warning_rule_set rule ON rule.ANALYSIS_POINT_ID = b.ANALYSIS_OBJ_SEQ
        AND rule.WARNING_NAME = '注意'
        AND rule.ANALYSIS_TYPE = b.ANALYSIS_TYPE
        <where>
            <if test="STATION != null and  STATION != '' ">
                b.STATION = #{STATION}
            </if>
            <if test="EQUIPMENTNAME != null and  EQUIPMENTNAME != '' ">
                AND b.EQUIPMENT_NAME = #{EQUIPMENTNAME}
            </if>
            <if test="SUBARRAY != null and  SUBARRAY != '' ">
                AND b.SUBARRAY = #{SUBARRAY}
            </if>
            <if test="HEALTHLEVEL != null and  HEALTHLEVEL != '' ">
                AND b.HEALTH_LEVEL = #{HEALTHLEVEL}
            </if>
            <if test="POINTNAME != null and  POINTNAME != '' ">
                AND b.INDEX_ADDRESS = #{POINTNAME}
            </if>

        </where>

    </select>


    <select id="warningData" resultType="com.yeejoin.amos.boot.module.jxiop.biz.entity.IdxBizPvWarningRecord">
        SELECT
        *
        FROM
        idx_biz_pv_warning_record re
        WHERE
        re.`STATUS` = 0
        <if test="EQUIPMENTNAME != '' and EQUIPMENTNAME != null">
            AND re.EQUIPMENT_NAME = #{EQUIPMENTNAME}
        </if>
        <if test="SUBARRAY != null and  SUBARRAY != '' ">
            AND re.SUBARRAY = #{SUBARRAY}
        </if>
        <if test="STATION != null and  STATION != '' ">
            AND re.STATION = #{STATION}
        </if>
    </select>

    <select id="pointNum" resultType="int">
        SELECT
        count(1)
        FROM
        idx_biz_pv_point_process_variable_classification cl
        WHERE
        cl.TAG_CODE = '分析变量'
        <if test="EQUIPMENTNAME != '' and EQUIPMENTNAME != null">
            AND cl.EQUIPMENT_NAME = #{EQUIPMENTNAME}
        </if>
        <if test="SUBARRAY != null and  SUBARRAY != '' ">
            AND cl.SUBARRAY = #{SUBARRAY}
        </if>
        <if test="STATION != null and  STATION != '' ">
            AND cl.STATION = #{STATION}
        </if>
    </select>


    <select id="selectPointByANALYSISTYPE" resultType="map">
        SELECT
        b.*
        FROM
        (
        SELECT
        POINT_NAME,
        STATION,
        CAST(HEALTH_INDEX AS UNSIGNED) as HEALTH_INDEX,
        CAST(HEALTH_INDEX AS UNSIGNED) as value,
        HEALTH_LEVEL,
        ( CASE HEALTH_LEVEL WHEN '危险' THEN 3 WHEN '警告' THEN 2 WHEN '注意' THEN 1 ELSE 0 END ) AS STATUS,
        SUBARRAY,
        REC_DATE,
        EQUIPMENT_NAME,
        INDEX_ADDRESS,
        ANALYSIS_OBJ_SEQ,
        ANALYSIS_TYPE
        FROM
        `idx_biz_pv_health_index`
        WHERE
        ANALYSIS_OBJ_TYPE = '测点'
        <if test="(startTime == null or startTime == '' ) and (endTime == null or endTime =='')  ">
            AND ( ANALYSIS_TYPE = '按小时' AND REC_DATE >= DATE_SUB( NOW( ), INTERVAL 15 HOUR ) )
            OR ( REC_DATE >=DATE_SUB( NOW(), INTERVAL 15 DAY ) AND ANALYSIS_TYPE = '按天' )
            OR ( ANALYSIS_TYPE = '按10分钟' AND REC_DATE >= DATE_SUB( NOW( ), INTERVAL 150 MINUTE ) )
        </if>
        <if test="startTime != null and startTime != '' or endTime != null and endTime != '' ">
            AND REC_DATE >= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            <![CDATA[ AND REC_DATE  <= #{endTime}]]>
        </if>
        ) b
        INNER JOIN idx_biz_pv_warning_rule_set rule ON rule.ANALYSIS_POINT_ID = b.ANALYSIS_OBJ_SEQ
        AND rule.WARNING_NAME = '注意'
        AND rule.ANALYSIS_TYPE = b.ANALYSIS_TYPE
        <where>
            <if test="STATION != null and  STATION != '' ">
                b.STATION = #{STATION}
            </if>
            <if test="SUBARRAY != null and  SUBARRAY != '' ">
                AND b.SUBARRAY = #{SUBARRAY}
            </if>
            <if test="EQUIPMENTNAME != null and  EQUIPMENTNAME != '' ">
                AND b.EQUIPMENT_NAME = #{EQUIPMENTNAME}
            </if>
            <if test="HEALTHLEVEL != null and  HEALTHLEVEL != '' ">
                AND b.HEALTH_LEVEL = #{HEALTHLEVEL}
            </if>
            <if test="POINTNAME != null and  POINTNAME != '' ">
                AND b.INDEX_ADDRESS = #{POINTNAME}
            </if>
        </where>

    </select>

    <insert id="saveBatchHealthIndexLatestInfo">
            insert into
        fan_health_index_latest_data
        (
        `REC_DATE`,
        `ANALYSIS_OBJ_TYPE`,
        `ANALYSIS_OBJ_SEQ`,
        `WEIGTH`,
        `HEALTH_INDEX`,
        `HEALTH_LEVEL`,
        `ANALYSIS_TYPE`,
        `ANALYSIS_START_TIME`,
        `ANALYSIS_END_TIME`,
        `ARAE`,
        `STATION`,
        `SUB_SYSTEM`,
        `NUMBER`,
        `EQUIPMENT_NAME`,
        `GATEWAY_ID`,
        `INDEX_ADDRESS`,
        `ANOMALY`,
        `POINT_NAME`,
        `ANALYSIS_TIME`,
        `KKS`,
        `ORG_CODE`
        )
        values
            <foreach collection="list" separator="," item="item" index="index">
                (
                #{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.analysisType, jdbcType=VARCHAR},
                #{item.analysisStartTime, jdbcType=VARCHAR},
                #{item.analysisEndTime, jdbcType=VARCHAR},
                #{item.area, jdbcType=VARCHAR},
                #{item.station, jdbcType=VARCHAR},
                #{item.subSystem, jdbcType=VARCHAR},
                #{item.number, 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>

    <delete id="deleteAllDataByTableName">
        delete  from ${tableName} where analysis_type = #{analysisType}
    </delete>

    <insert id="saveBatchHealthIndexLatestInfoPv">
        insert into pv_health_index_latest_data
        (
        `REC_DATE`,
        `ANALYSIS_OBJ_TYPE`,
        `ANALYSIS_OBJ_SEQ`,
        `WEIGTH`,
        `HEALTH_INDEX`,
        `HEALTH_LEVEL`,
        `ANALYSIS_TYPE`,
        `ANALYSIS_START_TIME`,
        `ANALYSIS_END_TIME`,
        `ARAE`,
        `STATION`,
        `SUBARRAY`,
        `MANUFACTURER`,
        `DEVICE_TYPE`,
        `GATEWAY_ID`,
        `INDEX_ADDRESS`,
        `EQUIPMENT_NAME`,
        `ANOMALY`,
        `POINT_NAME`,
        `ANALYSIS_TIME`,
        `KKS`,
        `ORG_CODE`
        )
        values
        <foreach collection="list" separator="," item="item" index="index">
            (
            #{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.analysisType, 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.gatewayId, jdbcType=VARCHAR},
            #{item.indexAddress, jdbcType=VARCHAR},
            #{item.equipmentName, jdbcType=VARCHAR},
            #{item.anomaly, jdbcType=FLOAT},
            #{item.pointName, jdbcType=VARCHAR},
            #{item.analysisTime, jdbcType=VARCHAR},
            #{item.kks, jdbcType=VARCHAR},
            #{item.orgCode, jdbcType=VARCHAR}
            )
        </foreach>
    </insert>
</mapper>
