<?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="vip.xiaonuo.coldchain.core.alarm.mapper.SensorAlarmMapper">

  <select id="getTop10Warning" resultType="vip.xiaonuo.coldchain.modular.monitorsearchhistory.dto.TopWarningDto">
    SELECT
    *
    FROM
      (SELECT
        COUNT(*) AS totalWarnings,
        s.device_id as deviceId,
        r.name as device_name,
        s.sensor_code as sensorCode,
        s.sensor_route as sensorRoute
      FROM sensor_alarm s
      left join monitor_target_region r
      on s.device_id = r.id
      WHERE r.delete_flag = 'NOT_DELETE'
      AND s.type in
      <foreach collection="types" item="type" open="(" separator="," close=")">
        #{type}
      </foreach>
      <if test="month != null">
        AND s.alarm_time LIKE concat(#{month}, '%')
      </if>
      AND s.CREATE_ORG = #{orgId}
      GROUP BY s.device_id , s.sensor_route) tmp
    ORDER BY tmp.totalWarnings DESC
    limit 10
  </select>
</mapper>