<?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.ho.flow.mapper.EventMapper">

    <select id="selectByInfo" resultType="com.ho.flow.vo.resp.event.EventRespVO">
        select
        <include refid="Base_Column_List"/>
        from event
        <where>
            <if test="record.targetDevice != null">
                and target_device = #{record.targetDevice}
            </if>
            <if test="record.srcIdList != null and record.srcIdList.size != 0 ">
                and target_device in
                <foreach collection="record.srcIdList" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="record.eventColReqList != null and record.eventColReqList.size != 0 ">
                and
                <foreach collection="record.eventColReqList" open="(" close=")" separator=" or " item="item">
                    ( target_device = #{item.srcId}
                    <if test="item.colList != null">
                        and `signal` in
                        <foreach collection="item.colList" open="(" close=")" separator="," item="st">
                            #{st}
                        </foreach>
                    </if>
                    )
                </foreach>

            </if>
            <if test="record.description != null and record.description != ''">
                and description LIKE concat('%',#{record.description},'%')
            </if>
            <if test="record.stationIds != null and record.stationIds.size != 0">
                and station_id in
                <foreach collection="record.stationIds" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="record.deviceTypeList != null and record.deviceTypeList.size != 0">
                and device_type in
                <foreach collection="record.deviceTypeList" open="(" close=")" separator="," item="deviceType">
                    #{deviceType.deviceType}
                </foreach>
            </if>
            <if test="record.startTime != null">
                and time_stamp &gt;= #{record.startTime}
            </if>
            <if test="record.endTime != null">
                and time_stamp &lt;= #{record.endTime}
            </if>
            <if test="depts != null and depts.size != 0">
                and dept_id in
                <foreach collection="depts" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="record.status != null">
                and status = #{record.status}
            </if>
            <trim prefix="and (" suffix=")">
                <if test="record.eventTypes != null and record.eventTypes.size != 0">
                    event_type in
                    <foreach collection="record.eventTypes" open="(" close=")" separator="," item="type">
                        #{type}
                    </foreach>
                </if>
                <if test="record.eventTypes != null and record.eventTypes.size != 0
             and record.eventLevels != null and record.eventLevels.size != 0">
                    or
                </if>
                <if test="record.eventLevels != null and record.eventLevels.size != 0">
                    event_level in
                    <foreach collection="record.eventLevels" open="(" close=")" separator="," item="level">
                        #{level}
                    </foreach>
                </if>
            </trim>
            <if test="record.userLevel != null and record.userLevel>0">
                and (`signal` = 'polymerization' or device_type ='accesspoint')
            </if>
            <if test="record.userLevel == null or record.userLevel==0">
                and `signal` != 'polymerization'
            </if>
            order by time_stamp desc
            <!--限制的查询记录数,防止记录集过大-->
            <if test="record.limitCount!=null">
                limit #{record.limitCount}
            </if>
        </where>

    </select>

    <select id="selectCountStatusByInfo" resultType="java.lang.Integer">
        select count(1)
        from event
        where status = 0
        <if test="record.targetDevice != null">
            and target_device = #{record.targetDevice}
        </if>
        <if test="record.description != null and record.description != ''">
            and description LIKE concat('%',#{record.description},'%')
        </if>
        <if test="record.stationIds != null and record.stationIds.size != 0">
            and station_id in
            <foreach collection="record.stationIds" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="record.deviceTypeList != null and record.deviceTypeList.size != 0">
            and device_type in
            <foreach collection="record.deviceTypeList" open="(" close=")" separator="," item="deviceType">
                #{deviceType.deviceType}
            </foreach>
        </if>
        <if test="record.startTime != null">
            and time_stamp &gt;= #{record.startTime}
        </if>
        <if test="record.endTime != null">
            and time_stamp &lt;= #{record.endTime}
        </if>
        <trim prefix="and (" suffix=")">
            <if test="record.eventTypes != null and record.eventTypes.size != 0">
                event_type in
                <foreach collection="record.eventTypes" open="(" close=")" separator="," item="type">
                    #{type}
                </foreach>
            </if>
            <if test="record.eventTypes != null and record.eventTypes.size != 0
             and record.eventLevels != null and record.eventLevels.size != 0">
                or
            </if>
            <if test="record.eventLevels != null and record.eventLevels.size != 0">
                event_level in
                <foreach collection="record.eventLevels" open="(" close=")" separator="," item="level">
                    #{level}
                </foreach>
            </if>
        </trim>
        <if test="record.userLevel != null and record.userLevel>0">
            and (`signal` = 'polymerization' or device_type ='accesspoint')
        </if>
        <if test="record.userLevel == null or record.userLevel==0">
            and `signal` != 'polymerization'
        </if>
    </select>

    <sql id="Base_Column_List">
        id
        ,station_id,dept_id,group_id,
        event_type,event_level,time_stamp,
        target_device,device_type, device_type_id, `signal`,`status`,
        value,description,confirm_status,
        confirm_man,confirm_time,remark,
        suggestion,create_time,update_time,
        category,type,sub_type,sens_type,is_recovery,before_event_id
    </sql>


    <select id="selectByPrimaryKey" resultType="com.ho.flow.vo.Event">
        select
        <include refid="Base_Column_List"/>
        from event
        where id = #{id}
    </select>

    <select id="selectByBeforeEventId" resultType="com.ho.flow.vo.Event">
        select
        <include refid="Base_Column_List"/>
        from event
        where before_event_id = #{id}
    </select>

    <select id="selectNumByType" resultType="com.ho.flow.vo.resp.event.EventTypeNum">
        SELECT event_type,COUNT(event_type) AS COUNT
        FROM event
        where
        event_type IS NOT NULL
        <if test="record.startTime != null">
            and time_stamp &gt;= #{record.startTime}
        </if>
        <if test="record.endTime != null">
            and time_stamp &lt;= #{record.endTime}
        </if>
        <if test="record.status != null">
            and status = #{record.status}
        </if>
        <if test="record.srcIdList != null and record.srcIdList.size !=0 ">
            and target_device in
            <foreach collection="record.srcIdList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="record.eventColReqList != null and record.eventColReqList.size != 0 ">
            and
            <foreach collection="record.eventColReqList" open="(" close=")" separator=" or " item="item">
                ( target_device = #{item.srcId}
                <if test="item.colList != null">
                    and `signal` in
                    <foreach collection="item.colList" open="(" close=")" separator="," item="st">
                        #{st}
                    </foreach>
                </if>
                )
            </foreach>

        </if>
        <if test="record.description != null">
            and description like concat('%',#{record.description},'%')
        </if>
        <if test="record.stationIds != null and record.stationIds.size != 0">
            and station_id in
            <foreach collection="record.stationIds" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="depts != null and depts.size != 0">
            and dept_id in
            <foreach collection="depts" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <!--选中的deviceTypeIds-->
        <if test="record.deviceTypeList != null and record.deviceTypeList.size != 0">
            and device_type in
            <foreach collection="record.deviceTypeList" open="(" close=")" separator="," item="deviceType">
                #{deviceType.deviceType}
            </foreach>
        </if>
        <if test="record.userLevel != null and record.userLevel>0">
            and (`signal` = 'polymerization' or device_type ='accesspoint')
        </if>
        <if test="record.userLevel == null or record.userLevel==0">
            and `signal` != 'polymerization'
        </if>
        GROUP BY event_type
    </select>
    <select id="selectNumByLevel" resultType="com.ho.flow.vo.resp.event.EventLevelNum">
        SELECT event_level,COUNT(event_level) AS COUNT
        FROM `event`
        where
        event_level IS NOT NULL
        <if test="record.startTime != null">
            and time_stamp &gt;= #{record.startTime}
        </if>
        <if test="record.endTime != null">
            and time_stamp &lt;= #{record.endTime}
        </if>
        <if test="record.status != null">
            and status = #{record.status}
        </if>
        <if test="record.srcIdList != null and record.srcIdList.size !=0 ">
            and target_device in
            <foreach collection="record.srcIdList" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="record.eventColReqList != null and record.eventColReqList.size != 0 ">
            and
            <foreach collection="record.eventColReqList" open="(" close=")" separator=" or " item="item">
                ( target_device = #{item.srcId}
                <if test="item.colList != null">
                    and `signal` in
                    <foreach collection="item.colList" open="(" close=")" separator="," item="st">
                        #{st}
                    </foreach>
                </if>
                )
            </foreach>

        </if>
        <if test="record.description != null">
            and description like concat('%',#{record.description},'%')
        </if>
        <if test="record.stationIds != null and record.stationIds.size != 0">
            and station_id in
            <foreach collection="record.stationIds" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="depts != null and depts.size != 0">
            and dept_id in
            <foreach collection="depts" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <!--选中的deviceTypeIds-->
        <if test="record.deviceTypeList != null and record.deviceTypeList.size != 0">
            and device_type in
            <foreach collection="record.deviceTypeList" open="(" close=")" separator="," item="deviceType">
                #{deviceType.deviceType}
            </foreach>
        </if>
        <if test="record.userLevel != null and record.userLevel>0">
            and (`signal` = 'polymerization' or device_type ='accesspoint')
        </if>
        <if test="record.userLevel == null or record.userLevel==0">
            and `signal` != 'polymerization'
        </if>
        GROUP BY event_level
    </select>

    <select id="selectByIds" resultType="com.ho.flow.vo.Event">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `event`
        where id in
        <foreach collection="list" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        order by time_stamp desc
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete
        from event
        where id = #{id,jdbcType=BIGINT}
    </delete>
    <insert id="insert" parameterType="com.ho.flow.vo.Event">
        insert into event (
            id,station_id,dept_id,group_id,
            event_type,event_level,time_stamp,
            target_device,device_type, device_type_id,`signal`,status,
            `value`,`description`,confirm_status,
            confirm_man,confirm_time,remark,
            suggestion,category,type,sub_type,sens_type,is_recovery,before_event_id
        ) values(
            #{id},#{stationId},#{deptId},#{groupId},
            #{eventType},#{eventLevel},#{timeStamp},
            #{targetDevice},#{deviceType}, #{deviceTypeId},#{signal},#{status},
            #{value},#{description},#{confirmStatus},
            #{confirmMan},#{confirmTime},#{remark},
            #{suggestion},#{category},#{type},#{subType},#{sensType},#{isRecovery},#{beforeEventId}
        )
    </insert>
    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.ho.flow.vo.Event"
            useGeneratedKeys="true">
        insert into event
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="groupId != null">group_id,</if>
            <if test="eventType != null">event_type,</if>
            <if test="eventLevel!= null">event_level,</if>
            <if test="timeStamp != null">time_stamp,</if>
            <if test="targetDevice != null">target_device,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="deviceTypeId != null">device_type_id,</if>
            <if test="signal != null">signal,</if>
            <if test="status != null">status,</if>
            <if test="value != null">value,</if>
            <if test="description != null">description,</if>
            <if test="confirmStatus != null">confirm_status,</if>
            <if test="confirmMan != null">confirm_man,</if>
            <if test="confirmTime != null">confirm_time,</if>
            <if test="remark != null">remark,</if>
            <if test="suggestion != null">suggestion,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="stationId != null">station_id,</if>
            <if test="category != null">category,</if>
            <if test="type != null">type,</if>
            <if test="subType != null">sub_type,</if>
            <if test="isRecovery != null">is_recovery,</if>
            <if test="beforeEventId != null">before_event_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id,jdbcType=BIGINT},</if>
            <if test="deptId != null">#{deptId,jdbcType=BIGINT},</if>
            <if test="groupId != null">#{groupId,jdbcType=BIGINT},</if>
            <if test="eventType != null">#{eventType,jdbcType=TINYINT},</if>
            <if test="eventLevel!= null">#{eventLevel,jdbcType=TINYINT},</if>
            <if test="timeStamp != null">#{timeStamp,jdbcType=TIMESTAMP},</if>
            <if test="targetDevice != null">#{targetDevice},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="deviceTypeId != null">#{deviceTypeId},</if>
            <if test="signal != null">#{signal,jdbcType=VARCHAR},</if>
            <if test="status != null">#{status,jdbcType=INTEGER},</if>
            <if test="value != null">#{value,jdbcType=DECIMAL},</if>
            <if test="description != null">#{description,jdbcType=VARCHAR},</if>
            <if test="confirmStatus != null">#{confirmStatus,jdbcType=TINYINT},</if>
            <if test="confirmMan != null">#{confirmMan,jdbcType=VARCHAR},</if>
            <if test="confirmTime != null">#{confirmTime,jdbcType=TIMESTAMP},</if>
            <if test="remark != null">#{remark,jdbcType=VARCHAR},</if>
            <if test="suggestion != null">#{suggestion,jdbcType=VARCHAR},</if>
            <if test="createTime != null">#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="updateTime != null">#{updateTime,jdbcType=TIMESTAMP},</if>
            <if test="stationId != null">#{stationId,jdbcType=INTEGER},</if>
            <if test="category != null">#{category,jdbcType=INTEGER},</if>
            <if test="type != null">#{type,jdbcType=INTEGER},</if>
            <if test="subType != null">#{subType,jdbcType=INTEGER},</if>
            <if test="isRecovery != null">#{isRecovery},</if>
            <if test="beforeEventId != null">#{beforeEventId},</if>
        </trim>
    </insert>

    <insert id="insertBatch">
        INSERT INTO event (
        id,station_id,dept_id,group_id,
        event_type,event_level,time_stamp,
        target_device,device_type, device_type_id,`signal`,status,
        `value`,`description`,confirm_status,
        confirm_man,confirm_time,remark,
        suggestion,category,type,sub_type,sens_type,is_recovery,before_event_id
        )
        VALUES
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.id},#{item.stationId},#{item.deptId},#{item.groupId},
            #{item.eventType},#{item.eventLevel},#{item.timeStamp},
            #{item.targetDevice},#{item.deviceType}, #{item.deviceTypeId},#{item.signal},#{item.status},
            #{item.value},#{item.description},#{item.confirmStatus},
            #{item.confirmMan},#{item.confirmTime},#{item.remark},
            #{item.suggestion},#{item.category},#{item.type},#{item.subType},#{item.sensType},#{item.isRecovery},#{item.beforeEventId}
            )
        </foreach>
    </insert>
    <update id="updateBatchById">
        update `event`
        <set>
            <if test="event.timeStamp != null">
                time_stamp = #{event.timeStamp,jdbcType=TIMESTAMP},
            </if>
            <if test="event.value != null">
                `value` = #{event.value,jdbcType=DECIMAL},
            </if>
            <if test="event.description != null">
                description = #{event.description,jdbcType=VARCHAR},
            </if>
            <if test="event.updateTime != null">
                update_time = #{event.updateTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        WHERE id = #{event.id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.ho.flow.vo.Event">
        update event
        set dept_id     = #{deptId,jdbcType=INTEGER},
            group_id    = #{groupId,jdbcType=INTEGER},
            event_type  = #{eventType,jdbcType=TINYINT},
            event_level = #{ eventLevel,jdbcType=TINYINT} time_stamp = #{timeStamp,jdbcType=TIMESTAMP},
            target_device = #{targetDevice,jdbcType=INTEGER},
            signal = #{signal,jdbcType=VARCHAR},
            status = #{status,jdbcType=INTEGER},
            value = #{value,jdbcType=DECIMAL},
            description = #{description,jdbcType=VARCHAR},
            confirm_status = #{confirmStatus,jdbcType=TINYINT},
            confirm_man = #{confirmMan,jdbcType=VARCHAR},
            confirm_time = #{confirmTime,jdbcType=TIMESTAMP},
            remark = #{remark,jdbcType=VARCHAR},
            suggestion = #{suggestion,jdbcType=VARCHAR},
            create_time = #{createTime,jdbcType=TIMESTAMP},
            update_time = #{updateTime,jdbcType=TIMESTAMP},
            station_id = #{stationId,jdbcType=INTEGER},
            category = #{category,jdbcType=INTEGER},
            type = #{type,jdbcType=INTEGER},
            sub_type = #{subType,jdbcType=INTEGER}
        where id = #{id,jdbcType=BIGINT}
    </update>
    <update id="updateByPrimaryKeySelective">
        update event
        <set>
            <if test="deptId != null">
                dept_id = #{deptId,jdbcType=INTEGER},
            </if>
            <if test="groupId != null">
                group_id = #{groupId,jdbcType=INTEGER},
            </if>

            <if test="eventType != null">
                event_type = #{eventType,jdbcType=TINYINT},
            </if>
            <if test="eventLevel!= null">
                event_level = #{eventLevel,jdbcType=TINYINT}
            </if>
            <if test="timeStamp != null">
                time_stamp = #{timeStamp,jdbcType=TIMESTAMP},
            </if>
            <if test="targetDevice != null">
                target_device = #{targetDevice,jdbcType=INTEGER},
            </if>
            <if test="signal != null">
                signal = #{signal,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                status = #{status,jdbcType=INTEGER},
            </if>
            <if test="value != null">
                value = #{value,jdbcType=DECIMAL},
            </if>
            <if test="description != null">
                description = #{description,jdbcType=VARCHAR},
            </if>
            <if test="confirmStatus != null">
                confirm_status = #{confirmStatus,jdbcType=TINYINT},
            </if>
            <if test="confirmMan != null">
                confirm_man = #{confirmMan,jdbcType=VARCHAR},
            </if>
            <if test="confirmTime != null">
                confirm_time = #{confirmTime,jdbcType=TIMESTAMP},
            </if>
            <if test="remark != null">
                remark = #{remark,jdbcType=VARCHAR},
            </if>
            <if test="suggestion != null">
                suggestion = #{suggestion,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="stationId != null">
                station_id = #{stationId,jdbcType=INTEGER},
            </if>
            <if test="category != null">
                category = #{category,jdbcType=INTEGER},
            </if>
            <if test="type != null">
                type = #{type,jdbcType=INTEGER},
            </if>
            <if test="subType != null">
                sub_type = #{subType,jdbcType=INTEGER},
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <update id="updateByIds">
        update event
        set
        status = 1,
        confirm_time = NOW(),
        confirm_man=#{userDetail.username}
        where id in
        <if test="ids != null and ids.size() != 0">
            <foreach collection="ids" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>

    </update>
    <update id="updateConfirm">
        update event
        set
        confirm_status = 1,
        confirm_time = NOW(),
        confirm_man = #{name,jdbcType=VARCHAR}
        where id in
        <if test="ids != null and ids.size() != 0">
            <foreach collection="ids" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
    </update>
    <update id="setRemarkByIds">
        update event
        set
        remark = "已转工单"
        <where>
            <if test="ids != null and ids.size() != 0">
                and id in
                <foreach collection="ids" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
    </update>
    <!--    List<Event> selectByParams(@Param("stationId") Integer stationId,@Param("targetDevice") Integer targetDevice,@Param("signal") String signal,@Param("isRecovery") Integer isRecovery);-->
    <select id="selectByParams" resultType="com.ho.flow.vo.Event">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `event`
        <where>
            <if test="stationId != null">
                and station_id = #{stationId,jdbcType=INTEGER}
            </if>
            <if test="targetDevice != null">
                and target_device = #{targetDevice,jdbcType=INTEGER}
            </if>
            <if test="signal != null">
                and `signal` = #{signal,jdbcType=VARCHAR}
            </if>
            <if test="isRecovery != null">
                and is_recovery = #{isRecovery,jdbcType=INTEGER}
            </if>
            order by time_stamp desc limit 1
        </where>
    </select>

    <select id="selectByParam" resultType="com.ho.flow.vo.Event">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `event`
        <where>
            <if test="vo.stationId != null">
                and station_id = #{vo.stationId,jdbcType=INTEGER}
            </if>
            <if test="vo.timeStamp != null">
                and time_stamp &lt;= #{vo.timeStamp}
            </if>
            <if test="vo.targetDevice != null">
                and target_device = #{vo.targetDevice,jdbcType=INTEGER}
            </if>
            <if test="vo.signal != null">
                and `signal` = #{vo.signal,jdbcType=VARCHAR}
            </if>
            <if test="vo.isRecovery != null">
                and is_recovery = #{vo.isRecovery,jdbcType=INTEGER}
            </if>
            <if test="vo.eventLevel != null">
                and event_level = #{vo.eventLevel,jdbcType=INTEGER}
            </if>
            order by time_stamp desc limit 1
        </where>
    </select>

    <select id="selectByTargetDevice" resultType="com.ho.flow.vo.Event">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `event`
        <where>
            <if test="alarmConfigQueryVo.groupId != null">
                and group_id = #{alarmConfigQueryVo.groupId}
            </if>
            <if test="alarmConfigQueryVo.stationId != null">
                and station_id = #{alarmConfigQueryVo.stationId}
            </if>
            <if test="alarmConfigQueryVo.deviceType != null">
                and device_type = #{alarmConfigQueryVo.deviceType}
            </if>
            <if test="alarmConfigQueryVo.srcId != null">
                and target_device = #{alarmConfigQueryVo.srcId}
            </if>
            <if test="alarmConfigQueryVo.startTime != null">
                and time_stamp &gt;= #{alarmConfigQueryVo.startTime}
            </if>
            <if test="alarmConfigQueryVo.endTime != null">
                and time_stamp &lt; #{alarmConfigQueryVo.endTime}
            </if>
            <if test="alarmConfigQueryVo.signal != null">
                and `signal` = #{alarmConfigQueryVo.signal}
            </if>
            <if test="alarmConfigQueryVo.isRecovery != null">
                and is_recovery = #{alarmConfigQueryVo.isRecovery}
            </if>
            <if test="alarmConfigQueryVo.eventLevelList != null and alarmConfigQueryVo.eventLevelList.size !=0 ">
                and event_level in
                <foreach collection="alarmConfigQueryVo.eventLevelList" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
        </where>
        order by time_stamp desc
    </select>

    <select id="selectByTargetDeviceAsc" resultType="com.ho.flow.vo.Event">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `event`
        <where>
            <if test="alarmConfigQueryVo.groupId != null">
                and group_id = #{alarmConfigQueryVo.groupId}
            </if>
            <if test="alarmConfigQueryVo.stationId != null">
                and station_id = #{alarmConfigQueryVo.stationId}
            </if>
            <if test="alarmConfigQueryVo.deviceType != null">
                and device_type = #{alarmConfigQueryVo.deviceType}
            </if>
            <if test="alarmConfigQueryVo.srcId != null">
                and target_device = #{alarmConfigQueryVo.srcId}
            </if>
            <if test="alarmConfigQueryVo.eventLevelList != null and alarmConfigQueryVo.eventLevelList.size !=0 ">
                and event_level in
                <foreach collection="alarmConfigQueryVo.eventLevelList" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            <if test="alarmConfigQueryVo.startTime != null">
                and time_stamp &gt;= #{alarmConfigQueryVo.startTime}
            </if>
            <if test="alarmConfigQueryVo.endTime != null">
                and time_stamp &lt; #{alarmConfigQueryVo.endTime}
            </if>
            and `signal` != "polymerization"
        </where>
        order by time_stamp DESC
    </select>

    <select id="selectLastTime" resultType="com.ho.flow.vo.Event">
        SELECT
        <include refid="Base_Column_List"/>
        FROM `event`
        <where>
            <if test="alarmConfigQueryVo.groupId != null">
                and group_id = #{alarmConfigQueryVo.groupId}
            </if>
            <if test="alarmConfigQueryVo.stationId != null">
                and station_id = #{alarmConfigQueryVo.stationId}
            </if>
            <if test="alarmConfigQueryVo.deviceType != null">
                and device_type = #{alarmConfigQueryVo.deviceType}
            </if>
            <if test="alarmConfigQueryVo.srcId != null">
                and target_device = #{alarmConfigQueryVo.srcId}
            </if>
            <if test="alarmConfigQueryVo.startTime != null">
                and time_stamp &gt;= #{alarmConfigQueryVo.startTime}
            </if>
            <if test="alarmConfigQueryVo.endTime != null">
                and time_stamp &lt; #{alarmConfigQueryVo.endTime}
            </if>
        </where>
        order by time_stamp desc LIMIT 1
    </select>

    <select id="countEventByDay" resultType="com.ho.flow.vo.resp.event.EventDayNum">
        SELECT DATE_FORMAT(time_stamp,'%Y-%m-%d') as day,COUNT(event_type) AS COUNT
        FROM event
        where event_type IS NOT NULL
        <if test="record.startTime != null">
            and time_stamp &gt;= #{record.startTime}
        </if>
        <if test="record.endTime != null">
            and time_stamp &lt;= #{record.endTime}
        </if>
        <if test="record.stationIds != null and record.stationIds.size != 0">
            and station_id in
            <foreach collection="record.stationIds" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="record.userLevel != null and record.userLevel>0">
            and (`signal` = 'polymerization' or device_type ='accesspoint')
        </if>
        <if test="record.userLevel == null or record.userLevel==0">
            and `signal` != 'polymerization'
        </if>
        group by day
    </select>

    <delete id="deleteByParam" parameterType="java.lang.Long">
        delete
        FROM `event`
        <where>
            and status = 0
            <if test="vo.stationId != null">
                and station_id = #{vo.stationId,jdbcType=INTEGER}
            </if>
            <if test="vo.timeStamp != null">
                and time_stamp &lt;= #{vo.timeStamp}
            </if>
            <if test="vo.targetDevice != null">
                and target_device = #{vo.targetDevice,jdbcType=INTEGER}
            </if>
            <if test="vo.signal != null">
                and `signal` = #{vo.signal,jdbcType=VARCHAR}
            </if>
            <if test="vo.isRecovery != null">
                and is_recovery = #{vo.isRecovery,jdbcType=INTEGER}
            </if>
            <if test="vo.eventLevel != null">
                and event_level = #{vo.eventLevel,jdbcType=INTEGER}
            </if>
            <if test="vo.excludeId != null">
                and id != #{vo.excludeId}
            </if>
        </where>
    </delete>

    <select id="eventDeviceNum" resultType="java.lang.Integer">
        SELECT count(0) faultDevice FROM event where status = 0 and station_id = #{vo.stationId} group by target_device
    </select>
</mapper>
