<?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.ruoyi.business.mapper.HwAlarmInfoMapper">
    
    <resultMap type="HwAlarmInfo" id="HwAlarmInfoResult">
        <result property="alarmInfoId"    column="alarm_info_id"    />
        <result property="alarmInfoType"    column="alarm_info_type"    />
        <result property="alarmReleatedId"    column="alarm_releated_id"    />
        <result property="fenceAreaId"    column="fence_area_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="monitorUnitId"    column="monitor_unit_id"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="sceneId"    column="scene_id"    />
        <result property="alarmLevelId"    column="alarm_level_id"    />
        <result property="alarmTypeId"    column="alarm_type_id"    />
        <result property="modeFunctionId"    column="mode_function_id"    />
        <result property="functionName"    column="function_name"    />
        <result property="functionIdentifier"    column="function_identifier"    />
        <result property="functionValue"    column="function_value"    />
        <result property="triggerStatus"    column="trigger_status"    />
        <result property="handleStatus"    column="handle_status"    />
        <result property="alarmTime"    column="alarm_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="alarmInfoField"    column="alarm_info_field"    />
        <result property="monitorUnitName"    column="monitor_unit_name"    />
        <result property="alarmLevelName"    column="alarm_level_name"    />
        <result property="alarmTypeName"    column="alarm_type_name"    />
    </resultMap>

    <resultMap type="AlarmTypeVo" id="AlarmTypeVoResult">
        <result property="alarmCount"    column="alarm_count"    />
        <result property="alarmTypeId"    column="alarm_type_id"    />
        <result property="alarmTypeName"    column="alarm_type_name"    />
    </resultMap>

    <resultMap type="AlarmInfoVo" id="AlarmInfoVoResult">
        <result property="alarmCount"    column="alarm_count"    />
    </resultMap>


    <sql id="selectHwAlarmInfoVo">
        select hai.alarm_info_id, hai.alarm_info_type, hai.alarm_releated_id, hai.device_id,
               hai.monitor_unit_id, hai.tenant_id, hai.scene_id, hai.alarm_level_id,
               hai.alarm_type_id, hai.mode_function_id, hai.function_name, hai.function_identifier,
               hai.function_value, hai.trigger_status, hai.handle_status, hai.alarm_time,
               hai.create_time, hai.update_by, hai.update_time, hai.alarm_info_field,
               hmu.monitor_unit_name,hal.alarm_level_name,hat.alarm_type_name
        from hw_alarm_info hai left join hw_monitor_unit hmu on hai.monitor_unit_id = hmu.monitor_unit_id
        left join hw_alarm_level hal on hai.alarm_level_id=hal.alarm_level_id
        left join hw_alarm_type hat on hai.alarm_type_id=hat.alarm_type_id
    </sql>

    <select id="selectHwAlarmInfoList" parameterType="HwAlarmInfo" resultMap="HwAlarmInfoResult">
        <include refid="selectHwAlarmInfoVo"/>
        <where>  
            <if test="alarmInfoType != null  and alarmInfoType != ''"> and alarm_info_type = #{alarmInfoType}</if>
            <if test="alarmReleatedId != null "> and alarm_releated_id = #{alarmReleatedId}</if>
            <if test="deviceId != null "> and device_id = #{deviceId}</if>
            <if test="monitorUnitId != null "> and monitor_unit_id = #{monitorUnitId}</if>
            <if test="tenantId != null "> and tenant_id = #{tenantId}</if>
            <if test="sceneId != null "> and hai.scene_id = #{sceneId}</if>
            <if test="alarmLevelId != null "> and alarm_level_id = #{alarmLevelId}</if>
            <if test="alarmTypeId != null "> and alarm_type_id = #{alarmTypeId}</if>
            <if test="modeFunctionId != null "> and mode_function_id = #{modeFunctionId}</if>
            <if test="functionName != null  and functionName != ''"> and function_name like concat('%', #{functionName}, '%')</if>
            <if test="functionIdentifier != null  and functionIdentifier != ''"> and function_identifier = #{functionIdentifier}</if>
            <if test="functionValue != null  and functionValue != ''"> and function_value = #{functionValue}</if>
            <if test="triggerStatus != null  and triggerStatus != ''"> and trigger_status = #{triggerStatus}</if>
            <if test="handleStatus != null  and handleStatus != ''"> and handle_status = #{handleStatus}</if>
            <if test="alarmTime != null "> and alarm_time = #{alarmTime}</if>
            <if test="alarmInfoField != null  and alarmInfoField != ''"> and alarm_info_field = #{alarmInfoField}</if>
            <!-- 租户数据范围过滤 -->
            ${params.tenantDataScope}
        </where>
        order by alarm_info_id desc
    </select>
    
    <select id="selectHwAlarmInfoByAlarmInfoId" parameterType="Long" resultMap="HwAlarmInfoResult">
        <include refid="selectHwAlarmInfoVo"/>
        where alarm_info_id = #{alarmInfoId}
    </select>
        
    <insert id="insertHwAlarmInfo" parameterType="HwAlarmInfo" useGeneratedKeys="true" keyProperty="alarmInfoId">
        insert into hw_alarm_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="alarmInfoType != null and alarmInfoType != ''">alarm_info_type,</if>
            <if test="alarmReleatedId != null">alarm_releated_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="monitorUnitId != null">monitor_unit_id,</if>
            <if test="tenantId != null">tenant_id,</if>
            <if test="sceneId != null">scene_id,</if>
            <if test="alarmLevelId != null">alarm_level_id,</if>
            <if test="alarmTypeId != null">alarm_type_id,</if>
            <if test="modeFunctionId != null">mode_function_id,</if>
            <if test="functionName != null">function_name,</if>
            <if test="functionIdentifier != null">function_identifier,</if>
            <if test="functionValue != null">function_value,</if>
            <if test="triggerStatus != null">trigger_status,</if>
            <if test="handleStatus != null and handleStatus != ''">handle_status,</if>
            <if test="alarmTime != null">alarm_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="alarmInfoField != null">alarm_info_field,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="alarmInfoType != null and alarmInfoType != ''">#{alarmInfoType},</if>
            <if test="alarmReleatedId != null">#{alarmReleatedId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="monitorUnitId != null">#{monitorUnitId},</if>
            <if test="tenantId != null">#{tenantId},</if>
            <if test="sceneId != null">#{sceneId},</if>
            <if test="alarmLevelId != null">#{alarmLevelId},</if>
            <if test="alarmTypeId != null">#{alarmTypeId},</if>
            <if test="modeFunctionId != null">#{modeFunctionId},</if>
            <if test="functionName != null">#{functionName},</if>
            <if test="functionIdentifier != null">#{functionIdentifier},</if>
            <if test="functionValue != null">#{functionValue},</if>
            <if test="triggerStatus != null">#{triggerStatus},</if>
            <if test="handleStatus != null and handleStatus != ''">#{handleStatus},</if>
            <if test="alarmTime != null">#{alarmTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="alarmInfoField != null">#{alarmInfoField},</if>
         </trim>
    </insert>

    <update id="updateHwAlarmInfo" parameterType="HwAlarmInfo">
        update hw_alarm_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="alarmInfoType != null and alarmInfoType != ''">alarm_info_type = #{alarmInfoType},</if>
            <if test="alarmReleatedId != null">alarm_releated_id = #{alarmReleatedId},</if>
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="monitorUnitId != null">monitor_unit_id = #{monitorUnitId},</if>
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="sceneId != null">scene_id = #{sceneId},</if>
            <if test="alarmLevelId != null">alarm_level_id = #{alarmLevelId},</if>
            <if test="alarmTypeId != null">alarm_type_id = #{alarmTypeId},</if>
            <if test="modeFunctionId != null">mode_function_id = #{modeFunctionId},</if>
            <if test="functionName != null">function_name = #{functionName},</if>
            <if test="functionIdentifier != null">function_identifier = #{functionIdentifier},</if>
            <if test="functionValue != null">function_value = #{functionValue},</if>
            <if test="triggerStatus != null">trigger_status = #{triggerStatus},</if>
            <if test="handleStatus != null and handleStatus != ''">handle_status = #{handleStatus},</if>
            <if test="alarmTime != null">alarm_time = #{alarmTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="alarmInfoField != null">alarm_info_field = #{alarmInfoField},</if>
        </trim>
        where alarm_info_id = #{alarmInfoId}
    </update>

    <delete id="deleteHwAlarmInfoByAlarmInfoId" parameterType="Long">
        delete from hw_alarm_info where alarm_info_id = #{alarmInfoId}
    </delete>

    <delete id="deleteHwAlarmInfoByAlarmInfoIds" parameterType="String">
        delete from hw_alarm_info where alarm_info_id in 
        <foreach item="alarmInfoId" collection="array" open="(" separator="," close=")">
            #{alarmInfoId}
        </foreach>
    </delete>


    <select id="selectAlarmsByAlarmType" parameterType="Long" resultMap="AlarmTypeVoResult">
        SELECT count(*) as alarm_count,alarm_type_id from hw_alarm_info
        where scene_id = #{sceneId} and alarm_type_id is not null group by alarm_type_id;
    </select>

    <select id="selectAlarmCountByFenceArea" parameterType="Long" resultMap="AlarmInfoVoResult">
        SELECT count(distinct(device_id)) as alarm_count from hw_alarm_info hai
        where hai.fence_area_id = #{fenceAreaId}
    </select>

</mapper>