<?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.gyee.wisdom.alarm.schedule.mapper.AlarmCountMapper">

    <insert id="singleInsert" >
        insert into alarmcount (id, alarmdate, snapid, count)
        values
        (#{item.id}, #{item.alarmdate}, #{item.snapId}, #{item.count})
    </insert>

    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="false">
        insert into alarmcount ( ID, alarmdate, snapid, count,time )
        <foreach collection="list" item="item" index="index" separator="union all" >
            ( select
            #{item.id}, #{item.alarmDate}, #{item.snapId}, #{item.count} ,#{item.time}
            from dual )
        </foreach>
    </insert>



    <select id="selectAlarmHistoryBySnapid" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.schedule.entity.AlarmHistory"  >
             select * from ${alarmhistory} where snapid = #{snapid}
             and alerttime &gt;= #{startdate,jdbcType=DATE} and alerttime &lt; #{enddate,jdbcType=DATE} order by alerttime asc
    </select>




    <select id="statAlarmCount1" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.schedule.entity.AlarmStat1"  >
        select t.*, s.stationid, s.windturbineid, s.alerttext from (
            select snapid, sum(count) as count,sum(time) time from alarmcount a
            left join alarmsnap ss on a.snapid = ss.id
            where ss.stationid = #{stationid} and ss.windturbineid is not null
            and alarmdate &gt;= #{startdate,jdbcType=DATE} and alarmdate &lt; #{enddate,jdbcType=DATE}
            group by snapid ) t
        left join alarmsnap s on t.snapid = s.id
        order by s.windturbineid
    </select>

    <select id="statAlarmCount5" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.schedule.entity.AlarmStat1"  >
        select * from alarmcount a
            LEFT JOIN alarmsnap s on a.snapid = s.id
        <where>
            1=1
            <if test="startdate !=null and enddate !=null">
                and a.alarmdate &gt;= #{startdate,jdbcType=DATE} and a.alarmdate &lt;= #{enddate,jdbcType=DATE}
            </if>
            <if test="stationid !=null and stationid !=''">
                and s.stationid=#{stationid}
            </if>
            <if test="windturbineid !=null and windturbineid !=''">
                and s.windturbineid=#{windturbineid}
            </if>
        </where>
    </select>

    <select id="statAlarmCount" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.schedule.entity.AlarmStat"  >
        select t.snapid, t.count as sum, t.times as times, s.windturbineid as wtnum, s.alerttext as text from (
        select snapid, sum(count)  as count, sum(time) as times  from alarmcount a
        left join alarmsnap ss on a.snapid = ss.id
        where ss.stationid = #{stationid} and ss.windturbineid is not null
        and alarmdate >= #{startdate,jdbcType=DATE} and alarmdate &lt;= #{enddate,jdbcType=DATE}
        group by snapid ) t
        left join alarmsnap s on t.snapid = s.id
        order by s.windturbineid
    </select>

    <select id="statAlarmCount3" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.schedule.entity.AlarmStat"  >
        select t.snapid, t.count as sum, t.times as times, s.windturbineid as wtnum, s.alerttext as text from (
            select snapid, sum(count)  as count, sum(time) as times  from alarmcount a
            left join alarmsnap ss on a.snapid = ss.id
        <where>
        1=1
            <if test="startdate !=null and enddate !=null">
                and a.alarmdate &gt;= #{startdate,jdbcType=DATE} and a.alarmdate &lt;= #{enddate,jdbcType=DATE}
            </if>
            <if test="stationid !=null and stationid !=''">
                and ss.stationid=#{stationid}
            </if>
            <if test="windturbineid !=null and windturbineid !=''">
                and ss.windturbineid=#{windturbineid}
            </if>
        </where>
            group by snapid ) t
            left join alarmsnap s on t.snapid = s.id
        order by s.windturbineid
    </select>

    <select id="statAlarmCountByName" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.schedule.entity.AlarmStat"  >
        select t.count as sum, s.windturbineid as wtnum, s.alerttext as text from (
        select snapid, sum(count)  as count  from alarmcount a
        left join alarmsnap ss on a.snapid = ss.id
        where ss.alerttext=#{name} and ss.stationid = #{stationid} and ss.windturbineid is not null
        and alarmdate >= #{startdate,jdbcType=DATE} and alarmdate &lt;= #{enddate,jdbcType=DATE}
        group by snapid ) t
        left join alarmsnap s on t.snapid = s.id
        order by s.windturbineid
    </select>

    <select id="selectAlertRuleNames" resultType="com.gyee.wisdom.alarm.schedule.model.CodeValue" >
        select name as code, description as value from alertrule2 group by name, description
    </select>


    <select id="statAlarmCount2" resultType="com.gyee.wisdom.alarm.schedule.entity.AlarmStat1">
        select t.*, s.stationid, s.windturbineid, s.alerttext from (
            select snapid, sum(count) as count,sum(time) time from alarmcount a
            left join alarmsnap ss on a.snapid = ss.id
            where
            ss.windturbineid is not null
            and a.alarmdate &gt;= #{currentDate,jdbcType=DATE}
            <if test="stationid !=null and stationid !=''">
                and  ss.stationid = #{stationid}
            </if>
            group by snapid ) t
        left join alarmsnap s on t.snapid = s.id
        order by s.windturbineid
    </select>

    <select id="getAlarmCountInfo" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.schedule.entity.AlarmCountInfo">
        SELECT
            a.id, a.alarmdate,a.snapid,a.count, a.time,
            s.ALERTTEXT, s.WINDTURBINEID, s.RANK,
            r.id as alertruleid, r.relatedParts, r.PROJECT
        FROM
            alarmcount a
        LEFT JOIN alarmsnap s ON a.snapid = s.id
        LEFT JOIN alertrule2 r ON s.ALERTVALUE = r.ednaValue
        WHERE
            a.alarmdate = #{currentDate}
        AND s.windturbineid = #{windturbineid}
    </select>

</mapper>
