<?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.usky.iot.mapper.BaseAlarmMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.usky.iot.domain.BaseAlarm">
        <id column="id" property="id" />
        <result column="device_id" property="deviceId" />
        <result column="alarm_time" property="alarmTime" />
        <result column="alarm_type" property="alarmType" />
        <result column="alarm_object" property="alarmObject" />
        <result column="alarm_data" property="alarmData" />
        <result column="alarm_attribute" property="alarmAttribute" />
        <result column="alarm_content" property="alarmContent" />
        <result column="alarm_grade" property="alarmGrade" />
        <result column="alarm_address" property="alarmAddress" />
        <result column="handle_by" property="handleBy" />
        <result column="handle_time" property="handleTime" />
        <result column="handle_content" property="handleContent" />
        <result column="handle_phone" property="handlePhone" />
        <result column="handle_status" property="handleStatus" />
        <result column="alarm_false" property="alarmFalse" />
        <result column="site_photo" property="sitePhoto" />
        <result column="dept_id" property="deptId" />
        <result column="tenant_id" property="tenantId" />
    </resultMap>
    <select id="getAlarmTypeOne" resultType="com.usky.iot.domain.BaseAlarmType">
        select
        *
        from base_alarm_type
        <where>
            <if test="alarmType != null">
                and type_code = #{alarmType}
            </if>
            <if test="1 == 1">
                and delete_flag = 0
            </if>
        </where>
    </select>
    <select id="getAlarmTypeList" resultType="com.usky.iot.domain.BaseAlarmType">
        select
        *
        from base_alarm_type
        <where>
            <if test="1 == 1">
                and delete_flag = 0
            </if>
        </where>
    </select>
    <select id="getDeviceInfoList" resultType="com.usky.iot.domain.DmpDeviceInfo">
        select ddi.*
        from dmp_device ddi
        <where>
            <if test="1 == 1">
                and delete_flag = 0
            </if>
            <if test="1 == 1">
                and device_id in
                <foreach item="item" collection="devList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="getUnDeviceAlarmList" resultType="com.usky.iot.domain.BaseAlarm">
        select distinct device_id as deviceId,product_code as productCode
        from base_alarm ddi
        <where>
            <if test="1 == 1">
                and handle_status = 0
            </if>
            <if test="1 == 1">
                and device_id in
                <foreach item="item" collection="devList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="getDeviceAlarmList" resultType="com.usky.iot.domain.BaseAlarm">
        select distinct device_id as deviceId,product_code as productCode
        from base_alarm ddi
        <where>
            <if test="1 == 1">
                and handle_status = 1
            </if>
            <if test="1 == 1">
                and device_id in
                <foreach item="item" collection="devList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="getAlarmStatisticDay" resultType="java.util.Map">
        SELECT
        `date`,
        MAX( `sum` ) AS `sum`
        FROM
            (
            SELECT
                @cdate := DATE_ADD( @cdate, INTERVAL - 1 DAY ) `date`,
                0 AS `sum`
            FROM
                ( SELECT @cdate := DATE_ADD( CURDATE( ), INTERVAL + 1 DAY ) FROM base_build ) t1
            WHERE
                @cdate > #{startTime} UNION ALL
            SELECT
                DATE(alarm_time) AS date,
                COUNT( * ) AS 'sum'
            FROM
                `base_alarm`
            WHERE
                base_alarm.alarm_time >= #{startTime} AND base_alarm.tenant_id = #{tenantId}
            GROUP BY
                DATE DESC
            ) _tmpAllTable
            WHERE
               `date` BETWEEN #{startTime} AND #{endTime}
        GROUP BY
            `date` DESC
    </select>
</mapper>
