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

    <insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="false">
        insert into faultsnap ( ID, STATIONID, PROJECTID, LINEID, WINDTURBINEID, ALERTVALUE, CATEGORY1, CATEGORY2, CATEGORY3, RANK, ISOPENED, LASTUPDATETIME, STATIONNAME, PROJECTNAME, LINENAME, WINDTURBINENAME, ALERTTEXT, MODELID, TESTINGPOINTKEY,ALARMSNAPID )
        <foreach collection="list" item="item" index="index" separator="union all" >
            ( select
                #{item.id},#{item.stationId},#{item.projectId},#{item.lineId},#{item.windturbineId},
                #{item.alertValue},#{item.category1},#{item.category2},#{item.category3},#{item.rank},
                #{item.isOpened},#{item.lastUpdateTime},#{item.stationName},#{item.projectName},
                #{item.lineName},#{item.windturbineName},#{item.alertText},#{item.modelId},
                #{item.testingpointKey},#{item.alarmSnapId}
            from dual )
        </foreach>
    </insert>

    <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            UPDATE faultsnap
            <set>
                <if test="item.isOpened != null">ISOPENED = #{item.isOpened},</if>
                <if test="item.lastUpdateTime != null">LASTUPDATETIME = #{item.lastUpdateTime},</if>
                <if test="item.alertText != null">ALERTTEXT = #{item.alertText},</if>
            </set>
            <where>
                <if test="item.id != null">
                    and id = #{item.id}
                </if>
            </where>
        </foreach>
    </update>

    <select id="findByAlarmSnapId" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.FaultSnap">
        select * from  FAULTSNAP a
        <where>
            a.alarmsnapid=#{alarmSnapId}  and rownum = 1
        </where>
    </select>



    <select id="queryMultiFaultSnap" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.FaultSnap">
        select  ID,STATIONID,PROJECTID,LINEID,WINDTURBINEID,ALERTVALUE,CATEGORY1,CATEGORY2,CATEGORY3,RANK,ISOPENED,LASTUPDATETIME,LASTUPDATEPERSON,STATIONNAME,PROJECTNAME,LINENAME,WINDTURBINENAME,ALERTTEXT,MODELID,TESTINGPOINTKEY,ALARMSNAPID
        from  FAULTSNAP a where a.id in
        <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </select>


    <select id="queryByTagId" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.FaultSnap">
        select * from  FAULTSNAP a
        <where>
            a.testingpointkey=#{tagId} and rownum = 1
        </where>
    </select>

    <select id="queryByTimeAndStation"  resultType="com.gyee.wisdom.alarm.sharding.entity.FaultSnap">
        select * from  FAULTSNAP a
        <where>
            a.LASTUPDATETIME &gt;= #{startTime}
            and a.LASTUPDATETIME &lt;= #{endTime}
            and a.STATIONID = #{stationId}
            and a.CATEGORY1 = 'FJ'
        </where>
    </select>

    <select id="getTopNumFault" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.FaultSnap">
        select * from ( select * from faultsnap h order by h.lastupdatetime desc) t
        where rownum &lt;= #{topnum}
    </select>

</mapper>
