package com.yfd.monitor.storager.dao;

import com.yfd.monitor.gdw2019.bean.DeviceAlarm;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 用于存储设备的报警信息
 */
@Mapper
@Repository
public interface DeviceAlarmMapper {

    @Insert("INSERT INTO mon_device_alarm (deviceId, channelId, alarmPriority, alarmMethod, alarmTime, alarmDescription, longitude, latitude, alarmType , createTime ) " +
            "VALUES (#{deviceId}, #{channelId}, #{alarmPriority}, #{alarmMethod}, #{alarmTime}, #{alarmDescription}, #{longitude}, #{latitude}, #{alarmType}, #{createTime})")
    int add(DeviceAlarm alarm);


    @Select( value = {" <script>" +
            " SELECT * FROM mon_device_alarm " +
            " WHERE 1=1 " +
            " <if test=\"deviceId != null\" >  AND deviceId = #{deviceId}</if>" +
            " <if test=\"alarmPriority != null\" >  AND alarmPriority = #{alarmPriority} </if>" +
            " <if test=\"alarmMethod != null\" >  AND alarmMethod = #{alarmMethod} </if>" +
            " <if test=\"alarmType != null\" >  AND alarmType = #{alarmType} </if>" +
            " <if test=\"startTime != null\" >  AND alarmTime &gt;= #{startTime} </if>" +
            " <if test=\"endTime != null\" >  AND alarmTime &lt;= #{endTime} </if>" +
            " ORDER BY alarmTime ASC " +
            " </script>"})
    List<DeviceAlarm> query(String deviceId, String alarmPriority, String alarmMethod,
                            String alarmType, String startTime, String endTime);


    @Delete(" <script>" +
            "DELETE FROM mon_device_alarm WHERE 1=1 " +
            " <if test=\"deviceIdList != null and id == null \" > AND deviceId in " +
            "<foreach collection='deviceIdList'  item='item'  open='(' separator=',' close=')' > #{item}</foreach>" +
            "</if>" +
            " <if test=\"time != null and id == null \" > AND alarmTime &lt;= #{time}</if>" +
            " <if test=\"id != null\" > AND id = #{id}</if>" +
            " </script>"
            )
    int clearAlarmBeforeTime(Integer id, List<String> deviceIdList, String time);

    @Select( value = {" <script>" +
            " SELECT da.*,dc.status FROM mon_device_alarm da inner join mon_device_channel dc on da.deviceId=dc.deviceId " +
            " WHERE 1=1 " +
            " <if test=\"code != null\" >  AND da.channelId = #{code}</if>" +
            " <if test=\"level != null\" >  AND da.alarmPriority = #{level} </if>" +
            " <if test=\"alarmMethod != null\" >  AND da.alarmMethod = #{alarmMethod} </if>" +
            " <if test=\"type != null\" >  AND da.alarmType = #{type} </if>" +
            " <if test=\"beginTime != null\" >  AND da.alarmTime &gt;= #{beginTime} </if>" +
            " <if test=\"endTime != null\" >  AND da.alarmTime &lt;= #{endTime} </if>" +
            " ORDER BY da.alarmTime ASC " +
            "<if test=\"fromIndex != null and toIndex != null\">"+
            // 添加了LIMIT语句
            "LIMIT #{fromIndex}, #{size}"+
            "</if>"+
            " </script>"})
    List<Map<String,Object>> queryHistoryAlarmlog(String code, String userCode, String type, String beginTime, String endTime, String level, int fromIndex, int size);

}
