<?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.fastbee.iot.mapper.AlertLogMapper">

    <resultMap type="com.fastbee.iot.domain.AlertLog" id="AlertLogResult">
        <result property="alertLogId"    column="alert_log_id"    />
        <result property="alertName"    column="alert_name"    />
        <result property="alertLevel"    column="alert_level"    />
        <result property="status"    column="status"    />
        <result property="deviceName"    column="device_name"    />
        <result property="productId"    column="product_id"    />
        <result property="detail"    column="detail"    />
        <result property="serialNumber"    column="serial_number"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="userId" column="user_id"/>
    </resultMap>

    <resultMap type="com.fastbee.iot.model.vo.AlertLogVO" id="AlertLogVOResult">
        <result property="alertLogId"    column="alert_log_id"    />
        <result property="alertName"    column="alert_name"    />
        <result property="alertLevel"    column="alert_level"    />
        <result property="status"    column="status"    />
        <result property="deviceId"    column="device_id"    />
        <result property="deviceName"    column="device_name"    />
        <result property="productId"    column="product_id"    />
        <result property="detail"    column="detail"    />
        <result property="serialNumber"    column="serial_number"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="userId" column="user_id"/>
        <result property="sceneId" column="scene_id"/>
        <result property="sceneName" column="scene_name"/>
    </resultMap>

    <resultMap type="com.fastbee.iot.model.DeviceAlertCount" id="DeviceAlertCount">
        <result property="serialNumber"    column="serial_number"    />
        <result property="sceneId"    column="scene_id"    />
        <result property="alertCount"    column="alert_count"    />
        <result property="noprocessedCount"    column="noprocessed_count"    />
        <result property="unprocessedCount"    column="unprocessed_count"    />
        <result property="processedCount"    column="processed_count"    />
    </resultMap>
    
    <resultMap id="AlertCountVo" type="com.fastbee.iot.model.vo.AlertCountVO">
        <result property="count" column="count" />
        <result property="type" column="status" />
    </resultMap>

    <resultMap id="AlertCountVoLevel" type="com.fastbee.iot.model.vo.AlertCountVO">
        <result property="count" column="count" />
        <result property="type" column="alert_level" />
    </resultMap>

    <sql id="selectAlertLogVo">
        select alert_log_id, alert_name, alert_level, status, product_id, detail, serial_number, create_by, create_time, remark, device_name, user_id, update_time from iot_alert_log
    </sql>

    <select id="selectAlertLogListAndScene" parameterType="com.fastbee.iot.domain.AlertLog" resultMap="AlertLogVOResult">
        select al.alert_log_id, al.alert_name, al.alert_level, al.status, al.product_id, al.detail,
                al.serial_number, al.create_time, al.remark, al.device_name, al.user_id,
                s.scene_id,s.scene_name
        from iot_alert_log al
        left join iot_scene s on CAST(COALESCE(NULLIF(TRIM(al.create_by), ''), 0) AS signed) = s.scene_id
        <where>
            <if test="alertName != null  and alertName != ''"> and al.alert_name like concat('%', #{alertName}, '%')</if>
            <if test="alertLevel != null "> and al.alert_level = #{alertLevel}</if>
            <if test="status != null "> and al.status = #{status}</if>
            <if test="productId != null "> and al.product_id = #{productId}</if>
            <if test="serialNumber != null "> and al.serial_number = #{serialNumber}</if>
            <if test="userId != null "> and al.user_id = #{userId}</if>
            <if test="beginTime != null "><!-- 开始时间检索 -->
                and al.create_time &gt;= #{beginTime}
            </if>
            <if test="endTime != null "><!-- 结束时间检索 -->
                and al.create_time &lt;= #{endTime}
            </if>
        </where>
        order by al.create_time desc
    </select>

    <select id="selectAlertLogListCount" parameterType="com.fastbee.iot.domain.AlertLog" resultType="Long">
        select distinct count(l.alert_log_id)
        from iot_alert_log l
        left join iot_device d on l.serial_number=d.serial_number
        left join iot_device_user u on d.device_id=u.device_id
        <where>
            <if test="alertName != null  and alertName != ''"> and l.alert_name like concat('%', #{alertName}, '%')</if>
            <if test="alertLevel != null "> and l.alert_level = #{alertLevel}</if>
            <if test="status != null "> and l.status = #{status}</if>
            <if test="userId != null "> and u.user_id = #{userId}</if>
            <if test="productId != null "> and l.product_id = #{productId}</if>
            <if test="createBy != null  and createBy != ''"> and l.create_by = #{createBy}</if>
            <if test="serialNumber != null  and serialNumber != ''"> and l.serial_number = #{serialNumber}</if>
        </where>
    </select>

    <select id="selectDeviceAlertCount" resultMap="DeviceAlertCount">
        SELECT
            serial_number,
            COUNT(alert_log_id) AS alert_count,
            SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS noprocessed_count,
            SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS unprocessed_count,
            SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) AS processed_count
        FROM
            iot_alert_log
        WHERE serial_number in
            <foreach collection="serialNumberList" item="serialNumber" open="(" separator="," close=")">
                #{serialNumber}
            </foreach>
        GROUP BY
            serial_number;
    </select>

    <select id="selectDeviceAlertCountBySN"  parameterType="String" resultMap="DeviceAlertCount">
        SELECT
            serial_number,
            COUNT(alert_log_id) AS alert_count,
            SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS noprocessed_count,
            SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS unprocessed_count,
            SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) AS processed_count
        FROM
            iot_alert_log
        where serial_number = #{serialNumber}
    </select>

    <select id="selectSceneAlertCount"  resultMap="DeviceAlertCount">
        SELECT
            create_by AS scene_id,
            COUNT(alert_log_id) AS alert_count,
            SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS noprocessed_count,
            SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS unprocessed_count,
            SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) AS processed_count
        FROM
            iot_alert_log
        GROUP BY
            create_by;
    </select>

    <select id="selectSceneAlertCountBySceneId"  parameterType="String" resultMap="DeviceAlertCount">
        SELECT
            create_by AS scene_id,
            COUNT(alert_log_id) AS alert_count,
            SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS noprocessed_count,
            SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) AS unprocessed_count,
            SUM(CASE WHEN status = 3 THEN 1 ELSE 0 END) AS processed_count
        FROM
            iot_alert_log
        where create_by = #{sceneId}
    </select>

    <select id="countAlertProcess" resultMap="AlertCountVo">
        select count(1) count, status
        from iot_alert_log
        where user_id = #{dataCenterParam.tenantId}
        <if test="dataCenterParam.serialNumber != null and dataCenterParam.serialNumber != ''">
            and serial_number = #{dataCenterParam.serialNumber}
        </if>
        <if test="beginTime != null and endTime != null">
            and create_time between #{beginTime} and #{endTime}
        </if>
        group by status
    </select>

    <select id="countAlertLevel" resultMap="AlertCountVoLevel">
        select count(1) count, alert_level
        from iot_alert_log
        where user_id = #{dataCenterParam.tenantId}
        <if test="dataCenterParam.serialNumber != null and dataCenterParam.serialNumber != ''">
            and serial_number = #{dataCenterParam.serialNumber}
        </if>
        <if test="beginTime != null and endTime != null">
            and create_time between #{beginTime} and #{endTime}
        </if>
        group by alert_level
    </select>

</mapper>
