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

<insert id="saveBatchWarningRecords">

    insert
    into
        fan_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.healthIndexSeq, jdbcType=VARCHAR},-->
        #{item.healthIndex, jdbcType=VARCHAR},
        #{item.analysisPointId, jdbcType=VARCHAR},
        #{item.warningName, jdbcType=VARCHAR},
        #{item.arae, 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.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="selectFanWarningNum" resultType="map">

        SELECT `b`.`EQUIPMENT_NAME` AS `EQUIPMENT_NAME`,
        `b`.`STATION` AS `STATION`,
        `b`.`警告` AS `警告`,
        `b`.`注意` AS `注意`,
        `b`.`危险` AS `危险`,
        sum(((`b`.`注意` + `b`.`危险`) + `b`.`警告`)) AS `sort`
        FROM (select `a`.`EQUIPMENT_NAME` as `EQUIPMENT_NAME`,
        a.`STATION` as `STATION`,
        max(case `a`.`WARNING_NAME` when '危险' then `a`.`num` else 0 end) as `危险`,
        max(case `a`.`WARNING_NAME` when '注意' then `a`.`num` else 0 end) as `注意`,
        max(case `a`.`WARNING_NAME` when '警告' then `a`.`num` else 0 end) as `警告`
        from (
        select `warning_name` as `WARNING_NAME`,
        `equipment_name` as `EQUIPMENT_NAME`,
        `station` as `STATION`,
        count(1) as `num`
        from analysis_data.fan_warning_record
        where `status` = 0
        or (`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 `station`,
        `equipment_name`,
        `warning_name`) a
        group by `a`.`EQUIPMENT_NAME`, a.`STATION`) b
        GROUP BY `b`.`EQUIPMENT_NAME`,
        `b`.`STATION`, `b`.`警告`, `b`.`注意`, `b`.`危险`
        order by sort DESC

    </select>


    <select id="warningData" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.FanWarningRecord">
        SELECT
        warning_name as WarningName
        FROM
        analysis_data.fan_warning_record re
        WHERE
        re.`status` = 0
        <if test="EQUIPMENTNAME != '' and EQUIPMENTNAME != null">
            AND re.equipment_name = #{EQUIPMENTNAME}
        </if>
        <if test="SUBSYSTEM != null and  SUBSYSTEM != '' ">
            AND re.sub_system = #{SUBSYSTEM}
        </if>
        <if test="STATION != null and  STATION != '' ">
            AND re.station = #{STATION}
        </if>
    </select>

    <select id="selectWarningPoint" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.FanWarningRecord">
        select * from (
        SELECT
        a.station,
        a.warning_name,
        a.content,
        a.equipment_name,
        a.point_name,
        a.rec_date
        from
        analysis_data.fan_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="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
        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.fan_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>
        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.fan_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="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`
    </select>

<select id="getInfoByPage" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.FanWarningRecord">
    SELECT * FROM fan_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.subSystem!= null and dto.subSystem!= ''">AND sub_system = #{dto.subSystem} </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.gatewayIds != null and dto.gatewayIds.size() > 0"> and GATEWAY_ID in
            <foreach item="item" index="index" collection="dto.gatewayIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </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 fan_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.subSystem!= null and dto.subSystem!= ''">AND sub_system = #{dto.subSystem} </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.gatewayIds != null and dto.gatewayIds.size() > 0"> and GATEWAY_ID in
            <foreach item="item" index="index" collection="dto.gatewayIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </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.FanWarningRecord">
    select * from analysis_data.fan_warning_record where ts = #{ts,jdbcType=TIMESTAMP} limit 1
    </select>

<insert id="updateStatusByTs">
    insert
    into
    fan_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.fan_warning_record  a
        where (a.disposotion_state = '待确认')
           or (a.disposotion_state = '已确认' and a.ts >(now()-3d -8h ))
        group by arae,
                 warning_name

    </select>

    <select id="getEquipWarningInfoByPage" resultType="com.yeejoin.amos.boot.module.jxiop.biz.tdengine.FanWarningRecord">
        select
        a.*
        from
        (
        select
               ts,
        rec_date as recDate,
        station as station,
        equipment_name as equipmentName,
        content as content,
        warning_name as warningName,
        disposotion_state as disposotionState,
        arae as arae,
        'pv' as stationType,
        gateway_id,
        concat(point_name, '异常') as pointName,
        disposotion_date as disposotionDate,
        (case when warning_name = '注意' then 1
        when warning_name = '警告' then 2
        when warning_name = '危险' then 3
        end) as sort
        from
        analysis_data.pv_warning_record union all
        select
        ts,
        rec_date as recDate,
        station as station,
        equipment_name as equipmentName,
        content as content,
        warning_name as warningName,
        disposotion_state as disposotionState,
        arae as arae,
        'fan' as stationType,
        gateway_id,
        concat(point_name, '异常') as pointName,
        disposotion_date as disposotionDate,
        (case when warning_name = '注意' then 1
        when warning_name = '警告' then 2
        when warning_name = '危险' then 3
        end) as sort
        from
        analysis_data.fan_warning_record
        ) a
        <where>
           ( (a.disposotionstate = '待确认') or (a.disposotionstate = '已确认' and a.ts >( now()- 3d - 8h )))
            <if test="arae != '' and arae != null">
                and a.arae  like #{arae}
            </if>
            <if test="station != '' and station != null">
                and a.station = #{station}
            </if>
            <if test="stationType != '' and stationType != null">
                and a.stationtype = #{stationType}
            </if>
            <if test="warningName != '' and warningName != null">
                and a.warningname = #{warningName}
            </if>
            <if test="stationId != '' and stationId != null">
                and a.gateway_id = #{stationId}
            </if>
            <if test="startDate != '' and startDate != null">
                and a.recDate >= concat(#{startDate}, ' 00:00:00')
            </if>
            <if test="endDate != '' and endDate != null">
                and concat(#{endDate}, ' 23:59:59') >= a.recdate
            </if>
        </where>
        order by recDate desc
        limit #{current}, #{size}
    </select>


    <select id="getEquipWarningInfoByPageCount" resultType="integer">
        select count(1) from
        (select
        a.*
        from
        (
        select
        rec_date as recDate,
        ts,
        station as station,
        equipment_name as equipmentName,
        content as content,
        warning_name as warningName,
        disposotion_state as disposotionState,
        arae as arae,
        'pv' as stationType,
        gateway_id,
        disposotion_date as disposotionDate
        from
        analysis_data.pv_warning_record union all
        select
        rec_date as recFate,
        ts,
        station as station,
        equipment_name as equipmentName,
        content as content,
        warning_name as warningName,
        disposotion_state as disposotionState,
        arae as arae,
        'fan' as stationType,
        gateway_id,
        disposotion_date as disposotionDate
        from
        analysis_data.fan_warning_record
        ) a
        <where>
            (  a.disposotionState = '待确认'
            or (a.disposotionState = '已确认' and a.ts > ( now() - 3D - 8h )))
            <if test="arae != '' and arae != null">
                and a.arae  like #{arae}
            </if>
            <if test="station != '' and station != null">
                and a.station = #{station}
            </if>
            <if test="stationType != '' and stationType != null">
                and a.stationType = #{stationType}
            </if>
            <if test="warningName != '' and warningName != null">
                and a.warningname = #{warningName}
            </if>
            <if test="stationId != '' and stationId != null">
                and a.gateway_id = #{stationId}
            </if>
            <if test="startDate != '' and startDate != null">
                and a.recDate >= concat(#{startDate}, ' 00:00:00')
            </if>
            <if test="endDate != '' and endDate != null">
                and concat(#{endDate}, ' 23:59:59') >= a.recdate
            </if>


    </where>) b
    </select>


    <select id="getAllEquipAlarmInfoAnalysisByArea" resultType="map">
        select
            *
        from
            (
                select
                    concat('风电-',`b`.`EQUIPMENT_NAME`) as `EQUIPMENT_NAME`,
                    `b`.`STATION` as `STATION`,
                    `b`.`arae` as `arae`,
                    `b`.`警告` as `警告`,
                    `b`.`注意` as `注意`,
                    `b`.`危险` as `危险`,
                    '风电' as stationType,
                    sum(((`b`.`注意` + `b`.`危险`) + `b`.`警告`)) as `sort`
                from
                    (
                        select
                            `a`.`EQUIPMENT_NAME` as `EQUIPMENT_NAME`,
                            a.`STATION` as `STATION`,
                            `a`.`arae` as `arae`,
                            max(case `a`.`WARNING_NAME` when '危险' then `a`.`num` else 0 end) as `危险`,
                            max(case `a`.`WARNING_NAME` when '注意' then `a`.`num` else 0 end) as `注意`,
                            max(case `a`.`WARNING_NAME` when '警告' then `a`.`num` else 0 end) as `警告`
                        from
                            (
                                select
                                    `warning_name` as `WARNING_NAME`,
                                    `point_name` as `EQUIPMENT_NAME`,
                                    `station` as `STATION`,
                                    `arae` as `arae`,

                                    count(1) as `num`
                                from
                                    analysis_data.fan_warning_record
                                where
                                    `status` = 0
                                   or (`status` = 1
                                    and `ts` > (now() - 3D - 8h))
                                group by
                                    `station`,
                                    `point_name`,
                                    `warning_name`,arae) a
                        group by
                            `a`.`EQUIPMENT_NAME`,

                            a.`STATION`,a.`arae`) b
                group by
                    `b`.`EQUIPMENT_NAME`,
                    `b`.`STATION`,
                    `b`.`arae`,
                    `b`.`警告`,
                    `b`.`注意`,
                    `b`.`危险`
                union all
                (
                    select
                        concat('光伏-',`b`.`EQUIPMENT_NAME`)  as `EQUIPMENT_NAME`,
                        `b`.`STATION` as `STATION`,
                        `b`.`arae` as `arae`,
                        `b`.`警告` as `警告`,
                        `b`.`注意` as `注意`,
                        `b`.`危险` as `危险`,
                        '光伏' as stationType,
                        sum(((`b`.`注意` + `b`.`危险`) + `b`.`警告`)) as `sort`
                    from
                        (
                            select
                                `a`.`EQUIPMENT_NAME` as `EQUIPMENT_NAME`,
                                a.`STATION` as `STATION`,
                                a.`arae` as `arae`,
                                max(case `a`.`WARNING_NAME` when '危险' then `a`.`num` else 0 end) as `危险`,
                                max(case `a`.`WARNING_NAME` when '注意' then `a`.`num` else 0 end) as `注意`,
                                max(case `a`.`WARNING_NAME` when '警告' then `a`.`num` else 0 end) as `警告`
                            from
                                (
                                    select
                                        `warning_name` as `WARNING_NAME`,
                                        `point_name` as `EQUIPMENT_NAME`,
                                        `station` as `STATION`,
                                        `arae` as `arae`,
                                        count(1) as `num`
                                    from
                                        analysis_data.pv_warning_record
                                    where
                                        `status` = 0
                                       or (`status` = 1
                                        and `ts` > (now() - 3D - 8h))
                                    group by
                                        `station`,
                                        `point_name`,
                                        `warning_name`,arae) a
                            group by
                                `a`.`EQUIPMENT_NAME`,
                                a.`STATION`,a.`arae`) b
                    group by
                        `b`.`EQUIPMENT_NAME`,
                        `b`.`STATION`,
                        `b`.`arae`,
                        `b`.`警告`,
                        `b`.`注意`,
                        `b`.`危险`
                )) b
        <where>
            <if test="stationType != '' and stationType != null">
                and b.stationType = #{stationType}
            </if>
            <if test="araeCode != '' and araeCode != null">
                and b.arae like #{araeCode}
            </if>
        </where>
       order  by  b.`sort` ASC
    </select>

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

        select * from

        ( select arae         as area,
        warning_name as warningname,
        count(1)     as num,
        station
        from  analysis_data.fan_warning_record  a
        where (a.disposotion_state = '待确认')
        or (a.disposotion_state = '已确认' and a.ts >(now()-3d -8h ))
        group by arae,
        warning_name,station
        union  all
        select arae         as area,
        warning_name as warningname,
        count(1)     as num,
        station
        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,station ) b
        <where>
        <if test="area != null and area != ''">
            area like #{area}
        </if>
        </where>

    </select>
</mapper>
