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

    <select id="queryFaultInfo" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.FaultInfo">
        SELECT
        h.id,h.faultTime,h.messageType,h.snapID,h.confirmType,h.confirmTime,h.confirmPerson,
        s.stationId,s.projectId,s.lineId,s.windturbineId,s.alertValue,s.rank,s.category1,s.category2,s.category3,
        s.isOpened,s.lastUpdateTime,s.lastUpdatePerson,s.stationName,s.projectName,s.lineName,s.windturbineName,
        s.alertText,s.modelId
        FROM
        faulthistory h left join faultsnap s on h.snapid = s.id
        <where>
            1=1
            <if test="starttime !=null and endtime !=null">
                and h.faulttime &gt;= #{starttime} and h.faulttime &lt;= #{endtime}
            </if>
            <if test="stid !=null and stid !=''">
                and s.stationid=#{stid}
            </if>
            <if test="keyword !=null and keyword !=''">
                and s.alerttext like #{keyword}
            </if>
            <if test="messageType !=null and messageType !=''">
                and h.messagetype = #{messageType}
            </if>
            <if test="likeString !=null and likeString !=''">
                and (s.alerttext like '%故障停机状态%'  or s.alerttext like '%总故障%')
            </if>

        </where>
        order by h.faultTime desc
    </select>

    <select id="getFaultInfoByIds" resultType="com.gyee.wisdom.alarm.sharding.entity.FaultInfo">
        SELECT
        h.id,h.faultTime,h.messageType,h.snapID,h.confirmType,h.confirmTime,h.confirmPerson,
        s.stationId,s.projectId,s.lineId,s.windturbineId,s.alertValue,s.rank,s.category1,s.category2,s.category3,
        s.isOpened,s.lastUpdateTime,s.lastUpdatePerson,s.stationName,s.projectName,s.lineName,s.windturbineName,
        s.alertText,s.modelId
        FROM
        faulthistory h left join faultsnap s on h.snapid = s.id
            where h.id in
        <foreach collection="array" item="ids" index="index" open="(" close=")" separator=",">
            #{ids}
        </foreach>
        order by h.faultTime desc
    </select>

    <select id="getRecentFault" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.FaultInfo">
        SELECT
        h.id,h.faultTime,h.messageType,h.snapID,h.confirmType,h.confirmTime,h.confirmPerson,
        s.stationId,s.projectId,s.lineId,s.windturbineId,s.alertValue,s.rank,s.category1,s.category2,s.category3,
        s.isOpened,s.lastUpdateTime,s.lastUpdatePerson,s.stationName,s.projectName,s.lineName,s.windturbineName,
        s.alertText,s.modelId
        FROM
        faulthistory h left join faultsnap s on h.snapid = s.id
        WHERE
        h.confirmType = 0
        AND s.isopened = 1
        AND h.messageType = 1
        AND h.faultTime >= #{faulttime}
        order by h.faultTime desc
    </select>
    <select id="getRecentFault_bak" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.FaultInfo">
        SELECT
        s.lastupdatetime as faultTime , s.isopened as messageType, s.id as snapID, '0' as confirmType,s.lastupdatetime as confirmTime, '' as confirmPerson,
        s.id,
        s.stationId,s.projectId,s.lineId,s.windturbineId,s.alertValue,s.rank,s.category1,s.category2,s.category3,
        s.isOpened,s.lastUpdateTime,s.lastUpdatePerson,s.stationName,s.projectName,s.lineName,s.windturbineName,
        s.alertText,s.modelId
        FROM
        faultsnap s
        WHERE
        s.lastupdatetime >= #{faulttime}
        order by s.lastupdatetime asc limit 0,16
    </select>


    <select id="getTopNumFault" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.FaultInfo">
        select * from (
        SELECT
        h.id,h.faultTime,h.messageType,h.snapID,h.confirmType,h.confirmTime,h.confirmPerson,
        s.stationId,s.projectId,s.lineId,s.windturbineId,s.alertValue,s.rank,s.category1,s.category2,s.category3,
        s.isOpened,s.lastUpdateTime,s.lastUpdatePerson,s.stationName,s.projectName,s.lineName,s.windturbineName,
       (concat(concat(s.alertText,''),h.ALERTTEXTLAST)) AS alertText,s.modelId
        FROM
        faulthistory h left join faultsnap s on h.snapid = s.id
        order by h.faultTime desc)
         where limit 0,#{topnum}
    </select>

    <select id="queryFaultInfo1" parameterType="java.util.Map" resultType="com.gyee.wisdom.alarm.sharding.entity.FaultInfo">
        SELECT
        h.id,h.faultTime,h.messageType,h.snapID,h.confirmType,h.confirmTime,h.confirmPerson,
        s.stationId,s.projectId,s.lineId,s.windturbineId,s.alertValue,s.rank,s.category1,s.category2,s.category3,
        s.isOpened,s.lastUpdateTime,s.lastUpdatePerson,s.stationName,s.projectName,s.lineName,s.windturbineName,
        (concat(concat(s.alertText,''),h.ALERTTEXTLAST)) AS alertText,s.modelId
        FROM
        faulthistory h left join faultsnap s on h.snapid = s.id
        <where>
            1=1
            <if test="starttime !=null and endtime !=null">
                and h.faulttime &gt;= #{starttime} and h.faulttime &lt;= #{endtime}
            </if>
            <if test="stid !=null and stid !=''">
                and s.stationid=#{stid}
            </if>
            <if test="keyword !=null and keyword !=''">
                and s.alerttext like #{keyword}
            </if>
            <if test="category1 !=null and category1 !=''">
                and s.category1=#{category1}
            </if>
            <if test="isopened !=null ">
                and s.isopened=#{isopened}
            </if>
        </where>
        order by h.faultTime desc
    </select>

</mapper>
