<?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.PvWaringRecordMapper">

<insert id="saveBatchWarningRecords">
    insert
    into
    pv_warning_record
<!--    (ts,-->
<!--    rec_date,-->
<!--    disposotion_state,-->
<!--    health_index_seq,-->
<!--    health_index,-->
<!--    analysis_point_id,-->
<!--    warning_name,-->
<!--    arae,-->
<!--    station,-->
<!--    subarray,-->
<!--    manufacturer,-->
<!--    device_type,-->
<!--    equipment_name,-->
<!--    gateway_id,-->
<!--    index_address,-->
<!--    content,-->
<!--    point_name,-->
<!--    health_level,-->
<!--    disposotion_date,-->
<!--    kks,-->
<!--    warning_period,-->
<!--    status)-->
    values
    <foreach collection="list" separator="," item="item" index="index">
        (#{item.ts, jdbcType=TIMESTAMP},
        #{item.recDate, jdbcType=VARCHAR},
        #{item.disposotionState, jdbcType=VARCHAR},
        #{item.healthIndex, jdbcType=VARCHAR},
        #{item.analysisPointId, jdbcType=VARCHAR},
        #{item.warningName, jdbcType=VARCHAR},
        #{item.arae, 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.content, jdbcType=VARCHAR},
        #{item.pointName, jdbcType=VARCHAR},
        #{item.healthLevel, jdbcType=VARCHAR},
        #{item.disposotionDate, jdbcType=VARCHAR},
        #{item.kks, jdbcType=VARCHAR},
        #{item.warningPeriod, jdbcType=VARCHAR},
        #{item.status, jdbcType=VARCHAR},
        #{item.orgCode, jdbcType=VARCHAR})
    </foreach>
    </insert>


    <select id="warningData" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvWarningRecord">
        SELECT
        warning_name as WarningName
        FROM
        analysis_data.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="selectFanWarningNum" resultType="map">

        select
        `a`.`subarray` as `subarray`,
        `a`.`station` as `station`,
        `a`.`警告` as `警告`,
        `a`.`注意` as `注意`,
        `a`.`危险` as `危险`,
        sum(((`a`.`注意` + `a`.`危险`) + `a`.`警告`)) AS `sort`
        from
        (select
        a.subarray,
        a.station,
        max( case a.warning_name when '危险' then num else 0 end ) as `危险`,
        max( case a.warning_name when '注意' then num else 0 end ) as `注意`,
        max( case a.warning_name when '警告' then num else 0 end ) as `警告`
        from
        (
        select
        re.warning_name,
        re.subarray,
        re.station,
        count( 1 ) as num
        from
        analysis_data.pv_warning_record re
        where
        re.`status` = 0
        or ( re.`status` = 1  and `ts` > (now() - 3d - 8h))
        <if test="station != null">
            and station = #{station}
        </if>
        <if test="gatewayIds != null and gatewayIds.size() > 0"> and GATEWAY_ID in
            <foreach item="item" index="index" collection="gatewayIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by
        re.station,
        re.subarray,
        re.warning_name
        ) a
        group by
        a.subarray,a.station) a
        group by
        `a`.`subarray`,
        `a`.`station`,`a`.`警告`, `a`.`注意`, `a`.`危险`
        order by sort DESC
    </select>


    <select id="selectWarningPoint" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvWarningRecord">
        SELECT
        a.station,
        a.warning_name,
        a.content,
        a.equipment_name,
        a.point_name,
        a.rec_date
        from
        analysis_data.pv_warning_record a
        where
        a.status = 0
        <if test="STATION != null and  STATION != '' ">
            AND a.station = #{STATION}
        </if>
        <if test="EQUIPMENTNAME != '' and EQUIPMENTNAME != null">
            AND a.equipment_name = #{EQUIPMENTNAME}
        </if>
        <if test="SUBARRAY != '' and SUBARRAY != null">
            AND a.subarray = #{SUBARRAY}
        </if>
        <if test="gatewayIds != null and gatewayIds.size() > 0"> AND GATEWAY_ID in
            <foreach item="item" index="index" collection="gatewayIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by
        station,
        equipment_name,
        point_name,
        rec_date ,
        content,
        warning_name
        limit #{current},#{size}
    </select>


    <select id="selectWarningPointTotal"  resultType="int">
        select count(1) from
        (SELECT
        a.station,
        a.warning_name,
        a.content,
        a.equipment_name,
        a.point_name,
        a.rec_date
        from
        analysis_data.pv_warning_record a
        where
        a.status = 0
        <if test="STATION != null and  STATION != '' ">
            AND a.station = #{STATION}
        </if>
        <if test="EQUIPMENTNAME != '' and EQUIPMENTNAME != null">
            AND a.equipment_name = #{EQUIPMENTNAME}
        </if>
        <if test="SUBARRAY != '' and SUBARRAY != null">
            AND a.subarray = #{SUBARRAY}
        </if>
        <if test="gatewayIds != null and gatewayIds.size() > 0"> AND GATEWAY_ID in
            <foreach item="item" index="index" collection="gatewayIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by
        station,
        equipment_name,
        point_name,
        rec_date ,
        content,
        warning_name) b
    </select>

    <select id="selectEquipWarningTotal" resultType="map">
        SELECT
        `STATION` AS `STATION`,
        `EQUIPMENT_NAME` AS `EQUIPMENT_NAME`,
        max( ( case `WARNING_NAME` WHEN '注意' THEN `num` ELSE 0 END ) ) AS `zhuyi`,
        max( ( case `WARNING_NAME` WHEN '警告' THEN `num` ELSE 0 END ) ) AS `jinggao`,
        max( ( case `WARNING_NAME` WHEN '危险' THEN `num` ELSE 0 END ) ) AS `weixian`,
        sum(
        (
        (
        ( CASE `WARNING_NAME` WHEN '注意' THEN `num` ELSE 0 END ) + ( case `WARNING_NAME` WHEN '警告' THEN `num` ELSE 0 END
        )
        ) + ( case `WARNING_NAME` WHEN '危险' THEN `num` ELSE 0 END )
        )
        ) AS `total`
        FROM
        (
        select
        `z`.`station` as `STATION`,
        `z`.`equipment_name` as `EQUIPMENT_NAME`,
        `z`.`warning_name` as `WARNING_NAME`,
        count( 1 ) as `num`
        from
        analysis_data.pv_warning_record z where
        ( `z`.`status` = 0 )
        <if test="STATION != null and  STATION != '' ">
            AND z.station = #{STATION}
        </if>
        <if test="EQUIPMENTNAME != '' and EQUIPMENTNAME != null">
            AND z.equipment_name = #{EQUIPMENTNAME}
        </if>
        <if test="SUBARRAY != '' and SUBARRAY != null">
            AND z.subarray = #{SUBARRAY}
        </if>
        <if test="gatewayIds != null and gatewayIds.size() > 0"> AND GATEWAY_ID in
            <foreach item="item" index="index" collection="gatewayIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by
        `z`.`station`,
        `z`.`warning_name`,
        `z`.`equipment_name`
        ) b
        GROUP BY
        `b`.`STATION`,
        `b`.`EQUIPMENT_NAME`
        limit 1
    </select>

<select id="getInfoByPage" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvWarningRecord">
    SELECT * FROM pv_warning_record
    <where>
        <if test="dto.area!= null and dto.area!= ''"> AND arae = #{dto.area} </if>
        <if test="dto.station!= null and dto.station!= ''">AND station = #{dto.station} </if>
        <if test="dto.equipmentName!= null and dto.equipmentName!= ''">AND equipment_name = #{dto.equipmentName}</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.warningName!= null and dto.warningName!= ''">AND warning_name = #{dto.warningName} </if>
        <if test="dto.warningPeriod!= null and dto.warningPeriod!= ''">AND warning_period = #{dto.warningPeriod} </if>
        <if test="dto.disposotionState!= null and dto.disposotionState!= ''">AND disposotion_state = #{dto.disposotionState} </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.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_warning_record
    <where>
        <if test="dto.area!= null and dto.area!= ''"> AND arae = #{dto.area} </if>
        <if test="dto.station!= null and dto.station!= ''">AND station = #{dto.station} </if>
        <if test="dto.equipmentName!= null and dto.equipmentName!= ''">AND equipment_name = #{dto.equipmentName}</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.warningName!= null and dto.warningName!= ''">AND warning_name = #{dto.warningName} </if>
        <if test="dto.warningPeriod!= null and dto.warningPeriod!= ''">AND warning_period = #{dto.warningPeriod} </if>
        <if test="dto.disposotionState!= null and dto.disposotionState!= ''">AND disposotion_state = #{dto.disposotionState} </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.orgCode != null and dto.orgCode != ''">
            and org_code like #{dto.orgCode}
        </if>
    </where>

    </select>

<select id="getInfoByTs" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.PvWarningRecord">
    select * from analysis_data.pv_warning_record where ts = #{ts,jdbcType=TIMESTAMP} limit 1
    </select>

<insert id="updateStatusByTs">
    insert
    into
    pv_warning_record
    (ts,
    disposotion_state,
    disposotion_date,
    status)
    values
    <foreach collection="list" separator="," item="item" index="index">
        (#{item.ts, jdbcType=TIMESTAMP},
        #{item.disposotionState, jdbcType=VARCHAR},
        #{item.disposotionDate, jdbcType=VARCHAR},
        #{item.status, jdbcType=VARCHAR})
    </foreach>
    </insert>

    <select id="getAllEquipAlarmInfo" resultType="map">

        select arae         as area,
               warning_name as warningName,
               count(1)     as num
        from  analysis_data.pv_warning_record  a
        where (a.disposotion_state = '待确认')
           or (a.disposotion_state = '已确认' and a.ts >(now()-3d -8h ))
        group by arae,
                 warning_name

    </select>
</mapper>
