<?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.sharding.mapper.AlarmHisotryMapper">

    <insert id="singleInsert" >
        insert into ${tbName} (id, alerttime, messagetype,snapid, datainfo)
        values
        (#{item.id}, #{item.alertTime}, #{item.messageType},#{item.snapId},#{item.dataInfo})
    </insert>

    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="false">
        insert into ${tbName} ( ID, ALERTTIME, MESSAGETYPE, SNAPID, DATAINFO,ALERTTEXTLAST)
        <foreach collection="list" item="item" index="index" separator="union all" >
            ( select
            #{item.id}, #{item.alertTime}, #{item.messageType},#{item.snapId},#{item.dataInfo},#{item.alertTextLast}
            from dual )
        </foreach>
    </insert>

    <!--<insert id="batchInsert" useGeneratedKeys="false">-->
        <!--INSERT ALL-->
        <!--<foreach item="item" index="index" collection="list">-->
            <!--into ${tbName}-->
            <!--values-->
            <!--(#{item.id}, #{item.alertTime}, #{item.messageType},#{item.snapId},#{item.dataInfo})-->
        <!--</foreach>-->
        <!--SELECT 1 FROM DUAL-->
    <!--</insert>-->

    <!-- 单表查询 -->
    <select id="pageQueryAll" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.model.AlarmHistoryInfo">
        select a.*,
        b.stationid,
        b.windturbineid,
        b.projectid,
        b.lineid,
        b.alertvalue,
        b.category1,
        b.category2,
        b.category3,
        b.rank,
        b.stationname,
        b.projectname,
        b.linename,
        b.windturbinename,
        b.alerttext,
        b.modelid
        from ${tbName} a
        left join alarmsnap b on a.snapid = b.id
        <where>
            1=1
            <if test="starttime !=null and endtime !=null">
                and a.alerttime &gt;= #{starttime} and a.alerttime &lt;= #{endtime}
            </if>
            <if test="stid !=null and stid !=''">
                and b.stationid=#{stid}
            </if>
            <if test="wtid !=null and wtid !=''">
                and b.windturbineid=#{wtid}
            </if>
            <if test="category1 !=null and category1 !=''">
                and b.category1=#{category1}
            </if>
            <if test="category2 !=null and category2 !=''">
                and b.category2=#{category2}
            </if>
            <if test="rank !=null and rank !=''">
                and b.rank=#{rank}
            </if>
            <if test="modelid !=null and modelid !=''">
                and b.modelid=#{modelid}
            </if>
            <if test="snapid !=null ">
                and a.snapid=#{snapid}
            </if>
            <if test="messagetype !=null ">
                and a.messagetype=#{messagetype}
            </if>
            <if test="keyword !=null and keyword !=''">
                and b.alerttext like '%${keyword}%'
            </if>
        </where>
        order by a.alerttime  desc
    </select>


    <!-- 联合查询 -->
    <select id="pageQueryAll2" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.model.AlarmHistoryInfo">
        select a.*,
        b.stationid,
        b.windturbineid,
        b.projectid,
        b.lineid,
        b.alertvalue,
        b.category1,
        b.category2,
        b.category3,
        b.rank,
        b.stationname,
        b.projectname,
        b.linename,
        b.windturbinename,
        b.alerttext,
        b.modelid
        from (
        <foreach collection="simpleStationIdList" item="station" index="index" separator="union">
            select * from alarmhistory_${station}_${yearmonth}
        </foreach>
        ) a
        left join alarmsnap b on a.snapid = b.id
        <where>
            1=1
            <if test="starttime !=null and endtime !=null">
                and a.alerttime &gt;= #{starttime,jdbcType=DATE} and a.alerttime &lt;= #{endtime,jdbcType=DATE}
            </if>
            <if test="stid !=null and stid !=''">
                and b.stationid=#{stid}
            </if>
            <if test="wtid !=null and wtid !=''">
                and b.windturbineid=#{wtid}
            </if>
            <if test="category1 !=null and category1 !=''">
                and b.category1=#{category1}
            </if>
            <if test="category2 !=null and category2 !=''">
                and b.category2=#{category2}
            </if>
            <if test="rank !=null and rank !=''">
                and b.rank=#{rank}
            </if>
            <if test="modelid !=null and modelid !=''">
                and b.modelid=#{modelid}
            </if>
            <if test="snapid !=null ">
                and a.snapid=#{snapid}
            </if>
            <if test="messagetype !=null ">
                and a.messagetype=#{messagetype}
            </if>
            <if test="keyword !=null and keyword !=''">
                and b.alerttext like '%${keyword}%'
            </if>
        </where>
        order by a.alerttime  desc
    </select>

    <select id="selectSnapCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        select count(*) from ${tbName} where snapid = #{snapid}
            and messagetype = '1'
            and alerttime &gt;= #{starttime,jdbcType=DATE} and alerttime &lt; #{endtime,jdbcType=DATE}
    </select>


    <select id="selectHistoryAlarmSnapAndWarning"
            resultType="com.gyee.wisdom.alarm.sharding.entity.FaultWarning">

        select h.id,
        a.stationid,
        h.snapid,
        a.windturbineid,
        a.windturbinename,
        a.lastupdatetime,
        w.ednavalue,
        h.alerttime,
        w.warningtypeid
        from ${tbName} h
        left join alarmsnap a on h.snapid = a.id
        left join warning2 w on a.alertvalue = w.ednavalue
        and a.modelid = w.modelid
        <where>
            and a.category1 = 'windturbine'
            and h.messagetype = '1'
            and
            <if test="starttime !=null and endtime !=null">
                h.alerttime  &gt;= #{starttime,jdbcType=DATE} and h.alerttime  &lt; #{endtime,jdbcType=DATE}
            </if>
        </where>


    </select>

    <update id="timedCreatTable">
        CREATE TABLE alarmhistory_${stationname}_${yearmonth} (
                                                   `ID` decimal(65,0) NOT NULL,
                                                   `ALERTTIME` datetime(6) NOT NULL,
                                                   `MESSAGETYPE` char(1) NOT NULL,
                                                   `SNAPID` decimal(65,0) NOT NULL,
                                                   `DATAINFO` text,
                                                   `ALERTTEXTLAST` varchar(50)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    </update>
    <update id="timedAlterTable">
        alter table alarmhistory_${stationname}_${yearmonth}
            add primary key (`id`)
    </update>
    <update id="timedCreatAlerttimeIndex">
        create index IX_AH_${stationname}_${yearmonth}_ALARMTIME on alarmhistory_${stationname}_${yearmonth} (alerttime)
    </update>
    <update id="timedCreatSnapidIndex">
        create index IX_AH_${stationname}_${yearmonth}_SNAPID on alarmhistory_${stationname}_${yearmonth} (snapid)
    </update>
    <select id="isHasTable" resultType="java.lang.Integer">
        select count(*) from information_schema.TABLES where table_name =upper('ALARMHISTORY_${stationname}_${yearmonth}')
    </select>


</mapper>
